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> |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 14 | #include <limits.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 15 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 16 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 17 | #include <lauxlib.h> |
| 18 | #include <lua.h> |
| 19 | #include <lualib.h> |
| 20 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 21 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 22 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 23 | #endif |
| 24 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | #include <ebpttree.h> |
| 26 | |
| 27 | #include <common/cfgparse.h> |
Willy Tarreau | b059b89 | 2018-10-16 17:57:36 +0200 | [diff] [blame] | 28 | #include <common/compiler.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 29 | #include <common/hathreads.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 30 | #include <common/initcall.h> |
| 31 | #include <common/xref.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 32 | #include <common/h1.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 34 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | #include <types/hlua.h> |
| 36 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 37 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 38 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 39 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 40 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 41 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 42 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 43 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 44 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 45 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 46 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 47 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 48 | #include <proto/http_fetch.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 49 | #include <proto/http_htx.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 50 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 51 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 52 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 53 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 54 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 55 | #include <proto/payload.h> |
| 56 | #include <proto/proto_http.h> |
| 57 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 58 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 59 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 60 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 61 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 62 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 63 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 64 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 65 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 66 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 67 | * macro is used only for identifying the function that can |
| 68 | * not return because a longjmp is executed. |
| 69 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 70 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 71 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 72 | */ |
| 73 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 74 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 75 | #define MAY_LJMP(func) func |
| 76 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 77 | /* This couple of function executes securely some Lua calls outside of |
| 78 | * the lua runtime environment. Each Lua call can return a longjmp |
| 79 | * if it encounter a memory error. |
| 80 | * |
| 81 | * Lua documentation extract: |
| 82 | * |
| 83 | * If an error happens outside any protected environment, Lua calls |
| 84 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 85 | * exiting the host application. Your panic function can avoid this |
| 86 | * exit by never returning (e.g., doing a long jump to your own |
| 87 | * recovery point outside Lua). |
| 88 | * |
| 89 | * The panic function runs as if it were a message handler (see |
| 90 | * §2.3); in particular, the error message is at the top of the |
| 91 | * stack. However, there is no guarantee about stack space. To push |
| 92 | * anything on the stack, the panic function must first check the |
| 93 | * available space (see §4.2). |
| 94 | * |
| 95 | * We must check all the Lua entry point. This includes: |
| 96 | * - The include/proto/hlua.h exported functions |
| 97 | * - the task wrapper function |
| 98 | * - The action wrapper function |
| 99 | * - The converters wrapper function |
| 100 | * - The sample-fetch wrapper functions |
| 101 | * |
| 102 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 103 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 104 | * |
| 105 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 106 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 107 | * because they must be exists in the program stack when the longjmp |
| 108 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 109 | * |
| 110 | * Note that the Lua processing is not really thread safe. It provides |
| 111 | * heavy system which consists to add our own lock function in the Lua |
| 112 | * code and recompile the library. This system will probably not accepted |
| 113 | * by maintainers of various distribs. |
| 114 | * |
| 115 | * Our main excution point of the Lua is the function lua_resume(). A |
| 116 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 117 | * of function and a lua_unlock() at the end of the function. So I |
| 118 | * conclude that the Lua thread safe mode just perform a mutex around |
| 119 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 120 | * easier for distro maintainers. |
| 121 | * |
| 122 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 123 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 124 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 125 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 126 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 127 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 129 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 130 | |
| 131 | #define SET_SAFE_LJMP(__L) \ |
| 132 | ({ \ |
| 133 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 134 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 135 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 136 | lua_atpanic(__L, hlua_panic_safe); \ |
| 137 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 138 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 139 | } else { \ |
| 140 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 141 | ret = 1; \ |
| 142 | } \ |
| 143 | ret; \ |
| 144 | }) |
| 145 | |
| 146 | /* If we are the last function catching Lua errors, we |
| 147 | * must reset the panic function. |
| 148 | */ |
| 149 | #define RESET_SAFE_LJMP(__L) \ |
| 150 | do { \ |
| 151 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 152 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 153 | } while(0) |
| 154 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 155 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 156 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 157 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 158 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 159 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 160 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 161 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 162 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 163 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 164 | /* The main Lua execution context. */ |
| 165 | struct hlua gL; |
| 166 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 167 | /* This is the memory pool containing struct lua for applets |
| 168 | * (including cli). |
| 169 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 170 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 171 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 172 | /* Used for Socket connection. */ |
| 173 | static struct proxy socket_proxy; |
| 174 | static struct server socket_tcp; |
| 175 | #ifdef USE_OPENSSL |
| 176 | static struct server socket_ssl; |
| 177 | #endif |
| 178 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 179 | /* List head of the function called at the initialisation time. */ |
| 180 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 181 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 182 | /* The following variables contains the reference of the different |
| 183 | * Lua classes. These references are useful for identify metadata |
| 184 | * associated with an object. |
| 185 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 186 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 187 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 188 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 189 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 190 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 191 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 192 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 193 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 194 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 195 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 196 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 197 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 198 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 199 | * because a task may remain alive during all the haproxy execution. |
| 200 | */ |
| 201 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 202 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 203 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 204 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 205 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 206 | * it is used for preventing infinite loops. |
| 207 | * |
| 208 | * I test the scheer with an infinite loop containing one incrementation |
| 209 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 210 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 211 | * to one interrupt each 10 000 instructions. |
| 212 | * |
| 213 | * configured | Number of |
| 214 | * instructions | loops executed |
| 215 | * between two | in milions |
| 216 | * forced yields | |
| 217 | * ---------------+--------------- |
| 218 | * 10 | 160 |
| 219 | * 500 | 670 |
| 220 | * 1000 | 680 |
| 221 | * 5000 | 700 |
| 222 | * 7000 | 700 |
| 223 | * 8000 | 700 |
| 224 | * 9000 | 710 <- ceil |
| 225 | * 10000 | 710 |
| 226 | * 100000 | 710 |
| 227 | * 1000000 | 710 |
| 228 | * |
| 229 | */ |
| 230 | static unsigned int hlua_nb_instruction = 10000; |
| 231 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 232 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 233 | * be enforced on any memory allocation. |
| 234 | */ |
| 235 | struct hlua_mem_allocator { |
| 236 | size_t allocated; |
| 237 | size_t limit; |
| 238 | }; |
| 239 | |
| 240 | static struct hlua_mem_allocator hlua_global_allocator; |
| 241 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 242 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 243 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 244 | "Cache-Control: no-cache\r\n" |
| 245 | "Connection: close\r\n" |
| 246 | "Content-Type: text/html\r\n" |
| 247 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 248 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n"; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 249 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 250 | /* These functions converts types between HAProxy internal args or |
| 251 | * sample and LUA types. Another function permits to check if the |
| 252 | * LUA stack contains arguments according with an required ARG_T |
| 253 | * format. |
| 254 | */ |
| 255 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 256 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 257 | __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] | 258 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 259 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 260 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 261 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 262 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 263 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 264 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 265 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 266 | do { \ |
| 267 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 268 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 269 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 270 | } while (0) |
| 271 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 272 | /* Used to check an Lua function type in the stack. It creates and |
| 273 | * returns a reference of the function. This function throws an |
| 274 | * error if the rgument is not a "function". |
| 275 | */ |
| 276 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 277 | { |
| 278 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 279 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 280 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 281 | } |
| 282 | lua_pushvalue(L, argno); |
| 283 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 284 | } |
| 285 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 286 | /* Return the string that is of the top of the stack. */ |
| 287 | const char *hlua_get_top_error_string(lua_State *L) |
| 288 | { |
| 289 | if (lua_gettop(L) < 1) |
| 290 | return "unknown error"; |
| 291 | if (lua_type(L, -1) != LUA_TSTRING) |
| 292 | return "unknown error"; |
| 293 | return lua_tostring(L, -1); |
| 294 | } |
| 295 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 296 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 297 | { |
| 298 | lua_Debug ar; |
| 299 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 300 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 301 | int filled = 0; |
| 302 | |
| 303 | while (lua_getstack(L, level++, &ar)) { |
| 304 | |
| 305 | /* Add separator */ |
| 306 | if (filled) |
| 307 | chunk_appendf(msg, ", "); |
| 308 | filled = 1; |
| 309 | |
| 310 | /* Fill fields: |
| 311 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 312 | * 'l': fills in the field currentline; |
| 313 | * 'n': fills in the field name and namewhat; |
| 314 | * 't': fills in the field istailcall; |
| 315 | */ |
| 316 | lua_getinfo(L, "Slnt", &ar); |
| 317 | |
| 318 | /* Append code localisation */ |
| 319 | if (ar.currentline > 0) |
| 320 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 321 | else |
| 322 | chunk_appendf(msg, "%s ", ar.short_src); |
| 323 | |
| 324 | /* |
| 325 | * Get function name |
| 326 | * |
| 327 | * if namewhat is no empty, name is defined. |
| 328 | * what contains "Lua" for Lua function, "C" for C function, |
| 329 | * or "main" for main code. |
| 330 | */ |
| 331 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 332 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 333 | |
| 334 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 335 | chunk_appendf(msg, "main chunk"); |
| 336 | |
| 337 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 338 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 339 | |
| 340 | else /* nothing left... */ |
| 341 | chunk_appendf(msg, "?"); |
| 342 | |
| 343 | |
| 344 | /* Display tailed call */ |
| 345 | if (ar.istailcall) |
| 346 | chunk_appendf(msg, " ..."); |
| 347 | } |
| 348 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 349 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 353 | /* This function check the number of arguments available in the |
| 354 | * stack. If the number of arguments available is not the same |
| 355 | * then <nb> an error is throwed. |
| 356 | */ |
| 357 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 358 | { |
| 359 | if (lua_gettop(L) == nb) |
| 360 | return; |
| 361 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 362 | } |
| 363 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 364 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 365 | * and the line number where the error is encountered. |
| 366 | */ |
| 367 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 368 | { |
| 369 | va_list argp; |
| 370 | va_start(argp, fmt); |
| 371 | luaL_where(L, 1); |
| 372 | lua_pushvfstring(L, fmt, argp); |
| 373 | va_end(argp); |
| 374 | lua_concat(L, 2); |
| 375 | return 1; |
| 376 | } |
| 377 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 378 | /* This functions is used with sample fetch and converters. It |
| 379 | * converts the HAProxy configuration argument in a lua stack |
| 380 | * values. |
| 381 | * |
| 382 | * It takes an array of "arg", and each entry of the array is |
| 383 | * converted and pushed in the LUA stack. |
| 384 | */ |
| 385 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 386 | { |
| 387 | switch (arg->type) { |
| 388 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | case ARGT_TIME: |
| 390 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 391 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 395 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 396 | break; |
| 397 | |
| 398 | case ARGT_IPV4: |
| 399 | case ARGT_IPV6: |
| 400 | case ARGT_MSK4: |
| 401 | case ARGT_MSK6: |
| 402 | case ARGT_FE: |
| 403 | case ARGT_BE: |
| 404 | case ARGT_TAB: |
| 405 | case ARGT_SRV: |
| 406 | case ARGT_USR: |
| 407 | case ARGT_MAP: |
| 408 | default: |
| 409 | lua_pushnil(L); |
| 410 | break; |
| 411 | } |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | /* This function take one entrie in an LUA stack at the index "ud", |
| 416 | * and try to convert it in an HAProxy argument entry. This is useful |
| 417 | * with sample fetch wrappers. The input arguments are gived to the |
| 418 | * lua wrapper and converted as arg list by thi function. |
| 419 | */ |
| 420 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 421 | { |
| 422 | switch (lua_type(L, ud)) { |
| 423 | |
| 424 | case LUA_TNUMBER: |
| 425 | case LUA_TBOOLEAN: |
| 426 | arg->type = ARGT_SINT; |
| 427 | arg->data.sint = lua_tointeger(L, ud); |
| 428 | break; |
| 429 | |
| 430 | case LUA_TSTRING: |
| 431 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 432 | arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data); |
Tim Duesterhus | f371b83 | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 433 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 434 | arg->data.str.size = arg->data.str.data; |
| 435 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 436 | break; |
| 437 | |
| 438 | case LUA_TUSERDATA: |
| 439 | case LUA_TNIL: |
| 440 | case LUA_TTABLE: |
| 441 | case LUA_TFUNCTION: |
| 442 | case LUA_TTHREAD: |
| 443 | case LUA_TLIGHTUSERDATA: |
| 444 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 445 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | /* the following functions are used to convert a struct sample |
| 452 | * in Lua type. This useful to convert the return of the |
| 453 | * fetchs or converters. |
| 454 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 455 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 457 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 459 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | break; |
| 462 | |
| 463 | case SMP_T_BIN: |
| 464 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 465 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 466 | break; |
| 467 | |
| 468 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 469 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 470 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 471 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 472 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 473 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 474 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 475 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 476 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 477 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 478 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 479 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 480 | break; |
| 481 | default: |
| 482 | lua_pushnil(L); |
| 483 | break; |
| 484 | } |
| 485 | break; |
| 486 | |
| 487 | case SMP_T_IPV4: |
| 488 | case SMP_T_IPV6: |
| 489 | 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] | 490 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 491 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 492 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 493 | else |
| 494 | lua_pushnil(L); |
| 495 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 496 | default: |
| 497 | lua_pushnil(L); |
| 498 | break; |
| 499 | } |
| 500 | return 1; |
| 501 | } |
| 502 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 503 | /* the following functions are used to convert a struct sample |
| 504 | * in Lua strings. This is useful to convert the return of the |
| 505 | * fetchs or converters. |
| 506 | */ |
| 507 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 508 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 509 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 510 | |
| 511 | case SMP_T_BIN: |
| 512 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 513 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 514 | break; |
| 515 | |
| 516 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 517 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 518 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 519 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 520 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 521 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 522 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 523 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 524 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 525 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 526 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 527 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 528 | break; |
| 529 | default: |
| 530 | lua_pushstring(L, ""); |
| 531 | break; |
| 532 | } |
| 533 | break; |
| 534 | |
| 535 | case SMP_T_SINT: |
| 536 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 537 | case SMP_T_IPV4: |
| 538 | case SMP_T_IPV6: |
| 539 | 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] | 540 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 541 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 542 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 543 | else |
| 544 | lua_pushstring(L, ""); |
| 545 | break; |
| 546 | default: |
| 547 | lua_pushstring(L, ""); |
| 548 | break; |
| 549 | } |
| 550 | return 1; |
| 551 | } |
| 552 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 553 | /* the following functions are used to convert an Lua type in a |
| 554 | * struct sample. This is useful to provide data from a converter |
| 555 | * to the LUA code. |
| 556 | */ |
| 557 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 558 | { |
| 559 | switch (lua_type(L, ud)) { |
| 560 | |
| 561 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 562 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 563 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | break; |
| 565 | |
| 566 | |
| 567 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 568 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 569 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 573 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 574 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 575 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Tim Duesterhus | f371b83 | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 576 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 577 | smp->data.u.str.size = smp->data.u.str.data; |
| 578 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | break; |
| 580 | |
| 581 | case LUA_TUSERDATA: |
| 582 | case LUA_TNIL: |
| 583 | case LUA_TTABLE: |
| 584 | case LUA_TFUNCTION: |
| 585 | case LUA_TTHREAD: |
| 586 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 587 | case LUA_TNONE: |
| 588 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 589 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 590 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 597 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | * 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] | 599 | * |
| 600 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 601 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 603 | __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] | 604 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 605 | { |
| 606 | int min_arg; |
| 607 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 608 | struct proxy *px; |
| 609 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | |
| 611 | idx = 0; |
| 612 | min_arg = ARGM(mask); |
| 613 | mask >>= ARGM_BITS; |
| 614 | |
| 615 | while (1) { |
| 616 | |
| 617 | /* Check oversize. */ |
| 618 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 619 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* Check for mandatory arguments. */ |
| 623 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 624 | if (idx < min_arg) { |
| 625 | |
| 626 | /* If miss other argument than the first one, we return an error. */ |
| 627 | if (idx > 0) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 629 | |
| 630 | /* If first argument have a certain type, some default values |
| 631 | * may be used. See the function smp_resolve_args(). |
| 632 | */ |
| 633 | switch (mask & ARGT_MASK) { |
| 634 | |
| 635 | case ARGT_FE: |
| 636 | if (!(p->cap & PR_CAP_FE)) |
| 637 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 638 | argp[idx].data.prx = p; |
| 639 | argp[idx].type = ARGT_FE; |
| 640 | argp[idx+1].type = ARGT_STOP; |
| 641 | break; |
| 642 | |
| 643 | case ARGT_BE: |
| 644 | if (!(p->cap & PR_CAP_BE)) |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 646 | argp[idx].data.prx = p; |
| 647 | argp[idx].type = ARGT_BE; |
| 648 | argp[idx+1].type = ARGT_STOP; |
| 649 | break; |
| 650 | |
| 651 | case ARGT_TAB: |
| 652 | argp[idx].data.prx = p; |
| 653 | argp[idx].type = ARGT_TAB; |
| 654 | argp[idx+1].type = ARGT_STOP; |
| 655 | break; |
| 656 | |
| 657 | default: |
| 658 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 659 | break; |
| 660 | } |
| 661 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | /* Check for exceed the number of requiered argument. */ |
| 666 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 667 | argp[idx].type != ARGT_STOP) { |
| 668 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 669 | } |
| 670 | |
| 671 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 672 | argp[idx].type == ARGT_STOP) { |
| 673 | return 0; |
| 674 | } |
| 675 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 676 | /* Convert some argument types. */ |
| 677 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 678 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 679 | if (argp[idx].type != ARGT_SINT) |
| 680 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 681 | argp[idx].type = ARGT_SINT; |
| 682 | break; |
| 683 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 684 | case ARGT_TIME: |
| 685 | if (argp[idx].type != ARGT_SINT) |
| 686 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 687 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 688 | break; |
| 689 | |
| 690 | case ARGT_SIZE: |
| 691 | if (argp[idx].type != ARGT_SINT) |
| 692 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 693 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 694 | break; |
| 695 | |
| 696 | case ARGT_FE: |
| 697 | if (argp[idx].type != ARGT_STR) |
| 698 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 699 | memcpy(trash.area, argp[idx].data.str.area, |
| 700 | argp[idx].data.str.data); |
| 701 | trash.area[argp[idx].data.str.data] = 0; |
| 702 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 703 | if (!argp[idx].data.prx) |
| 704 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 705 | argp[idx].type = ARGT_FE; |
| 706 | break; |
| 707 | |
| 708 | case ARGT_BE: |
| 709 | if (argp[idx].type != ARGT_STR) |
| 710 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 711 | memcpy(trash.area, argp[idx].data.str.area, |
| 712 | argp[idx].data.str.data); |
| 713 | trash.area[argp[idx].data.str.data] = 0; |
| 714 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | if (!argp[idx].data.prx) |
| 716 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 717 | argp[idx].type = ARGT_BE; |
| 718 | break; |
| 719 | |
| 720 | case ARGT_TAB: |
| 721 | if (argp[idx].type != ARGT_STR) |
| 722 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 723 | memcpy(trash.area, argp[idx].data.str.area, |
| 724 | argp[idx].data.str.data); |
| 725 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | e351e76 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 726 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 727 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 728 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 729 | argp[idx].type = ARGT_TAB; |
| 730 | break; |
| 731 | |
| 732 | case ARGT_SRV: |
| 733 | if (argp[idx].type != ARGT_STR) |
| 734 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 735 | memcpy(trash.area, argp[idx].data.str.area, |
| 736 | argp[idx].data.str.data); |
| 737 | trash.area[argp[idx].data.str.data] = 0; |
| 738 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 739 | if (sname) { |
| 740 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 741 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 742 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 743 | if (!px) |
| 744 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 745 | } |
| 746 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 747 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 748 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 749 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | argp[idx].data.srv = findserver(px, sname); |
| 751 | if (!argp[idx].data.srv) |
| 752 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 753 | argp[idx].type = ARGT_SRV; |
| 754 | break; |
| 755 | |
| 756 | case ARGT_IPV4: |
Christopher Faulet | e7f1fcf | 2020-08-07 09:07:26 +0200 | [diff] [blame] | 757 | if (argp[idx].type != ARGT_STR) |
| 758 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 759 | memcpy(trash.area, argp[idx].data.str.area, |
| 760 | argp[idx].data.str.data); |
| 761 | trash.area[argp[idx].data.str.data] = 0; |
| 762 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 764 | argp[idx].type = ARGT_IPV4; |
| 765 | break; |
| 766 | |
| 767 | case ARGT_MSK4: |
Christopher Faulet | ff8de2c | 2020-08-07 09:11:22 +0200 | [diff] [blame^] | 768 | if (argp[idx].type == ARGT_SINT) |
| 769 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 770 | else if (argp[idx].type == ARGT_STR) { |
| 771 | memcpy(trash.area, argp[idx].data.str.area, |
| 772 | argp[idx].data.str.data); |
| 773 | trash.area[argp[idx].data.str.data] = 0; |
| 774 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
| 775 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 776 | } |
| 777 | else |
| 778 | WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected")); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | argp[idx].type = ARGT_MSK4; |
| 780 | break; |
| 781 | |
| 782 | case ARGT_IPV6: |
Christopher Faulet | e7f1fcf | 2020-08-07 09:07:26 +0200 | [diff] [blame] | 783 | if (argp[idx].type != ARGT_STR) |
| 784 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 785 | memcpy(trash.area, argp[idx].data.str.area, |
| 786 | argp[idx].data.str.data); |
| 787 | trash.area[argp[idx].data.str.data] = 0; |
| 788 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 789 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 790 | argp[idx].type = ARGT_IPV6; |
| 791 | break; |
| 792 | |
| 793 | case ARGT_MSK6: |
Christopher Faulet | ff8de2c | 2020-08-07 09:11:22 +0200 | [diff] [blame^] | 794 | if (argp[idx].type == ARGT_SINT) |
| 795 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 796 | else if (argp[idx].type == ARGT_STR) { |
| 797 | memcpy(trash.area, argp[idx].data.str.area, |
| 798 | argp[idx].data.str.data); |
| 799 | trash.area[argp[idx].data.str.data] = 0; |
| 800 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
| 801 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 802 | } |
| 803 | else |
| 804 | WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected")); |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 805 | argp[idx].type = ARGT_MSK6; |
| 806 | break; |
| 807 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 808 | case ARGT_MAP: |
| 809 | case ARGT_REG: |
| 810 | case ARGT_USR: |
| 811 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 812 | break; |
| 813 | } |
| 814 | |
| 815 | /* Check for type of argument. */ |
| 816 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 817 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 818 | arg_type_names[(mask & ARGT_MASK)], |
| 819 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 820 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 821 | } |
| 822 | |
| 823 | /* Next argument. */ |
| 824 | mask >>= ARGT_BITS; |
| 825 | idx++; |
| 826 | } |
| 827 | } |
| 828 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 829 | /* |
| 830 | * The following functions are used to make correspondance between the the |
| 831 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 832 | * |
| 833 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 834 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 835 | */ |
| 836 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 837 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 838 | struct hlua **hlua = lua_getextraspace(L); |
| 839 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 840 | } |
| 841 | static inline void hlua_sethlua(struct hlua *hlua) |
| 842 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 843 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 844 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 847 | /* This function is used to send logs. It try to send on screen (stderr) |
| 848 | * and on the default syslog server. |
| 849 | */ |
| 850 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 851 | { |
| 852 | struct tm tm; |
| 853 | char *p; |
| 854 | |
| 855 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 856 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 857 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 858 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 859 | /* Break the message if exceed the buffer size. */ |
| 860 | *(p-4) = ' '; |
| 861 | *(p-3) = '.'; |
| 862 | *(p-2) = '.'; |
| 863 | *(p-1) = '.'; |
| 864 | break; |
| 865 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 866 | if (isprint(*msg)) |
| 867 | *p = *msg; |
| 868 | else |
| 869 | *p = '.'; |
| 870 | } |
| 871 | *p = '\0'; |
| 872 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 873 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 874 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 875 | get_localtime(date.tv_sec, &tm); |
| 876 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 877 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 878 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 879 | fflush(stderr); |
| 880 | } |
| 881 | } |
| 882 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 883 | /* This function just ensure that the yield will be always |
| 884 | * returned with a timeout and permit to set some flags |
| 885 | */ |
| 886 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 887 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 888 | { |
| 889 | struct hlua *hlua = hlua_gethlua(L); |
| 890 | |
| 891 | /* Set the wake timeout. If timeout is required, we set |
| 892 | * the expiration time. |
| 893 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 894 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 895 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 896 | hlua->flags |= flags; |
| 897 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 898 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 899 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 902 | /* This function initialises the Lua environment stored in the stream. |
| 903 | * 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] | 904 | * 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] | 905 | * |
| 906 | * This function is particular. it initialises a new Lua thread. If the |
| 907 | * initialisation fails (example: out of memory error), the lua function |
| 908 | * throws an error (longjmp). |
| 909 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 910 | * In some case (at least one), this function can be called from safe |
| 911 | * environement, so we must not initialise it. While the support of |
| 912 | * threads appear, the safe environment set a lock to ensure only one |
| 913 | * Lua execution at a time. If we initialize safe environment in another |
| 914 | * safe environmenet, we have a dead lock. |
| 915 | * |
| 916 | * set "already_safe" true if the context is initialized form safe |
| 917 | * Lua fonction. |
| 918 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 919 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 920 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 921 | * MUST NOT manipulate the created thread stack state, because it is not |
| 922 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 923 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 924 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 925 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 926 | if (!already_safe) { |
| 927 | if (!SET_SAFE_LJMP(gL.T)) { |
| 928 | lua->Tref = LUA_REFNIL; |
| 929 | return 0; |
| 930 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 931 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 932 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 933 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 934 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 935 | lua->T = lua_newthread(gL.T); |
| 936 | if (!lua->T) { |
| 937 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 938 | if (!already_safe) |
| 939 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 940 | return 0; |
| 941 | } |
| 942 | hlua_sethlua(lua); |
| 943 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 944 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 945 | if (!already_safe) |
| 946 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 947 | return 1; |
| 948 | } |
| 949 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 950 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 951 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 952 | * is not freed. |
| 953 | */ |
| 954 | void hlua_ctx_destroy(struct hlua *lua) |
| 955 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 956 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 957 | return; |
| 958 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 959 | if (!lua->T) |
| 960 | goto end; |
| 961 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 962 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 963 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 964 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 965 | if (!SET_SAFE_LJMP(lua->T)) |
| 966 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 967 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 968 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 969 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 970 | if (!SET_SAFE_LJMP(gL.T)) |
| 971 | return; |
| 972 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 973 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 974 | /* Forces a garbage collecting process. If the Lua program is finished |
| 975 | * without error, we run the GC on the thread pointer. Its freed all |
| 976 | * the unused memory. |
| 977 | * If the thread is finnish with an error or is currently yielded, |
| 978 | * it seems that the GC applied on the thread doesn't clean anything, |
| 979 | * so e run the GC on the main thread. |
| 980 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 981 | * the garbage collection. |
| 982 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 983 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 984 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 985 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 986 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 987 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 988 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 989 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 990 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 991 | |
| 992 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 993 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* This function is used to restore the Lua context when a coroutine |
| 997 | * fails. This function copy the common memory between old coroutine |
| 998 | * and the new coroutine. The old coroutine is destroyed, and its |
| 999 | * replaced by the new coroutine. |
| 1000 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1001 | * as string error message and it is copied in the new stack. |
| 1002 | */ |
| 1003 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1004 | { |
| 1005 | lua_State *T; |
| 1006 | int new_ref; |
| 1007 | |
| 1008 | /* Renew the main LUA stack doesn't have sense. */ |
| 1009 | if (lua == &gL) |
| 1010 | return 0; |
| 1011 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1012 | /* New Lua coroutine. */ |
| 1013 | T = lua_newthread(gL.T); |
| 1014 | if (!T) |
| 1015 | return 0; |
| 1016 | |
| 1017 | /* Copy last error message. */ |
| 1018 | if (keep_msg) |
| 1019 | lua_xmove(lua->T, T, 1); |
| 1020 | |
| 1021 | /* Copy data between the coroutines. */ |
| 1022 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1023 | lua_xmove(lua->T, T, 1); |
| 1024 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1025 | |
| 1026 | /* Destroy old data. */ |
| 1027 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1028 | |
| 1029 | /* The thread is garbage collected by Lua. */ |
| 1030 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1031 | |
| 1032 | /* Fill the struct with the new coroutine values. */ |
| 1033 | lua->Mref = new_ref; |
| 1034 | lua->T = T; |
| 1035 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1036 | |
| 1037 | /* Set context. */ |
| 1038 | hlua_sethlua(lua); |
| 1039 | |
| 1040 | return 1; |
| 1041 | } |
| 1042 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1043 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1044 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1045 | struct hlua *hlua = hlua_gethlua(L); |
| 1046 | |
| 1047 | /* Lua cannot yield when its returning from a function, |
| 1048 | * so, we can fix the interrupt hook to 1 instruction, |
| 1049 | * expecting that the function is finnished. |
| 1050 | */ |
| 1051 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1052 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | /* restore the interrupt condition. */ |
| 1057 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1058 | |
| 1059 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1060 | * If the state is not yieldable, trying yield causes an error. |
| 1061 | */ |
| 1062 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1063 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1064 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1065 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1066 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1067 | hlua->run_time += now_ms - hlua->start_time; |
| 1068 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1069 | lua_pushfstring(L, "execution timeout"); |
| 1070 | WILL_LJMP(lua_error(L)); |
| 1071 | } |
| 1072 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1073 | /* Update the start time. */ |
| 1074 | hlua->start_time = now_ms; |
| 1075 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1076 | /* Try to interrupt the process at the end of the current |
| 1077 | * unyieldable function. |
| 1078 | */ |
| 1079 | 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] | 1080 | } |
| 1081 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1082 | /* This function start or resumes the Lua stack execution. If the flag |
| 1083 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1084 | * The function return an error. |
| 1085 | * |
| 1086 | * The function can returns 4 values: |
| 1087 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1088 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1089 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1090 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1091 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1092 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1093 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1094 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1095 | * LUA code. |
| 1096 | */ |
| 1097 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1098 | { |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1099 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1100 | int nres; |
| 1101 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1102 | int ret; |
| 1103 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1104 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1106 | /* Initialise run time counter. */ |
| 1107 | if (!HLUA_IS_RUNNING(lua)) |
| 1108 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1109 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1110 | /* Lock the whole Lua execution. This lock must be before the |
| 1111 | * label "resume_execution". |
| 1112 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1113 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1114 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1115 | resume_execution: |
| 1116 | |
| 1117 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1118 | * instructions. it is used for preventing infinite loops. |
| 1119 | */ |
| 1120 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1121 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1122 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1123 | HLUA_SET_RUN(lua); |
| 1124 | HLUA_CLR_CTRLYIELD(lua); |
| 1125 | HLUA_CLR_WAKERESWR(lua); |
| 1126 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1127 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1128 | /* Update the start time. */ |
| 1129 | lua->start_time = now_ms; |
| 1130 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1131 | /* Call the function. */ |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1132 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1133 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1134 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1135 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1136 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1137 | switch (ret) { |
| 1138 | |
| 1139 | case LUA_OK: |
| 1140 | ret = HLUA_E_OK; |
| 1141 | break; |
| 1142 | |
| 1143 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1144 | /* Check if the execution timeout is expired. It it is the case, we |
| 1145 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1146 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1147 | tv_update_date(0, 1); |
| 1148 | lua->run_time += now_ms - lua->start_time; |
| 1149 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1150 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1151 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1152 | break; |
| 1153 | } |
| 1154 | /* Process the forced yield. if the general yield is not allowed or |
| 1155 | * if no task were associated this the current Lua execution |
| 1156 | * coroutine, we resume the execution. Else we want to return in the |
| 1157 | * scheduler and we want to be waked up again, to continue the |
| 1158 | * current Lua execution. So we schedule our own task. |
| 1159 | */ |
| 1160 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1161 | if (!yield_allowed || !lua->task) |
| 1162 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1163 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1164 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1165 | if (!yield_allowed) { |
| 1166 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1167 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1168 | break; |
| 1169 | } |
| 1170 | ret = HLUA_E_AGAIN; |
| 1171 | break; |
| 1172 | |
| 1173 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1174 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1175 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1176 | * because the errors ares the only one mean to return immediately |
| 1177 | * from and lua execution. |
| 1178 | */ |
| 1179 | if (lua->flags & HLUA_EXIT) { |
| 1180 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1181 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1182 | break; |
| 1183 | } |
| 1184 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1185 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1186 | if (!lua_checkstack(lua->T, 1)) { |
| 1187 | ret = HLUA_E_ERR; |
| 1188 | break; |
| 1189 | } |
| 1190 | msg = lua_tostring(lua->T, -1); |
| 1191 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1192 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1193 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1194 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1195 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1196 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1197 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | ret = HLUA_E_ERRMSG; |
| 1199 | break; |
| 1200 | |
| 1201 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1202 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1203 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1204 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | break; |
| 1206 | |
| 1207 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1208 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | if (!lua_checkstack(lua->T, 1)) { |
| 1210 | ret = HLUA_E_ERR; |
| 1211 | break; |
| 1212 | } |
| 1213 | msg = lua_tostring(lua->T, -1); |
| 1214 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1215 | lua_pop(lua->T, 1); |
| 1216 | if (msg) |
| 1217 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1218 | else |
| 1219 | lua_pushfstring(lua->T, "message handler error"); |
| 1220 | ret = HLUA_E_ERRMSG; |
| 1221 | break; |
| 1222 | |
| 1223 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1224 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1225 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1226 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1227 | break; |
| 1228 | } |
| 1229 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1230 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1231 | if (lua->flags & HLUA_MUST_GC && |
| 1232 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1233 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1234 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1235 | switch (ret) { |
| 1236 | case HLUA_E_AGAIN: |
| 1237 | break; |
| 1238 | |
| 1239 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1240 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1241 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1242 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1243 | break; |
| 1244 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1245 | case HLUA_E_ETMOUT: |
| 1246 | case HLUA_E_NOMEM: |
| 1247 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1248 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1249 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1250 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1251 | hlua_ctx_renew(lua, 0); |
| 1252 | break; |
| 1253 | |
| 1254 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1255 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1256 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1257 | break; |
| 1258 | } |
| 1259 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1260 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1261 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1262 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1263 | return ret; |
| 1264 | } |
| 1265 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1266 | /* This function exit the current code. */ |
| 1267 | __LJMP static int hlua_done(lua_State *L) |
| 1268 | { |
| 1269 | struct hlua *hlua = hlua_gethlua(L); |
| 1270 | |
| 1271 | hlua->flags |= HLUA_EXIT; |
| 1272 | WILL_LJMP(lua_error(L)); |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1277 | /* This function is an LUA binding. It provides a function |
| 1278 | * for deleting ACL from a referenced ACL file. |
| 1279 | */ |
| 1280 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1281 | { |
| 1282 | const char *name; |
| 1283 | const char *key; |
| 1284 | struct pat_ref *ref; |
| 1285 | |
| 1286 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1287 | |
| 1288 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1289 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1290 | |
| 1291 | ref = pat_ref_lookup(name); |
| 1292 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1293 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1294 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1295 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1296 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1297 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* This function is an LUA binding. It provides a function |
| 1302 | * for deleting map entry from a referenced map file. |
| 1303 | */ |
| 1304 | static int hlua_del_map(lua_State *L) |
| 1305 | { |
| 1306 | const char *name; |
| 1307 | const char *key; |
| 1308 | struct pat_ref *ref; |
| 1309 | |
| 1310 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1311 | |
| 1312 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1313 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1314 | |
| 1315 | ref = pat_ref_lookup(name); |
| 1316 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1317 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1318 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1319 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1320 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1321 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | /* This function is an LUA binding. It provides a function |
| 1326 | * for adding ACL pattern from a referenced ACL file. |
| 1327 | */ |
| 1328 | static int hlua_add_acl(lua_State *L) |
| 1329 | { |
| 1330 | const char *name; |
| 1331 | const char *key; |
| 1332 | struct pat_ref *ref; |
| 1333 | |
| 1334 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1335 | |
| 1336 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1337 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1338 | |
| 1339 | ref = pat_ref_lookup(name); |
| 1340 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1341 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1342 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1343 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1344 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1345 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1346 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | /* This function is an LUA binding. It provides a function |
| 1351 | * for setting map pattern and sample from a referenced map |
| 1352 | * file. |
| 1353 | */ |
| 1354 | static int hlua_set_map(lua_State *L) |
| 1355 | { |
| 1356 | const char *name; |
| 1357 | const char *key; |
| 1358 | const char *value; |
| 1359 | struct pat_ref *ref; |
| 1360 | |
| 1361 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1362 | |
| 1363 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1364 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1365 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1366 | |
| 1367 | ref = pat_ref_lookup(name); |
| 1368 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1369 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1370 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1371 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1372 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1373 | pat_ref_set(ref, key, value, NULL); |
| 1374 | else |
| 1375 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1376 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1377 | return 0; |
| 1378 | } |
| 1379 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1380 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1381 | * an integer or user data. This data is associated with a metatable. This |
| 1382 | * metatable have an original version registred in the global context with |
| 1383 | * the name of the object (_G[<name>] = <metable> ). |
| 1384 | * |
| 1385 | * A metable is a table that modify the standard behavior of a standard |
| 1386 | * access to the associated data. The entries of this new metatable are |
| 1387 | * defined as is: |
| 1388 | * |
| 1389 | * http://lua-users.org/wiki/MetatableEvents |
| 1390 | * |
| 1391 | * __index |
| 1392 | * |
| 1393 | * we access an absent field in a table, the result is nil. This is |
| 1394 | * true, but it is not the whole truth. Actually, such access triggers |
| 1395 | * the interpreter to look for an __index metamethod: If there is no |
| 1396 | * such method, as usually happens, then the access results in nil; |
| 1397 | * otherwise, the metamethod will provide the result. |
| 1398 | * |
| 1399 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1400 | * the key does not appear in the table, but the metatable has an __index |
| 1401 | * property: |
| 1402 | * |
| 1403 | * - if the value is a function, the function is called, passing in the |
| 1404 | * table and the key; the return value of that function is returned as |
| 1405 | * the result. |
| 1406 | * |
| 1407 | * - if the value is another table, the value of the key in that table is |
| 1408 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1409 | * table's metatable has an __index property, then it continues on up) |
| 1410 | * |
| 1411 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1412 | * |
| 1413 | * http://www.lua.org/pil/13.4.1.html |
| 1414 | * |
| 1415 | * __newindex |
| 1416 | * |
| 1417 | * Like __index, but control property assignment. |
| 1418 | * |
| 1419 | * __mode - Control weak references. A string value with one or both |
| 1420 | * of the characters 'k' and 'v' which specifies that the the |
| 1421 | * keys and/or values in the table are weak references. |
| 1422 | * |
| 1423 | * __call - Treat a table like a function. When a table is followed by |
| 1424 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1425 | * a __call key pointing to a function, that function is invoked |
| 1426 | * (passing any specified arguments) and the return value is |
| 1427 | * returned. |
| 1428 | * |
| 1429 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1430 | * called, if the metatable for myTable has a __metatable |
| 1431 | * key, the value of that key is returned instead of the |
| 1432 | * actual metatable. |
| 1433 | * |
| 1434 | * __tostring - Control string representation. When the builtin |
| 1435 | * "tostring( myTable )" function is called, if the metatable |
| 1436 | * for myTable has a __tostring property set to a function, |
| 1437 | * that function is invoked (passing myTable to it) and the |
| 1438 | * return value is used as the string representation. |
| 1439 | * |
| 1440 | * __len - Control table length. When the table length is requested using |
| 1441 | * the length operator ( '#' ), if the metatable for myTable has |
| 1442 | * a __len key pointing to a function, that function is invoked |
| 1443 | * (passing myTable to it) and the return value used as the value |
| 1444 | * of "#myTable". |
| 1445 | * |
| 1446 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1447 | * collected, if the metatable has a __gc field pointing to a |
| 1448 | * function, that function is first invoked, passing the userdata |
| 1449 | * to it. The __gc metamethod is not called for tables. |
| 1450 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1451 | * |
| 1452 | * Special metamethods for redefining standard operators: |
| 1453 | * http://www.lua.org/pil/13.1.html |
| 1454 | * |
| 1455 | * __add "+" |
| 1456 | * __sub "-" |
| 1457 | * __mul "*" |
| 1458 | * __div "/" |
| 1459 | * __unm "!" |
| 1460 | * __pow "^" |
| 1461 | * __concat ".." |
| 1462 | * |
| 1463 | * Special methods for redfining standar relations |
| 1464 | * http://www.lua.org/pil/13.2.html |
| 1465 | * |
| 1466 | * __eq "==" |
| 1467 | * __lt "<" |
| 1468 | * __le "<=" |
| 1469 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1470 | |
| 1471 | /* |
| 1472 | * |
| 1473 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1474 | * Class Map |
| 1475 | * |
| 1476 | * |
| 1477 | */ |
| 1478 | |
| 1479 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1480 | * a class session, otherwise it throws an error. |
| 1481 | */ |
| 1482 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1483 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1484 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /* This function is the map constructor. It don't need |
| 1488 | * the class Map object. It creates and return a new Map |
| 1489 | * object. It must be called only during "body" or "init" |
| 1490 | * context because it process some filesystem accesses. |
| 1491 | */ |
| 1492 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1493 | { |
| 1494 | const char *fn; |
| 1495 | int match = PAT_MATCH_STR; |
| 1496 | struct sample_conv conv; |
| 1497 | const char *file = ""; |
| 1498 | int line = 0; |
| 1499 | lua_Debug ar; |
| 1500 | char *err = NULL; |
| 1501 | struct arg args[2]; |
| 1502 | |
| 1503 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1504 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1505 | |
| 1506 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1507 | |
| 1508 | if (lua_gettop(L) >= 2) { |
| 1509 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1510 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1511 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1512 | } |
| 1513 | |
| 1514 | /* Get Lua filename and line number. */ |
| 1515 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1516 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1517 | if (ar.currentline > 0) { /* is there info? */ |
| 1518 | file = ar.short_src; |
| 1519 | line = ar.currentline; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* fill fake sample_conv struct. */ |
| 1524 | conv.kw = ""; /* unused. */ |
| 1525 | conv.process = NULL; /* unused. */ |
| 1526 | conv.arg_mask = 0; /* unused. */ |
| 1527 | conv.val_args = NULL; /* unused. */ |
| 1528 | conv.out_type = SMP_T_STR; |
| 1529 | conv.private = (void *)(long)match; |
| 1530 | switch (match) { |
| 1531 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1532 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1533 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1534 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1535 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1536 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1537 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1538 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1539 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1540 | default: |
| 1541 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1542 | } |
| 1543 | |
| 1544 | /* fill fake args. */ |
| 1545 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1546 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1547 | args[1].type = ARGT_STOP; |
| 1548 | |
| 1549 | /* load the map. */ |
| 1550 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1551 | /* error case: we cant use luaL_error because we must |
| 1552 | * free the err variable. |
| 1553 | */ |
| 1554 | luaL_where(L, 1); |
| 1555 | lua_pushfstring(L, "'new': %s.", err); |
| 1556 | lua_concat(L, 2); |
| 1557 | free(err); |
| 1558 | WILL_LJMP(lua_error(L)); |
| 1559 | } |
| 1560 | |
| 1561 | /* create the lua object. */ |
| 1562 | lua_newtable(L); |
| 1563 | lua_pushlightuserdata(L, args[0].data.map); |
| 1564 | lua_rawseti(L, -2, 0); |
| 1565 | |
| 1566 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1567 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1568 | lua_setmetatable(L, -2); |
| 1569 | |
| 1570 | |
| 1571 | return 1; |
| 1572 | } |
| 1573 | |
| 1574 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1575 | { |
| 1576 | struct map_descriptor *desc; |
| 1577 | struct pattern *pat; |
| 1578 | struct sample smp; |
| 1579 | |
| 1580 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1581 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1582 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1583 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1584 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1585 | } |
| 1586 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1587 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1588 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1589 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1593 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1594 | if (str) |
| 1595 | lua_pushstring(L, ""); |
| 1596 | else |
| 1597 | lua_pushnil(L); |
| 1598 | return 1; |
| 1599 | } |
| 1600 | |
| 1601 | /* The Lua pattern must return a string, so we can't check the returned type */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1602 | lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1603 | return 1; |
| 1604 | } |
| 1605 | |
| 1606 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1607 | { |
| 1608 | return _hlua_map_lookup(L, 0); |
| 1609 | } |
| 1610 | |
| 1611 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1612 | { |
| 1613 | return _hlua_map_lookup(L, 1); |
| 1614 | } |
| 1615 | |
| 1616 | /* |
| 1617 | * |
| 1618 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1619 | * Class Socket |
| 1620 | * |
| 1621 | * |
| 1622 | */ |
| 1623 | |
| 1624 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1625 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1626 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /* This function is the handler called for each I/O on the established |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1630 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1631 | * received. |
| 1632 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1633 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1634 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1635 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1636 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1637 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1638 | if (appctx->ctx.hlua_cosocket.die) { |
| 1639 | si_shutw(si); |
| 1640 | si_shutr(si); |
| 1641 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1642 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1643 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1644 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1645 | } |
| 1646 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1647 | /* If the connection object is not available, close all the |
| 1648 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1649 | */ |
| 1650 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | si_shutw(si); |
| 1652 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1653 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1654 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1655 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1656 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1657 | } |
| 1658 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1659 | /* If we cant write, wakeup the pending write signals. */ |
| 1660 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1661 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1662 | |
| 1663 | /* If we cant read, wakeup the pending read signals. */ |
| 1664 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1665 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1666 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1667 | /* if the connection is not estabkished, inform the stream that we want |
| 1668 | * to be notified whenever the connection completes. |
| 1669 | */ |
| 1670 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1671 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1672 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1673 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1674 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1675 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1676 | |
| 1677 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1678 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1679 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1680 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1681 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1682 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | |
| 1684 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1685 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1686 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1687 | |
| 1688 | /* Some data were injected in the buffer, notify the stream |
| 1689 | * interface. |
| 1690 | */ |
| 1691 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1692 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1693 | |
| 1694 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1695 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1696 | */ |
| 1697 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1698 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | } |
| 1700 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1701 | /* This function is called when the "struct stream" is destroyed. |
| 1702 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | * Wake all the pending signals. |
| 1704 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1705 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1707 | struct xref *peer; |
| 1708 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1709 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1710 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1711 | if (peer) |
| 1712 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | |
| 1714 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1715 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1716 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1720 | * uses this object. If the stream does not exists, just quit. |
| 1721 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | * pending signal can rest in the read and write lists. destroy |
| 1723 | * it. |
| 1724 | */ |
| 1725 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1726 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1727 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1728 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1729 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1730 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1731 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1732 | |
| 1733 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1734 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1735 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1736 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1737 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1738 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1739 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1740 | appctx->ctx.hlua_cosocket.die = 1; |
| 1741 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1743 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1744 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1749 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1750 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1751 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1752 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1753 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1754 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1755 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1756 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1757 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1758 | |
| 1759 | /* Check if we run on the same thread than the xreator thread. |
| 1760 | * We cannot access to the socket if the thread is different. |
| 1761 | */ |
| 1762 | if (socket->tid != tid) |
| 1763 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1764 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1765 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1766 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1767 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1768 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1769 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1770 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1771 | appctx->ctx.hlua_cosocket.die = 1; |
| 1772 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1773 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1774 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1775 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1776 | return 0; |
| 1777 | } |
| 1778 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1779 | /* The close function calls close_helper. |
| 1780 | */ |
| 1781 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1782 | { |
| 1783 | MAY_LJMP(check_args(L, 1, "close")); |
| 1784 | return hlua_socket_close_helper(L); |
| 1785 | } |
| 1786 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1787 | /* This Lua function assumes that the stack contain three parameters. |
| 1788 | * 1 - USERDATA containing a struct socket |
| 1789 | * 2 - INTEGER with values of the macro defined below |
| 1790 | * If the integer is -1, we must read at most one line. |
| 1791 | * If the integer is -2, we ust read all the data until the |
| 1792 | * end of the stream. |
| 1793 | * If the integer is positive value, we must read a number of |
| 1794 | * bytes corresponding to this value. |
| 1795 | */ |
| 1796 | #define HLSR_READ_LINE (-1) |
| 1797 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1798 | __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] | 1799 | { |
| 1800 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1801 | int wanted = lua_tointeger(L, 2); |
| 1802 | struct hlua *hlua = hlua_gethlua(L); |
| 1803 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1804 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1805 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1806 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1807 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1808 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1809 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1810 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1811 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1812 | struct stream_interface *si; |
| 1813 | struct stream *s; |
| 1814 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1815 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1816 | |
| 1817 | /* Check if this lua stack is schedulable. */ |
| 1818 | if (!hlua || !hlua->task) |
| 1819 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1820 | "'frontend', 'backend' or 'task'")); |
| 1821 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1822 | /* Check if we run on the same thread than the xreator thread. |
| 1823 | * We cannot access to the socket if the thread is different. |
| 1824 | */ |
| 1825 | if (socket->tid != tid) |
| 1826 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1827 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1828 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1829 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1830 | if (!peer) |
| 1831 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1832 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1833 | si = appctx->owner; |
| 1834 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1836 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1838 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1839 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | if (nblk < 0) /* Connection close. */ |
| 1841 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1842 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1844 | |
| 1845 | /* remove final \r\n. */ |
| 1846 | if (nblk == 1) { |
| 1847 | if (blk1[len1-1] == '\n') { |
| 1848 | len1--; |
| 1849 | skip_at_end++; |
| 1850 | if (blk1[len1-1] == '\r') { |
| 1851 | len1--; |
| 1852 | skip_at_end++; |
| 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | else { |
| 1857 | if (blk2[len2-1] == '\n') { |
| 1858 | len2--; |
| 1859 | skip_at_end++; |
| 1860 | if (blk2[len2-1] == '\r') { |
| 1861 | len2--; |
| 1862 | skip_at_end++; |
| 1863 | } |
| 1864 | } |
| 1865 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1870 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | if (nblk < 0) /* Connection close. */ |
| 1872 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1873 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1874 | goto connection_empty; |
| 1875 | } |
| 1876 | |
| 1877 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1878 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1879 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | if (nblk < 0) /* Connection close. */ |
| 1881 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1882 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1883 | goto connection_empty; |
| 1884 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1885 | missing_bytes = wanted - socket->b.n; |
| 1886 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1888 | len1 = missing_bytes; |
| 1889 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1890 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | len = len1; |
| 1894 | |
| 1895 | luaL_addlstring(&socket->b, blk1, len1); |
| 1896 | if (nblk == 2) { |
| 1897 | len += len2; |
| 1898 | luaL_addlstring(&socket->b, blk2, len2); |
| 1899 | } |
| 1900 | |
| 1901 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1902 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | |
| 1904 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1905 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | |
| 1907 | /* If the pattern reclaim to read all the data |
| 1908 | * in the connection, got out. |
| 1909 | */ |
| 1910 | if (wanted == HLSR_READ_ALL) |
| 1911 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1912 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1913 | goto connection_empty; |
| 1914 | |
| 1915 | /* Return result. */ |
| 1916 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1917 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1918 | return 1; |
| 1919 | |
| 1920 | connection_closed: |
| 1921 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1922 | xref_unlock(&socket->xref, peer); |
| 1923 | |
| 1924 | no_peer: |
| 1925 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | /* If the buffer containds data. */ |
| 1927 | if (socket->b.n > 0) { |
| 1928 | luaL_pushresult(&socket->b); |
| 1929 | return 1; |
| 1930 | } |
| 1931 | lua_pushnil(L); |
| 1932 | lua_pushstring(L, "connection closed."); |
| 1933 | return 2; |
| 1934 | |
| 1935 | connection_empty: |
| 1936 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1937 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1938 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1939 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1940 | } |
| 1941 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1942 | MAY_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] | 1943 | return 0; |
| 1944 | } |
| 1945 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1946 | /* This Lua function gets two parameters. The first one can be string |
| 1947 | * or a number. If the string is "*l", the user requires one line. If |
| 1948 | * the string is "*a", the user requires all the contents of the stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1949 | * If the value is a number, the user require a number of bytes equal |
| 1950 | * to the value. The default value is "*l" (a line). |
| 1951 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1952 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1953 | * integer takes this values: |
| 1954 | * -1 : read a line |
| 1955 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1956 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1958 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | * concatenated with the read data. |
| 1960 | */ |
| 1961 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1962 | { |
| 1963 | int wanted = HLSR_READ_LINE; |
| 1964 | const char *pattern; |
| 1965 | int type; |
| 1966 | char *error; |
| 1967 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1968 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | |
| 1970 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1971 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1972 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1973 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1975 | /* Check if we run on the same thread than the xreator thread. |
| 1976 | * We cannot access to the socket if the thread is different. |
| 1977 | */ |
| 1978 | if (socket->tid != tid) |
| 1979 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1980 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | /* check for pattern. */ |
| 1982 | if (lua_gettop(L) >= 2) { |
| 1983 | type = lua_type(L, 2); |
| 1984 | if (type == LUA_TSTRING) { |
| 1985 | pattern = lua_tostring(L, 2); |
| 1986 | if (strcmp(pattern, "*a") == 0) |
| 1987 | wanted = HLSR_READ_ALL; |
| 1988 | else if (strcmp(pattern, "*l") == 0) |
| 1989 | wanted = HLSR_READ_LINE; |
| 1990 | else { |
| 1991 | wanted = strtoll(pattern, &error, 10); |
| 1992 | if (*error != '\0') |
| 1993 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1994 | } |
| 1995 | } |
| 1996 | else if (type == LUA_TNUMBER) { |
| 1997 | wanted = lua_tointeger(L, 2); |
| 1998 | if (wanted < 0) |
| 1999 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /* Set pattern. */ |
| 2004 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2005 | |
| 2006 | /* Check if we would replace the top by itself. */ |
| 2007 | if (lua_gettop(L) != 2) |
| 2008 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2009 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2010 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | luaL_buffinit(L, &socket->b); |
| 2012 | |
| 2013 | /* Check prefix. */ |
| 2014 | if (lua_gettop(L) >= 3) { |
| 2015 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2016 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2017 | pattern = lua_tolstring(L, 3, &len); |
| 2018 | luaL_addlstring(&socket->b, pattern, len); |
| 2019 | } |
| 2020 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2021 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2025 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2027 | 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] | 2028 | { |
| 2029 | struct hlua_socket *socket; |
| 2030 | struct hlua *hlua = hlua_gethlua(L); |
| 2031 | struct appctx *appctx; |
| 2032 | size_t buf_len; |
| 2033 | const char *buf; |
| 2034 | int len; |
| 2035 | int send_len; |
| 2036 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2037 | struct xref *peer; |
| 2038 | struct stream_interface *si; |
| 2039 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | |
| 2041 | /* Check if this lua stack is schedulable. */ |
| 2042 | if (!hlua || !hlua->task) |
| 2043 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2044 | "'frontend', 'backend' or 'task'")); |
| 2045 | |
| 2046 | /* Get object */ |
| 2047 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2048 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2049 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2051 | /* Check if we run on the same thread than the xreator thread. |
| 2052 | * We cannot access to the socket if the thread is different. |
| 2053 | */ |
| 2054 | if (socket->tid != tid) |
| 2055 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2056 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2057 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2058 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2059 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2060 | lua_pushinteger(L, -1); |
| 2061 | return 1; |
| 2062 | } |
| 2063 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2064 | si = appctx->owner; |
| 2065 | s = si_strm(si); |
| 2066 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2068 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2069 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | lua_pushinteger(L, -1); |
| 2071 | return 1; |
| 2072 | } |
| 2073 | |
| 2074 | /* Update the input buffer data. */ |
| 2075 | buf += sent; |
| 2076 | send_len = buf_len - sent; |
| 2077 | |
| 2078 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2079 | if (sent >= buf_len) { |
| 2080 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2082 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2084 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2085 | * the request buffer if its not required. |
| 2086 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2087 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2088 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2089 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2090 | } |
| 2091 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2092 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2093 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2094 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2096 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | |
| 2098 | /* send data */ |
| 2099 | if (len < send_len) |
| 2100 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2101 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2102 | |
| 2103 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2104 | * the data" (-2) are not expected because the available length |
| 2105 | * is tested. |
| 2106 | * Other unknown error are also not expected. |
| 2107 | */ |
| 2108 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2109 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2110 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2111 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2112 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2114 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2115 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | return 1; |
| 2117 | } |
| 2118 | |
| 2119 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2120 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2121 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2122 | s->req.rex = TICK_ETERNITY; |
| 2123 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2124 | |
| 2125 | /* Update length sent. */ |
| 2126 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2127 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2128 | |
| 2129 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2130 | if (sent + len >= buf_len) { |
| 2131 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2132 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2133 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | |
| 2135 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2136 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2137 | xref_unlock(&socket->xref, peer); |
| 2138 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2139 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2140 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2141 | MAY_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] | 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | /* This function initiate the send of data. It just check the input |
| 2146 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2147 | * amount of data written to the buffer. This is used by the function |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | * "hlua_socket_write_yield" that can yield. |
| 2149 | * |
| 2150 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2151 | * the associated object. The second is a string buffer. The third is |
| 2152 | * a facultative integer that represents where is the buffer position |
| 2153 | * of the start of the data that can send. The first byte is the |
| 2154 | * position "1". The default value is "1". The fourth argument is a |
| 2155 | * facultative integer that represents where is the buffer position |
| 2156 | * of the end of the data that can send. The default is the last byte. |
| 2157 | */ |
| 2158 | static int hlua_socket_send(struct lua_State *L) |
| 2159 | { |
| 2160 | int i; |
| 2161 | int j; |
| 2162 | const char *buf; |
| 2163 | size_t buf_len; |
| 2164 | |
| 2165 | /* Check number of arguments. */ |
| 2166 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2167 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2168 | |
| 2169 | /* Get the string. */ |
| 2170 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2171 | |
| 2172 | /* Get and check j. */ |
| 2173 | if (lua_gettop(L) == 4) { |
| 2174 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2175 | if (j < 0) |
| 2176 | j = buf_len + j + 1; |
| 2177 | if (j > buf_len) |
| 2178 | j = buf_len + 1; |
| 2179 | lua_pop(L, 1); |
| 2180 | } |
| 2181 | else |
| 2182 | j = buf_len; |
| 2183 | |
| 2184 | /* Get and check i. */ |
| 2185 | if (lua_gettop(L) == 3) { |
| 2186 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2187 | if (i < 0) |
| 2188 | i = buf_len + i + 1; |
| 2189 | if (i > buf_len) |
| 2190 | i = buf_len + 1; |
| 2191 | lua_pop(L, 1); |
| 2192 | } else |
| 2193 | i = 1; |
| 2194 | |
| 2195 | /* Check bth i and j. */ |
| 2196 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2197 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | return 1; |
| 2199 | } |
| 2200 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2201 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | return 1; |
| 2203 | } |
| 2204 | if (i == 0) |
| 2205 | i = 1; |
| 2206 | if (j == 0) |
| 2207 | j = 1; |
| 2208 | |
| 2209 | /* Pop the string. */ |
| 2210 | lua_pop(L, 1); |
| 2211 | |
| 2212 | /* Update the buffer length. */ |
| 2213 | buf += i - 1; |
| 2214 | buf_len = j - i + 1; |
| 2215 | lua_pushlstring(L, buf, buf_len); |
| 2216 | |
| 2217 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2218 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2219 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2220 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2221 | } |
| 2222 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2223 | #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] | 2224 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2225 | { |
| 2226 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2227 | int ret; |
| 2228 | int len; |
| 2229 | char *p; |
| 2230 | |
| 2231 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2232 | if (ret <= 0) { |
| 2233 | lua_pushnil(L); |
| 2234 | return 1; |
| 2235 | } |
| 2236 | |
| 2237 | if (ret == AF_UNIX) { |
| 2238 | lua_pushstring(L, buffer+1); |
| 2239 | return 1; |
| 2240 | } |
| 2241 | else if (ret == AF_INET6) { |
| 2242 | buffer[0] = '['; |
| 2243 | len = strlen(buffer); |
| 2244 | buffer[len] = ']'; |
| 2245 | len++; |
| 2246 | buffer[len] = ':'; |
| 2247 | len++; |
| 2248 | p = buffer; |
| 2249 | } |
| 2250 | else if (ret == AF_INET) { |
| 2251 | p = buffer + 1; |
| 2252 | len = strlen(p); |
| 2253 | p[len] = ':'; |
| 2254 | len++; |
| 2255 | } |
| 2256 | else { |
| 2257 | lua_pushnil(L); |
| 2258 | return 1; |
| 2259 | } |
| 2260 | |
| 2261 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2262 | lua_pushnil(L); |
| 2263 | return 1; |
| 2264 | } |
| 2265 | |
| 2266 | lua_pushstring(L, p); |
| 2267 | return 1; |
| 2268 | } |
| 2269 | |
| 2270 | /* Returns information about the peer of the connection. */ |
| 2271 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2272 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2273 | struct hlua_socket *socket; |
| 2274 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2275 | struct xref *peer; |
| 2276 | struct appctx *appctx; |
| 2277 | struct stream_interface *si; |
| 2278 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2280 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2282 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2283 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2285 | /* Check if we run on the same thread than the xreator thread. |
| 2286 | * We cannot access to the socket if the thread is different. |
| 2287 | */ |
| 2288 | if (socket->tid != tid) |
| 2289 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2290 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2291 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2292 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2293 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | lua_pushnil(L); |
| 2295 | return 1; |
| 2296 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2297 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2298 | si = appctx->owner; |
| 2299 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2301 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2303 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | lua_pushnil(L); |
| 2305 | return 1; |
| 2306 | } |
| 2307 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2308 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2309 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2310 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2311 | lua_pushnil(L); |
| 2312 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | } |
| 2314 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2315 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2316 | xref_unlock(&socket->xref, peer); |
| 2317 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /* Returns information about my connection side. */ |
| 2321 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2322 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2323 | struct hlua_socket *socket; |
| 2324 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | struct appctx *appctx; |
| 2326 | struct xref *peer; |
| 2327 | struct stream_interface *si; |
| 2328 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2329 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2330 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2332 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2333 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2335 | /* Check if we run on the same thread than the xreator thread. |
| 2336 | * We cannot access to the socket if the thread is different. |
| 2337 | */ |
| 2338 | if (socket->tid != tid) |
| 2339 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2340 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2341 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2342 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2343 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2344 | lua_pushnil(L); |
| 2345 | return 1; |
| 2346 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2348 | si = appctx->owner; |
| 2349 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2351 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | lua_pushnil(L); |
| 2355 | return 1; |
| 2356 | } |
| 2357 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2358 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2361 | lua_pushnil(L); |
| 2362 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | } |
| 2364 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2365 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2366 | xref_unlock(&socket->xref, peer); |
| 2367 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2371 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | .obj_type = OBJ_TYPE_APPLET, |
| 2373 | .name = "<LUA_TCP>", |
| 2374 | .fct = hlua_socket_handler, |
| 2375 | .release = hlua_socket_release, |
| 2376 | }; |
| 2377 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2378 | __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] | 2379 | { |
| 2380 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2381 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2382 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2384 | struct stream_interface *si; |
| 2385 | struct stream *s; |
| 2386 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2387 | /* Check if we run on the same thread than the xreator thread. |
| 2388 | * We cannot access to the socket if the thread is different. |
| 2389 | */ |
| 2390 | if (socket->tid != tid) |
| 2391 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2392 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2393 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2394 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2395 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2396 | lua_pushnil(L); |
| 2397 | lua_pushstring(L, "Can't connect"); |
| 2398 | return 2; |
| 2399 | } |
| 2400 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2401 | si = appctx->owner; |
| 2402 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2404 | /* Check if we run on the same thread than the xreator thread. |
| 2405 | * We cannot access to the socket if the thread is different. |
| 2406 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2407 | if (socket->tid != tid) { |
| 2408 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2409 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2410 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2411 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2413 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2414 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | lua_pushnil(L); |
| 2416 | lua_pushstring(L, "Can't connect"); |
| 2417 | return 2; |
| 2418 | } |
| 2419 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2420 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2421 | |
| 2422 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2423 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2424 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2425 | lua_pushinteger(L, 1); |
| 2426 | return 1; |
| 2427 | } |
| 2428 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2429 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2430 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2432 | } |
| 2433 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2434 | MAY_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] | 2435 | return 0; |
| 2436 | } |
| 2437 | |
| 2438 | /* This function fail or initite the connection. */ |
| 2439 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2440 | { |
| 2441 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2442 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2443 | const char *ip; |
| 2444 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2445 | struct hlua *hlua; |
| 2446 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2447 | int low, high; |
| 2448 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2449 | struct xref *peer; |
| 2450 | struct stream_interface *si; |
| 2451 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2452 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2453 | if (lua_gettop(L) < 2) |
| 2454 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2455 | |
| 2456 | /* Get args. */ |
| 2457 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2458 | |
| 2459 | /* Check if we run on the same thread than the xreator thread. |
| 2460 | * We cannot access to the socket if the thread is different. |
| 2461 | */ |
| 2462 | if (socket->tid != tid) |
| 2463 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2464 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2466 | if (lua_gettop(L) >= 3) { |
| 2467 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2468 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2470 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2471 | * that are not enclosed within square brackets. |
| 2472 | */ |
| 2473 | if (port > 0) { |
| 2474 | luaL_buffinit(L, &b); |
| 2475 | luaL_addstring(&b, ip); |
| 2476 | luaL_addchar(&b, ':'); |
| 2477 | luaL_pushresult(&b); |
| 2478 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2479 | } |
| 2480 | } |
| 2481 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2482 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2484 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2485 | lua_pushnil(L); |
| 2486 | return 1; |
| 2487 | } |
| 2488 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2489 | si = appctx->owner; |
| 2490 | s = si_strm(si); |
| 2491 | |
| 2492 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2493 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2494 | if (!conn) { |
| 2495 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2497 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2498 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2499 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2500 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2501 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2503 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2504 | if (!addr) { |
| 2505 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2506 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2507 | } |
| 2508 | if (low != high) { |
| 2509 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2510 | 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] | 2511 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2512 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2513 | |
| 2514 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2515 | if (low == 0) { |
| 2516 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2517 | if (port == -1) { |
| 2518 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2519 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2520 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2521 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2522 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2523 | if (port == -1) { |
| 2524 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2525 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2526 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2527 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2528 | } |
| 2529 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2531 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2532 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2533 | |
| 2534 | /* inform the stream that we want to be notified whenever the |
| 2535 | * connection completes. |
| 2536 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2537 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2538 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2539 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2540 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2541 | hlua->flags |= HLUA_MUST_GC; |
| 2542 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2543 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2544 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2545 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2546 | } |
| 2547 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2548 | |
| 2549 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2550 | /* Return yield waiting for connection. */ |
| 2551 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2552 | MAY_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] | 2553 | |
| 2554 | return 0; |
| 2555 | } |
| 2556 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2557 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2558 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2559 | { |
| 2560 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2561 | struct xref *peer; |
| 2562 | struct appctx *appctx; |
| 2563 | struct stream_interface *si; |
| 2564 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2565 | |
| 2566 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2567 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2568 | |
| 2569 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2570 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2572 | lua_pushnil(L); |
| 2573 | return 1; |
| 2574 | } |
| 2575 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2576 | si = appctx->owner; |
| 2577 | s = si_strm(si); |
| 2578 | |
| 2579 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2580 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2581 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2582 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2583 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | |
| 2585 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2586 | { |
| 2587 | return 0; |
| 2588 | } |
| 2589 | |
| 2590 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2591 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2592 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2593 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2594 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2595 | struct xref *peer; |
| 2596 | struct appctx *appctx; |
| 2597 | struct stream_interface *si; |
| 2598 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2599 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2601 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2602 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2603 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2604 | /* convert the timeout to millis */ |
| 2605 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2607 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2608 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2609 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2610 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2611 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2612 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2613 | |
| 2614 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2615 | if (tmout == 0) |
| 2616 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2617 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2618 | /* Check if we run on the same thread than the xreator thread. |
| 2619 | * We cannot access to the socket if the thread is different. |
| 2620 | */ |
| 2621 | if (socket->tid != tid) |
| 2622 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2623 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2624 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2625 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2626 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2627 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2628 | WILL_LJMP(lua_error(L)); |
| 2629 | return 0; |
| 2630 | } |
| 2631 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2632 | si = appctx->owner; |
| 2633 | s = si_strm(si); |
| 2634 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2635 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2636 | s->req.rto = tmout; |
| 2637 | s->req.wto = tmout; |
| 2638 | s->res.rto = tmout; |
| 2639 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2640 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2641 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2642 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2643 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2644 | |
| 2645 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2646 | task_queue(s->task); |
| 2647 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2648 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2649 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2650 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2651 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2655 | { |
| 2656 | struct hlua_socket *socket; |
| 2657 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2658 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2659 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2660 | |
| 2661 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2662 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | hlua_pusherror(L, "socket: full stack"); |
| 2664 | goto out_fail_conf; |
| 2665 | } |
| 2666 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2667 | /* Create the object: obj[0] = userdata. */ |
| 2668 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2669 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2670 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2672 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2673 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2674 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2675 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2676 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2677 | goto out_fail_conf; |
| 2678 | } |
| 2679 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2680 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2681 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2682 | lua_setmetatable(L, -2); |
| 2683 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2684 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2685 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2686 | if (!appctx) { |
| 2687 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2688 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2689 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2690 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2691 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2692 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2693 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2694 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2695 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2696 | /* Now create a session, task and stream for this applet */ |
| 2697 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2698 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2699 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2700 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2701 | } |
| 2702 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2703 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2704 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2706 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | } |
| 2708 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2709 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2710 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2712 | /* Configure "right" stream interface. this "si" is used to connect |
| 2713 | * and retrieve data from the server. The connection is initialized |
| 2714 | * with the "struct server". |
| 2715 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2716 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2717 | |
| 2718 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2719 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2720 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2721 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2722 | return 1; |
| 2723 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2724 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2725 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2726 | out_fail_sess: |
| 2727 | appctx_free(appctx); |
| 2728 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2729 | WILL_LJMP(lua_error(L)); |
| 2730 | return 0; |
| 2731 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2732 | |
| 2733 | /* |
| 2734 | * |
| 2735 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2736 | * Class Channel |
| 2737 | * |
| 2738 | * |
| 2739 | */ |
| 2740 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2741 | /* This function is called before the Lua execution. It stores |
| 2742 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2743 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2744 | 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] | 2745 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2746 | c->mode = stream->be->mode; |
| 2747 | switch (c->mode) { |
| 2748 | case PR_MODE_HTTP: |
| 2749 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2750 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2751 | c->data.http.state = stream->txn->req.msg_state; |
| 2752 | else |
| 2753 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2754 | break; |
| 2755 | default: |
| 2756 | break; |
| 2757 | } |
| 2758 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2759 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2760 | /* This function is called after the Lua execution. it |
| 2761 | * returns true if the parser state is consistent, otherwise, |
| 2762 | * it return false. |
| 2763 | * |
| 2764 | * In HTTP mode, the parser state must be in the same state |
| 2765 | * or greater when we exit the function. Even if we do a |
| 2766 | * control yield. This prevent to break the HTTP message |
| 2767 | * from the Lua code. |
| 2768 | */ |
| 2769 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2770 | { |
| 2771 | if (c->mode != stream->be->mode) |
| 2772 | return 0; |
| 2773 | |
| 2774 | switch (c->mode) { |
| 2775 | case PR_MODE_HTTP: |
| 2776 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2777 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2778 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2779 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2780 | else |
| 2781 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2782 | default: |
| 2783 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2784 | } |
| 2785 | return 1; |
| 2786 | } |
| 2787 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2788 | /* Returns the struct hlua_channel join to the class channel in the |
| 2789 | * stack entry "ud" or throws an argument error. |
| 2790 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2791 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2793 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2794 | } |
| 2795 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2796 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2797 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2798 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2799 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2801 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2802 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2803 | return 0; |
| 2804 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2805 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2806 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2807 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2808 | |
| 2809 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2810 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2811 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2812 | return 1; |
| 2813 | } |
| 2814 | |
| 2815 | /* Duplicate all the data present in the input channel and put it |
| 2816 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2817 | * the stack if the channel is closed and all the data are consumed, |
| 2818 | * returns 0 if no data are available, otherwise it returns the length |
| 2819 | * of the builded string. |
| 2820 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2821 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2822 | { |
| 2823 | char *blk1; |
| 2824 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2825 | size_t len1; |
| 2826 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2827 | int ret; |
| 2828 | luaL_Buffer b; |
| 2829 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2830 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2831 | if (unlikely(ret == 0)) |
| 2832 | return 0; |
| 2833 | |
| 2834 | if (unlikely(ret < 0)) { |
| 2835 | lua_pushnil(L); |
| 2836 | return -1; |
| 2837 | } |
| 2838 | |
| 2839 | luaL_buffinit(L, &b); |
| 2840 | luaL_addlstring(&b, blk1, len1); |
| 2841 | if (unlikely(ret == 2)) |
| 2842 | luaL_addlstring(&b, blk2, len2); |
| 2843 | luaL_pushresult(&b); |
| 2844 | |
| 2845 | if (unlikely(ret == 2)) |
| 2846 | return len1 + len2; |
| 2847 | return len1; |
| 2848 | } |
| 2849 | |
| 2850 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2851 | * a yield. This function keep the data in the buffer. |
| 2852 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2853 | __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] | 2854 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2855 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2856 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2857 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2858 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2859 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2860 | WILL_LJMP(lua_error(L)); |
| 2861 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2862 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2863 | MAY_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] | 2864 | return 1; |
| 2865 | } |
| 2866 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2867 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2868 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2869 | { |
| 2870 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2871 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2872 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2873 | } |
| 2874 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2875 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2876 | * a yield. This function consumes the data in the buffer. It returns |
| 2877 | * a string containing the data or a nil pointer if no data are available |
| 2878 | * and the channel is closed. |
| 2879 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2880 | __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] | 2881 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2882 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2883 | int ret; |
| 2884 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2885 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2886 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2887 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2888 | WILL_LJMP(lua_error(L)); |
| 2889 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2890 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2891 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2892 | MAY_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] | 2893 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | if (unlikely(ret == -1)) |
| 2895 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2896 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2897 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | return 1; |
| 2899 | } |
| 2900 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2901 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2902 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2903 | { |
| 2904 | MAY_LJMP(check_args(L, 1, "get")); |
| 2905 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2906 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2907 | } |
| 2908 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2909 | /* This functions consumes and returns one line. If the channel is closed, |
| 2910 | * and the last data does not contains a final '\n', the data are returned |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2911 | * without the final '\n'. When no more data are available, it returns nil |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | * value. |
| 2913 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2914 | __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] | 2915 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | char *blk1; |
| 2917 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2918 | size_t len1; |
| 2919 | size_t len2; |
| 2920 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2921 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | int ret; |
| 2923 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2924 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2925 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2926 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2927 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2928 | WILL_LJMP(lua_error(L)); |
| 2929 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2930 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2932 | MAY_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] | 2933 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | if (ret == -1) { |
| 2935 | lua_pushnil(L); |
| 2936 | return 1; |
| 2937 | } |
| 2938 | |
| 2939 | luaL_buffinit(L, &b); |
| 2940 | luaL_addlstring(&b, blk1, len1); |
| 2941 | len = len1; |
| 2942 | if (unlikely(ret == 2)) { |
| 2943 | luaL_addlstring(&b, blk2, len2); |
| 2944 | len += len2; |
| 2945 | } |
| 2946 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2947 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2948 | return 1; |
| 2949 | } |
| 2950 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2951 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2952 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2953 | { |
| 2954 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2955 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2956 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2957 | } |
| 2958 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2959 | /* This function takes a string as input, and append it at the |
| 2960 | * input side of channel. If the data is too big, but a space |
| 2961 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2962 | * yields. If the data is bigger than the buffer, or if the |
| 2963 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2964 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2965 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2966 | __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] | 2967 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2968 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2969 | size_t len; |
| 2970 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2971 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2972 | int ret; |
| 2973 | int max; |
| 2974 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2975 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2976 | WILL_LJMP(lua_error(L)); |
| 2977 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2978 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2979 | * the request buffer if its not required. |
| 2980 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2981 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2982 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2983 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2984 | } |
| 2985 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2986 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2987 | if (max > len - l) |
| 2988 | max = len - l; |
| 2989 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2990 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | if (ret == -2 || ret == -3) { |
| 2992 | lua_pushinteger(L, -1); |
| 2993 | return 1; |
| 2994 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2995 | if (ret == -1) { |
| 2996 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2997 | MAY_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] | 2998 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | l += ret; |
| 3000 | lua_pop(L, 1); |
| 3001 | lua_pushinteger(L, l); |
| 3002 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3003 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3004 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3005 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | * in this case, we cannot add more data, so we cannot yield, |
| 3007 | * we return the amount of copyied data. |
| 3008 | */ |
| 3009 | return 1; |
| 3010 | } |
| 3011 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3012 | MAY_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] | 3013 | return 1; |
| 3014 | } |
| 3015 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3016 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3017 | * of the written string, or -1 if the channel is closed or if the |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | * buffer size is too little for the data. |
| 3019 | */ |
| 3020 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3021 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3022 | size_t len; |
| 3023 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3025 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3026 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3027 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3028 | lua_pushinteger(L, 0); |
| 3029 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3030 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | } |
| 3032 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3033 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3035 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3036 | * or -1 if the channel is closed or if the buffer size is too |
| 3037 | * little for the data. |
| 3038 | */ |
| 3039 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3040 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3041 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3042 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3043 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3044 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3045 | lua_pushinteger(L, 0); |
| 3046 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3047 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3048 | WILL_LJMP(lua_error(L)); |
| 3049 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3050 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3051 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3052 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | } |
| 3054 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3055 | /* Append data in the output side of the buffer. This data is immediately |
| 3056 | * sent. The function returns the amount of data written. If the buffer |
| 3057 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3058 | * if the channel is closed. |
| 3059 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3060 | __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] | 3061 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3062 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | size_t len; |
| 3064 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3065 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3066 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3067 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3069 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3070 | WILL_LJMP(lua_error(L)); |
| 3071 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3072 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | lua_pushinteger(L, -1); |
| 3074 | return 1; |
| 3075 | } |
| 3076 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3077 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3078 | * the request buffer if its not required. |
| 3079 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3080 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3081 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3082 | MAY_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] | 3083 | } |
| 3084 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3085 | /* The written data will be immediately sent, so we can check |
| 3086 | * the available space without taking in account the reserve. |
| 3087 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3088 | * data, because the buffer will be flushed. |
| 3089 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3090 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3091 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3092 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3093 | * in this case, we cannot add more data, so we cannot yield, |
| 3094 | * we return the amount of copyied data. |
| 3095 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3096 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3097 | return 1; |
| 3098 | |
| 3099 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | if (max > len - l) |
| 3101 | max = len - l; |
| 3102 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3103 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3104 | * detects a non contiguous buffer and realign it. |
| 3105 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3106 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3107 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3108 | |
| 3109 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3110 | max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3111 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | /* buffer replace considers that the input part is filled. |
| 3113 | * so, I must forward these new data in the output part. |
| 3114 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3115 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | |
| 3117 | l += max; |
| 3118 | lua_pop(L, 1); |
| 3119 | lua_pushinteger(L, l); |
| 3120 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3121 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3122 | * in this case, we cannot add more data, so we cannot yield, |
| 3123 | * we return the amount of copyied data. |
| 3124 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3125 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3126 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3127 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3128 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3129 | if (l < len) { |
| 3130 | /* If we are waiting for space in the response buffer, we |
| 3131 | * must set the flag WAKERESWR. This flag required the task |
| 3132 | * wake up if any activity is detected on the response buffer. |
| 3133 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3134 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3135 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3136 | else |
| 3137 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3138 | MAY_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] | 3139 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3140 | |
| 3141 | return 1; |
| 3142 | } |
| 3143 | |
| 3144 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3145 | * yield the LUA process, and resume it without checking the |
| 3146 | * input arguments. |
| 3147 | */ |
| 3148 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3149 | { |
| 3150 | MAY_LJMP(check_args(L, 2, "send")); |
| 3151 | lua_pushinteger(L, 0); |
| 3152 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3153 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | /* This function forward and amount of butes. The data pass from |
| 3157 | * the input side of the buffer to the output side, and can be |
| 3158 | * forwarded. This function never fails. |
| 3159 | * |
| 3160 | * The Lua function takes an amount of bytes to be forwarded in |
| 3161 | * imput. It returns the number of bytes forwarded. |
| 3162 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3163 | __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] | 3164 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3165 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3166 | int len; |
| 3167 | int l; |
| 3168 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3169 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | |
| 3171 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3172 | |
| 3173 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3174 | WILL_LJMP(lua_error(L)); |
| 3175 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3176 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3177 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3178 | |
| 3179 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3180 | if (max > ci_data(chn)) |
| 3181 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3182 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3183 | l += max; |
| 3184 | |
| 3185 | lua_pop(L, 1); |
| 3186 | lua_pushinteger(L, l); |
| 3187 | |
| 3188 | /* Check if it miss bytes to forward. */ |
| 3189 | if (l < len) { |
| 3190 | /* The the input channel or the output channel are closed, we |
| 3191 | * must return the amount of data forwarded. |
| 3192 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3193 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3194 | return 1; |
| 3195 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3196 | /* If we are waiting for space data in the response buffer, we |
| 3197 | * must set the flag WAKERESWR. This flag required the task |
| 3198 | * wake up if any activity is detected on the response buffer. |
| 3199 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3200 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3201 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3202 | else |
| 3203 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3204 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3205 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3206 | MAY_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] | 3207 | } |
| 3208 | |
| 3209 | return 1; |
| 3210 | } |
| 3211 | |
| 3212 | /* Just check the input and prepare the stack for the previous |
| 3213 | * function "hlua_channel_forward_yield" |
| 3214 | */ |
| 3215 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3216 | { |
| 3217 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3218 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3219 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3220 | |
| 3221 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3222 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | /* Just returns the number of bytes available in the input |
| 3226 | * side of the buffer. This function never fails. |
| 3227 | */ |
| 3228 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3229 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3230 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3231 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3232 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3233 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3234 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3235 | struct htx *htx = htxbuf(&chn->buf); |
| 3236 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3237 | } |
| 3238 | else |
| 3239 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3240 | return 1; |
| 3241 | } |
| 3242 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3243 | /* Returns true if the channel is full. */ |
| 3244 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3245 | { |
| 3246 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3247 | |
| 3248 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3249 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0136ebb | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3250 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3251 | * applet). |
| 3252 | */ |
| 3253 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3254 | return 1; |
| 3255 | } |
| 3256 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3257 | /* Just returns the number of bytes available in the output |
| 3258 | * side of the buffer. This function never fails. |
| 3259 | */ |
| 3260 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3261 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3262 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3263 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3264 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3265 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3266 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3267 | return 1; |
| 3268 | } |
| 3269 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3270 | /* |
| 3271 | * |
| 3272 | * |
| 3273 | * Class Fetches |
| 3274 | * |
| 3275 | * |
| 3276 | */ |
| 3277 | |
| 3278 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3279 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3280 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3281 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3282 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3283 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /* This function creates and push in the stack a fetch object according |
| 3287 | * with a current TXN. |
| 3288 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3289 | 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] | 3290 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3291 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3292 | |
| 3293 | /* Check stack size. */ |
| 3294 | if (!lua_checkstack(L, 3)) |
| 3295 | return 0; |
| 3296 | |
| 3297 | /* Create the object: obj[0] = userdata. |
| 3298 | * Note that the base of the Fetches object is the |
| 3299 | * transaction object. |
| 3300 | */ |
| 3301 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3302 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3303 | lua_rawseti(L, -2, 0); |
| 3304 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3305 | hsmp->s = txn->s; |
| 3306 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3307 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3308 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3309 | |
| 3310 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3311 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3312 | lua_setmetatable(L, -2); |
| 3313 | |
| 3314 | return 1; |
| 3315 | } |
| 3316 | |
| 3317 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3318 | * It uses closure argument to store the associated sample-fetch. It |
| 3319 | * returns only one argument or throws an error. An error is thrown |
| 3320 | * only if an error is encountered during the argument parsing. If |
| 3321 | * the "sample-fetch" function fails, nil is returned. |
| 3322 | */ |
| 3323 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3324 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3325 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3326 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3327 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3328 | int i; |
| 3329 | struct sample smp; |
| 3330 | |
| 3331 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3332 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3333 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3334 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3335 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3336 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3337 | /* Check execution authorization. */ |
| 3338 | if (f->use & SMP_USE_HTTP_ANY && |
| 3339 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3340 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3341 | "is not available in Lua services", f->kw); |
| 3342 | WILL_LJMP(lua_error(L)); |
| 3343 | } |
| 3344 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3345 | /* Get extra arguments. */ |
| 3346 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3347 | if (i >= ARGM_NBARGS) |
| 3348 | break; |
| 3349 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3350 | } |
| 3351 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3352 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3353 | |
| 3354 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3355 | 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] | 3356 | |
| 3357 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3358 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3359 | lua_pushfstring(L, "error in arguments"); |
| 3360 | WILL_LJMP(lua_error(L)); |
| 3361 | } |
| 3362 | |
| 3363 | /* Initialise the sample. */ |
| 3364 | memset(&smp, 0, sizeof(smp)); |
| 3365 | |
| 3366 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3367 | 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] | 3368 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3369 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3370 | lua_pushstring(L, ""); |
| 3371 | else |
| 3372 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3373 | return 1; |
| 3374 | } |
| 3375 | |
| 3376 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3377 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3378 | hlua_smp2lua_str(L, &smp); |
| 3379 | else |
| 3380 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3381 | return 1; |
| 3382 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3383 | |
| 3384 | /* |
| 3385 | * |
| 3386 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3387 | * Class Converters |
| 3388 | * |
| 3389 | * |
| 3390 | */ |
| 3391 | |
| 3392 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3393 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3394 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3395 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3396 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3397 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | /* This function creates and push in the stack a Converters object |
| 3401 | * according with a current TXN. |
| 3402 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3403 | 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] | 3404 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3405 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3406 | |
| 3407 | /* Check stack size. */ |
| 3408 | if (!lua_checkstack(L, 3)) |
| 3409 | return 0; |
| 3410 | |
| 3411 | /* Create the object: obj[0] = userdata. |
| 3412 | * Note that the base of the Converters object is the |
| 3413 | * same than the TXN object. |
| 3414 | */ |
| 3415 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3416 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3417 | lua_rawseti(L, -2, 0); |
| 3418 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3419 | hsmp->s = txn->s; |
| 3420 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3421 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3422 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3423 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3424 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3425 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3426 | lua_setmetatable(L, -2); |
| 3427 | |
| 3428 | return 1; |
| 3429 | } |
| 3430 | |
| 3431 | /* This function is an LUA binding. It is called with each converter. |
| 3432 | * It uses closure argument to store the associated converter. It |
| 3433 | * returns only one argument or throws an error. An error is thrown |
| 3434 | * only if an error is encountered during the argument parsing. If |
| 3435 | * the converter function function fails, nil is returned. |
| 3436 | */ |
| 3437 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3438 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3439 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3440 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3441 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3442 | int i; |
| 3443 | struct sample smp; |
| 3444 | |
| 3445 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3446 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3447 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3448 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3449 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3450 | |
| 3451 | /* Get extra arguments. */ |
| 3452 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3453 | if (i >= ARGM_NBARGS) |
| 3454 | break; |
| 3455 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3456 | } |
| 3457 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3458 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3459 | |
| 3460 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3461 | 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] | 3462 | |
| 3463 | /* Run the special args checker. */ |
| 3464 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3465 | hlua_pusherror(L, "error in arguments"); |
| 3466 | WILL_LJMP(lua_error(L)); |
| 3467 | } |
| 3468 | |
| 3469 | /* Initialise the sample. */ |
| 3470 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3471 | hlua_pusherror(L, "error in the input argument"); |
| 3472 | WILL_LJMP(lua_error(L)); |
| 3473 | } |
| 3474 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3475 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3476 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3477 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3478 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3479 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3480 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3481 | WILL_LJMP(lua_error(L)); |
| 3482 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3483 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3484 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3485 | hlua_pusherror(L, "error during the input argument casting"); |
| 3486 | WILL_LJMP(lua_error(L)); |
| 3487 | } |
| 3488 | |
| 3489 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3490 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3491 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3492 | lua_pushstring(L, ""); |
| 3493 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3494 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3495 | return 1; |
| 3496 | } |
| 3497 | |
| 3498 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3499 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3500 | hlua_smp2lua_str(L, &smp); |
| 3501 | else |
| 3502 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3503 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3504 | } |
| 3505 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3506 | /* |
| 3507 | * |
| 3508 | * |
| 3509 | * Class AppletTCP |
| 3510 | * |
| 3511 | * |
| 3512 | */ |
| 3513 | |
| 3514 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3515 | * a class stream, otherwise it throws an error. |
| 3516 | */ |
| 3517 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3518 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3519 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | /* This function creates and push in the stack an Applet object |
| 3523 | * according with a current TXN. |
| 3524 | */ |
| 3525 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3526 | { |
| 3527 | struct hlua_appctx *appctx; |
| 3528 | struct stream_interface *si = ctx->owner; |
| 3529 | struct stream *s = si_strm(si); |
| 3530 | struct proxy *p = s->be; |
| 3531 | |
| 3532 | /* Check stack size. */ |
| 3533 | if (!lua_checkstack(L, 3)) |
| 3534 | return 0; |
| 3535 | |
| 3536 | /* Create the object: obj[0] = userdata. |
| 3537 | * Note that the base of the Converters object is the |
| 3538 | * same than the TXN object. |
| 3539 | */ |
| 3540 | lua_newtable(L); |
| 3541 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3542 | lua_rawseti(L, -2, 0); |
| 3543 | appctx->appctx = ctx; |
| 3544 | appctx->htxn.s = s; |
| 3545 | appctx->htxn.p = p; |
| 3546 | |
| 3547 | /* Create the "f" field that contains a list of fetches. */ |
| 3548 | lua_pushstring(L, "f"); |
| 3549 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3550 | return 0; |
| 3551 | lua_settable(L, -3); |
| 3552 | |
| 3553 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3554 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3555 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3556 | return 0; |
| 3557 | lua_settable(L, -3); |
| 3558 | |
| 3559 | /* Create the "c" field that contains a list of converters. */ |
| 3560 | lua_pushstring(L, "c"); |
| 3561 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3562 | return 0; |
| 3563 | lua_settable(L, -3); |
| 3564 | |
| 3565 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3566 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3567 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3568 | return 0; |
| 3569 | lua_settable(L, -3); |
| 3570 | |
| 3571 | /* Pop a class stream metatable and affect it to the table. */ |
| 3572 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3573 | lua_setmetatable(L, -2); |
| 3574 | |
| 3575 | return 1; |
| 3576 | } |
| 3577 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3578 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3579 | { |
| 3580 | struct hlua_appctx *appctx; |
| 3581 | struct stream *s; |
| 3582 | const char *name; |
| 3583 | size_t len; |
| 3584 | struct sample smp; |
| 3585 | |
| 3586 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3587 | |
| 3588 | /* It is useles to retrieve the stream, but this function |
| 3589 | * runs only in a stream context. |
| 3590 | */ |
| 3591 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3592 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3593 | s = appctx->htxn.s; |
| 3594 | |
| 3595 | /* Converts the third argument in a sample. */ |
| 3596 | hlua_lua2smp(L, 3, &smp); |
| 3597 | |
| 3598 | /* Store the sample in a variable. */ |
| 3599 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3600 | vars_set_by_name(name, len, &smp); |
| 3601 | return 0; |
| 3602 | } |
| 3603 | |
| 3604 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3605 | { |
| 3606 | struct hlua_appctx *appctx; |
| 3607 | struct stream *s; |
| 3608 | const char *name; |
| 3609 | size_t len; |
| 3610 | struct sample smp; |
| 3611 | |
| 3612 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3613 | |
| 3614 | /* It is useles to retrieve the stream, but this function |
| 3615 | * runs only in a stream context. |
| 3616 | */ |
| 3617 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3618 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3619 | s = appctx->htxn.s; |
| 3620 | |
| 3621 | /* Unset the variable. */ |
| 3622 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3623 | vars_unset_by_name(name, len, &smp); |
| 3624 | return 0; |
| 3625 | } |
| 3626 | |
| 3627 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3628 | { |
| 3629 | struct hlua_appctx *appctx; |
| 3630 | struct stream *s; |
| 3631 | const char *name; |
| 3632 | size_t len; |
| 3633 | struct sample smp; |
| 3634 | |
| 3635 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3636 | |
| 3637 | /* It is useles to retrieve the stream, but this function |
| 3638 | * runs only in a stream context. |
| 3639 | */ |
| 3640 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3641 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3642 | s = appctx->htxn.s; |
| 3643 | |
| 3644 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3645 | if (!vars_get_by_name(name, len, &smp)) { |
| 3646 | lua_pushnil(L); |
| 3647 | return 1; |
| 3648 | } |
| 3649 | |
| 3650 | return hlua_smp2lua(L, &smp); |
| 3651 | } |
| 3652 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3653 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3654 | { |
| 3655 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3656 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3657 | struct hlua *hlua; |
| 3658 | |
| 3659 | /* 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] | 3660 | if (!s->hlua) |
| 3661 | return 0; |
| 3662 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3663 | |
| 3664 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3665 | |
| 3666 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3667 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3668 | |
| 3669 | /* Get and store new value. */ |
| 3670 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3671 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3672 | |
| 3673 | return 0; |
| 3674 | } |
| 3675 | |
| 3676 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3677 | { |
| 3678 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3679 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3680 | struct hlua *hlua; |
| 3681 | |
| 3682 | /* 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] | 3683 | if (!s->hlua) { |
| 3684 | lua_pushnil(L); |
| 3685 | return 1; |
| 3686 | } |
| 3687 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3688 | |
| 3689 | /* Push configuration index in the stack. */ |
| 3690 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3691 | |
| 3692 | return 1; |
| 3693 | } |
| 3694 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3695 | /* If expected data not yet available, it returns a yield. This function |
| 3696 | * consumes the data in the buffer. It returns a string containing the |
| 3697 | * data. This string can be empty. |
| 3698 | */ |
| 3699 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3700 | { |
| 3701 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3702 | struct stream_interface *si = appctx->appctx->owner; |
| 3703 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3704 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3705 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3706 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3707 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3708 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3709 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3710 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3711 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3712 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3713 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3714 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3715 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | /* End of data: commit the total strings and return. */ |
| 3719 | if (ret < 0) { |
| 3720 | luaL_pushresult(&appctx->b); |
| 3721 | return 1; |
| 3722 | } |
| 3723 | |
| 3724 | /* Ensure that the block 2 length is usable. */ |
| 3725 | if (ret == 1) |
| 3726 | len2 = 0; |
| 3727 | |
| 3728 | /* dont check the max length read and dont check. */ |
| 3729 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3730 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3731 | |
| 3732 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3733 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3734 | luaL_pushresult(&appctx->b); |
| 3735 | return 1; |
| 3736 | } |
| 3737 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3738 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3739 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3740 | { |
| 3741 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3742 | |
| 3743 | /* Initialise the string catenation. */ |
| 3744 | luaL_buffinit(L, &appctx->b); |
| 3745 | |
| 3746 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3747 | } |
| 3748 | |
| 3749 | /* If expected data not yet available, it returns a yield. This function |
| 3750 | * consumes the data in the buffer. It returns a string containing the |
| 3751 | * data. This string can be empty. |
| 3752 | */ |
| 3753 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3754 | { |
| 3755 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3756 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3757 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3758 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3759 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3760 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3761 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3762 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3763 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3764 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3765 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3766 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3767 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3768 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3769 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3770 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | /* End of data: commit the total strings and return. */ |
| 3774 | if (ret < 0) { |
| 3775 | luaL_pushresult(&appctx->b); |
| 3776 | return 1; |
| 3777 | } |
| 3778 | |
| 3779 | /* Ensure that the block 2 length is usable. */ |
| 3780 | if (ret == 1) |
| 3781 | len2 = 0; |
| 3782 | |
| 3783 | if (len == -1) { |
| 3784 | |
| 3785 | /* If len == -1, catenate all the data avalaile and |
| 3786 | * yield because we want to get all the data until |
| 3787 | * the end of data stream. |
| 3788 | */ |
| 3789 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3790 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3791 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3792 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3793 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3794 | |
| 3795 | } else { |
| 3796 | |
| 3797 | /* Copy the fisrt block caping to the length required. */ |
| 3798 | if (len1 > len) |
| 3799 | len1 = len; |
| 3800 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3801 | len -= len1; |
| 3802 | |
| 3803 | /* Copy the second block. */ |
| 3804 | if (len2 > len) |
| 3805 | len2 = len; |
| 3806 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3807 | len -= len2; |
| 3808 | |
| 3809 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3810 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3811 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3812 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3813 | if (len > 0) { |
| 3814 | lua_pushinteger(L, len); |
| 3815 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3816 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3817 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | /* return the result. */ |
| 3821 | luaL_pushresult(&appctx->b); |
| 3822 | return 1; |
| 3823 | } |
| 3824 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3825 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3826 | hlua_pusherror(L, "Lua: internal error"); |
| 3827 | WILL_LJMP(lua_error(L)); |
| 3828 | return 0; |
| 3829 | } |
| 3830 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3831 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3832 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3833 | { |
| 3834 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3835 | int len = -1; |
| 3836 | |
| 3837 | if (lua_gettop(L) > 2) |
| 3838 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3839 | if (lua_gettop(L) >= 2) { |
| 3840 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3841 | lua_pop(L, 1); |
| 3842 | } |
| 3843 | |
| 3844 | /* Confirm or set the required length */ |
| 3845 | lua_pushinteger(L, len); |
| 3846 | |
| 3847 | /* Initialise the string catenation. */ |
| 3848 | luaL_buffinit(L, &appctx->b); |
| 3849 | |
| 3850 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3851 | } |
| 3852 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3853 | /* Append data in the output side of the buffer. This data is immediately |
| 3854 | * sent. The function returns the amount of data written. If the buffer |
| 3855 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3856 | * if the channel is closed. |
| 3857 | */ |
| 3858 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3859 | { |
| 3860 | size_t len; |
| 3861 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3862 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3863 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3864 | struct stream_interface *si = appctx->appctx->owner; |
| 3865 | struct channel *chn = si_ic(si); |
| 3866 | int max; |
| 3867 | |
| 3868 | /* Get the max amount of data which can write as input in the channel. */ |
| 3869 | max = channel_recv_max(chn); |
| 3870 | if (max > (len - l)) |
| 3871 | max = len - l; |
| 3872 | |
| 3873 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3874 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3875 | |
| 3876 | /* update counters. */ |
| 3877 | l += max; |
| 3878 | lua_pop(L, 1); |
| 3879 | lua_pushinteger(L, l); |
| 3880 | |
| 3881 | /* If some data is not send, declares the situation to the |
| 3882 | * applet, and returns a yield. |
| 3883 | */ |
| 3884 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3885 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3886 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | return 1; |
| 3890 | } |
| 3891 | |
| 3892 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3893 | * yield the LUA process, and resume it without checking the |
| 3894 | * input arguments. |
| 3895 | */ |
| 3896 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3897 | { |
| 3898 | MAY_LJMP(check_args(L, 2, "send")); |
| 3899 | lua_pushinteger(L, 0); |
| 3900 | |
| 3901 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3902 | } |
| 3903 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3904 | /* |
| 3905 | * |
| 3906 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3907 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3908 | * |
| 3909 | * |
| 3910 | */ |
| 3911 | |
| 3912 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3913 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3914 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3915 | __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] | 3916 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3917 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3918 | } |
| 3919 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3920 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3921 | * according with a current TXN. |
| 3922 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3923 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3924 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3925 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3926 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3927 | struct stream_interface *si = ctx->owner; |
| 3928 | struct stream *s = si_strm(si); |
| 3929 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3930 | |
| 3931 | /* Check stack size. */ |
| 3932 | if (!lua_checkstack(L, 3)) |
| 3933 | return 0; |
| 3934 | |
| 3935 | /* Create the object: obj[0] = userdata. |
| 3936 | * Note that the base of the Converters object is the |
| 3937 | * same than the TXN object. |
| 3938 | */ |
| 3939 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3940 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3941 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3942 | appctx->appctx = ctx; |
| 3943 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3944 | 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] | 3945 | appctx->htxn.s = s; |
| 3946 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3947 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3948 | /* Create the "f" field that contains a list of fetches. */ |
| 3949 | lua_pushstring(L, "f"); |
| 3950 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3951 | return 0; |
| 3952 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3953 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3954 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3955 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3956 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3957 | return 0; |
| 3958 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3959 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3960 | /* Create the "c" field that contains a list of converters. */ |
| 3961 | lua_pushstring(L, "c"); |
| 3962 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3963 | return 0; |
| 3964 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3965 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3966 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3967 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3968 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3969 | return 0; |
| 3970 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3971 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3972 | if (IS_HTX_STRM(s)) { |
| 3973 | /* HTX version */ |
| 3974 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3975 | struct htx_blk *blk; |
| 3976 | struct htx_sl *sl; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3977 | struct ist path; |
| 3978 | unsigned long long len = 0; |
| 3979 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3980 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3981 | blk = htx_get_first_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3982 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3983 | sl = htx_get_blk_ptr(htx, blk); |
| 3984 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3985 | /* Stores the request method. */ |
| 3986 | lua_pushstring(L, "method"); |
| 3987 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3988 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3989 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3990 | /* Stores the http version. */ |
| 3991 | lua_pushstring(L, "version"); |
| 3992 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3993 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3994 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3995 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3996 | * the array on the top of the stack. |
| 3997 | */ |
| 3998 | lua_pushstring(L, "headers"); |
| 3999 | htxn.s = s; |
| 4000 | htxn.p = px; |
| 4001 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4002 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4003 | return 0; |
| 4004 | lua_settable(L, -3); |
| 4005 | |
| 4006 | path = http_get_path(htx_sl_req_uri(sl)); |
| 4007 | if (path.ptr) { |
| 4008 | char *p, *q, *end; |
| 4009 | |
| 4010 | p = path.ptr; |
| 4011 | end = path.ptr + path.len; |
| 4012 | q = p; |
| 4013 | while (q < end && *q != '?') |
| 4014 | q++; |
| 4015 | |
| 4016 | /* Stores the request path. */ |
| 4017 | lua_pushstring(L, "path"); |
| 4018 | lua_pushlstring(L, p, q - p); |
| 4019 | lua_settable(L, -3); |
| 4020 | |
| 4021 | /* Stores the query string. */ |
| 4022 | lua_pushstring(L, "qs"); |
| 4023 | if (*q == '?') |
| 4024 | q++; |
| 4025 | lua_pushlstring(L, q, end - q); |
| 4026 | lua_settable(L, -3); |
| 4027 | } |
| 4028 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4029 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4030 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4031 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4032 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4033 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4034 | break; |
| 4035 | if (type == HTX_BLK_DATA) |
| 4036 | len += htx_get_blksz(blk); |
| 4037 | } |
| 4038 | if (htx->extra != ULLONG_MAX) |
| 4039 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4040 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4041 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4042 | lua_pushstring(L, "length"); |
| 4043 | lua_pushinteger(L, len); |
| 4044 | lua_settable(L, -3); |
| 4045 | } |
| 4046 | else { |
| 4047 | /* Legacy HTTP version */ |
| 4048 | struct http_txn *txn = s->txn; |
| 4049 | const char *path; |
| 4050 | const char *end; |
| 4051 | const char *p; |
| 4052 | |
| 4053 | /* Stores the request method. */ |
| 4054 | lua_pushstring(L, "method"); |
| 4055 | lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4056 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4057 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4058 | /* Stores the http version. */ |
| 4059 | lua_pushstring(L, "version"); |
| 4060 | lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4061 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4062 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4063 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4064 | * the array on the top of the stack. |
| 4065 | */ |
| 4066 | lua_pushstring(L, "headers"); |
| 4067 | htxn.s = s; |
| 4068 | htxn.p = px; |
| 4069 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4070 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4071 | return 0; |
| 4072 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4073 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4074 | /* Get path and qs */ |
| 4075 | path = http_txn_get_path(txn); |
| 4076 | if (path) { |
| 4077 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4078 | p = path; |
| 4079 | while (p < end && *p != '?') |
| 4080 | p++; |
| 4081 | |
| 4082 | /* Stores the request path. */ |
| 4083 | lua_pushstring(L, "path"); |
| 4084 | lua_pushlstring(L, path, p - path); |
| 4085 | lua_settable(L, -3); |
| 4086 | |
| 4087 | /* Stores the query string. */ |
| 4088 | lua_pushstring(L, "qs"); |
| 4089 | if (*p == '?') |
| 4090 | p++; |
| 4091 | lua_pushlstring(L, p, end - p); |
| 4092 | lua_settable(L, -3); |
| 4093 | } |
| 4094 | |
| 4095 | /* Stores the request path. */ |
| 4096 | lua_pushstring(L, "length"); |
| 4097 | lua_pushinteger(L, txn->req.body_len); |
| 4098 | lua_settable(L, -3); |
| 4099 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4100 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4101 | /* Create an empty array of HTTP request headers. */ |
| 4102 | lua_pushstring(L, "response"); |
| 4103 | lua_newtable(L); |
| 4104 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4105 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4106 | /* Pop a class stream metatable and affect it to the table. */ |
| 4107 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4108 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4109 | |
| 4110 | return 1; |
| 4111 | } |
| 4112 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4113 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4114 | { |
| 4115 | struct hlua_appctx *appctx; |
| 4116 | struct stream *s; |
| 4117 | const char *name; |
| 4118 | size_t len; |
| 4119 | struct sample smp; |
| 4120 | |
| 4121 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4122 | |
| 4123 | /* It is useles to retrieve the stream, but this function |
| 4124 | * runs only in a stream context. |
| 4125 | */ |
| 4126 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4127 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4128 | s = appctx->htxn.s; |
| 4129 | |
| 4130 | /* Converts the third argument in a sample. */ |
| 4131 | hlua_lua2smp(L, 3, &smp); |
| 4132 | |
| 4133 | /* Store the sample in a variable. */ |
| 4134 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4135 | vars_set_by_name(name, len, &smp); |
| 4136 | return 0; |
| 4137 | } |
| 4138 | |
| 4139 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4140 | { |
| 4141 | struct hlua_appctx *appctx; |
| 4142 | struct stream *s; |
| 4143 | const char *name; |
| 4144 | size_t len; |
| 4145 | struct sample smp; |
| 4146 | |
| 4147 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4148 | |
| 4149 | /* It is useles to retrieve the stream, but this function |
| 4150 | * runs only in a stream context. |
| 4151 | */ |
| 4152 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4153 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4154 | s = appctx->htxn.s; |
| 4155 | |
| 4156 | /* Unset the variable. */ |
| 4157 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4158 | vars_unset_by_name(name, len, &smp); |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
| 4162 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4163 | { |
| 4164 | struct hlua_appctx *appctx; |
| 4165 | struct stream *s; |
| 4166 | const char *name; |
| 4167 | size_t len; |
| 4168 | struct sample smp; |
| 4169 | |
| 4170 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4171 | |
| 4172 | /* It is useles to retrieve the stream, but this function |
| 4173 | * runs only in a stream context. |
| 4174 | */ |
| 4175 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4176 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4177 | s = appctx->htxn.s; |
| 4178 | |
| 4179 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4180 | if (!vars_get_by_name(name, len, &smp)) { |
| 4181 | lua_pushnil(L); |
| 4182 | return 1; |
| 4183 | } |
| 4184 | |
| 4185 | return hlua_smp2lua(L, &smp); |
| 4186 | } |
| 4187 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4188 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4189 | { |
| 4190 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4191 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4192 | struct hlua *hlua; |
| 4193 | |
| 4194 | /* 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] | 4195 | if (!s->hlua) |
| 4196 | return 0; |
| 4197 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4198 | |
| 4199 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4200 | |
| 4201 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4202 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4203 | |
| 4204 | /* Get and store new value. */ |
| 4205 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4206 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4207 | |
| 4208 | return 0; |
| 4209 | } |
| 4210 | |
| 4211 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4212 | { |
| 4213 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4214 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4215 | struct hlua *hlua; |
| 4216 | |
| 4217 | /* 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] | 4218 | if (!s->hlua) { |
| 4219 | lua_pushnil(L); |
| 4220 | return 1; |
| 4221 | } |
| 4222 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4223 | |
| 4224 | /* Push configuration index in the stack. */ |
| 4225 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4226 | |
| 4227 | return 1; |
| 4228 | } |
| 4229 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4230 | /* If expected data not yet available, it returns a yield. This function |
| 4231 | * consumes the data in the buffer. It returns a string containing the |
| 4232 | * data. This string can be empty. |
| 4233 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4234 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4235 | { |
| 4236 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4237 | struct stream_interface *si = appctx->appctx->owner; |
| 4238 | struct channel *req = si_oc(si); |
| 4239 | struct htx *htx; |
| 4240 | struct htx_blk *blk; |
| 4241 | size_t count; |
| 4242 | int stop = 0; |
| 4243 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4244 | htx = htx_from_buf(&req->buf); |
| 4245 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4246 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4247 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4248 | while (count && !stop && blk) { |
| 4249 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4250 | uint32_t sz = htx_get_blksz(blk); |
| 4251 | struct ist v; |
| 4252 | uint32_t vlen; |
| 4253 | char *nl; |
| 4254 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4255 | if (type == HTX_BLK_EOM) { |
| 4256 | stop = 1; |
| 4257 | break; |
| 4258 | } |
| 4259 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4260 | vlen = sz; |
| 4261 | if (vlen > count) { |
| 4262 | if (type != HTX_BLK_DATA) |
| 4263 | break; |
| 4264 | vlen = count; |
| 4265 | } |
| 4266 | |
| 4267 | switch (type) { |
| 4268 | case HTX_BLK_UNUSED: |
| 4269 | break; |
| 4270 | |
| 4271 | case HTX_BLK_DATA: |
| 4272 | v = htx_get_blk_value(htx, blk); |
| 4273 | v.len = vlen; |
| 4274 | nl = istchr(v, '\n'); |
| 4275 | if (nl != NULL) { |
| 4276 | stop = 1; |
| 4277 | vlen = nl - v.ptr + 1; |
| 4278 | } |
| 4279 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4280 | break; |
| 4281 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4282 | case HTX_BLK_TLR: |
| 4283 | case HTX_BLK_EOM: |
| 4284 | stop = 1; |
| 4285 | break; |
| 4286 | |
| 4287 | default: |
| 4288 | break; |
| 4289 | } |
| 4290 | |
| 4291 | co_set_data(req, co_data(req) - vlen); |
| 4292 | count -= vlen; |
| 4293 | if (sz == vlen) |
| 4294 | blk = htx_remove_blk(htx, blk); |
| 4295 | else { |
| 4296 | htx_cut_data_blk(htx, blk, vlen); |
| 4297 | break; |
| 4298 | } |
| 4299 | } |
| 4300 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4301 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4302 | if (!stop) { |
| 4303 | si_cant_get(si); |
| 4304 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4305 | } |
| 4306 | |
| 4307 | /* return the result. */ |
| 4308 | luaL_pushresult(&appctx->b); |
| 4309 | return 1; |
| 4310 | } |
| 4311 | |
| 4312 | |
| 4313 | /* If expected data not yet available, it returns a yield. This function |
| 4314 | * consumes the data in the buffer. It returns a string containing the |
| 4315 | * data. This string can be empty. |
| 4316 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4317 | __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] | 4318 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4319 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4320 | struct stream_interface *si = appctx->appctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4321 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4322 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4323 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4324 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4325 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4326 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4327 | /* Check for the end of the data. */ |
| 4328 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4329 | luaL_pushresult(&appctx->b); |
| 4330 | return 1; |
| 4331 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4332 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4333 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4334 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4335 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4336 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4337 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4338 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4339 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4340 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4341 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4342 | /* End of data: commit the total strings and return. */ |
| 4343 | if (ret < 0) { |
| 4344 | luaL_pushresult(&appctx->b); |
| 4345 | return 1; |
| 4346 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4347 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4348 | /* Ensure that the block 2 length is usable. */ |
| 4349 | if (ret == 1) |
| 4350 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4351 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4352 | /* Copy the fisrt block caping to the length required. */ |
| 4353 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4354 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4355 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4356 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4357 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4358 | /* Copy the second block. */ |
| 4359 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4360 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4361 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4362 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4363 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4364 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4365 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4366 | luaL_pushresult(&appctx->b); |
| 4367 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4368 | } |
| 4369 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4370 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4371 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4372 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4373 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4374 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4375 | /* Initialise the string catenation. */ |
| 4376 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4377 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4378 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4379 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4380 | else |
| 4381 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4382 | } |
| 4383 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4384 | /* If expected data not yet available, it returns a yield. This function |
| 4385 | * consumes the data in the buffer. It returns a string containing the |
| 4386 | * data. This string can be empty. |
| 4387 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4388 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4389 | { |
| 4390 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4391 | struct stream_interface *si = appctx->appctx->owner; |
| 4392 | struct channel *req = si_oc(si); |
| 4393 | struct htx *htx; |
| 4394 | struct htx_blk *blk; |
| 4395 | size_t count; |
| 4396 | int len; |
| 4397 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4398 | htx = htx_from_buf(&req->buf); |
| 4399 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4400 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4401 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4402 | while (count && len && blk) { |
| 4403 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4404 | uint32_t sz = htx_get_blksz(blk); |
| 4405 | struct ist v; |
| 4406 | uint32_t vlen; |
| 4407 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4408 | if (type == HTX_BLK_EOM) { |
| 4409 | len = 0; |
| 4410 | break; |
| 4411 | } |
| 4412 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4413 | vlen = sz; |
| 4414 | if (len > 0 && vlen > len) |
| 4415 | vlen = len; |
| 4416 | if (vlen > count) { |
| 4417 | if (type != HTX_BLK_DATA) |
| 4418 | break; |
| 4419 | vlen = count; |
| 4420 | } |
| 4421 | |
| 4422 | switch (type) { |
| 4423 | case HTX_BLK_UNUSED: |
| 4424 | break; |
| 4425 | |
| 4426 | case HTX_BLK_DATA: |
| 4427 | v = htx_get_blk_value(htx, blk); |
| 4428 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4429 | break; |
| 4430 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4431 | case HTX_BLK_TLR: |
| 4432 | case HTX_BLK_EOM: |
| 4433 | len = 0; |
| 4434 | break; |
| 4435 | |
| 4436 | default: |
| 4437 | break; |
| 4438 | } |
| 4439 | |
| 4440 | co_set_data(req, co_data(req) - vlen); |
| 4441 | count -= vlen; |
| 4442 | if (len > 0) |
| 4443 | len -= vlen; |
| 4444 | if (sz == vlen) |
| 4445 | blk = htx_remove_blk(htx, blk); |
| 4446 | else { |
| 4447 | htx_cut_data_blk(htx, blk, vlen); |
| 4448 | break; |
| 4449 | } |
| 4450 | } |
| 4451 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4452 | htx_to_buf(htx, &req->buf); |
| 4453 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4454 | /* If we are no other data available, yield waiting for new data. */ |
| 4455 | if (len) { |
| 4456 | if (len > 0) { |
| 4457 | lua_pushinteger(L, len); |
| 4458 | lua_replace(L, 2); |
| 4459 | } |
| 4460 | si_cant_get(si); |
| 4461 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4462 | } |
| 4463 | |
| 4464 | /* return the result. */ |
| 4465 | luaL_pushresult(&appctx->b); |
| 4466 | return 1; |
| 4467 | } |
| 4468 | |
| 4469 | /* If expected data not yet available, it returns a yield. This function |
| 4470 | * consumes the data in the buffer. It returns a string containing the |
| 4471 | * data. This string can be empty. |
| 4472 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4473 | __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] | 4474 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4475 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4476 | struct stream_interface *si = appctx->appctx->owner; |
| 4477 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4478 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4479 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4480 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4481 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4482 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4483 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4484 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4485 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4486 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4487 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4488 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4489 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4490 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | /* End of data: commit the total strings and return. */ |
| 4494 | if (ret < 0) { |
| 4495 | luaL_pushresult(&appctx->b); |
| 4496 | return 1; |
| 4497 | } |
| 4498 | |
| 4499 | /* Ensure that the block 2 length is usable. */ |
| 4500 | if (ret == 1) |
| 4501 | len2 = 0; |
| 4502 | |
| 4503 | /* Copy the fisrt block caping to the length required. */ |
| 4504 | if (len1 > len) |
| 4505 | len1 = len; |
| 4506 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4507 | len -= len1; |
| 4508 | |
| 4509 | /* Copy the second block. */ |
| 4510 | if (len2 > len) |
| 4511 | len2 = len; |
| 4512 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4513 | len -= len2; |
| 4514 | |
| 4515 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4516 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4517 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4518 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4519 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4520 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4521 | if (len > 0) { |
| 4522 | lua_pushinteger(L, len); |
| 4523 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4524 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4525 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | /* return the result. */ |
| 4529 | luaL_pushresult(&appctx->b); |
| 4530 | return 1; |
| 4531 | } |
| 4532 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4533 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4534 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4535 | { |
| 4536 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4537 | int len = -1; |
| 4538 | |
| 4539 | /* Check arguments. */ |
| 4540 | if (lua_gettop(L) > 2) |
| 4541 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4542 | if (lua_gettop(L) >= 2) { |
| 4543 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4544 | lua_pop(L, 1); |
| 4545 | } |
| 4546 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4547 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4548 | /* HTX version */ |
| 4549 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4550 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4551 | /* Initialise the string catenation. */ |
| 4552 | luaL_buffinit(L, &appctx->b); |
| 4553 | |
| 4554 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4555 | } |
| 4556 | else { |
| 4557 | /* Legacy HTTP version */ |
| 4558 | /* Check the required length */ |
| 4559 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4560 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4561 | lua_pushinteger(L, len); |
| 4562 | |
| 4563 | /* Initialise the string catenation. */ |
| 4564 | luaL_buffinit(L, &appctx->b); |
| 4565 | |
| 4566 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4567 | } |
| 4568 | } |
| 4569 | |
| 4570 | /* Append data in the output side of the buffer. This data is immediately |
| 4571 | * sent. The function returns the amount of data written. If the buffer |
| 4572 | * cannot contain the data, the function yields. The function returns -1 |
| 4573 | * if the channel is closed. |
| 4574 | */ |
| 4575 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4576 | { |
| 4577 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4578 | struct stream_interface *si = appctx->appctx->owner; |
| 4579 | struct channel *res = si_ic(si); |
| 4580 | struct htx *htx = htx_from_buf(&res->buf); |
| 4581 | const char *data; |
| 4582 | size_t len; |
| 4583 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4584 | int max; |
| 4585 | |
Christopher Faulet | 4e96301 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4586 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4587 | if (!max) |
| 4588 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4589 | |
| 4590 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4591 | |
| 4592 | /* Get the max amount of data which can write as input in the channel. */ |
| 4593 | if (max > (len - l)) |
| 4594 | max = len - l; |
| 4595 | |
| 4596 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4597 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4598 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4599 | |
| 4600 | /* update counters. */ |
| 4601 | l += max; |
| 4602 | lua_pop(L, 1); |
| 4603 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4604 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4605 | /* If some data is not send, declares the situation to the |
| 4606 | * applet, and returns a yield. |
| 4607 | */ |
| 4608 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4609 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4610 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4611 | si_rx_room_blk(si); |
| 4612 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4613 | } |
| 4614 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4615 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4616 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4617 | } |
| 4618 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4619 | /* Append data in the output side of the buffer. This data is immediately |
| 4620 | * sent. The function returns the amount of data written. If the buffer |
| 4621 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4622 | * if the channel is closed. |
| 4623 | */ |
| 4624 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4625 | { |
| 4626 | size_t len; |
| 4627 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4628 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4629 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4630 | struct stream_interface *si = appctx->appctx->owner; |
| 4631 | struct channel *chn = si_ic(si); |
| 4632 | int max; |
| 4633 | |
| 4634 | /* Get the max amount of data which can write as input in the channel. */ |
| 4635 | max = channel_recv_max(chn); |
| 4636 | if (max > (len - l)) |
| 4637 | max = len - l; |
| 4638 | |
| 4639 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4640 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4641 | |
| 4642 | /* update counters. */ |
| 4643 | l += max; |
| 4644 | lua_pop(L, 1); |
| 4645 | lua_pushinteger(L, l); |
| 4646 | |
| 4647 | /* If some data is not send, declares the situation to the |
| 4648 | * applet, and returns a yield. |
| 4649 | */ |
| 4650 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4651 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4652 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | return 1; |
| 4656 | } |
| 4657 | |
| 4658 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4659 | * yield the LUA process, and resume it without checking the |
| 4660 | * input arguments. |
| 4661 | */ |
| 4662 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4663 | { |
| 4664 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4665 | |
| 4666 | /* We want to send some data. Headers must be sent. */ |
| 4667 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4668 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4669 | WILL_LJMP(lua_error(L)); |
| 4670 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4671 | |
| 4672 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4673 | /* HTX version */ |
| 4674 | /* This interger is used for followinf the amount of data sent. */ |
| 4675 | lua_pushinteger(L, 0); |
| 4676 | |
| 4677 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4678 | } |
| 4679 | else { |
| 4680 | /* Legacy HTTP version */ |
| 4681 | size_t len; |
| 4682 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4683 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4684 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4685 | |
| 4686 | /* If transfer encoding chunked is selected, we surround the data |
| 4687 | * by chunk data. |
| 4688 | */ |
| 4689 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4690 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4691 | lua_pushfstring(L, "%s\r\n", hex); |
| 4692 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4693 | lua_pushstring(L, "\r\n"); |
| 4694 | lua_concat(L, 3); |
| 4695 | } |
| 4696 | |
| 4697 | /* This interger is used for followinf the amount of data sent. */ |
| 4698 | lua_pushinteger(L, 0); |
| 4699 | |
| 4700 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4701 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4705 | { |
| 4706 | const char *name; |
| 4707 | int ret; |
| 4708 | |
| 4709 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4710 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4711 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4712 | |
| 4713 | /* Push in the stack the "response" entry. */ |
| 4714 | ret = lua_getfield(L, 1, "response"); |
| 4715 | if (ret != LUA_TTABLE) { |
| 4716 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4717 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4718 | WILL_LJMP(lua_error(L)); |
| 4719 | } |
| 4720 | |
| 4721 | /* check if the header is already registered if it is not |
| 4722 | * the case, register it. |
| 4723 | */ |
| 4724 | ret = lua_getfield(L, -1, name); |
| 4725 | if (ret == LUA_TNIL) { |
| 4726 | |
| 4727 | /* Entry not found. */ |
| 4728 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4729 | |
| 4730 | /* Insert the new header name in the array in the top of the stack. |
| 4731 | * It left the new array in the top of the stack. |
| 4732 | */ |
| 4733 | lua_newtable(L); |
| 4734 | lua_pushvalue(L, 2); |
| 4735 | lua_pushvalue(L, -2); |
| 4736 | lua_settable(L, -4); |
| 4737 | |
| 4738 | } else if (ret != LUA_TTABLE) { |
| 4739 | |
| 4740 | /* corruption error. */ |
| 4741 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4742 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4743 | WILL_LJMP(lua_error(L)); |
| 4744 | } |
| 4745 | |
| 4746 | /* Now the top od thestack is an array of values. We push |
| 4747 | * the header value as new entry. |
| 4748 | */ |
| 4749 | lua_pushvalue(L, 3); |
| 4750 | ret = lua_rawlen(L, -2); |
| 4751 | lua_rawseti(L, -2, ret + 1); |
| 4752 | lua_pushboolean(L, 1); |
| 4753 | return 1; |
| 4754 | } |
| 4755 | |
| 4756 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4757 | { |
| 4758 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4759 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4760 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4761 | |
| 4762 | if (status < 100 || status > 599) { |
| 4763 | lua_pushboolean(L, 0); |
| 4764 | return 1; |
| 4765 | } |
| 4766 | |
| 4767 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4768 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4769 | lua_pushboolean(L, 1); |
| 4770 | return 1; |
| 4771 | } |
| 4772 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4773 | |
| 4774 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4775 | { |
| 4776 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4777 | struct stream_interface *si = appctx->appctx->owner; |
| 4778 | struct channel *res = si_ic(si); |
| 4779 | struct htx *htx; |
| 4780 | struct htx_sl *sl; |
| 4781 | struct h1m h1m; |
| 4782 | const char *status, *reason; |
| 4783 | const char *name, *value; |
| 4784 | size_t nlen, vlen; |
| 4785 | unsigned int flags; |
| 4786 | |
| 4787 | /* Send the message at once. */ |
| 4788 | htx = htx_from_buf(&res->buf); |
| 4789 | h1m_init_res(&h1m); |
| 4790 | |
| 4791 | /* Use the same http version than the request. */ |
| 4792 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4793 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4794 | if (reason == NULL) |
| 4795 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4796 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4797 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4798 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4799 | } |
| 4800 | else { |
| 4801 | flags = HTX_SL_F_IS_RESP; |
| 4802 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4803 | } |
| 4804 | if (!sl) { |
| 4805 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4806 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4807 | WILL_LJMP(lua_error(L)); |
| 4808 | } |
| 4809 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4810 | |
| 4811 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4812 | lua_pushvalue(L, 0); |
| 4813 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4814 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4815 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4816 | WILL_LJMP(lua_error(L)); |
| 4817 | } |
| 4818 | |
| 4819 | /* Browse the list of headers. */ |
| 4820 | lua_pushnil(L); |
| 4821 | while(lua_next(L, -2) != 0) { |
| 4822 | /* We expect a string as -2. */ |
| 4823 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4824 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4825 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4826 | lua_typename(L, lua_type(L, -2))); |
| 4827 | WILL_LJMP(lua_error(L)); |
| 4828 | } |
| 4829 | name = lua_tolstring(L, -2, &nlen); |
| 4830 | |
| 4831 | /* We expect an array as -1. */ |
| 4832 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4833 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4834 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4835 | name, |
| 4836 | lua_typename(L, lua_type(L, -1))); |
| 4837 | WILL_LJMP(lua_error(L)); |
| 4838 | } |
| 4839 | |
| 4840 | /* Browse the table who is on the top of the stack. */ |
| 4841 | lua_pushnil(L); |
| 4842 | while(lua_next(L, -2) != 0) { |
| 4843 | int id; |
| 4844 | |
| 4845 | /* We expect a number as -2. */ |
| 4846 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4847 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4848 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4849 | name, |
| 4850 | lua_typename(L, lua_type(L, -2))); |
| 4851 | WILL_LJMP(lua_error(L)); |
| 4852 | } |
| 4853 | id = lua_tointeger(L, -2); |
| 4854 | |
| 4855 | /* We expect a string as -2. */ |
| 4856 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4857 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4858 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4859 | name, id, |
| 4860 | lua_typename(L, lua_type(L, -1))); |
| 4861 | WILL_LJMP(lua_error(L)); |
| 4862 | } |
| 4863 | value = lua_tolstring(L, -1, &vlen); |
| 4864 | |
| 4865 | /* Simple Protocol checks. */ |
| 4866 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4867 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4868 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4869 | struct ist v = ist2(value, vlen); |
| 4870 | int ret; |
| 4871 | |
| 4872 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4873 | if (ret < 0) { |
| 4874 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4875 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4876 | name); |
| 4877 | WILL_LJMP(lua_error(L)); |
| 4878 | } |
| 4879 | else if (ret == 0) |
| 4880 | goto next; /* Skip it */ |
| 4881 | } |
| 4882 | |
| 4883 | /* Add a new header */ |
| 4884 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4885 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4886 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4887 | name); |
| 4888 | WILL_LJMP(lua_error(L)); |
| 4889 | } |
| 4890 | next: |
| 4891 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4892 | lua_pop(L, 1); |
| 4893 | } |
| 4894 | |
| 4895 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4896 | lua_pop(L, 1); |
| 4897 | } |
| 4898 | |
| 4899 | if (h1m.flags & H1_MF_CHNK) |
| 4900 | h1m.flags &= ~H1_MF_CLEN; |
| 4901 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4902 | h1m.flags |= H1_MF_XFER_LEN; |
| 4903 | |
| 4904 | /* Uset HTX start-line flags */ |
| 4905 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4906 | flags |= HTX_SL_F_XFER_ENC; |
| 4907 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4908 | flags |= HTX_SL_F_XFER_LEN; |
| 4909 | if (h1m.flags & H1_MF_CHNK) |
| 4910 | flags |= HTX_SL_F_CHNK; |
| 4911 | else if (h1m.flags & H1_MF_CLEN) |
| 4912 | flags |= HTX_SL_F_CLEN; |
| 4913 | if (h1m.body_len == 0) |
| 4914 | flags |= HTX_SL_F_BODYLESS; |
| 4915 | } |
| 4916 | sl->flags |= flags; |
| 4917 | |
| 4918 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4919 | * and the status code implies the presence of a message body, we must |
| 4920 | * announce a transfer encoding chunked. This is required by haproxy |
| 4921 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4922 | * chunked itslef, don't do anything. |
| 4923 | */ |
| 4924 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4925 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4926 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4927 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4928 | /* Add a new header */ |
| 4929 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4930 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4931 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4932 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4933 | WILL_LJMP(lua_error(L)); |
| 4934 | } |
| 4935 | } |
| 4936 | |
| 4937 | /* Finalize headers. */ |
| 4938 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4939 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4940 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4941 | WILL_LJMP(lua_error(L)); |
| 4942 | } |
| 4943 | |
| 4944 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4945 | b_reset(&res->buf); |
| 4946 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4947 | WILL_LJMP(lua_error(L)); |
| 4948 | } |
| 4949 | |
| 4950 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4951 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4952 | |
| 4953 | /* Headers sent, set the flag. */ |
| 4954 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4955 | return 0; |
| 4956 | |
| 4957 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4958 | /* We will build the status line and the headers of the HTTP response. |
| 4959 | * We will try send at once if its not possible, we give back the hand |
| 4960 | * waiting for more room. |
| 4961 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4962 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4963 | { |
| 4964 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4965 | struct stream_interface *si = appctx->appctx->owner; |
| 4966 | struct channel *res = si_ic(si); |
| 4967 | |
| 4968 | if (co_data(res)) { |
| 4969 | si_rx_room_blk(si); |
| 4970 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 4971 | } |
| 4972 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 4973 | } |
| 4974 | |
| 4975 | |
| 4976 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 4977 | { |
| 4978 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 4979 | } |
| 4980 | |
| 4981 | /* We will build the status line and the headers of the HTTP response. |
| 4982 | * We will try send at once if its not possible, we give back the hand |
| 4983 | * waiting for more room. |
| 4984 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4985 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4986 | { |
| 4987 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4988 | struct stream_interface *si = appctx->appctx->owner; |
| 4989 | struct channel *chn = si_ic(si); |
| 4990 | int ret; |
| 4991 | size_t len; |
| 4992 | const char *msg; |
| 4993 | |
| 4994 | /* Get the message as the first argument on the stack. */ |
| 4995 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4996 | |
| 4997 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4998 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4999 | |
| 5000 | /* if ret == -2 or -3 the channel closed or the message si too |
| 5001 | * big for the buffers. |
| 5002 | */ |
| 5003 | if (ret == -2 || ret == -3) { |
| 5004 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5005 | WILL_LJMP(lua_error(L)); |
| 5006 | } |
| 5007 | |
| 5008 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 5009 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 5010 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5011 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | /* Headers sent, set the flag. */ |
| 5015 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 5016 | return 0; |
| 5017 | } |
| 5018 | |
| 5019 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 5020 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5021 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5022 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5023 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5024 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5025 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5026 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5027 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5028 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5029 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5030 | const char *reason; |
| 5031 | |
| 5032 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5033 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5034 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5035 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5036 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5037 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5038 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5039 | tmp = get_trash_chunk(); |
| 5040 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5041 | /* Use the same http version than the request. */ |
| 5042 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5043 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5044 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5045 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5046 | |
| 5047 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5048 | lua_pushvalue(L, 0); |
| 5049 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5050 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5051 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5052 | WILL_LJMP(lua_error(L)); |
| 5053 | } |
| 5054 | |
| 5055 | /* Browse the list of headers. */ |
| 5056 | lua_pushnil(L); |
| 5057 | while(lua_next(L, -2) != 0) { |
| 5058 | |
| 5059 | /* We expect a string as -2. */ |
| 5060 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5061 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5062 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5063 | lua_typename(L, lua_type(L, -2))); |
| 5064 | WILL_LJMP(lua_error(L)); |
| 5065 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5066 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5067 | |
| 5068 | /* We expect an array as -1. */ |
| 5069 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5070 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5071 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5072 | name, |
| 5073 | lua_typename(L, lua_type(L, -1))); |
| 5074 | WILL_LJMP(lua_error(L)); |
| 5075 | } |
| 5076 | |
| 5077 | /* Browse the table who is on the top of the stack. */ |
| 5078 | lua_pushnil(L); |
| 5079 | while(lua_next(L, -2) != 0) { |
| 5080 | |
| 5081 | /* We expect a number as -2. */ |
| 5082 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5083 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5084 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5085 | name, |
| 5086 | lua_typename(L, lua_type(L, -2))); |
| 5087 | WILL_LJMP(lua_error(L)); |
| 5088 | } |
| 5089 | id = lua_tointeger(L, -2); |
| 5090 | |
| 5091 | /* We expect a string as -2. */ |
| 5092 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5093 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5094 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5095 | name, id, |
| 5096 | lua_typename(L, lua_type(L, -1))); |
| 5097 | WILL_LJMP(lua_error(L)); |
| 5098 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5099 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5100 | |
| 5101 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5102 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5103 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5104 | tmp->data += name_len; |
| 5105 | tmp->area[tmp->data++] = ':'; |
| 5106 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5107 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5108 | memcpy(tmp->area + tmp->data, value, |
| 5109 | value_len); |
| 5110 | tmp->data += value_len; |
| 5111 | tmp->area[tmp->data++] = '\r'; |
| 5112 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5113 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5114 | |
| 5115 | /* Protocol checks. */ |
| 5116 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5117 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5118 | * is done without control. If it contains a bad value, |
| 5119 | * the content-length remains negative so that we can |
| 5120 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5121 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5122 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5123 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5124 | |
| 5125 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5126 | if (name_len == 17 && value_len == 7 && |
| 5127 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5128 | strcasecmp("chunked", value) == 0) |
| 5129 | hdr_chunked = 1; |
| 5130 | |
| 5131 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5132 | lua_pop(L, 1); |
| 5133 | } |
| 5134 | |
| 5135 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5136 | lua_pop(L, 1); |
| 5137 | } |
| 5138 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5139 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5140 | * and the status code implies the presence of a message body, we must |
| 5141 | * announce a transfer encoding chunked. This is required by haproxy |
| 5142 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5143 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5144 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5145 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5146 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5147 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5148 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5149 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5150 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5151 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5152 | } |
| 5153 | |
| 5154 | /* Finalize headers. */ |
| 5155 | chunk_appendf(tmp, "\r\n"); |
| 5156 | |
| 5157 | /* Remove the last entry and the array of headers */ |
| 5158 | lua_pop(L, 2); |
| 5159 | |
| 5160 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5161 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5162 | |
| 5163 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5164 | } |
| 5165 | |
| 5166 | /* |
| 5167 | * |
| 5168 | * |
| 5169 | * Class HTTP |
| 5170 | * |
| 5171 | * |
| 5172 | */ |
| 5173 | |
| 5174 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5175 | * a class stream, otherwise it throws an error. |
| 5176 | */ |
| 5177 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5178 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5179 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | /* This function creates and push in the stack a HTTP object |
| 5183 | * according with a current TXN. |
| 5184 | */ |
| 5185 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5186 | { |
| 5187 | struct hlua_txn *htxn; |
| 5188 | |
| 5189 | /* Check stack size. */ |
| 5190 | if (!lua_checkstack(L, 3)) |
| 5191 | return 0; |
| 5192 | |
| 5193 | /* Create the object: obj[0] = userdata. |
| 5194 | * Note that the base of the Converters object is the |
| 5195 | * same than the TXN object. |
| 5196 | */ |
| 5197 | lua_newtable(L); |
| 5198 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5199 | lua_rawseti(L, -2, 0); |
| 5200 | |
| 5201 | htxn->s = txn->s; |
| 5202 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5203 | htxn->dir = txn->dir; |
| 5204 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5205 | |
| 5206 | /* Pop a class stream metatable and affect it to the table. */ |
| 5207 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5208 | lua_setmetatable(L, -2); |
| 5209 | |
| 5210 | return 1; |
| 5211 | } |
| 5212 | |
| 5213 | /* This function creates ans returns an array of HTTP headers. |
| 5214 | * This function does not fails. It is used as wrapper with the |
| 5215 | * 2 following functions. |
| 5216 | */ |
| 5217 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5218 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5219 | /* Create the table. */ |
| 5220 | lua_newtable(L); |
| 5221 | |
| 5222 | if (!htxn->s->txn) |
| 5223 | return 1; |
| 5224 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5225 | if (IS_HTX_STRM(htxn->s)) { |
| 5226 | /* HTX version */ |
| 5227 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5228 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5229 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5230 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5231 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5232 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5233 | struct ist n, v; |
| 5234 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5235 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5236 | if (type == HTX_BLK_HDR) { |
| 5237 | n = htx_get_blk_name(htx,blk); |
| 5238 | v = htx_get_blk_value(htx, blk); |
| 5239 | } |
| 5240 | else if (type == HTX_BLK_EOH) |
| 5241 | break; |
| 5242 | else |
| 5243 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5244 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5245 | /* Check for existing entry: |
| 5246 | * assume that the table is on the top of the stack, and |
| 5247 | * push the key in the stack, the function lua_gettable() |
| 5248 | * perform the lookup. |
| 5249 | */ |
| 5250 | lua_pushlstring(L, n.ptr, n.len); |
| 5251 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5252 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5253 | switch (lua_type(L, -1)) { |
| 5254 | case LUA_TNIL: |
| 5255 | /* Table not found, create it. */ |
| 5256 | lua_pop(L, 1); /* remove the nil value. */ |
| 5257 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5258 | lua_newtable(L); /* create and push empty table. */ |
| 5259 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5260 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5261 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5262 | break; |
| 5263 | |
| 5264 | case LUA_TTABLE: |
| 5265 | /* Entry found: push the value in the table. */ |
| 5266 | len = lua_rawlen(L, -1); |
| 5267 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5268 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5269 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5270 | break; |
| 5271 | |
| 5272 | default: |
| 5273 | /* Other cases are errors. */ |
| 5274 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5275 | WILL_LJMP(lua_error(L)); |
| 5276 | } |
| 5277 | } |
| 5278 | } |
| 5279 | else { |
| 5280 | /* Legacy HTTP version */ |
| 5281 | const char *cur_ptr, *cur_next, *p; |
| 5282 | int old_idx, cur_idx; |
| 5283 | struct hdr_idx_elem *cur_hdr; |
| 5284 | const char *hn, *hv; |
| 5285 | int hnl, hvl; |
| 5286 | const char *in; |
| 5287 | char *out; |
| 5288 | int len; |
| 5289 | |
| 5290 | /* Build array of headers. */ |
| 5291 | old_idx = 0; |
| 5292 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5293 | |
| 5294 | while (1) { |
| 5295 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5296 | if (!cur_idx) |
| 5297 | break; |
| 5298 | old_idx = cur_idx; |
| 5299 | |
| 5300 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5301 | cur_ptr = cur_next; |
| 5302 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5303 | |
| 5304 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5305 | * and the next header starts at cur_next. We'll check |
| 5306 | * this header in the list as well as against the default |
| 5307 | * rule. |
| 5308 | */ |
| 5309 | |
| 5310 | /* look for ': *'. */ |
| 5311 | hn = cur_ptr; |
| 5312 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5313 | if (p >= cur_ptr+cur_hdr->len) |
| 5314 | continue; |
| 5315 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5316 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5317 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5318 | p++; |
| 5319 | if (p >= cur_ptr+cur_hdr->len) |
| 5320 | continue; |
| 5321 | hv = p; |
| 5322 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5323 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5324 | /* Lowercase the key. Don't check the size of trash, it have |
| 5325 | * the size of one buffer and the input data contains in one |
| 5326 | * buffer. |
| 5327 | */ |
| 5328 | out = trash.area; |
| 5329 | for (in=hn; in<hn+hnl; in++, out++) |
| 5330 | *out = tolower(*in); |
| 5331 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5332 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5333 | /* Check for existing entry: |
| 5334 | * assume that the table is on the top of the stack, and |
| 5335 | * push the key in the stack, the function lua_gettable() |
| 5336 | * perform the lookup. |
| 5337 | */ |
| 5338 | lua_pushlstring(L, trash.area, hnl); |
| 5339 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5340 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5341 | switch (lua_type(L, -1)) { |
| 5342 | case LUA_TNIL: |
| 5343 | /* Table not found, create it. */ |
| 5344 | lua_pop(L, 1); /* remove the nil value. */ |
| 5345 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5346 | lua_newtable(L); /* create and push empty table. */ |
| 5347 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5348 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5349 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5350 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5351 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5352 | case LUA_TTABLE: |
| 5353 | /* Entry found: push the value in the table. */ |
| 5354 | len = lua_rawlen(L, -1); |
| 5355 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5356 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5357 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5358 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5359 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5360 | default: |
| 5361 | /* Other cases are errors. */ |
| 5362 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5363 | WILL_LJMP(lua_error(L)); |
| 5364 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5365 | } |
| 5366 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5367 | return 1; |
| 5368 | } |
| 5369 | |
| 5370 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5371 | { |
| 5372 | struct hlua_txn *htxn; |
| 5373 | |
| 5374 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5375 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5376 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5377 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5378 | WILL_LJMP(lua_error(L)); |
| 5379 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5380 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5381 | } |
| 5382 | |
| 5383 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5384 | { |
| 5385 | struct hlua_txn *htxn; |
| 5386 | |
| 5387 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5388 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5389 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5390 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5391 | WILL_LJMP(lua_error(L)); |
| 5392 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5393 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5394 | } |
| 5395 | |
| 5396 | /* This function replace full header, or just a value in |
| 5397 | * the request or in the response. It is a wrapper fir the |
| 5398 | * 4 following functions. |
| 5399 | */ |
| 5400 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5401 | struct http_msg *msg, int action) |
| 5402 | { |
| 5403 | size_t name_len; |
| 5404 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5405 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5406 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5407 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5408 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5409 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5410 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5411 | |
Christopher Faulet | 5a8549e | 2019-06-17 13:36:06 +0200 | [diff] [blame] | 5412 | if (IS_HTX_STRM(htxn->s)) { |
| 5413 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5414 | |
| 5415 | htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action); |
| 5416 | } |
| 5417 | else |
| 5418 | http_transform_header_str(htxn->s, msg, name, name_len, value, re, action); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5419 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5420 | return 0; |
| 5421 | } |
| 5422 | |
| 5423 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5424 | { |
| 5425 | struct hlua_txn *htxn; |
| 5426 | |
| 5427 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5428 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5429 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5430 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5431 | WILL_LJMP(lua_error(L)); |
| 5432 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5433 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5434 | } |
| 5435 | |
| 5436 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5437 | { |
| 5438 | struct hlua_txn *htxn; |
| 5439 | |
| 5440 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5441 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5442 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5443 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5444 | WILL_LJMP(lua_error(L)); |
| 5445 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5446 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5447 | } |
| 5448 | |
| 5449 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5450 | { |
| 5451 | struct hlua_txn *htxn; |
| 5452 | |
| 5453 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5454 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5455 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5456 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5457 | WILL_LJMP(lua_error(L)); |
| 5458 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5459 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5460 | } |
| 5461 | |
| 5462 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5463 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5464 | struct hlua_txn *htxn; |
| 5465 | |
| 5466 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5467 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5468 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5469 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5470 | WILL_LJMP(lua_error(L)); |
| 5471 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5472 | 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] | 5473 | } |
| 5474 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5475 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5476 | * It is a wrapper for the 2 following functions. |
| 5477 | */ |
| 5478 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5479 | { |
| 5480 | size_t len; |
| 5481 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5482 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5483 | if (IS_HTX_STRM(htxn->s)) { |
| 5484 | /* HTX version */ |
| 5485 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5486 | struct http_hdr_ctx ctx; |
| 5487 | |
| 5488 | ctx.blk = NULL; |
| 5489 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5490 | http_remove_header(htx, &ctx); |
| 5491 | } |
| 5492 | else { |
| 5493 | /* Legacy HTTP version */ |
| 5494 | struct hdr_ctx ctx; |
| 5495 | struct http_txn *txn = htxn->s->txn; |
| 5496 | |
| 5497 | ctx.idx = 0; |
| 5498 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5499 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5500 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5501 | return 0; |
| 5502 | } |
| 5503 | |
| 5504 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5505 | { |
| 5506 | struct hlua_txn *htxn; |
| 5507 | |
| 5508 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5509 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5510 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5511 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5512 | WILL_LJMP(lua_error(L)); |
| 5513 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5514 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5518 | { |
| 5519 | struct hlua_txn *htxn; |
| 5520 | |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5521 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5522 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5523 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5524 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5525 | WILL_LJMP(lua_error(L)); |
| 5526 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5527 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5528 | } |
| 5529 | |
| 5530 | /* This function adds an header. It is a wrapper used by |
| 5531 | * the 2 following functions. |
| 5532 | */ |
| 5533 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5534 | { |
| 5535 | size_t name_len; |
| 5536 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5537 | size_t value_len; |
| 5538 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5539 | char *p; |
| 5540 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5541 | if (IS_HTX_STRM(htxn->s)) { |
| 5542 | /* HTX version */ |
| 5543 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5544 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5545 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5546 | ist2(value, value_len))); |
| 5547 | } |
| 5548 | else { |
| 5549 | /* Legacy HTTP version */ |
| 5550 | /* Check length. */ |
| 5551 | trash.data = value_len + name_len + 2; |
| 5552 | if (trash.data > trash.size) |
| 5553 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5554 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5555 | /* Creates the header string. */ |
| 5556 | p = trash.area; |
| 5557 | memcpy(p, name, name_len); |
| 5558 | p += name_len; |
| 5559 | *p = ':'; |
| 5560 | p++; |
| 5561 | *p = ' '; |
| 5562 | p++; |
| 5563 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5564 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5565 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5566 | trash.area, trash.data) != 0); |
| 5567 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5568 | return 0; |
| 5569 | } |
| 5570 | |
| 5571 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5572 | { |
| 5573 | struct hlua_txn *htxn; |
| 5574 | |
| 5575 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5576 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5577 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5578 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5579 | WILL_LJMP(lua_error(L)); |
| 5580 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5581 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5585 | { |
| 5586 | struct hlua_txn *htxn; |
| 5587 | |
| 5588 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5589 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5590 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5591 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5592 | WILL_LJMP(lua_error(L)); |
| 5593 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5594 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5598 | { |
| 5599 | struct hlua_txn *htxn; |
| 5600 | |
| 5601 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5602 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5603 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5604 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5605 | WILL_LJMP(lua_error(L)); |
| 5606 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5607 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5608 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5612 | { |
| 5613 | struct hlua_txn *htxn; |
| 5614 | |
| 5615 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5616 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5617 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5618 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5619 | WILL_LJMP(lua_error(L)); |
| 5620 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5621 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5622 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | /* This function set the method. */ |
| 5626 | static int hlua_http_req_set_meth(lua_State *L) |
| 5627 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5628 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5629 | size_t name_len; |
| 5630 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5631 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5632 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5633 | WILL_LJMP(lua_error(L)); |
| 5634 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5635 | 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] | 5636 | return 1; |
| 5637 | } |
| 5638 | |
| 5639 | /* This function set the method. */ |
| 5640 | static int hlua_http_req_set_path(lua_State *L) |
| 5641 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5642 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5643 | size_t name_len; |
| 5644 | 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] | 5645 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5646 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5647 | WILL_LJMP(lua_error(L)); |
| 5648 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5649 | 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] | 5650 | return 1; |
| 5651 | } |
| 5652 | |
| 5653 | /* This function set the query-string. */ |
| 5654 | static int hlua_http_req_set_query(lua_State *L) |
| 5655 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5656 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5657 | size_t name_len; |
| 5658 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5659 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5660 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5661 | WILL_LJMP(lua_error(L)); |
| 5662 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5663 | /* Check length. */ |
| 5664 | if (name_len > trash.size - 1) { |
| 5665 | lua_pushboolean(L, 0); |
| 5666 | return 1; |
| 5667 | } |
| 5668 | |
| 5669 | /* Add the mark question as prefix. */ |
| 5670 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5671 | trash.area[trash.data++] = '?'; |
| 5672 | memcpy(trash.area + trash.data, name, name_len); |
| 5673 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5674 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5675 | lua_pushboolean(L, |
| 5676 | http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5677 | return 1; |
| 5678 | } |
| 5679 | |
| 5680 | /* This function set the uri. */ |
| 5681 | static int hlua_http_req_set_uri(lua_State *L) |
| 5682 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5683 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5684 | size_t name_len; |
| 5685 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5686 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5687 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5688 | WILL_LJMP(lua_error(L)); |
| 5689 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5690 | 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] | 5691 | return 1; |
| 5692 | } |
| 5693 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5694 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5695 | static int hlua_http_res_set_status(lua_State *L) |
| 5696 | { |
| 5697 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5698 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5699 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5700 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5701 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5702 | WILL_LJMP(lua_error(L)); |
| 5703 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5704 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5705 | return 0; |
| 5706 | } |
| 5707 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5708 | /* |
| 5709 | * |
| 5710 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5711 | * Class TXN |
| 5712 | * |
| 5713 | * |
| 5714 | */ |
| 5715 | |
| 5716 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5717 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5718 | */ |
| 5719 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5720 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5721 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5722 | } |
| 5723 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5724 | __LJMP static int hlua_set_var(lua_State *L) |
| 5725 | { |
| 5726 | struct hlua_txn *htxn; |
| 5727 | const char *name; |
| 5728 | size_t len; |
| 5729 | struct sample smp; |
| 5730 | |
| 5731 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5732 | |
| 5733 | /* It is useles to retrieve the stream, but this function |
| 5734 | * runs only in a stream context. |
| 5735 | */ |
| 5736 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5737 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5738 | |
| 5739 | /* Converts the third argument in a sample. */ |
| 5740 | hlua_lua2smp(L, 3, &smp); |
| 5741 | |
| 5742 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5743 | 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] | 5744 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5745 | return 0; |
| 5746 | } |
| 5747 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5748 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5749 | { |
| 5750 | struct hlua_txn *htxn; |
| 5751 | const char *name; |
| 5752 | size_t len; |
| 5753 | struct sample smp; |
| 5754 | |
| 5755 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5756 | |
| 5757 | /* It is useles to retrieve the stream, but this function |
| 5758 | * runs only in a stream context. |
| 5759 | */ |
| 5760 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5761 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5762 | |
| 5763 | /* Unset the variable. */ |
| 5764 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5765 | vars_unset_by_name(name, len, &smp); |
| 5766 | return 0; |
| 5767 | } |
| 5768 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5769 | __LJMP static int hlua_get_var(lua_State *L) |
| 5770 | { |
| 5771 | struct hlua_txn *htxn; |
| 5772 | const char *name; |
| 5773 | size_t len; |
| 5774 | struct sample smp; |
| 5775 | |
| 5776 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5777 | |
| 5778 | /* It is useles to retrieve the stream, but this function |
| 5779 | * runs only in a stream context. |
| 5780 | */ |
| 5781 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5782 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5783 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5784 | 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] | 5785 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5786 | lua_pushnil(L); |
| 5787 | return 1; |
| 5788 | } |
| 5789 | |
| 5790 | return hlua_smp2lua(L, &smp); |
| 5791 | } |
| 5792 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5793 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5794 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5795 | struct hlua *hlua; |
| 5796 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5797 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5798 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5799 | /* It is useles to retrieve the stream, but this function |
| 5800 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5801 | */ |
| 5802 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5803 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5804 | |
| 5805 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5806 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5807 | |
| 5808 | /* Get and store new value. */ |
| 5809 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5810 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5811 | |
| 5812 | return 0; |
| 5813 | } |
| 5814 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5815 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5816 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5817 | struct hlua *hlua; |
| 5818 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5819 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5820 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5821 | /* It is useles to retrieve the stream, but this function |
| 5822 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5823 | */ |
| 5824 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5825 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5826 | |
| 5827 | /* Push configuration index in the stack. */ |
| 5828 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5829 | |
| 5830 | return 1; |
| 5831 | } |
| 5832 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5833 | /* Create stack entry containing a class TXN. This function |
| 5834 | * return 0 if the stack does not contains free slots, |
| 5835 | * otherwise it returns 1. |
| 5836 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5837 | 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] | 5838 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5839 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5840 | |
| 5841 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5842 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5843 | return 0; |
| 5844 | |
| 5845 | /* NOTE: The allocation never fails. The failure |
| 5846 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5847 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5848 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5849 | /* Create the object: obj[0] = userdata. */ |
| 5850 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5851 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5852 | lua_rawseti(L, -2, 0); |
| 5853 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5854 | htxn->s = s; |
| 5855 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5856 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5857 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5858 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5859 | /* Create the "f" field that contains a list of fetches. */ |
| 5860 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5861 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5862 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5863 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5864 | |
| 5865 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5866 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5867 | 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] | 5868 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5869 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5870 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5871 | /* Create the "c" field that contains a list of converters. */ |
| 5872 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5873 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5874 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5875 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5876 | |
| 5877 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5878 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5879 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5880 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5881 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5882 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5883 | /* Create the "req" field that contains the request channel object. */ |
| 5884 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5885 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5886 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5887 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5888 | |
| 5889 | /* Create the "res" field that contains the response channel object. */ |
| 5890 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5891 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5892 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5893 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5894 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5895 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5896 | lua_pushstring(L, "http"); |
| 5897 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5898 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5899 | return 0; |
| 5900 | } |
| 5901 | else |
| 5902 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5903 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5904 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5905 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5906 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5907 | lua_setmetatable(L, -2); |
| 5908 | |
| 5909 | return 1; |
| 5910 | } |
| 5911 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5912 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5913 | { |
| 5914 | const char *msg; |
| 5915 | struct hlua_txn *htxn; |
| 5916 | |
| 5917 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5918 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5919 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5920 | |
| 5921 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5922 | return 0; |
| 5923 | } |
| 5924 | |
| 5925 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5926 | { |
| 5927 | int level; |
| 5928 | const char *msg; |
| 5929 | struct hlua_txn *htxn; |
| 5930 | |
| 5931 | MAY_LJMP(check_args(L, 3, "log")); |
| 5932 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5933 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5934 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5935 | |
| 5936 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5937 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5938 | |
| 5939 | hlua_sendlog(htxn->s->be, level, msg); |
| 5940 | return 0; |
| 5941 | } |
| 5942 | |
| 5943 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5944 | { |
| 5945 | const char *msg; |
| 5946 | struct hlua_txn *htxn; |
| 5947 | |
| 5948 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5949 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5950 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5951 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5952 | return 0; |
| 5953 | } |
| 5954 | |
| 5955 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5956 | { |
| 5957 | const char *msg; |
| 5958 | struct hlua_txn *htxn; |
| 5959 | |
| 5960 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5961 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5962 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5963 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5964 | return 0; |
| 5965 | } |
| 5966 | |
| 5967 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5968 | { |
| 5969 | const char *msg; |
| 5970 | struct hlua_txn *htxn; |
| 5971 | |
| 5972 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5973 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5974 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5975 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5976 | return 0; |
| 5977 | } |
| 5978 | |
| 5979 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5980 | { |
| 5981 | const char *msg; |
| 5982 | struct hlua_txn *htxn; |
| 5983 | |
| 5984 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5985 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5986 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5987 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5988 | return 0; |
| 5989 | } |
| 5990 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5991 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5992 | { |
| 5993 | struct hlua_txn *htxn; |
| 5994 | int ll; |
| 5995 | |
| 5996 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5997 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5998 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5999 | |
| 6000 | if (ll < 0 || ll > 7) |
| 6001 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 6002 | |
| 6003 | htxn->s->logs.level = ll; |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
| 6007 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 6008 | { |
| 6009 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6010 | int tos; |
| 6011 | |
| 6012 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 6013 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6014 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6015 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 6016 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6017 | return 0; |
| 6018 | } |
| 6019 | |
| 6020 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 6021 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6022 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6023 | int mark; |
| 6024 | |
| 6025 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 6026 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6027 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6028 | |
Lukas Tribus | b59291a | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 6029 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6030 | return 0; |
| 6031 | } |
| 6032 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6033 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6034 | { |
| 6035 | struct hlua_txn *htxn; |
| 6036 | |
| 6037 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6038 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6039 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6040 | return 0; |
| 6041 | } |
| 6042 | |
| 6043 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6044 | { |
| 6045 | struct hlua_txn *htxn; |
| 6046 | |
| 6047 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6048 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6049 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6050 | return 0; |
| 6051 | } |
| 6052 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6053 | /* This function is an Lua binding that send pending data |
| 6054 | * to the client, and close the stream interface. |
| 6055 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6056 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6057 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6058 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6059 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6060 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6061 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6062 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6063 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6064 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6065 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6066 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6067 | * session, so we just end the execution of the current |
| 6068 | * lua code. |
| 6069 | */ |
| 6070 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6071 | WILL_LJMP(hlua_done(L)); |
| 6072 | return 0; |
| 6073 | } |
| 6074 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6075 | ic = &htxn->s->req; |
| 6076 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6077 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6078 | if (IS_HTX_STRM(htxn->s)) { |
| 6079 | htxn->s->txn->status = 0; |
| 6080 | http_reply_and_close(htxn->s, 0, NULL); |
| 6081 | ic->analysers &= AN_REQ_FLT_END; |
| 6082 | oc->analysers &= AN_RES_FLT_END; |
| 6083 | } |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6084 | else { |
| 6085 | if (htxn->s->txn) { |
| 6086 | /* HTTP mode, let's stay in sync with the stream */ |
| 6087 | b_del(&ic->buf, htxn->s->txn->req.sov); |
| 6088 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6089 | htxn->s->txn->req.sov = 0; |
| 6090 | |
| 6091 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6092 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6093 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6094 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6095 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6096 | /* Note that if we want to support keep-alive, we need |
| 6097 | * to bypass the close/shutr_now calls below, but that |
| 6098 | * may only be done if the HTTP request was already |
| 6099 | * processed and the connection header is known (ie |
| 6100 | * not during TCP rules). |
| 6101 | */ |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6102 | } |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6103 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6104 | channel_auto_read(ic); |
| 6105 | channel_abort(ic); |
| 6106 | channel_auto_close(ic); |
| 6107 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6108 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6109 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 6110 | channel_auto_read(oc); |
| 6111 | channel_auto_close(oc); |
| 6112 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6113 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6114 | ic->analysers = 0; |
| 6115 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6116 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6117 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 6118 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 6119 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6120 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6121 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6122 | return 0; |
| 6123 | } |
| 6124 | |
| 6125 | __LJMP static int hlua_log(lua_State *L) |
| 6126 | { |
| 6127 | int level; |
| 6128 | const char *msg; |
| 6129 | |
| 6130 | MAY_LJMP(check_args(L, 2, "log")); |
| 6131 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6132 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6133 | |
| 6134 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6135 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6136 | |
| 6137 | hlua_sendlog(NULL, level, msg); |
| 6138 | return 0; |
| 6139 | } |
| 6140 | |
| 6141 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6142 | { |
| 6143 | const char *msg; |
| 6144 | |
| 6145 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6146 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6147 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6148 | return 0; |
| 6149 | } |
| 6150 | |
| 6151 | __LJMP static int hlua_log_info(lua_State *L) |
| 6152 | { |
| 6153 | const char *msg; |
| 6154 | |
| 6155 | MAY_LJMP(check_args(L, 1, "info")); |
| 6156 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6157 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6158 | return 0; |
| 6159 | } |
| 6160 | |
| 6161 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6162 | { |
| 6163 | const char *msg; |
| 6164 | |
| 6165 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6166 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6167 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6168 | return 0; |
| 6169 | } |
| 6170 | |
| 6171 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6172 | { |
| 6173 | const char *msg; |
| 6174 | |
| 6175 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6176 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6177 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6178 | return 0; |
| 6179 | } |
| 6180 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6181 | __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] | 6182 | { |
| 6183 | int wakeup_ms = lua_tointeger(L, -1); |
| 6184 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6185 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6186 | return 0; |
| 6187 | } |
| 6188 | |
| 6189 | __LJMP static int hlua_sleep(lua_State *L) |
| 6190 | { |
| 6191 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6192 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6193 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6194 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6195 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6196 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6197 | wakeup_ms = tick_add(now_ms, delay); |
| 6198 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6199 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6200 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6201 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6202 | } |
| 6203 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6204 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6205 | { |
| 6206 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6207 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6208 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6209 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6210 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6211 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6212 | wakeup_ms = tick_add(now_ms, delay); |
| 6213 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6214 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6215 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6216 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6217 | } |
| 6218 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6219 | /* This functionis an LUA binding. it permits to give back |
| 6220 | * the hand at the HAProxy scheduler. It is used when the |
| 6221 | * LUA processing consumes a lot of time. |
| 6222 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6223 | __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] | 6224 | { |
| 6225 | return 0; |
| 6226 | } |
| 6227 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6228 | __LJMP static int hlua_yield(lua_State *L) |
| 6229 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6230 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6231 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6232 | } |
| 6233 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6234 | /* This function change the nice of the currently executed |
| 6235 | * task. It is used set low or high priority at the current |
| 6236 | * task. |
| 6237 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6238 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6239 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6240 | struct hlua *hlua; |
| 6241 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6242 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6243 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6244 | hlua = hlua_gethlua(L); |
| 6245 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6246 | |
| 6247 | /* If he task is not set, I'm in a start mode. */ |
| 6248 | if (!hlua || !hlua->task) |
| 6249 | return 0; |
| 6250 | |
| 6251 | if (nice < -1024) |
| 6252 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6253 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6254 | nice = 1024; |
| 6255 | |
| 6256 | hlua->task->nice = nice; |
| 6257 | return 0; |
| 6258 | } |
| 6259 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6260 | /* This function is used as a callback of a task. It is called by the |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6261 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6262 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6263 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6264 | * |
| 6265 | * Task wrapper are longjmp safe because the only one Lua code |
| 6266 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6267 | */ |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6268 | struct task *hlua_process_task(struct task *task, void *context, unsigned short state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6269 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6270 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6271 | enum hlua_exec status; |
| 6272 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6273 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6274 | task_set_affinity(task, tid_bit); |
| 6275 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6276 | /* If it is the first call to the task, we must initialize the |
| 6277 | * execution timeouts. |
| 6278 | */ |
| 6279 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6280 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6281 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6282 | /* Execute the Lua code. */ |
| 6283 | status = hlua_ctx_resume(hlua, 1); |
| 6284 | |
| 6285 | switch (status) { |
| 6286 | /* finished or yield */ |
| 6287 | case HLUA_E_OK: |
| 6288 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6289 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6290 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6291 | break; |
| 6292 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6293 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6294 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6295 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6296 | break; |
| 6297 | |
| 6298 | /* finished with error. */ |
| 6299 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6300 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6301 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6302 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6303 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6304 | break; |
| 6305 | |
| 6306 | case HLUA_E_ERR: |
| 6307 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6308 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6309 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6310 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6311 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6312 | break; |
| 6313 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6314 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6315 | } |
| 6316 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6317 | /* This function is an LUA binding that register LUA function to be |
| 6318 | * executed after the HAProxy configuration parsing and before the |
| 6319 | * HAProxy scheduler starts. This function expect only one LUA |
| 6320 | * argument that is a function. This function returns nothing, but |
| 6321 | * throws if an error is encountered. |
| 6322 | */ |
| 6323 | __LJMP static int hlua_register_init(lua_State *L) |
| 6324 | { |
| 6325 | struct hlua_init_function *init; |
| 6326 | int ref; |
| 6327 | |
| 6328 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6329 | |
| 6330 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6331 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6332 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6333 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6334 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6335 | |
| 6336 | init->function_ref = ref; |
| 6337 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6338 | return 0; |
| 6339 | } |
| 6340 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6341 | /* This functio is an LUA binding. It permits to register a task |
| 6342 | * executed in parallel of the main HAroxy activity. The task is |
| 6343 | * created and it is set in the HAProxy scheduler. It can be called |
| 6344 | * from the "init" section, "post init" or during the runtime. |
| 6345 | * |
| 6346 | * Lua prototype: |
| 6347 | * |
| 6348 | * <none> core.register_task(<function>) |
| 6349 | */ |
| 6350 | static int hlua_register_task(lua_State *L) |
| 6351 | { |
| 6352 | struct hlua *hlua; |
| 6353 | struct task *task; |
| 6354 | int ref; |
| 6355 | |
| 6356 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6357 | |
| 6358 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6359 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6360 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6361 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6362 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6363 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6364 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6365 | if (!task) |
| 6366 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6367 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6368 | task->context = hlua; |
| 6369 | task->process = hlua_process_task; |
| 6370 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6371 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6372 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6373 | |
| 6374 | /* Restore the function in the stack. */ |
| 6375 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6376 | hlua->nargs = 0; |
| 6377 | |
| 6378 | /* Schedule task. */ |
| 6379 | task_schedule(task, now_ms); |
| 6380 | |
| 6381 | return 0; |
| 6382 | } |
| 6383 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6384 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6385 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6386 | * resume converters. |
| 6387 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6388 | 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] | 6389 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6390 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6391 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6392 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6393 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6394 | if (!stream) |
| 6395 | return 0; |
| 6396 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6397 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6398 | * Lua context can be not initialized. This behavior |
| 6399 | * permits to save performances because a systematic |
| 6400 | * Lua initialization cause 5% performances loss. |
| 6401 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6402 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6403 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6404 | if (!stream->hlua) { |
| 6405 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6406 | return 0; |
| 6407 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6408 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6409 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6410 | return 0; |
| 6411 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6412 | } |
| 6413 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6414 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6415 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6416 | |
| 6417 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6418 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6419 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6420 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6421 | else |
| 6422 | error = "critical error"; |
| 6423 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6424 | return 0; |
| 6425 | } |
| 6426 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6427 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6428 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6429 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6430 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6431 | return 0; |
| 6432 | } |
| 6433 | |
| 6434 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6435 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6436 | |
| 6437 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6438 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6439 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6440 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6441 | return 0; |
| 6442 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6443 | hlua_smp2lua(stream->hlua->T, smp); |
| 6444 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6445 | |
| 6446 | /* push keywords in the stack. */ |
| 6447 | if (arg_p) { |
| 6448 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6449 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6450 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6451 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6452 | return 0; |
| 6453 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6454 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6455 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6456 | } |
| 6457 | } |
| 6458 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6459 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6460 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6461 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6462 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6463 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6464 | } |
| 6465 | |
| 6466 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6467 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6468 | /* finished. */ |
| 6469 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6470 | /* If the stack is empty, the function fails. */ |
| 6471 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6472 | return 0; |
| 6473 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6474 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6475 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6476 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6477 | return 1; |
| 6478 | |
| 6479 | /* yield. */ |
| 6480 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6481 | 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] | 6482 | return 0; |
| 6483 | |
| 6484 | /* finished with error. */ |
| 6485 | case HLUA_E_ERRMSG: |
| 6486 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6487 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6488 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6489 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6490 | return 0; |
| 6491 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6492 | case HLUA_E_ETMOUT: |
| 6493 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6494 | return 0; |
| 6495 | |
| 6496 | case HLUA_E_NOMEM: |
| 6497 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6498 | return 0; |
| 6499 | |
| 6500 | case HLUA_E_YIELD: |
| 6501 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6502 | return 0; |
| 6503 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6504 | case HLUA_E_ERR: |
| 6505 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6506 | 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] | 6507 | |
| 6508 | default: |
| 6509 | return 0; |
| 6510 | } |
| 6511 | } |
| 6512 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6513 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6514 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6515 | * resume sample-fetches. This function will be called by the sample |
| 6516 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6517 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6518 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6519 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6520 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6521 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6522 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6523 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6524 | const struct buffer msg = { }; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6525 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6526 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6527 | if (!stream) |
| 6528 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6529 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6530 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6531 | * Lua context can be not initialized. This behavior |
| 6532 | * permits to save performances because a systematic |
| 6533 | * Lua initialization cause 5% performances loss. |
| 6534 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6535 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6536 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6537 | if (!stream->hlua) { |
| 6538 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6539 | return 0; |
| 6540 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6541 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6542 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6543 | return 0; |
| 6544 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6545 | } |
| 6546 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6547 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6548 | |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6549 | if (stream->be->mode == PR_MODE_HTTP) { |
| 6550 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 6551 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6552 | else |
| 6553 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6554 | } |
| 6555 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6556 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6557 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6558 | |
| 6559 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6560 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6561 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6562 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6563 | else |
| 6564 | error = "critical error"; |
| 6565 | 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] | 6566 | return 0; |
| 6567 | } |
| 6568 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6569 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6570 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6571 | 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] | 6572 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6573 | return 0; |
| 6574 | } |
| 6575 | |
| 6576 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6577 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6578 | |
| 6579 | /* push arguments in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6580 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6581 | 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] | 6582 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6583 | return 0; |
| 6584 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6585 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6586 | |
| 6587 | /* push keywords in the stack. */ |
| 6588 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6589 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6590 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6591 | 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] | 6592 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6593 | return 0; |
| 6594 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6595 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6596 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6597 | } |
| 6598 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6599 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6600 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6601 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6602 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6603 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6604 | } |
| 6605 | |
| 6606 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6607 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6608 | /* finished. */ |
| 6609 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6611 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6612 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6613 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6614 | /* If the stack is empty, the function fails. */ |
| 6615 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6616 | return 0; |
| 6617 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6618 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6619 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6620 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6621 | |
| 6622 | /* Set the end of execution flag. */ |
| 6623 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6624 | return 1; |
| 6625 | |
| 6626 | /* yield. */ |
| 6627 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6628 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6629 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6630 | 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] | 6631 | return 0; |
| 6632 | |
| 6633 | /* finished with error. */ |
| 6634 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6635 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6636 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6637 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6638 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6639 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6640 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6641 | return 0; |
| 6642 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6643 | case HLUA_E_ETMOUT: |
| 6644 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6645 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6646 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6647 | return 0; |
| 6648 | |
| 6649 | case HLUA_E_NOMEM: |
| 6650 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6651 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6652 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6653 | return 0; |
| 6654 | |
| 6655 | case HLUA_E_YIELD: |
| 6656 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6657 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6658 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6659 | return 0; |
| 6660 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6661 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6662 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6663 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6664 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6665 | 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] | 6666 | |
| 6667 | default: |
| 6668 | return 0; |
| 6669 | } |
| 6670 | } |
| 6671 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6672 | /* This function is an LUA binding used for registering |
| 6673 | * "sample-conv" functions. It expects a converter name used |
| 6674 | * in the haproxy configuration file, and an LUA function. |
| 6675 | */ |
| 6676 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6677 | { |
| 6678 | struct sample_conv_kw_list *sck; |
| 6679 | const char *name; |
| 6680 | int ref; |
| 6681 | int len; |
| 6682 | struct hlua_function *fcn; |
| 6683 | |
| 6684 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6685 | |
| 6686 | /* First argument : converter name. */ |
| 6687 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6688 | |
| 6689 | /* Second argument : lua function. */ |
| 6690 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6691 | |
| 6692 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6693 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6694 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6695 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6696 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6697 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6698 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6699 | |
| 6700 | /* Fill fcn. */ |
| 6701 | fcn->name = strdup(name); |
| 6702 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6703 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6704 | fcn->function_ref = ref; |
| 6705 | |
| 6706 | /* List head */ |
| 6707 | sck->list.n = sck->list.p = NULL; |
| 6708 | |
| 6709 | /* converter keyword. */ |
| 6710 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6711 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6712 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6713 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6714 | |
| 6715 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6716 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6717 | 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] | 6718 | sck->kw[0].val_args = NULL; |
| 6719 | sck->kw[0].in_type = SMP_T_STR; |
| 6720 | sck->kw[0].out_type = SMP_T_STR; |
| 6721 | sck->kw[0].private = fcn; |
| 6722 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6723 | /* Register this new converter */ |
| 6724 | sample_register_convs(sck); |
| 6725 | |
| 6726 | return 0; |
| 6727 | } |
| 6728 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6729 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6730 | * "sample-fetch" functions. It expects a converter name used |
| 6731 | * in the haproxy configuration file, and an LUA function. |
| 6732 | */ |
| 6733 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6734 | { |
| 6735 | const char *name; |
| 6736 | int ref; |
| 6737 | int len; |
| 6738 | struct sample_fetch_kw_list *sfk; |
| 6739 | struct hlua_function *fcn; |
| 6740 | |
| 6741 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6742 | |
| 6743 | /* First argument : sample-fetch name. */ |
| 6744 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6745 | |
| 6746 | /* Second argument : lua function. */ |
| 6747 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6748 | |
| 6749 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6750 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6751 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6752 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6753 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6754 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6755 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6756 | |
| 6757 | /* Fill fcn. */ |
| 6758 | fcn->name = strdup(name); |
| 6759 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6760 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6761 | fcn->function_ref = ref; |
| 6762 | |
| 6763 | /* List head */ |
| 6764 | sfk->list.n = sfk->list.p = NULL; |
| 6765 | |
| 6766 | /* sample-fetch keyword. */ |
| 6767 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6768 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6769 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6770 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6771 | |
| 6772 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6773 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6774 | 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] | 6775 | sfk->kw[0].val_args = NULL; |
| 6776 | sfk->kw[0].out_type = SMP_T_STR; |
| 6777 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6778 | sfk->kw[0].val = 0; |
| 6779 | sfk->kw[0].private = fcn; |
| 6780 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6781 | /* Register this new fetch. */ |
| 6782 | sample_register_fetches(sfk); |
| 6783 | |
| 6784 | return 0; |
| 6785 | } |
| 6786 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6787 | /* This function is a wrapper to execute each LUA function declared |
| 6788 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6789 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6790 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6791 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6792 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6793 | 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] | 6794 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6795 | { |
| 6796 | char **arg; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6797 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6798 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6799 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6800 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6801 | |
| 6802 | switch (rule->from) { |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6803 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6804 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6805 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6806 | case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6807 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6808 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6809 | return ACT_RET_CONT; |
| 6810 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6811 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6812 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6813 | * Lua context can be not initialized. This behavior |
| 6814 | * permits to save performances because a systematic |
| 6815 | * Lua initialization cause 5% performances loss. |
| 6816 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6817 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6818 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6819 | if (!s->hlua) { |
| 6820 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6821 | rule->arg.hlua_rule->fcn.name); |
| 6822 | return ACT_RET_CONT; |
| 6823 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6824 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6825 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6826 | rule->arg.hlua_rule->fcn.name); |
| 6827 | return ACT_RET_CONT; |
| 6828 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6829 | } |
| 6830 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6831 | consistency_set(s, dir, &s->hlua->cons); |
| 6832 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6833 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6834 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6835 | |
| 6836 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6837 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6838 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6839 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6840 | else |
| 6841 | error = "critical error"; |
| 6842 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6843 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6844 | return ACT_RET_CONT; |
| 6845 | } |
| 6846 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6847 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6848 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6849 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6850 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6851 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6852 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6853 | } |
| 6854 | |
| 6855 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6856 | 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] | 6857 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6858 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6859 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6860 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6861 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6862 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6863 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6864 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6865 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6866 | |
| 6867 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6868 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6869 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6870 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6871 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6872 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6873 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6874 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6875 | lua_pushstring(s->hlua->T, *arg); |
| 6876 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6877 | } |
| 6878 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6879 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6880 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6881 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6882 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6883 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6884 | } |
| 6885 | |
Christopher Faulet | 719ddc8 | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6886 | /* Always reset the analyse expiration timeout for the corresponding |
| 6887 | * channel in case the lua script yield, to be sure to not keep an |
| 6888 | * expired timeout. |
| 6889 | */ |
| 6890 | if (dir == SMP_OPT_DIR_REQ) |
| 6891 | s->req.analyse_exp = TICK_ETERNITY; |
| 6892 | else |
| 6893 | s->res.analyse_exp = TICK_ETERNITY; |
| 6894 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6895 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6896 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6897 | /* finished. */ |
| 6898 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6899 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6900 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6901 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6902 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6903 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6904 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6905 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6906 | |
| 6907 | /* yield. */ |
| 6908 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6909 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6910 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | ea0b0e2 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6911 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6912 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6913 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6914 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6915 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6916 | /* Some actions can be wake up when a "write" event |
| 6917 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6918 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6919 | */ |
Christopher Faulet | b22f650 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6920 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6921 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6922 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6923 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6924 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6925 | * because HAProxy is not able to manipulate data, it |
| 6926 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6927 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6928 | |
| 6929 | /* finished with error. */ |
| 6930 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6931 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6932 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6933 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6934 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6935 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6936 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6937 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6938 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6939 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6940 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6941 | case HLUA_E_ETMOUT: |
| 6942 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6943 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6944 | return ACT_RET_ERR; |
| 6945 | } |
| 6946 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6947 | return 0; |
| 6948 | |
| 6949 | case HLUA_E_NOMEM: |
| 6950 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6951 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6952 | return ACT_RET_ERR; |
| 6953 | } |
| 6954 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6955 | return 0; |
| 6956 | |
| 6957 | case HLUA_E_YIELD: |
| 6958 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6959 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6960 | return ACT_RET_ERR; |
| 6961 | } |
| 6962 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6963 | rule->arg.hlua_rule->fcn.name); |
| 6964 | return 0; |
| 6965 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6966 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6967 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6968 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6969 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6970 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6971 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6972 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6973 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6974 | |
| 6975 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6976 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6977 | } |
| 6978 | } |
| 6979 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6980 | struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6981 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6982 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6983 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6984 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6985 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6986 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6987 | } |
| 6988 | |
| 6989 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6990 | { |
| 6991 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6992 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6993 | struct task *task; |
| 6994 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6995 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6996 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6997 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6998 | if (!hlua) { |
| 6999 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 7000 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7001 | return 0; |
| 7002 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7003 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7004 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7005 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7006 | |
| 7007 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7008 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7009 | if (!task) { |
| 7010 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 7011 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7012 | return 0; |
| 7013 | } |
| 7014 | task->nice = 0; |
| 7015 | task->context = ctx; |
| 7016 | task->process = hlua_applet_wakeup; |
| 7017 | ctx->ctx.hlua_apptcp.task = task; |
| 7018 | |
| 7019 | /* In the execution wrappers linked with a stream, the |
| 7020 | * Lua context can be not initialized. This behavior |
| 7021 | * permits to save performances because a systematic |
| 7022 | * Lua initialization cause 5% performances loss. |
| 7023 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7024 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7025 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 7026 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7027 | return 0; |
| 7028 | } |
| 7029 | |
| 7030 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7031 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7032 | |
| 7033 | /* The following Lua calls can fail. */ |
| 7034 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7035 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7036 | error = lua_tostring(hlua->T, -1); |
| 7037 | else |
| 7038 | error = "critical error"; |
| 7039 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7040 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7041 | return 0; |
| 7042 | } |
| 7043 | |
| 7044 | /* Check stack available size. */ |
| 7045 | if (!lua_checkstack(hlua->T, 1)) { |
| 7046 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7047 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7048 | RESET_SAFE_LJMP(hlua->T); |
| 7049 | return 0; |
| 7050 | } |
| 7051 | |
| 7052 | /* Restore the function in the stack. */ |
| 7053 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7054 | |
| 7055 | /* Create and and push object stream in the stack. */ |
| 7056 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7057 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7058 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7059 | RESET_SAFE_LJMP(hlua->T); |
| 7060 | return 0; |
| 7061 | } |
| 7062 | hlua->nargs = 1; |
| 7063 | |
| 7064 | /* push keywords in the stack. */ |
| 7065 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7066 | if (!lua_checkstack(hlua->T, 1)) { |
| 7067 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7068 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7069 | RESET_SAFE_LJMP(hlua->T); |
| 7070 | return 0; |
| 7071 | } |
| 7072 | lua_pushstring(hlua->T, *arg); |
| 7073 | hlua->nargs++; |
| 7074 | } |
| 7075 | |
| 7076 | RESET_SAFE_LJMP(hlua->T); |
| 7077 | |
| 7078 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7079 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7080 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7081 | |
| 7082 | return 1; |
| 7083 | } |
| 7084 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7085 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7086 | { |
| 7087 | struct stream_interface *si = ctx->owner; |
| 7088 | struct stream *strm = si_strm(si); |
| 7089 | struct channel *res = si_ic(si); |
| 7090 | struct act_rule *rule = ctx->rule; |
| 7091 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7092 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7093 | |
| 7094 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7095 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7096 | /* eat the whole request */ |
| 7097 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7098 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7099 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7100 | |
| 7101 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7102 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7103 | return; |
| 7104 | |
| 7105 | /* Execute the function. */ |
| 7106 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7107 | /* finished. */ |
| 7108 | case HLUA_E_OK: |
| 7109 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7110 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7111 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7112 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7113 | res->flags |= CF_READ_NULL; |
| 7114 | si_shutr(si); |
| 7115 | return; |
| 7116 | |
| 7117 | /* yield. */ |
| 7118 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7119 | if (hlua->wake_time != TICK_ETERNITY) |
| 7120 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7121 | return; |
| 7122 | |
| 7123 | /* finished with error. */ |
| 7124 | case HLUA_E_ERRMSG: |
| 7125 | /* Display log. */ |
| 7126 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7127 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7128 | lua_pop(hlua->T, 1); |
| 7129 | goto error; |
| 7130 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7131 | case HLUA_E_ETMOUT: |
| 7132 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7133 | rule->arg.hlua_rule->fcn.name); |
| 7134 | goto error; |
| 7135 | |
| 7136 | case HLUA_E_NOMEM: |
| 7137 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7138 | rule->arg.hlua_rule->fcn.name); |
| 7139 | goto error; |
| 7140 | |
| 7141 | case HLUA_E_YIELD: /* unexpected */ |
| 7142 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7143 | rule->arg.hlua_rule->fcn.name); |
| 7144 | goto error; |
| 7145 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7146 | case HLUA_E_ERR: |
| 7147 | /* Display log. */ |
| 7148 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7149 | rule->arg.hlua_rule->fcn.name); |
| 7150 | goto error; |
| 7151 | |
| 7152 | default: |
| 7153 | goto error; |
| 7154 | } |
| 7155 | |
| 7156 | error: |
| 7157 | |
| 7158 | /* For all other cases, just close the stream. */ |
| 7159 | si_shutw(si); |
| 7160 | si_shutr(si); |
| 7161 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7162 | } |
| 7163 | |
| 7164 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7165 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7166 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7167 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7168 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7169 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7170 | } |
| 7171 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7172 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7173 | * an errors occurs and -1 if more data are required for initializing |
| 7174 | * the applet. |
| 7175 | */ |
| 7176 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7177 | { |
| 7178 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7179 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7180 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7181 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7182 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7183 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7184 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7185 | txn = strm->txn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7186 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7187 | /* We want two things in HTTP mode : |
| 7188 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7189 | * applet is released after each response ; |
| 7190 | * - enable request body transfer to the applet in order to resync |
| 7191 | * with the response body. |
| 7192 | */ |
| 7193 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7194 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7195 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7196 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7197 | if (!hlua) { |
| 7198 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7199 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7200 | return 0; |
| 7201 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7202 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7203 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7204 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7205 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7206 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7207 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7208 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7209 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7210 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7211 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7212 | if (!task) { |
| 7213 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7214 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7215 | return 0; |
| 7216 | } |
| 7217 | task->nice = 0; |
| 7218 | task->context = ctx; |
| 7219 | task->process = hlua_applet_wakeup; |
| 7220 | ctx->ctx.hlua_apphttp.task = task; |
| 7221 | |
| 7222 | /* In the execution wrappers linked with a stream, the |
| 7223 | * Lua context can be not initialized. This behavior |
| 7224 | * permits to save performances because a systematic |
| 7225 | * Lua initialization cause 5% performances loss. |
| 7226 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7227 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7228 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7229 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7230 | return 0; |
| 7231 | } |
| 7232 | |
| 7233 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7234 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7235 | |
| 7236 | /* The following Lua calls can fail. */ |
| 7237 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7238 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7239 | error = lua_tostring(hlua->T, -1); |
| 7240 | else |
| 7241 | error = "critical error"; |
| 7242 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7243 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7244 | return 0; |
| 7245 | } |
| 7246 | |
| 7247 | /* Check stack available size. */ |
| 7248 | if (!lua_checkstack(hlua->T, 1)) { |
| 7249 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7250 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7251 | RESET_SAFE_LJMP(hlua->T); |
| 7252 | return 0; |
| 7253 | } |
| 7254 | |
| 7255 | /* Restore the function in the stack. */ |
| 7256 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7257 | |
| 7258 | /* Create and and push object stream in the stack. */ |
| 7259 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7260 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7261 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7262 | RESET_SAFE_LJMP(hlua->T); |
| 7263 | return 0; |
| 7264 | } |
| 7265 | hlua->nargs = 1; |
| 7266 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7267 | /* push keywords in the stack. */ |
| 7268 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7269 | if (!lua_checkstack(hlua->T, 1)) { |
| 7270 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7271 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7272 | RESET_SAFE_LJMP(hlua->T); |
| 7273 | return 0; |
| 7274 | } |
| 7275 | lua_pushstring(hlua->T, *arg); |
| 7276 | hlua->nargs++; |
| 7277 | } |
| 7278 | |
| 7279 | RESET_SAFE_LJMP(hlua->T); |
| 7280 | |
| 7281 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7282 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7283 | |
| 7284 | return 1; |
| 7285 | } |
| 7286 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7287 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7288 | { |
| 7289 | struct stream_interface *si = ctx->owner; |
| 7290 | struct stream *strm = si_strm(si); |
| 7291 | struct channel *req = si_oc(si); |
| 7292 | struct channel *res = si_ic(si); |
| 7293 | struct act_rule *rule = ctx->rule; |
| 7294 | struct proxy *px = strm->be; |
| 7295 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7296 | struct htx *req_htx, *res_htx; |
| 7297 | |
| 7298 | res_htx = htx_from_buf(&res->buf); |
| 7299 | |
| 7300 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7301 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7302 | goto out; |
| 7303 | |
| 7304 | /* Check if the input buffer is avalaible. */ |
| 7305 | if (!b_size(&res->buf)) { |
| 7306 | si_rx_room_blk(si); |
| 7307 | goto out; |
| 7308 | } |
| 7309 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7310 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7311 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7312 | |
| 7313 | /* Set the currently running flag. */ |
| 7314 | if (!HLUA_IS_RUNNING(hlua) && |
| 7315 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7316 | struct htx_blk *blk; |
| 7317 | size_t count = co_data(req); |
| 7318 | |
| 7319 | if (!count) { |
| 7320 | si_cant_get(si); |
| 7321 | goto out; |
| 7322 | } |
| 7323 | |
| 7324 | /* We need to flush the request header. This left the body for |
| 7325 | * the Lua. |
| 7326 | */ |
| 7327 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7328 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7329 | while (count && blk) { |
| 7330 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7331 | uint32_t sz = htx_get_blksz(blk); |
| 7332 | |
| 7333 | if (sz > count) { |
| 7334 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7335 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7336 | goto out; |
| 7337 | } |
| 7338 | |
| 7339 | count -= sz; |
| 7340 | co_set_data(req, co_data(req) - sz); |
| 7341 | blk = htx_remove_blk(req_htx, blk); |
| 7342 | |
| 7343 | if (type == HTX_BLK_EOH) |
| 7344 | break; |
| 7345 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7346 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7347 | } |
| 7348 | |
| 7349 | /* Executes The applet if it is not done. */ |
| 7350 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7351 | |
| 7352 | /* Execute the function. */ |
| 7353 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7354 | /* finished. */ |
| 7355 | case HLUA_E_OK: |
| 7356 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7357 | break; |
| 7358 | |
| 7359 | /* yield. */ |
| 7360 | case HLUA_E_AGAIN: |
| 7361 | if (hlua->wake_time != TICK_ETERNITY) |
| 7362 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7363 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7364 | |
| 7365 | /* finished with error. */ |
| 7366 | case HLUA_E_ERRMSG: |
| 7367 | /* Display log. */ |
| 7368 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7369 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7370 | lua_pop(hlua->T, 1); |
| 7371 | goto error; |
| 7372 | |
| 7373 | case HLUA_E_ETMOUT: |
| 7374 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7375 | rule->arg.hlua_rule->fcn.name); |
| 7376 | goto error; |
| 7377 | |
| 7378 | case HLUA_E_NOMEM: |
| 7379 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7380 | rule->arg.hlua_rule->fcn.name); |
| 7381 | goto error; |
| 7382 | |
| 7383 | case HLUA_E_YIELD: /* unexpected */ |
| 7384 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7385 | rule->arg.hlua_rule->fcn.name); |
| 7386 | goto error; |
| 7387 | |
| 7388 | case HLUA_E_ERR: |
| 7389 | /* Display log. */ |
| 7390 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7391 | rule->arg.hlua_rule->fcn.name); |
| 7392 | goto error; |
| 7393 | |
| 7394 | default: |
| 7395 | goto error; |
| 7396 | } |
| 7397 | } |
| 7398 | |
| 7399 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7400 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7401 | goto done; |
| 7402 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7403 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7404 | goto error; |
| 7405 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7406 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7407 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7408 | si_rx_room_blk(si); |
| 7409 | goto out; |
| 7410 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7411 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7412 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7413 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7414 | } |
| 7415 | |
| 7416 | done: |
| 7417 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7418 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7419 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7420 | si_shutr(si); |
| 7421 | } |
| 7422 | |
| 7423 | /* eat the whole request */ |
| 7424 | if (co_data(req)) { |
| 7425 | req_htx = htx_from_buf(&req->buf); |
| 7426 | co_htx_skip(req, req_htx, co_data(req)); |
| 7427 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7432 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7433 | return; |
| 7434 | |
| 7435 | error: |
| 7436 | |
| 7437 | /* If we are in HTTP mode, and we are not send any |
| 7438 | * data, return a 500 server error in best effort: |
| 7439 | * if there is no room available in the buffer, |
| 7440 | * just close the connection. |
| 7441 | */ |
| 7442 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7443 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7444 | |
| 7445 | channel_erase(res); |
| 7446 | res->buf.data = b_data(err); |
| 7447 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7448 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7449 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7450 | } |
| 7451 | if (!(strm->flags & SF_ERR_MASK)) |
| 7452 | strm->flags |= SF_ERR_RESOURCE; |
| 7453 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7454 | goto done; |
| 7455 | } |
| 7456 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7457 | void hlua_applet_http_fct(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7458 | { |
| 7459 | struct stream_interface *si = ctx->owner; |
| 7460 | struct stream *strm = si_strm(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7461 | struct channel *req = si_oc(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7462 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7463 | struct act_rule *rule = ctx->rule; |
| 7464 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7465 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7466 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7467 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7468 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7469 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7470 | int ret; |
| 7471 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7472 | if (IS_HTX_STRM(strm)) |
| 7473 | return hlua_applet_htx_fct(ctx); |
| 7474 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7475 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7476 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7477 | goto out; |
| 7478 | |
| 7479 | /* Check if the input buffer is avalaible. */ |
| 7480 | if (!b_size(&res->buf)) { |
| 7481 | si_rx_room_blk(si); |
| 7482 | goto out; |
| 7483 | } |
| 7484 | /* check that the output is not closed */ |
| 7485 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 7486 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7487 | |
| 7488 | /* Set the currently running flag. */ |
| 7489 | if (!HLUA_IS_RUNNING(hlua) && |
| 7490 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7491 | /* Store the max amount of bytes that we can read. */ |
| 7492 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7493 | |
| 7494 | /* We need to flush the request header. This left the body |
| 7495 | * for the Lua. |
| 7496 | */ |
| 7497 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7498 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7499 | ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7500 | if (ret == -1) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7501 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7502 | |
| 7503 | /* No data available, ask for more data. */ |
| 7504 | if (ret == 1) |
| 7505 | len2 = 0; |
| 7506 | if (ret == 0) |
| 7507 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7508 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7509 | si_cant_get(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7510 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7511 | } |
| 7512 | |
| 7513 | /* skip the requests bytes. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7514 | co_skip(req, strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7515 | } |
| 7516 | |
| 7517 | /* Executes The applet if it is not done. */ |
| 7518 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7519 | |
| 7520 | /* Execute the function. */ |
| 7521 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7522 | /* finished. */ |
| 7523 | case HLUA_E_OK: |
| 7524 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7525 | break; |
| 7526 | |
| 7527 | /* yield. */ |
| 7528 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7529 | if (hlua->wake_time != TICK_ETERNITY) |
| 7530 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7531 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7532 | |
| 7533 | /* finished with error. */ |
| 7534 | case HLUA_E_ERRMSG: |
| 7535 | /* Display log. */ |
| 7536 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7537 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7538 | lua_pop(hlua->T, 1); |
| 7539 | goto error; |
| 7540 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7541 | case HLUA_E_ETMOUT: |
| 7542 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7543 | rule->arg.hlua_rule->fcn.name); |
| 7544 | goto error; |
| 7545 | |
| 7546 | case HLUA_E_NOMEM: |
| 7547 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7548 | rule->arg.hlua_rule->fcn.name); |
| 7549 | goto error; |
| 7550 | |
| 7551 | case HLUA_E_YIELD: /* unexpected */ |
| 7552 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7553 | rule->arg.hlua_rule->fcn.name); |
| 7554 | goto error; |
| 7555 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7556 | case HLUA_E_ERR: |
| 7557 | /* Display log. */ |
| 7558 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7559 | rule->arg.hlua_rule->fcn.name); |
| 7560 | goto error; |
| 7561 | |
| 7562 | default: |
| 7563 | goto error; |
| 7564 | } |
| 7565 | } |
| 7566 | |
| 7567 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7568 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7569 | goto done; |
| 7570 | |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7571 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7572 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7573 | |
| 7574 | /* We must send the final chunk. */ |
| 7575 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7576 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7577 | |
| 7578 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7579 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7580 | |
| 7581 | /* critical error. */ |
| 7582 | if (ret == -2 || ret == -3) { |
| 7583 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7584 | rule->arg.hlua_rule->fcn.name); |
| 7585 | goto error; |
| 7586 | } |
| 7587 | |
| 7588 | /* no enough space error. */ |
| 7589 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7590 | si_rx_room_blk(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7591 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7592 | } |
| 7593 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7594 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7595 | ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7596 | } |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7597 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7598 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7599 | done: |
| 7600 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7601 | if (!(res->flags & CF_SHUTR)) { |
| 7602 | res->flags |= CF_READ_NULL; |
| 7603 | si_shutr(si); |
| 7604 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7605 | |
| 7606 | /* eat the whole request */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7607 | if (co_data(req)) |
| 7608 | co_skip(req, co_data(req)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7609 | } |
| 7610 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7611 | out: |
| 7612 | return; |
| 7613 | |
| 7614 | error: |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7615 | |
| 7616 | /* If we are in HTTP mode, and we are not send any |
| 7617 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7618 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7619 | * just close the connection. |
| 7620 | */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7621 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7622 | channel_erase(res); |
| 7623 | ci_putblk(res, error_500, strlen(error_500)); |
| 7624 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7625 | if (!(strm->flags & SF_ERR_MASK)) |
| 7626 | strm->flags |= SF_ERR_RESOURCE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7627 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7628 | goto done; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7629 | } |
| 7630 | |
| 7631 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7632 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7633 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7634 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7635 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7636 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7637 | } |
| 7638 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7639 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7640 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7641 | * |
| 7642 | * This function can fail with an abort() due to an Lua critical error. |
| 7643 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7644 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7645 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7646 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7647 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7648 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7649 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7650 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7651 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7652 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7653 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7654 | if (!rule->arg.hlua_rule) { |
| 7655 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7656 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7657 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7658 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7659 | /* Memory for arguments. */ |
| 7660 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7661 | if (!rule->arg.hlua_rule->args) { |
| 7662 | memprintf(err, "out of memory error"); |
| 7663 | return ACT_RET_PRS_ERR; |
| 7664 | } |
| 7665 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7666 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7667 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7668 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7669 | /* Expect some arguments */ |
| 7670 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7671 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7672 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7673 | return ACT_RET_PRS_ERR; |
| 7674 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7675 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7676 | if (!rule->arg.hlua_rule->args[i]) { |
| 7677 | memprintf(err, "out of memory error"); |
| 7678 | return ACT_RET_PRS_ERR; |
| 7679 | } |
| 7680 | (*cur_arg)++; |
| 7681 | } |
| 7682 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7683 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7684 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7685 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7686 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7687 | } |
| 7688 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7689 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7690 | struct act_rule *rule, char **err) |
| 7691 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7692 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7693 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7694 | /* HTTP applets are forbidden in tcp-request rules. |
| 7695 | * HTTP applet request requires everything initilized by |
| 7696 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7697 | * The applet will be immediately initilized, but its before |
| 7698 | * the call of this analyzer. |
| 7699 | */ |
| 7700 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7701 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7702 | return ACT_RET_PRS_ERR; |
| 7703 | } |
| 7704 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7705 | /* Memory for the rule. */ |
| 7706 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7707 | if (!rule->arg.hlua_rule) { |
| 7708 | memprintf(err, "out of memory error"); |
| 7709 | return ACT_RET_PRS_ERR; |
| 7710 | } |
| 7711 | |
| 7712 | /* Reference the Lua function and store the reference. */ |
| 7713 | rule->arg.hlua_rule->fcn = *fcn; |
| 7714 | |
| 7715 | /* TODO: later accept arguments. */ |
| 7716 | rule->arg.hlua_rule->args = NULL; |
| 7717 | |
| 7718 | /* Add applet pointer in the rule. */ |
| 7719 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7720 | rule->applet.name = fcn->name; |
| 7721 | rule->applet.init = hlua_applet_http_init; |
| 7722 | rule->applet.fct = hlua_applet_http_fct; |
| 7723 | rule->applet.release = hlua_applet_http_release; |
| 7724 | rule->applet.timeout = hlua_timeout_applet; |
| 7725 | |
| 7726 | return ACT_RET_PRS_OK; |
| 7727 | } |
| 7728 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7729 | /* This function is an LUA binding used for registering |
| 7730 | * "sample-conv" functions. It expects a converter name used |
| 7731 | * in the haproxy configuration file, and an LUA function. |
| 7732 | */ |
| 7733 | __LJMP static int hlua_register_action(lua_State *L) |
| 7734 | { |
| 7735 | struct action_kw_list *akl; |
| 7736 | const char *name; |
| 7737 | int ref; |
| 7738 | int len; |
| 7739 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7740 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7741 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7742 | /* Initialise the number of expected arguments at 0. */ |
| 7743 | nargs = 0; |
| 7744 | |
| 7745 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7746 | 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] | 7747 | |
| 7748 | /* First argument : converter name. */ |
| 7749 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7750 | |
| 7751 | /* Second argument : environment. */ |
| 7752 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7753 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7754 | |
| 7755 | /* Third argument : lua function. */ |
| 7756 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7757 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7758 | /* Fourth argument : number of mandatory arguments expected on the configuration line. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7759 | if (lua_gettop(L) >= 4) |
| 7760 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7761 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7762 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7763 | lua_pushnil(L); |
| 7764 | while (lua_next(L, 2) != 0) { |
| 7765 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7766 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7767 | |
| 7768 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7769 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7770 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7771 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7772 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7773 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7774 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7775 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7776 | |
| 7777 | /* Fill fcn. */ |
| 7778 | fcn->name = strdup(name); |
| 7779 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7780 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7781 | fcn->function_ref = ref; |
| 7782 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7783 | /* Set the expected number od arguments. */ |
| 7784 | fcn->nargs = nargs; |
| 7785 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7786 | /* List head */ |
| 7787 | akl->list.n = akl->list.p = NULL; |
| 7788 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7789 | /* action keyword. */ |
| 7790 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7791 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7792 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7793 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7794 | |
| 7795 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7796 | |
| 7797 | akl->kw[0].match_pfx = 0; |
| 7798 | akl->kw[0].private = fcn; |
| 7799 | akl->kw[0].parse = action_register_lua; |
| 7800 | |
| 7801 | /* select the action registering point. */ |
| 7802 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7803 | tcp_req_cont_keywords_register(akl); |
| 7804 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7805 | tcp_res_cont_keywords_register(akl); |
| 7806 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7807 | http_req_keywords_register(akl); |
| 7808 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7809 | http_res_keywords_register(akl); |
| 7810 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7811 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7812 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7813 | "are expected.", lua_tostring(L, -1))); |
| 7814 | |
| 7815 | /* pop the environment string. */ |
| 7816 | lua_pop(L, 1); |
| 7817 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7818 | return ACT_RET_PRS_OK; |
| 7819 | } |
| 7820 | |
| 7821 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7822 | struct act_rule *rule, char **err) |
| 7823 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7824 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7825 | |
Christopher Faulet | eb70980 | 2019-04-11 22:04:08 +0200 | [diff] [blame] | 7826 | if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) { |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7827 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7828 | return ACT_RET_PRS_ERR; |
| 7829 | } |
| 7830 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7831 | /* Memory for the rule. */ |
| 7832 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7833 | if (!rule->arg.hlua_rule) { |
| 7834 | memprintf(err, "out of memory error"); |
| 7835 | return ACT_RET_PRS_ERR; |
| 7836 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7837 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7838 | /* Reference the Lua function and store the reference. */ |
| 7839 | rule->arg.hlua_rule->fcn = *fcn; |
| 7840 | |
| 7841 | /* TODO: later accept arguments. */ |
| 7842 | rule->arg.hlua_rule->args = NULL; |
| 7843 | |
| 7844 | /* Add applet pointer in the rule. */ |
| 7845 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7846 | rule->applet.name = fcn->name; |
| 7847 | rule->applet.init = hlua_applet_tcp_init; |
| 7848 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7849 | rule->applet.release = hlua_applet_tcp_release; |
| 7850 | rule->applet.timeout = hlua_timeout_applet; |
| 7851 | |
| 7852 | return 0; |
| 7853 | } |
| 7854 | |
| 7855 | /* This function is an LUA binding used for registering |
| 7856 | * "sample-conv" functions. It expects a converter name used |
| 7857 | * in the haproxy configuration file, and an LUA function. |
| 7858 | */ |
| 7859 | __LJMP static int hlua_register_service(lua_State *L) |
| 7860 | { |
| 7861 | struct action_kw_list *akl; |
| 7862 | const char *name; |
| 7863 | const char *env; |
| 7864 | int ref; |
| 7865 | int len; |
| 7866 | struct hlua_function *fcn; |
| 7867 | |
| 7868 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7869 | |
| 7870 | /* First argument : converter name. */ |
| 7871 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7872 | |
| 7873 | /* Second argument : environment. */ |
| 7874 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7875 | |
| 7876 | /* Third argument : lua function. */ |
| 7877 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7878 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7879 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7880 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7881 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7882 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7883 | fcn = calloc(1, sizeof(*fcn)); |
| 7884 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7885 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7886 | |
| 7887 | /* Fill fcn. */ |
| 7888 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7889 | fcn->name = calloc(1, len); |
| 7890 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7891 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7892 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7893 | fcn->function_ref = ref; |
| 7894 | |
| 7895 | /* List head */ |
| 7896 | akl->list.n = akl->list.p = NULL; |
| 7897 | |
| 7898 | /* converter keyword. */ |
| 7899 | len = strlen("lua.") + strlen(name) + 1; |
| 7900 | akl->kw[0].kw = calloc(1, len); |
| 7901 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7902 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7903 | |
| 7904 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7905 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7906 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7907 | if (strcmp(env, "tcp") == 0) |
| 7908 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7909 | else if (strcmp(env, "http") == 0) |
| 7910 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7911 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7912 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7913 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7914 | |
| 7915 | akl->kw[0].match_pfx = 0; |
| 7916 | akl->kw[0].private = fcn; |
| 7917 | |
| 7918 | /* End of array. */ |
| 7919 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7920 | |
| 7921 | /* Register this new converter */ |
| 7922 | service_keywords_register(akl); |
| 7923 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7924 | return 0; |
| 7925 | } |
| 7926 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7927 | /* This function initialises Lua cli handler. It copies the |
| 7928 | * arguments in the Lua stack and create channel IO objects. |
| 7929 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7930 | static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private) |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7931 | { |
| 7932 | struct hlua *hlua; |
| 7933 | struct hlua_function *fcn; |
| 7934 | int i; |
| 7935 | const char *error; |
| 7936 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7937 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7938 | appctx->ctx.hlua_cli.fcn = private; |
| 7939 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7940 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7941 | if (!hlua) { |
| 7942 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7943 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7944 | } |
| 7945 | HLUA_INIT(hlua); |
| 7946 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7947 | |
| 7948 | /* Create task used by signal to wakeup applets. |
| 7949 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7950 | * applet_http. It is absolutely compatible. |
| 7951 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7952 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7953 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7954 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7955 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7956 | } |
| 7957 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7958 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7959 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7960 | |
| 7961 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7962 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7963 | 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] | 7964 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7965 | } |
| 7966 | |
| 7967 | /* The following Lua calls can fail. */ |
| 7968 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7969 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7970 | error = lua_tostring(hlua->T, -1); |
| 7971 | else |
| 7972 | error = "critical error"; |
| 7973 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7974 | goto error; |
| 7975 | } |
| 7976 | |
| 7977 | /* Check stack available size. */ |
| 7978 | if (!lua_checkstack(hlua->T, 2)) { |
| 7979 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7980 | goto error; |
| 7981 | } |
| 7982 | |
| 7983 | /* Restore the function in the stack. */ |
| 7984 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7985 | |
| 7986 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7987 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7988 | */ |
| 7989 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7990 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7991 | goto error; |
| 7992 | } |
| 7993 | hlua->nargs = 1; |
| 7994 | |
| 7995 | /* push keywords in the stack. */ |
| 7996 | for (i = 0; *args[i]; i++) { |
| 7997 | /* Check stack available size. */ |
| 7998 | if (!lua_checkstack(hlua->T, 1)) { |
| 7999 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8000 | goto error; |
| 8001 | } |
| 8002 | lua_pushstring(hlua->T, args[i]); |
| 8003 | hlua->nargs++; |
| 8004 | } |
| 8005 | |
| 8006 | /* We must initialize the execution timeouts. */ |
| 8007 | hlua->max_time = hlua_timeout_session; |
| 8008 | |
| 8009 | /* At this point the execution is safe. */ |
| 8010 | RESET_SAFE_LJMP(hlua->T); |
| 8011 | |
| 8012 | /* It's ok */ |
| 8013 | return 0; |
| 8014 | |
| 8015 | /* It's not ok. */ |
| 8016 | error: |
| 8017 | RESET_SAFE_LJMP(hlua->T); |
| 8018 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8019 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8020 | return 1; |
| 8021 | } |
| 8022 | |
| 8023 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8024 | { |
| 8025 | struct hlua *hlua; |
| 8026 | struct stream_interface *si; |
| 8027 | struct hlua_function *fcn; |
| 8028 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8029 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8030 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8031 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8032 | |
| 8033 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8034 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8035 | return 1; |
| 8036 | |
| 8037 | /* Execute the function. */ |
| 8038 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8039 | |
| 8040 | /* finished. */ |
| 8041 | case HLUA_E_OK: |
| 8042 | return 1; |
| 8043 | |
| 8044 | /* yield. */ |
| 8045 | case HLUA_E_AGAIN: |
| 8046 | /* We want write. */ |
| 8047 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8048 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8049 | /* Set the timeout. */ |
| 8050 | if (hlua->wake_time != TICK_ETERNITY) |
| 8051 | task_schedule(hlua->task, hlua->wake_time); |
| 8052 | return 0; |
| 8053 | |
| 8054 | /* finished with error. */ |
| 8055 | case HLUA_E_ERRMSG: |
| 8056 | /* Display log. */ |
| 8057 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8058 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8059 | lua_pop(hlua->T, 1); |
| 8060 | return 1; |
| 8061 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8062 | case HLUA_E_ETMOUT: |
| 8063 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8064 | fcn->name); |
| 8065 | return 1; |
| 8066 | |
| 8067 | case HLUA_E_NOMEM: |
| 8068 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8069 | fcn->name); |
| 8070 | return 1; |
| 8071 | |
| 8072 | case HLUA_E_YIELD: /* unexpected */ |
| 8073 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8074 | fcn->name); |
| 8075 | return 1; |
| 8076 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8077 | case HLUA_E_ERR: |
| 8078 | /* Display log. */ |
| 8079 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8080 | fcn->name); |
| 8081 | return 1; |
| 8082 | |
| 8083 | default: |
| 8084 | return 1; |
| 8085 | } |
| 8086 | |
| 8087 | return 1; |
| 8088 | } |
| 8089 | |
| 8090 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8091 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8092 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8093 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8094 | } |
| 8095 | |
| 8096 | /* This function is an LUA binding used for registering |
| 8097 | * new keywords in the cli. It expects a list of keywords |
| 8098 | * which are the "path". It is limited to 5 keywords. A |
| 8099 | * description of the command, a function to be executed |
| 8100 | * for the parsing and a function for io handlers. |
| 8101 | */ |
| 8102 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8103 | { |
| 8104 | struct cli_kw_list *cli_kws; |
| 8105 | const char *message; |
| 8106 | int ref_io; |
| 8107 | int len; |
| 8108 | struct hlua_function *fcn; |
| 8109 | int index; |
| 8110 | int i; |
| 8111 | |
| 8112 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8113 | |
| 8114 | /* First argument : an array of maximum 5 keywords. */ |
| 8115 | if (!lua_istable(L, 1)) |
| 8116 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8117 | |
| 8118 | /* Second argument : string with contextual message. */ |
| 8119 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8120 | |
| 8121 | /* Third and fourth argument : lua function. */ |
| 8122 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8123 | |
| 8124 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8125 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8126 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8127 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8128 | fcn = calloc(1, sizeof(*fcn)); |
| 8129 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8130 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8131 | |
| 8132 | /* Fill path. */ |
| 8133 | index = 0; |
| 8134 | lua_pushnil(L); |
| 8135 | while(lua_next(L, 1) != 0) { |
| 8136 | if (index >= 5) |
| 8137 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8138 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8139 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8140 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8141 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8142 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8143 | index++; |
| 8144 | lua_pop(L, 1); |
| 8145 | } |
| 8146 | |
| 8147 | /* Copy help message. */ |
| 8148 | cli_kws->kw[0].usage = strdup(message); |
| 8149 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8150 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8151 | |
| 8152 | /* Fill fcn io handler. */ |
| 8153 | len = strlen("<lua.cli>") + 1; |
| 8154 | for (i = 0; i < index; i++) |
| 8155 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8156 | fcn->name = calloc(1, len); |
| 8157 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8158 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8159 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8160 | for (i = 0; i < index; i++) { |
| 8161 | strncat((char *)fcn->name, ".", len); |
| 8162 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8163 | } |
| 8164 | strncat((char *)fcn->name, ">", len); |
| 8165 | fcn->function_ref = ref_io; |
| 8166 | |
| 8167 | /* Fill last entries. */ |
| 8168 | cli_kws->kw[0].private = fcn; |
| 8169 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8170 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8171 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8172 | |
| 8173 | /* Register this new converter */ |
| 8174 | cli_register_kw(cli_kws); |
| 8175 | |
| 8176 | return 0; |
| 8177 | } |
| 8178 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8179 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8180 | struct proxy *defpx, const char *file, int line, |
| 8181 | char **err, unsigned int *timeout) |
| 8182 | { |
| 8183 | const char *error; |
| 8184 | |
| 8185 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8186 | if (error == PARSE_TIME_OVER) { |
| 8187 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8188 | args[1], args[0]); |
| 8189 | return -1; |
| 8190 | } |
| 8191 | else if (error == PARSE_TIME_UNDER) { |
| 8192 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8193 | args[1], args[0]); |
| 8194 | return -1; |
| 8195 | } |
| 8196 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8197 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8198 | return -1; |
| 8199 | } |
| 8200 | return 0; |
| 8201 | } |
| 8202 | |
| 8203 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8204 | struct proxy *defpx, const char *file, int line, |
| 8205 | char **err) |
| 8206 | { |
| 8207 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8208 | file, line, err, &hlua_timeout_session); |
| 8209 | } |
| 8210 | |
| 8211 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8212 | struct proxy *defpx, const char *file, int line, |
| 8213 | char **err) |
| 8214 | { |
| 8215 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8216 | file, line, err, &hlua_timeout_task); |
| 8217 | } |
| 8218 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8219 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8220 | struct proxy *defpx, const char *file, int line, |
| 8221 | char **err) |
| 8222 | { |
| 8223 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8224 | file, line, err, &hlua_timeout_applet); |
| 8225 | } |
| 8226 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8227 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8228 | struct proxy *defpx, const char *file, int line, |
| 8229 | char **err) |
| 8230 | { |
| 8231 | char *error; |
| 8232 | |
| 8233 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8234 | if (*error != '\0') { |
| 8235 | memprintf(err, "%s: invalid number", args[0]); |
| 8236 | return -1; |
| 8237 | } |
| 8238 | return 0; |
| 8239 | } |
| 8240 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8241 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8242 | struct proxy *defpx, const char *file, int line, |
| 8243 | char **err) |
| 8244 | { |
| 8245 | char *error; |
| 8246 | |
| 8247 | if (*(args[1]) == 0) { |
| 8248 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8249 | return -1; |
| 8250 | } |
| 8251 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8252 | if (*error != '\0') { |
| 8253 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8254 | return -1; |
| 8255 | } |
| 8256 | return 0; |
| 8257 | } |
| 8258 | |
| 8259 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8260 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8261 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8262 | * the main lua entry point. |
| 8263 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8264 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8265 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8266 | * |
| 8267 | * In some error case, LUA set an error message in top of the stack. This function |
| 8268 | * 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] | 8269 | * |
| 8270 | * This function can fail with an abort() due to an Lua critical error. |
| 8271 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8272 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8273 | */ |
| 8274 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8275 | struct proxy *defpx, const char *file, int line, |
| 8276 | char **err) |
| 8277 | { |
| 8278 | int error; |
| 8279 | |
| 8280 | /* Just load and compile the file. */ |
| 8281 | error = luaL_loadfile(gL.T, args[1]); |
| 8282 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8283 | memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8284 | lua_pop(gL.T, 1); |
| 8285 | return -1; |
| 8286 | } |
| 8287 | |
| 8288 | /* If no syntax error where detected, execute the code. */ |
| 8289 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8290 | switch (error) { |
| 8291 | case LUA_OK: |
| 8292 | break; |
| 8293 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8294 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8295 | lua_pop(gL.T, 1); |
| 8296 | return -1; |
| 8297 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8298 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8299 | return -1; |
| 8300 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8301 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8302 | lua_pop(gL.T, 1); |
| 8303 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8304 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8305 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8306 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8307 | lua_pop(gL.T, 1); |
| 8308 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8309 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8310 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8311 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8312 | lua_pop(gL.T, 1); |
| 8313 | return -1; |
| 8314 | } |
| 8315 | |
| 8316 | return 0; |
| 8317 | } |
| 8318 | |
| 8319 | /* configuration keywords declaration */ |
| 8320 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8321 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8322 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8323 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8324 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8325 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8326 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8327 | { 0, NULL, NULL }, |
| 8328 | }}; |
| 8329 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8330 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8331 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8332 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8333 | /* This function can fail with an abort() due to an Lua critical error. |
| 8334 | * We are in the initialisation process of HAProxy, this abort() is |
| 8335 | * tolerated. |
| 8336 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8337 | int hlua_post_init() |
| 8338 | { |
| 8339 | struct hlua_init_function *init; |
| 8340 | const char *msg; |
| 8341 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8342 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8343 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8344 | /* Call post initialisation function in safe environement. */ |
| 8345 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8346 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8347 | error = lua_tostring(gL.T, -1); |
| 8348 | else |
| 8349 | error = "critical error"; |
| 8350 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8351 | exit(1); |
| 8352 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8353 | |
| 8354 | #if USE_OPENSSL |
| 8355 | /* Initialize SSL server. */ |
| 8356 | if (socket_ssl.xprt->prepare_srv) { |
| 8357 | int saved_used_backed = global.ssl_used_backend; |
| 8358 | // don't affect maxconn automatic computation |
| 8359 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8360 | global.ssl_used_backend = saved_used_backed; |
| 8361 | } |
| 8362 | #endif |
| 8363 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8364 | hlua_fcn_post_init(gL.T); |
| 8365 | RESET_SAFE_LJMP(gL.T); |
| 8366 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8367 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8368 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8369 | ret = hlua_ctx_resume(&gL, 0); |
| 8370 | switch (ret) { |
| 8371 | case HLUA_E_OK: |
| 8372 | lua_pop(gL.T, -1); |
| 8373 | return 1; |
| 8374 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8375 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8376 | return 0; |
| 8377 | case HLUA_E_ERRMSG: |
| 8378 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8379 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8380 | return 0; |
| 8381 | case HLUA_E_ERR: |
| 8382 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8383 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8384 | return 0; |
| 8385 | } |
| 8386 | } |
| 8387 | return 1; |
| 8388 | } |
| 8389 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8390 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8391 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8392 | * is the previously allocated size or the kind of object in case of a new |
| 8393 | * allocation. <nsize> is the requested new size. |
| 8394 | */ |
| 8395 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8396 | { |
| 8397 | struct hlua_mem_allocator *zone = ud; |
| 8398 | |
| 8399 | if (nsize == 0) { |
| 8400 | /* it's a free */ |
| 8401 | if (ptr) |
| 8402 | zone->allocated -= osize; |
| 8403 | free(ptr); |
| 8404 | return NULL; |
| 8405 | } |
| 8406 | |
| 8407 | if (!ptr) { |
| 8408 | /* it's a new allocation */ |
| 8409 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8410 | return NULL; |
| 8411 | |
| 8412 | ptr = malloc(nsize); |
| 8413 | if (ptr) |
| 8414 | zone->allocated += nsize; |
| 8415 | return ptr; |
| 8416 | } |
| 8417 | |
| 8418 | /* it's a realloc */ |
| 8419 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8420 | return NULL; |
| 8421 | |
| 8422 | ptr = realloc(ptr, nsize); |
| 8423 | if (ptr) |
| 8424 | zone->allocated += nsize - osize; |
| 8425 | return ptr; |
| 8426 | } |
| 8427 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8428 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8429 | * We are in the initialisation process of HAProxy, this abort() is |
| 8430 | * tolerated. |
| 8431 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8432 | void hlua_init(void) |
| 8433 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8434 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8435 | int idx; |
| 8436 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8437 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8438 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8439 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8440 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8441 | struct srv_kw *kw; |
| 8442 | int tmp_error; |
| 8443 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8444 | char *args[] = { /* SSL client configuration. */ |
| 8445 | "ssl", |
| 8446 | "verify", |
| 8447 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8448 | NULL |
| 8449 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8450 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8451 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8452 | /* Init main lua stack. */ |
| 8453 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8454 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8455 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8456 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8457 | hlua_sethlua(&gL); |
| 8458 | gL.Tref = LUA_REFNIL; |
| 8459 | gL.task = NULL; |
| 8460 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8461 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8462 | * the Lua function can fail with an abort. We are in the initialisation |
| 8463 | * process of HAProxy, this abort() is tolerated. |
| 8464 | */ |
| 8465 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8466 | /* Initialise lua. */ |
| 8467 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8468 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8469 | /* Set safe environment for the initialisation. */ |
| 8470 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8471 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8472 | error_msg = lua_tostring(gL.T, -1); |
| 8473 | else |
| 8474 | error_msg = "critical error"; |
| 8475 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8476 | exit(1); |
| 8477 | } |
| 8478 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8479 | /* |
| 8480 | * |
| 8481 | * Create "core" object. |
| 8482 | * |
| 8483 | */ |
| 8484 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8485 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8486 | lua_newtable(gL.T); |
| 8487 | |
| 8488 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8489 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8490 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8491 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8492 | /* Register special functions. */ |
| 8493 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8494 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8495 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8496 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8497 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8498 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8499 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8500 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8501 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8502 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8503 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8504 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8505 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8506 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8507 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8508 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8509 | hlua_class_function(gL.T, "log", hlua_log); |
| 8510 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8511 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8512 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8513 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8514 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8515 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8516 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8517 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8518 | |
| 8519 | /* |
| 8520 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8521 | * Register class Map |
| 8522 | * |
| 8523 | */ |
| 8524 | |
| 8525 | /* This table entry is the object "Map" base. */ |
| 8526 | lua_newtable(gL.T); |
| 8527 | |
| 8528 | /* register pattern types. */ |
| 8529 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8530 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8531 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8532 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8533 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8534 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8535 | |
| 8536 | /* register constructor. */ |
| 8537 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8538 | |
| 8539 | /* Create and fill the metatable. */ |
| 8540 | lua_newtable(gL.T); |
| 8541 | |
| 8542 | /* Create and fille the __index entry. */ |
| 8543 | lua_pushstring(gL.T, "__index"); |
| 8544 | lua_newtable(gL.T); |
| 8545 | |
| 8546 | /* Register . */ |
| 8547 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8548 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8549 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8550 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8551 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8552 | /* Register previous table in the registry with reference and named entry. |
| 8553 | * The function hlua_register_metatable() pops the stack, so we |
| 8554 | * previously create a copy of the table. |
| 8555 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8556 | 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] | 8557 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8558 | |
| 8559 | /* Assign the metatable to the mai Map object. */ |
| 8560 | lua_setmetatable(gL.T, -2); |
| 8561 | |
| 8562 | /* Set a name to the table. */ |
| 8563 | lua_setglobal(gL.T, "Map"); |
| 8564 | |
| 8565 | /* |
| 8566 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8567 | * Register class Channel |
| 8568 | * |
| 8569 | */ |
| 8570 | |
| 8571 | /* Create and fill the metatable. */ |
| 8572 | lua_newtable(gL.T); |
| 8573 | |
| 8574 | /* Create and fille the __index entry. */ |
| 8575 | lua_pushstring(gL.T, "__index"); |
| 8576 | lua_newtable(gL.T); |
| 8577 | |
| 8578 | /* Register . */ |
| 8579 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8580 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8581 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8582 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8583 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8584 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8585 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8586 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8587 | 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] | 8588 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8589 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8590 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8591 | |
| 8592 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8593 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8594 | |
| 8595 | /* |
| 8596 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8597 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8598 | * |
| 8599 | */ |
| 8600 | |
| 8601 | /* Create and fill the metatable. */ |
| 8602 | lua_newtable(gL.T); |
| 8603 | |
| 8604 | /* Create and fille the __index entry. */ |
| 8605 | lua_pushstring(gL.T, "__index"); |
| 8606 | lua_newtable(gL.T); |
| 8607 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8608 | /* Browse existing fetches and create the associated |
| 8609 | * object method. |
| 8610 | */ |
| 8611 | sf = NULL; |
| 8612 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8613 | |
| 8614 | /* Dont register the keywork if the arguments check function are |
| 8615 | * not safe during the runtime. |
| 8616 | */ |
| 8617 | if ((sf->val_args != NULL) && |
| 8618 | (sf->val_args != val_payload_lv) && |
| 8619 | (sf->val_args != val_hdr)) |
| 8620 | continue; |
| 8621 | |
| 8622 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8623 | * by an underscore. |
| 8624 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8625 | strncpy(trash.area, sf->kw, trash.size); |
| 8626 | trash.area[trash.size - 1] = '\0'; |
| 8627 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8628 | if (*p == '.' || *p == '-' || *p == '+') |
| 8629 | *p = '_'; |
| 8630 | |
| 8631 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8632 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8633 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8634 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8635 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8636 | } |
| 8637 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8638 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8639 | |
| 8640 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8641 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8642 | |
| 8643 | /* |
| 8644 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8645 | * Register class Converters |
| 8646 | * |
| 8647 | */ |
| 8648 | |
| 8649 | /* Create and fill the metatable. */ |
| 8650 | lua_newtable(gL.T); |
| 8651 | |
| 8652 | /* Create and fill the __index entry. */ |
| 8653 | lua_pushstring(gL.T, "__index"); |
| 8654 | lua_newtable(gL.T); |
| 8655 | |
| 8656 | /* Browse existing converters and create the associated |
| 8657 | * object method. |
| 8658 | */ |
| 8659 | sc = NULL; |
| 8660 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8661 | /* Dont register the keywork if the arguments check function are |
| 8662 | * not safe during the runtime. |
| 8663 | */ |
| 8664 | if (sc->val_args != NULL) |
| 8665 | continue; |
| 8666 | |
| 8667 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8668 | * by an underscore. |
| 8669 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8670 | strncpy(trash.area, sc->kw, trash.size); |
| 8671 | trash.area[trash.size - 1] = '\0'; |
| 8672 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8673 | if (*p == '.' || *p == '-' || *p == '+') |
| 8674 | *p = '_'; |
| 8675 | |
| 8676 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8677 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8678 | lua_pushlightuserdata(gL.T, sc); |
| 8679 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8680 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8681 | } |
| 8682 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8683 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8684 | |
| 8685 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8686 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8687 | |
| 8688 | /* |
| 8689 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8690 | * Register class HTTP |
| 8691 | * |
| 8692 | */ |
| 8693 | |
| 8694 | /* Create and fill the metatable. */ |
| 8695 | lua_newtable(gL.T); |
| 8696 | |
| 8697 | /* Create and fille the __index entry. */ |
| 8698 | lua_pushstring(gL.T, "__index"); |
| 8699 | lua_newtable(gL.T); |
| 8700 | |
| 8701 | /* Register Lua functions. */ |
| 8702 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8703 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8704 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8705 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8706 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8707 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8708 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8709 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8710 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8711 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8712 | |
| 8713 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8714 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8715 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8716 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8717 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8718 | 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] | 8719 | 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] | 8720 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8721 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8722 | |
| 8723 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8724 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8725 | |
| 8726 | /* |
| 8727 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8728 | * Register class AppletTCP |
| 8729 | * |
| 8730 | */ |
| 8731 | |
| 8732 | /* Create and fill the metatable. */ |
| 8733 | lua_newtable(gL.T); |
| 8734 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8735 | /* Create and fille the __index entry. */ |
| 8736 | lua_pushstring(gL.T, "__index"); |
| 8737 | lua_newtable(gL.T); |
| 8738 | |
| 8739 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8740 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8741 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8742 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8743 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8744 | 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] | 8745 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8746 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8747 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8748 | |
| 8749 | lua_settable(gL.T, -3); |
| 8750 | |
| 8751 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8752 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8753 | |
| 8754 | /* |
| 8755 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8756 | * Register class AppletHTTP |
| 8757 | * |
| 8758 | */ |
| 8759 | |
| 8760 | /* Create and fill the metatable. */ |
| 8761 | lua_newtable(gL.T); |
| 8762 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8763 | /* Create and fille the __index entry. */ |
| 8764 | lua_pushstring(gL.T, "__index"); |
| 8765 | lua_newtable(gL.T); |
| 8766 | |
| 8767 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8768 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8769 | 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] | 8770 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8771 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8772 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8773 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8774 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8775 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8776 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8777 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8778 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8779 | |
| 8780 | lua_settable(gL.T, -3); |
| 8781 | |
| 8782 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8783 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8784 | |
| 8785 | /* |
| 8786 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8787 | * Register class TXN |
| 8788 | * |
| 8789 | */ |
| 8790 | |
| 8791 | /* Create and fill the metatable. */ |
| 8792 | lua_newtable(gL.T); |
| 8793 | |
| 8794 | /* Create and fille the __index entry. */ |
| 8795 | lua_pushstring(gL.T, "__index"); |
| 8796 | lua_newtable(gL.T); |
| 8797 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8798 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8799 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8800 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8801 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8802 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8803 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8804 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8805 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8806 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8807 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8808 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8809 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8810 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8811 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8812 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8813 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8814 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8815 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8816 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8817 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8818 | |
| 8819 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8820 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8821 | |
| 8822 | /* |
| 8823 | * |
| 8824 | * Register class Socket |
| 8825 | * |
| 8826 | */ |
| 8827 | |
| 8828 | /* Create and fill the metatable. */ |
| 8829 | lua_newtable(gL.T); |
| 8830 | |
| 8831 | /* Create and fille the __index entry. */ |
| 8832 | lua_pushstring(gL.T, "__index"); |
| 8833 | lua_newtable(gL.T); |
| 8834 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8835 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8836 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8837 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8838 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8839 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8840 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8841 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8842 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8843 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8844 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8845 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8846 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8847 | 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] | 8848 | |
| 8849 | /* Register the garbage collector entry. */ |
| 8850 | lua_pushstring(gL.T, "__gc"); |
| 8851 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8852 | 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] | 8853 | |
| 8854 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8855 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8856 | |
| 8857 | /* Proxy and server configuration initialisation. */ |
| 8858 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8859 | init_new_proxy(&socket_proxy); |
| 8860 | socket_proxy.parent = NULL; |
| 8861 | socket_proxy.last_change = now.tv_sec; |
| 8862 | socket_proxy.id = "LUA-SOCKET"; |
| 8863 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8864 | socket_proxy.maxconn = 0; |
| 8865 | socket_proxy.accept = NULL; |
| 8866 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8867 | socket_proxy.srv = NULL; |
| 8868 | socket_proxy.conn_retries = 0; |
| 8869 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8870 | |
| 8871 | /* Init TCP server: unchanged parameters */ |
| 8872 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8873 | socket_tcp.next = NULL; |
| 8874 | socket_tcp.proxy = &socket_proxy; |
| 8875 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8876 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8877 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8878 | socket_tcp.priv_conns = NULL; |
| 8879 | socket_tcp.idle_conns = NULL; |
| 8880 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8881 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8882 | socket_tcp.last_change = 0; |
| 8883 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8884 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8885 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8886 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8887 | |
| 8888 | /* XXX: Copy default parameter from default server, |
| 8889 | * but the default server is not initialized. |
| 8890 | */ |
| 8891 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8892 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8893 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8894 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8895 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8896 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8897 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8898 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8899 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8900 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8901 | |
| 8902 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8903 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8904 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8905 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8906 | socket_tcp.check.server = &socket_tcp; |
| 8907 | |
| 8908 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8909 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8910 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8911 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8912 | socket_tcp.agent.server = &socket_tcp; |
| 8913 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8914 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8915 | |
| 8916 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8917 | /* Init TCP server: unchanged parameters */ |
| 8918 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8919 | socket_ssl.next = NULL; |
| 8920 | socket_ssl.proxy = &socket_proxy; |
| 8921 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8922 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8923 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8924 | socket_ssl.priv_conns = NULL; |
| 8925 | socket_ssl.idle_conns = NULL; |
| 8926 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8927 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8928 | socket_ssl.last_change = 0; |
| 8929 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8930 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8931 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8932 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8933 | |
| 8934 | /* XXX: Copy default parameter from default server, |
| 8935 | * but the default server is not initialized. |
| 8936 | */ |
| 8937 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8938 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8939 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8940 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8941 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8942 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8943 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8944 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8945 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8946 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8947 | |
| 8948 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8949 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8950 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8951 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8952 | socket_ssl.check.server = &socket_ssl; |
| 8953 | |
| 8954 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8955 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8956 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8957 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8958 | socket_ssl.agent.server = &socket_ssl; |
| 8959 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8960 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8961 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8962 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8963 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8964 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8965 | /* |
| 8966 | * |
| 8967 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8968 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8969 | * features like client certificates and ssl_verify. |
| 8970 | * |
| 8971 | */ |
| 8972 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8973 | if (tmp_error != 0) { |
| 8974 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8975 | abort(); /* This must be never arrives because the command line |
| 8976 | not editable by the user. */ |
| 8977 | } |
| 8978 | idx += kw->skip; |
| 8979 | } |
| 8980 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8981 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8982 | |
| 8983 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8984 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8985 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8986 | static void hlua_register_build_options(void) |
| 8987 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8988 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8989 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8990 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8991 | hap_register_build_opts(ptr, 1); |
| 8992 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8993 | |
| 8994 | INITCALL0(STG_REGISTER, hlua_register_build_options); |