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 | |
Bertrand Jacquin | 892e4fd | 2021-01-21 21:14:07 +0000 | [diff] [blame^] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 16 | #include <limits.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 17 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 18 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 19 | #include <lauxlib.h> |
| 20 | #include <lua.h> |
| 21 | #include <lualib.h> |
| 22 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 23 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 24 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 25 | #endif |
| 26 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | #include <ebpttree.h> |
| 28 | |
| 29 | #include <common/cfgparse.h> |
Willy Tarreau | b059b89 | 2018-10-16 17:57:36 +0200 | [diff] [blame] | 30 | #include <common/compiler.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 31 | #include <common/hathreads.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 32 | #include <common/initcall.h> |
| 33 | #include <common/xref.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 34 | #include <common/h1.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 36 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 37 | #include <types/hlua.h> |
| 38 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 39 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 40 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 41 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 42 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 43 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 44 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 45 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 46 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 47 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 48 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 49 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 50 | #include <proto/http_fetch.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 51 | #include <proto/http_htx.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 52 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 53 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 54 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 55 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 56 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 57 | #include <proto/payload.h> |
| 58 | #include <proto/proto_http.h> |
| 59 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 60 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 61 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 62 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 63 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 64 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 65 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 66 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 67 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 68 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 69 | * macro is used only for identifying the function that can |
| 70 | * not return because a longjmp is executed. |
| 71 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 72 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 73 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 74 | */ |
| 75 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 76 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 77 | #define MAY_LJMP(func) func |
| 78 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 79 | /* This couple of function executes securely some Lua calls outside of |
| 80 | * the lua runtime environment. Each Lua call can return a longjmp |
| 81 | * if it encounter a memory error. |
| 82 | * |
| 83 | * Lua documentation extract: |
| 84 | * |
| 85 | * If an error happens outside any protected environment, Lua calls |
| 86 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 87 | * exiting the host application. Your panic function can avoid this |
| 88 | * exit by never returning (e.g., doing a long jump to your own |
| 89 | * recovery point outside Lua). |
| 90 | * |
| 91 | * The panic function runs as if it were a message handler (see |
| 92 | * §2.3); in particular, the error message is at the top of the |
| 93 | * stack. However, there is no guarantee about stack space. To push |
| 94 | * anything on the stack, the panic function must first check the |
| 95 | * available space (see §4.2). |
| 96 | * |
| 97 | * We must check all the Lua entry point. This includes: |
| 98 | * - The include/proto/hlua.h exported functions |
| 99 | * - the task wrapper function |
| 100 | * - The action wrapper function |
| 101 | * - The converters wrapper function |
| 102 | * - The sample-fetch wrapper functions |
| 103 | * |
| 104 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 105 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 106 | * |
| 107 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 108 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 109 | * because they must be exists in the program stack when the longjmp |
| 110 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 111 | * |
| 112 | * Note that the Lua processing is not really thread safe. It provides |
| 113 | * heavy system which consists to add our own lock function in the Lua |
| 114 | * code and recompile the library. This system will probably not accepted |
| 115 | * by maintainers of various distribs. |
| 116 | * |
| 117 | * Our main excution point of the Lua is the function lua_resume(). A |
| 118 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 119 | * of function and a lua_unlock() at the end of the function. So I |
| 120 | * conclude that the Lua thread safe mode just perform a mutex around |
| 121 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 122 | * easier for distro maintainers. |
| 123 | * |
| 124 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 125 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 126 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 127 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 128 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 129 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 130 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 131 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 132 | |
| 133 | #define SET_SAFE_LJMP(__L) \ |
| 134 | ({ \ |
| 135 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 136 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 137 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 138 | lua_atpanic(__L, hlua_panic_safe); \ |
| 139 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 140 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 141 | } else { \ |
| 142 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 143 | ret = 1; \ |
| 144 | } \ |
| 145 | ret; \ |
| 146 | }) |
| 147 | |
| 148 | /* If we are the last function catching Lua errors, we |
| 149 | * must reset the panic function. |
| 150 | */ |
| 151 | #define RESET_SAFE_LJMP(__L) \ |
| 152 | do { \ |
| 153 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 154 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 155 | } while(0) |
| 156 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 157 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 158 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 159 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 160 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 161 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 162 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 163 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 164 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 165 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 166 | /* The main Lua execution context. */ |
| 167 | struct hlua gL; |
| 168 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 169 | /* This is the memory pool containing struct lua for applets |
| 170 | * (including cli). |
| 171 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 172 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 173 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 174 | /* Used for Socket connection. */ |
| 175 | static struct proxy socket_proxy; |
| 176 | static struct server socket_tcp; |
| 177 | #ifdef USE_OPENSSL |
| 178 | static struct server socket_ssl; |
| 179 | #endif |
| 180 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 181 | /* List head of the function called at the initialisation time. */ |
| 182 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 183 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 184 | /* The following variables contains the reference of the different |
| 185 | * Lua classes. These references are useful for identify metadata |
| 186 | * associated with an object. |
| 187 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 188 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 189 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 190 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 191 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 192 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 193 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 194 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 195 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 196 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 197 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 198 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 199 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 200 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 201 | * because a task may remain alive during all the haproxy execution. |
| 202 | */ |
| 203 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 204 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 205 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 206 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 207 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 208 | * it is used for preventing infinite loops. |
| 209 | * |
| 210 | * I test the scheer with an infinite loop containing one incrementation |
| 211 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 212 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 213 | * to one interrupt each 10 000 instructions. |
| 214 | * |
| 215 | * configured | Number of |
| 216 | * instructions | loops executed |
| 217 | * between two | in milions |
| 218 | * forced yields | |
| 219 | * ---------------+--------------- |
| 220 | * 10 | 160 |
| 221 | * 500 | 670 |
| 222 | * 1000 | 680 |
| 223 | * 5000 | 700 |
| 224 | * 7000 | 700 |
| 225 | * 8000 | 700 |
| 226 | * 9000 | 710 <- ceil |
| 227 | * 10000 | 710 |
| 228 | * 100000 | 710 |
| 229 | * 1000000 | 710 |
| 230 | * |
| 231 | */ |
| 232 | static unsigned int hlua_nb_instruction = 10000; |
| 233 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 234 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 235 | * be enforced on any memory allocation. |
| 236 | */ |
| 237 | struct hlua_mem_allocator { |
| 238 | size_t allocated; |
| 239 | size_t limit; |
| 240 | }; |
| 241 | |
| 242 | static struct hlua_mem_allocator hlua_global_allocator; |
| 243 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 244 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 245 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 246 | "Cache-Control: no-cache\r\n" |
| 247 | "Connection: close\r\n" |
| 248 | "Content-Type: text/html\r\n" |
| 249 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 250 | "<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] | 251 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 252 | /* These functions converts types between HAProxy internal args or |
| 253 | * sample and LUA types. Another function permits to check if the |
| 254 | * LUA stack contains arguments according with an required ARG_T |
| 255 | * format. |
| 256 | */ |
| 257 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 258 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 259 | __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] | 260 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 261 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 262 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 263 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 264 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 265 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 266 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 267 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 268 | do { \ |
| 269 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 270 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 271 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 272 | } while (0) |
| 273 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 274 | /* Used to check an Lua function type in the stack. It creates and |
| 275 | * returns a reference of the function. This function throws an |
| 276 | * error if the rgument is not a "function". |
| 277 | */ |
| 278 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 279 | { |
| 280 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 281 | 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] | 282 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 283 | } |
| 284 | lua_pushvalue(L, argno); |
| 285 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 286 | } |
| 287 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 288 | /* Return the string that is of the top of the stack. */ |
| 289 | const char *hlua_get_top_error_string(lua_State *L) |
| 290 | { |
| 291 | if (lua_gettop(L) < 1) |
| 292 | return "unknown error"; |
| 293 | if (lua_type(L, -1) != LUA_TSTRING) |
| 294 | return "unknown error"; |
| 295 | return lua_tostring(L, -1); |
| 296 | } |
| 297 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 298 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 299 | { |
| 300 | lua_Debug ar; |
| 301 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 302 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 303 | int filled = 0; |
| 304 | |
| 305 | while (lua_getstack(L, level++, &ar)) { |
| 306 | |
| 307 | /* Add separator */ |
| 308 | if (filled) |
| 309 | chunk_appendf(msg, ", "); |
| 310 | filled = 1; |
| 311 | |
| 312 | /* Fill fields: |
| 313 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 314 | * 'l': fills in the field currentline; |
| 315 | * 'n': fills in the field name and namewhat; |
| 316 | * 't': fills in the field istailcall; |
| 317 | */ |
| 318 | lua_getinfo(L, "Slnt", &ar); |
| 319 | |
| 320 | /* Append code localisation */ |
| 321 | if (ar.currentline > 0) |
| 322 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 323 | else |
| 324 | chunk_appendf(msg, "%s ", ar.short_src); |
| 325 | |
| 326 | /* |
| 327 | * Get function name |
| 328 | * |
| 329 | * if namewhat is no empty, name is defined. |
| 330 | * what contains "Lua" for Lua function, "C" for C function, |
| 331 | * or "main" for main code. |
| 332 | */ |
| 333 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 334 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 335 | |
| 336 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 337 | chunk_appendf(msg, "main chunk"); |
| 338 | |
| 339 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 340 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 341 | |
| 342 | else /* nothing left... */ |
| 343 | chunk_appendf(msg, "?"); |
| 344 | |
| 345 | |
| 346 | /* Display tailed call */ |
| 347 | if (ar.istailcall) |
| 348 | chunk_appendf(msg, " ..."); |
| 349 | } |
| 350 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 351 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 355 | /* This function check the number of arguments available in the |
| 356 | * stack. If the number of arguments available is not the same |
| 357 | * then <nb> an error is throwed. |
| 358 | */ |
| 359 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 360 | { |
| 361 | if (lua_gettop(L) == nb) |
| 362 | return; |
| 363 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 364 | } |
| 365 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 366 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 367 | * and the line number where the error is encountered. |
| 368 | */ |
| 369 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 370 | { |
| 371 | va_list argp; |
| 372 | va_start(argp, fmt); |
| 373 | luaL_where(L, 1); |
| 374 | lua_pushvfstring(L, fmt, argp); |
| 375 | va_end(argp); |
| 376 | lua_concat(L, 2); |
| 377 | return 1; |
| 378 | } |
| 379 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 380 | /* This functions is used with sample fetch and converters. It |
| 381 | * converts the HAProxy configuration argument in a lua stack |
| 382 | * values. |
| 383 | * |
| 384 | * It takes an array of "arg", and each entry of the array is |
| 385 | * converted and pushed in the LUA stack. |
| 386 | */ |
| 387 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 388 | { |
| 389 | switch (arg->type) { |
| 390 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 391 | case ARGT_TIME: |
| 392 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 393 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 394 | break; |
| 395 | |
| 396 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 397 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 398 | break; |
| 399 | |
| 400 | case ARGT_IPV4: |
| 401 | case ARGT_IPV6: |
| 402 | case ARGT_MSK4: |
| 403 | case ARGT_MSK6: |
| 404 | case ARGT_FE: |
| 405 | case ARGT_BE: |
| 406 | case ARGT_TAB: |
| 407 | case ARGT_SRV: |
| 408 | case ARGT_USR: |
| 409 | case ARGT_MAP: |
| 410 | default: |
| 411 | lua_pushnil(L); |
| 412 | break; |
| 413 | } |
| 414 | return 1; |
| 415 | } |
| 416 | |
| 417 | /* This function take one entrie in an LUA stack at the index "ud", |
| 418 | * and try to convert it in an HAProxy argument entry. This is useful |
| 419 | * with sample fetch wrappers. The input arguments are gived to the |
| 420 | * lua wrapper and converted as arg list by thi function. |
| 421 | */ |
| 422 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 423 | { |
| 424 | switch (lua_type(L, ud)) { |
| 425 | |
| 426 | case LUA_TNUMBER: |
| 427 | case LUA_TBOOLEAN: |
| 428 | arg->type = ARGT_SINT; |
| 429 | arg->data.sint = lua_tointeger(L, ud); |
| 430 | break; |
| 431 | |
| 432 | case LUA_TSTRING: |
| 433 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 434 | 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] | 435 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 436 | arg->data.str.size = arg->data.str.data; |
| 437 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 438 | break; |
| 439 | |
| 440 | case LUA_TUSERDATA: |
| 441 | case LUA_TNIL: |
| 442 | case LUA_TTABLE: |
| 443 | case LUA_TFUNCTION: |
| 444 | case LUA_TTHREAD: |
| 445 | case LUA_TLIGHTUSERDATA: |
| 446 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 447 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | break; |
| 449 | } |
| 450 | return 1; |
| 451 | } |
| 452 | |
| 453 | /* the following functions are used to convert a struct sample |
| 454 | * in Lua type. This useful to convert the return of the |
| 455 | * fetchs or converters. |
| 456 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 457 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 459 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 460 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 462 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | case SMP_T_BIN: |
| 466 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 467 | 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] | 468 | break; |
| 469 | |
| 470 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 471 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 472 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 473 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 474 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 475 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 476 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 477 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 478 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 479 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 480 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 481 | 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] | 482 | break; |
| 483 | default: |
| 484 | lua_pushnil(L); |
| 485 | break; |
| 486 | } |
| 487 | break; |
| 488 | |
| 489 | case SMP_T_IPV4: |
| 490 | case SMP_T_IPV6: |
| 491 | 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] | 492 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 493 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 494 | 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] | 495 | else |
| 496 | lua_pushnil(L); |
| 497 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 498 | default: |
| 499 | lua_pushnil(L); |
| 500 | break; |
| 501 | } |
| 502 | return 1; |
| 503 | } |
| 504 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 505 | /* the following functions are used to convert a struct sample |
| 506 | * in Lua strings. This is useful to convert the return of the |
| 507 | * fetchs or converters. |
| 508 | */ |
| 509 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 510 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 511 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 512 | |
| 513 | case SMP_T_BIN: |
| 514 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 515 | 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] | 516 | break; |
| 517 | |
| 518 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 519 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 520 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 521 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 522 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 523 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 524 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 525 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 526 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 527 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 528 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 529 | 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] | 530 | break; |
| 531 | default: |
| 532 | lua_pushstring(L, ""); |
| 533 | break; |
| 534 | } |
| 535 | break; |
| 536 | |
| 537 | case SMP_T_SINT: |
| 538 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 539 | case SMP_T_IPV4: |
| 540 | case SMP_T_IPV6: |
| 541 | 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] | 542 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 543 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 544 | 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] | 545 | else |
| 546 | lua_pushstring(L, ""); |
| 547 | break; |
| 548 | default: |
| 549 | lua_pushstring(L, ""); |
| 550 | break; |
| 551 | } |
| 552 | return 1; |
| 553 | } |
| 554 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 555 | /* the following functions are used to convert an Lua type in a |
| 556 | * struct sample. This is useful to provide data from a converter |
| 557 | * to the LUA code. |
| 558 | */ |
| 559 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 560 | { |
| 561 | switch (lua_type(L, ud)) { |
| 562 | |
| 563 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 564 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 565 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | break; |
| 567 | |
| 568 | |
| 569 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 570 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 571 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 572 | break; |
| 573 | |
| 574 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 575 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 576 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 577 | 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] | 578 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 579 | smp->data.u.str.size = smp->data.u.str.data; |
| 580 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 581 | break; |
| 582 | |
| 583 | case LUA_TUSERDATA: |
| 584 | case LUA_TNIL: |
| 585 | case LUA_TTABLE: |
| 586 | case LUA_TFUNCTION: |
| 587 | case LUA_TTHREAD: |
| 588 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 589 | case LUA_TNONE: |
| 590 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 591 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 592 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 593 | break; |
| 594 | } |
| 595 | return 1; |
| 596 | } |
| 597 | |
| 598 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 599 | * 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] | 600 | * 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] | 601 | * |
| 602 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 603 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 604 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 605 | __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] | 606 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 607 | { |
| 608 | int min_arg; |
| 609 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 610 | struct proxy *px; |
| 611 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 612 | |
| 613 | idx = 0; |
| 614 | min_arg = ARGM(mask); |
| 615 | mask >>= ARGM_BITS; |
| 616 | |
| 617 | while (1) { |
| 618 | |
| 619 | /* Check oversize. */ |
| 620 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 621 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* Check for mandatory arguments. */ |
| 625 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 626 | if (idx < min_arg) { |
| 627 | |
| 628 | /* If miss other argument than the first one, we return an error. */ |
| 629 | if (idx > 0) |
| 630 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 631 | |
| 632 | /* If first argument have a certain type, some default values |
| 633 | * may be used. See the function smp_resolve_args(). |
| 634 | */ |
| 635 | switch (mask & ARGT_MASK) { |
| 636 | |
| 637 | case ARGT_FE: |
| 638 | if (!(p->cap & PR_CAP_FE)) |
| 639 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 640 | argp[idx].data.prx = p; |
| 641 | argp[idx].type = ARGT_FE; |
| 642 | argp[idx+1].type = ARGT_STOP; |
| 643 | break; |
| 644 | |
| 645 | case ARGT_BE: |
| 646 | if (!(p->cap & PR_CAP_BE)) |
| 647 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 648 | argp[idx].data.prx = p; |
| 649 | argp[idx].type = ARGT_BE; |
| 650 | argp[idx+1].type = ARGT_STOP; |
| 651 | break; |
| 652 | |
| 653 | case ARGT_TAB: |
| 654 | argp[idx].data.prx = p; |
| 655 | argp[idx].type = ARGT_TAB; |
| 656 | argp[idx+1].type = ARGT_STOP; |
| 657 | break; |
| 658 | |
| 659 | default: |
| 660 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 661 | break; |
| 662 | } |
| 663 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | /* Check for exceed the number of requiered argument. */ |
| 668 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 669 | argp[idx].type != ARGT_STOP) { |
| 670 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 671 | } |
| 672 | |
| 673 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 674 | argp[idx].type == ARGT_STOP) { |
| 675 | return 0; |
| 676 | } |
| 677 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 678 | /* Convert some argument types. */ |
| 679 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 680 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 681 | if (argp[idx].type != ARGT_SINT) |
| 682 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 683 | argp[idx].type = ARGT_SINT; |
| 684 | break; |
| 685 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 686 | case ARGT_TIME: |
| 687 | if (argp[idx].type != ARGT_SINT) |
| 688 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 689 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 690 | break; |
| 691 | |
| 692 | case ARGT_SIZE: |
| 693 | if (argp[idx].type != ARGT_SINT) |
| 694 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 695 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 696 | break; |
| 697 | |
| 698 | case ARGT_FE: |
| 699 | if (argp[idx].type != ARGT_STR) |
| 700 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 701 | memcpy(trash.area, argp[idx].data.str.area, |
| 702 | argp[idx].data.str.data); |
| 703 | trash.area[argp[idx].data.str.data] = 0; |
| 704 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 705 | if (!argp[idx].data.prx) |
| 706 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 707 | argp[idx].type = ARGT_FE; |
| 708 | break; |
| 709 | |
| 710 | case ARGT_BE: |
| 711 | if (argp[idx].type != ARGT_STR) |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 713 | memcpy(trash.area, argp[idx].data.str.area, |
| 714 | argp[idx].data.str.data); |
| 715 | trash.area[argp[idx].data.str.data] = 0; |
| 716 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 717 | if (!argp[idx].data.prx) |
| 718 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 719 | argp[idx].type = ARGT_BE; |
| 720 | break; |
| 721 | |
| 722 | case ARGT_TAB: |
| 723 | if (argp[idx].type != ARGT_STR) |
| 724 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 725 | memcpy(trash.area, argp[idx].data.str.area, |
| 726 | argp[idx].data.str.data); |
| 727 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | e351e76 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 728 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 729 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 730 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 731 | argp[idx].type = ARGT_TAB; |
| 732 | break; |
| 733 | |
| 734 | case ARGT_SRV: |
| 735 | if (argp[idx].type != ARGT_STR) |
| 736 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 737 | memcpy(trash.area, argp[idx].data.str.area, |
| 738 | argp[idx].data.str.data); |
| 739 | trash.area[argp[idx].data.str.data] = 0; |
| 740 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 741 | if (sname) { |
| 742 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 743 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 744 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 745 | if (!px) |
| 746 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 747 | } |
| 748 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 749 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 751 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 752 | argp[idx].data.srv = findserver(px, sname); |
| 753 | if (!argp[idx].data.srv) |
| 754 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 755 | argp[idx].type = ARGT_SRV; |
| 756 | break; |
| 757 | |
| 758 | case ARGT_IPV4: |
Christopher Faulet | e7f1fcf | 2020-08-07 09:07:26 +0200 | [diff] [blame] | 759 | if (argp[idx].type != ARGT_STR) |
| 760 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 761 | memcpy(trash.area, argp[idx].data.str.area, |
| 762 | argp[idx].data.str.data); |
| 763 | trash.area[argp[idx].data.str.data] = 0; |
| 764 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 765 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 766 | argp[idx].type = ARGT_IPV4; |
| 767 | break; |
| 768 | |
| 769 | case ARGT_MSK4: |
Christopher Faulet | ff8de2c | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 770 | if (argp[idx].type == ARGT_SINT) |
| 771 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 772 | else if (argp[idx].type == ARGT_STR) { |
| 773 | memcpy(trash.area, argp[idx].data.str.area, |
| 774 | argp[idx].data.str.data); |
| 775 | trash.area[argp[idx].data.str.data] = 0; |
| 776 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
| 777 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 778 | } |
| 779 | else |
| 780 | WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected")); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 781 | argp[idx].type = ARGT_MSK4; |
| 782 | break; |
| 783 | |
| 784 | case ARGT_IPV6: |
Christopher Faulet | e7f1fcf | 2020-08-07 09:07:26 +0200 | [diff] [blame] | 785 | if (argp[idx].type != ARGT_STR) |
| 786 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 787 | memcpy(trash.area, argp[idx].data.str.area, |
| 788 | argp[idx].data.str.data); |
| 789 | trash.area[argp[idx].data.str.data] = 0; |
| 790 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 791 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 792 | argp[idx].type = ARGT_IPV6; |
| 793 | break; |
| 794 | |
| 795 | case ARGT_MSK6: |
Christopher Faulet | ff8de2c | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 796 | if (argp[idx].type == ARGT_SINT) |
| 797 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 798 | else if (argp[idx].type == ARGT_STR) { |
| 799 | memcpy(trash.area, argp[idx].data.str.area, |
| 800 | argp[idx].data.str.data); |
| 801 | trash.area[argp[idx].data.str.data] = 0; |
| 802 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
| 803 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 804 | } |
| 805 | else |
| 806 | WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected")); |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 807 | argp[idx].type = ARGT_MSK6; |
| 808 | break; |
| 809 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 810 | case ARGT_MAP: |
| 811 | case ARGT_REG: |
| 812 | case ARGT_USR: |
| 813 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 814 | break; |
| 815 | } |
| 816 | |
| 817 | /* Check for type of argument. */ |
| 818 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 819 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 820 | arg_type_names[(mask & ARGT_MASK)], |
| 821 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 822 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 823 | } |
| 824 | |
| 825 | /* Next argument. */ |
| 826 | mask >>= ARGT_BITS; |
| 827 | idx++; |
| 828 | } |
| 829 | } |
| 830 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 831 | /* |
| 832 | * The following functions are used to make correspondance between the the |
| 833 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 834 | * |
| 835 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 836 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 837 | */ |
| 838 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 839 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 840 | struct hlua **hlua = lua_getextraspace(L); |
| 841 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 842 | } |
| 843 | static inline void hlua_sethlua(struct hlua *hlua) |
| 844 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 845 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 846 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 847 | } |
| 848 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 849 | /* This function is used to send logs. It try to send on screen (stderr) |
| 850 | * and on the default syslog server. |
| 851 | */ |
| 852 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 853 | { |
| 854 | struct tm tm; |
| 855 | char *p; |
| 856 | |
| 857 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 858 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 859 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 860 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 861 | /* Break the message if exceed the buffer size. */ |
| 862 | *(p-4) = ' '; |
| 863 | *(p-3) = '.'; |
| 864 | *(p-2) = '.'; |
| 865 | *(p-1) = '.'; |
| 866 | break; |
| 867 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 868 | if (isprint(*msg)) |
| 869 | *p = *msg; |
| 870 | else |
| 871 | *p = '.'; |
| 872 | } |
| 873 | *p = '\0'; |
| 874 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 875 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 876 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | b290edf | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 877 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 878 | return; |
| 879 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 880 | get_localtime(date.tv_sec, &tm); |
| 881 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 882 | 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] | 883 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 884 | fflush(stderr); |
| 885 | } |
| 886 | } |
| 887 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 888 | /* This function just ensure that the yield will be always |
| 889 | * returned with a timeout and permit to set some flags |
| 890 | */ |
| 891 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 892 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 893 | { |
| 894 | struct hlua *hlua = hlua_gethlua(L); |
| 895 | |
| 896 | /* Set the wake timeout. If timeout is required, we set |
| 897 | * the expiration time. |
| 898 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 899 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 900 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 901 | hlua->flags |= flags; |
| 902 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 903 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 904 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 905 | } |
| 906 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 907 | /* This function initialises the Lua environment stored in the stream. |
| 908 | * 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] | 909 | * 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] | 910 | * |
| 911 | * This function is particular. it initialises a new Lua thread. If the |
| 912 | * initialisation fails (example: out of memory error), the lua function |
| 913 | * throws an error (longjmp). |
| 914 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 915 | * In some case (at least one), this function can be called from safe |
| 916 | * environement, so we must not initialise it. While the support of |
| 917 | * threads appear, the safe environment set a lock to ensure only one |
| 918 | * Lua execution at a time. If we initialize safe environment in another |
| 919 | * safe environmenet, we have a dead lock. |
| 920 | * |
| 921 | * set "already_safe" true if the context is initialized form safe |
| 922 | * Lua fonction. |
| 923 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 924 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 925 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 926 | * MUST NOT manipulate the created thread stack state, because it is not |
| 927 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 928 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 929 | 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] | 930 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 931 | if (!already_safe) { |
| 932 | if (!SET_SAFE_LJMP(gL.T)) { |
| 933 | lua->Tref = LUA_REFNIL; |
| 934 | return 0; |
| 935 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 936 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 937 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 938 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 939 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 940 | lua->T = lua_newthread(gL.T); |
| 941 | if (!lua->T) { |
| 942 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 943 | if (!already_safe) |
| 944 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | hlua_sethlua(lua); |
| 948 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 949 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 950 | if (!already_safe) |
| 951 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 952 | return 1; |
| 953 | } |
| 954 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 955 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 956 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 957 | * is not freed. |
| 958 | */ |
| 959 | void hlua_ctx_destroy(struct hlua *lua) |
| 960 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 961 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 962 | return; |
| 963 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 964 | if (!lua->T) |
| 965 | goto end; |
| 966 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 967 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 968 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 969 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 970 | if (!SET_SAFE_LJMP(lua->T)) |
| 971 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 972 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 973 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 974 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 975 | if (!SET_SAFE_LJMP(gL.T)) |
| 976 | return; |
| 977 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 978 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 979 | /* Forces a garbage collecting process. If the Lua program is finished |
| 980 | * without error, we run the GC on the thread pointer. Its freed all |
| 981 | * the unused memory. |
| 982 | * If the thread is finnish with an error or is currently yielded, |
| 983 | * it seems that the GC applied on the thread doesn't clean anything, |
| 984 | * so e run the GC on the main thread. |
| 985 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 986 | * the garbage collection. |
| 987 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 988 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 989 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 990 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 991 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 992 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 993 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 994 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 995 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 996 | |
| 997 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 998 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* This function is used to restore the Lua context when a coroutine |
| 1002 | * fails. This function copy the common memory between old coroutine |
| 1003 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1004 | * replaced by the new coroutine. |
| 1005 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1006 | * as string error message and it is copied in the new stack. |
| 1007 | */ |
| 1008 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1009 | { |
| 1010 | lua_State *T; |
| 1011 | int new_ref; |
| 1012 | |
| 1013 | /* Renew the main LUA stack doesn't have sense. */ |
| 1014 | if (lua == &gL) |
| 1015 | return 0; |
| 1016 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1017 | /* New Lua coroutine. */ |
| 1018 | T = lua_newthread(gL.T); |
| 1019 | if (!T) |
| 1020 | return 0; |
| 1021 | |
| 1022 | /* Copy last error message. */ |
| 1023 | if (keep_msg) |
| 1024 | lua_xmove(lua->T, T, 1); |
| 1025 | |
| 1026 | /* Copy data between the coroutines. */ |
| 1027 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1028 | lua_xmove(lua->T, T, 1); |
| 1029 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1030 | |
| 1031 | /* Destroy old data. */ |
| 1032 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1033 | |
| 1034 | /* The thread is garbage collected by Lua. */ |
| 1035 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1036 | |
| 1037 | /* Fill the struct with the new coroutine values. */ |
| 1038 | lua->Mref = new_ref; |
| 1039 | lua->T = T; |
| 1040 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1041 | |
| 1042 | /* Set context. */ |
| 1043 | hlua_sethlua(lua); |
| 1044 | |
| 1045 | return 1; |
| 1046 | } |
| 1047 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1048 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1049 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1050 | struct hlua *hlua = hlua_gethlua(L); |
| 1051 | |
| 1052 | /* Lua cannot yield when its returning from a function, |
| 1053 | * so, we can fix the interrupt hook to 1 instruction, |
| 1054 | * expecting that the function is finnished. |
| 1055 | */ |
| 1056 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1057 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1058 | return; |
| 1059 | } |
| 1060 | |
| 1061 | /* restore the interrupt condition. */ |
| 1062 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1063 | |
| 1064 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1065 | * If the state is not yieldable, trying yield causes an error. |
| 1066 | */ |
| 1067 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1068 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1069 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1070 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1071 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1072 | hlua->run_time += now_ms - hlua->start_time; |
| 1073 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1074 | lua_pushfstring(L, "execution timeout"); |
| 1075 | WILL_LJMP(lua_error(L)); |
| 1076 | } |
| 1077 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1078 | /* Update the start time. */ |
| 1079 | hlua->start_time = now_ms; |
| 1080 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1081 | /* Try to interrupt the process at the end of the current |
| 1082 | * unyieldable function. |
| 1083 | */ |
| 1084 | 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] | 1085 | } |
| 1086 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1087 | /* This function start or resumes the Lua stack execution. If the flag |
| 1088 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1089 | * The function return an error. |
| 1090 | * |
| 1091 | * The function can returns 4 values: |
| 1092 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1093 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1094 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1095 | * - 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] | 1096 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1097 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1098 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1099 | * 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] | 1100 | * LUA code. |
| 1101 | */ |
| 1102 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1103 | { |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1104 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1105 | int nres; |
| 1106 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1107 | int ret; |
| 1108 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1109 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1110 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1111 | /* Initialise run time counter. */ |
| 1112 | if (!HLUA_IS_RUNNING(lua)) |
| 1113 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1114 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1115 | /* Lock the whole Lua execution. This lock must be before the |
| 1116 | * label "resume_execution". |
| 1117 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1118 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1119 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1120 | resume_execution: |
| 1121 | |
| 1122 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1123 | * instructions. it is used for preventing infinite loops. |
| 1124 | */ |
| 1125 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1126 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1127 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1128 | HLUA_SET_RUN(lua); |
| 1129 | HLUA_CLR_CTRLYIELD(lua); |
| 1130 | HLUA_CLR_WAKERESWR(lua); |
| 1131 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1132 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1133 | /* Update the start time. */ |
| 1134 | lua->start_time = now_ms; |
| 1135 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1136 | /* Call the function. */ |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1137 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1138 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1139 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1140 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1141 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1142 | switch (ret) { |
| 1143 | |
| 1144 | case LUA_OK: |
| 1145 | ret = HLUA_E_OK; |
| 1146 | break; |
| 1147 | |
| 1148 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1149 | /* Check if the execution timeout is expired. It it is the case, we |
| 1150 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1151 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1152 | tv_update_date(0, 1); |
| 1153 | lua->run_time += now_ms - lua->start_time; |
| 1154 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1155 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1156 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1157 | break; |
| 1158 | } |
| 1159 | /* Process the forced yield. if the general yield is not allowed or |
| 1160 | * if no task were associated this the current Lua execution |
| 1161 | * coroutine, we resume the execution. Else we want to return in the |
| 1162 | * scheduler and we want to be waked up again, to continue the |
| 1163 | * current Lua execution. So we schedule our own task. |
| 1164 | */ |
| 1165 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1166 | if (!yield_allowed || !lua->task) |
| 1167 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1168 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1169 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | if (!yield_allowed) { |
| 1171 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1172 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | break; |
| 1174 | } |
| 1175 | ret = HLUA_E_AGAIN; |
| 1176 | break; |
| 1177 | |
| 1178 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1179 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1180 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1181 | * because the errors ares the only one mean to return immediately |
| 1182 | * from and lua execution. |
| 1183 | */ |
| 1184 | if (lua->flags & HLUA_EXIT) { |
| 1185 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1186 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1187 | break; |
| 1188 | } |
| 1189 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1190 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1191 | if (!lua_checkstack(lua->T, 1)) { |
| 1192 | ret = HLUA_E_ERR; |
| 1193 | break; |
| 1194 | } |
| 1195 | msg = lua_tostring(lua->T, -1); |
| 1196 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1197 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1198 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1199 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1200 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1201 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1202 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1203 | ret = HLUA_E_ERRMSG; |
| 1204 | break; |
| 1205 | |
| 1206 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1207 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1208 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1209 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | break; |
| 1211 | |
| 1212 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1213 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | if (!lua_checkstack(lua->T, 1)) { |
| 1215 | ret = HLUA_E_ERR; |
| 1216 | break; |
| 1217 | } |
| 1218 | msg = lua_tostring(lua->T, -1); |
| 1219 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1220 | lua_pop(lua->T, 1); |
| 1221 | if (msg) |
| 1222 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1223 | else |
| 1224 | lua_pushfstring(lua->T, "message handler error"); |
| 1225 | ret = HLUA_E_ERRMSG; |
| 1226 | break; |
| 1227 | |
| 1228 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1229 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1230 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1231 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1232 | break; |
| 1233 | } |
| 1234 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1235 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1236 | if (lua->flags & HLUA_MUST_GC && |
| 1237 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1238 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1239 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1240 | switch (ret) { |
| 1241 | case HLUA_E_AGAIN: |
| 1242 | break; |
| 1243 | |
| 1244 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1245 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1246 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1247 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1248 | break; |
| 1249 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1250 | case HLUA_E_ETMOUT: |
| 1251 | case HLUA_E_NOMEM: |
| 1252 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1253 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1254 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1255 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1256 | hlua_ctx_renew(lua, 0); |
| 1257 | break; |
| 1258 | |
| 1259 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1260 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1261 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1262 | break; |
| 1263 | } |
| 1264 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1265 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1266 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1267 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1268 | return ret; |
| 1269 | } |
| 1270 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1271 | /* This function exit the current code. */ |
| 1272 | __LJMP static int hlua_done(lua_State *L) |
| 1273 | { |
| 1274 | struct hlua *hlua = hlua_gethlua(L); |
| 1275 | |
| 1276 | hlua->flags |= HLUA_EXIT; |
| 1277 | WILL_LJMP(lua_error(L)); |
| 1278 | |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1282 | /* This function is an LUA binding. It provides a function |
| 1283 | * for deleting ACL from a referenced ACL file. |
| 1284 | */ |
| 1285 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1286 | { |
| 1287 | const char *name; |
| 1288 | const char *key; |
| 1289 | struct pat_ref *ref; |
| 1290 | |
| 1291 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1292 | |
| 1293 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1294 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1295 | |
| 1296 | ref = pat_ref_lookup(name); |
| 1297 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1298 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1299 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1300 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1301 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1302 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | /* This function is an LUA binding. It provides a function |
| 1307 | * for deleting map entry from a referenced map file. |
| 1308 | */ |
| 1309 | static int hlua_del_map(lua_State *L) |
| 1310 | { |
| 1311 | const char *name; |
| 1312 | const char *key; |
| 1313 | struct pat_ref *ref; |
| 1314 | |
| 1315 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1316 | |
| 1317 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1318 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1319 | |
| 1320 | ref = pat_ref_lookup(name); |
| 1321 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1322 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1323 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1324 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1325 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1326 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | /* This function is an LUA binding. It provides a function |
| 1331 | * for adding ACL pattern from a referenced ACL file. |
| 1332 | */ |
| 1333 | static int hlua_add_acl(lua_State *L) |
| 1334 | { |
| 1335 | const char *name; |
| 1336 | const char *key; |
| 1337 | struct pat_ref *ref; |
| 1338 | |
| 1339 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1340 | |
| 1341 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1342 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1343 | |
| 1344 | ref = pat_ref_lookup(name); |
| 1345 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1346 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1347 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1348 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1349 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1350 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1351 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | /* This function is an LUA binding. It provides a function |
| 1356 | * for setting map pattern and sample from a referenced map |
| 1357 | * file. |
| 1358 | */ |
| 1359 | static int hlua_set_map(lua_State *L) |
| 1360 | { |
| 1361 | const char *name; |
| 1362 | const char *key; |
| 1363 | const char *value; |
| 1364 | struct pat_ref *ref; |
| 1365 | |
| 1366 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1367 | |
| 1368 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1369 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1370 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1371 | |
| 1372 | ref = pat_ref_lookup(name); |
| 1373 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1374 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1375 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1376 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1377 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1378 | pat_ref_set(ref, key, value, NULL); |
| 1379 | else |
| 1380 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1381 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1382 | return 0; |
| 1383 | } |
| 1384 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1385 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1386 | * an integer or user data. This data is associated with a metatable. This |
| 1387 | * metatable have an original version registred in the global context with |
| 1388 | * the name of the object (_G[<name>] = <metable> ). |
| 1389 | * |
| 1390 | * A metable is a table that modify the standard behavior of a standard |
| 1391 | * access to the associated data. The entries of this new metatable are |
| 1392 | * defined as is: |
| 1393 | * |
| 1394 | * http://lua-users.org/wiki/MetatableEvents |
| 1395 | * |
| 1396 | * __index |
| 1397 | * |
| 1398 | * we access an absent field in a table, the result is nil. This is |
| 1399 | * true, but it is not the whole truth. Actually, such access triggers |
| 1400 | * the interpreter to look for an __index metamethod: If there is no |
| 1401 | * such method, as usually happens, then the access results in nil; |
| 1402 | * otherwise, the metamethod will provide the result. |
| 1403 | * |
| 1404 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1405 | * the key does not appear in the table, but the metatable has an __index |
| 1406 | * property: |
| 1407 | * |
| 1408 | * - if the value is a function, the function is called, passing in the |
| 1409 | * table and the key; the return value of that function is returned as |
| 1410 | * the result. |
| 1411 | * |
| 1412 | * - if the value is another table, the value of the key in that table is |
| 1413 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1414 | * table's metatable has an __index property, then it continues on up) |
| 1415 | * |
| 1416 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1417 | * |
| 1418 | * http://www.lua.org/pil/13.4.1.html |
| 1419 | * |
| 1420 | * __newindex |
| 1421 | * |
| 1422 | * Like __index, but control property assignment. |
| 1423 | * |
| 1424 | * __mode - Control weak references. A string value with one or both |
| 1425 | * of the characters 'k' and 'v' which specifies that the the |
| 1426 | * keys and/or values in the table are weak references. |
| 1427 | * |
| 1428 | * __call - Treat a table like a function. When a table is followed by |
| 1429 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1430 | * a __call key pointing to a function, that function is invoked |
| 1431 | * (passing any specified arguments) and the return value is |
| 1432 | * returned. |
| 1433 | * |
| 1434 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1435 | * called, if the metatable for myTable has a __metatable |
| 1436 | * key, the value of that key is returned instead of the |
| 1437 | * actual metatable. |
| 1438 | * |
| 1439 | * __tostring - Control string representation. When the builtin |
| 1440 | * "tostring( myTable )" function is called, if the metatable |
| 1441 | * for myTable has a __tostring property set to a function, |
| 1442 | * that function is invoked (passing myTable to it) and the |
| 1443 | * return value is used as the string representation. |
| 1444 | * |
| 1445 | * __len - Control table length. When the table length is requested using |
| 1446 | * the length operator ( '#' ), if the metatable for myTable has |
| 1447 | * a __len key pointing to a function, that function is invoked |
| 1448 | * (passing myTable to it) and the return value used as the value |
| 1449 | * of "#myTable". |
| 1450 | * |
| 1451 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1452 | * collected, if the metatable has a __gc field pointing to a |
| 1453 | * function, that function is first invoked, passing the userdata |
| 1454 | * to it. The __gc metamethod is not called for tables. |
| 1455 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1456 | * |
| 1457 | * Special metamethods for redefining standard operators: |
| 1458 | * http://www.lua.org/pil/13.1.html |
| 1459 | * |
| 1460 | * __add "+" |
| 1461 | * __sub "-" |
| 1462 | * __mul "*" |
| 1463 | * __div "/" |
| 1464 | * __unm "!" |
| 1465 | * __pow "^" |
| 1466 | * __concat ".." |
| 1467 | * |
| 1468 | * Special methods for redfining standar relations |
| 1469 | * http://www.lua.org/pil/13.2.html |
| 1470 | * |
| 1471 | * __eq "==" |
| 1472 | * __lt "<" |
| 1473 | * __le "<=" |
| 1474 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1475 | |
| 1476 | /* |
| 1477 | * |
| 1478 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1479 | * Class Map |
| 1480 | * |
| 1481 | * |
| 1482 | */ |
| 1483 | |
| 1484 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1485 | * a class session, otherwise it throws an error. |
| 1486 | */ |
| 1487 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1488 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1489 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* This function is the map constructor. It don't need |
| 1493 | * the class Map object. It creates and return a new Map |
| 1494 | * object. It must be called only during "body" or "init" |
| 1495 | * context because it process some filesystem accesses. |
| 1496 | */ |
| 1497 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1498 | { |
| 1499 | const char *fn; |
| 1500 | int match = PAT_MATCH_STR; |
| 1501 | struct sample_conv conv; |
| 1502 | const char *file = ""; |
| 1503 | int line = 0; |
| 1504 | lua_Debug ar; |
| 1505 | char *err = NULL; |
| 1506 | struct arg args[2]; |
| 1507 | |
| 1508 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1509 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1510 | |
| 1511 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1512 | |
| 1513 | if (lua_gettop(L) >= 2) { |
| 1514 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1515 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1516 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1517 | } |
| 1518 | |
| 1519 | /* Get Lua filename and line number. */ |
| 1520 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1521 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1522 | if (ar.currentline > 0) { /* is there info? */ |
| 1523 | file = ar.short_src; |
| 1524 | line = ar.currentline; |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | /* fill fake sample_conv struct. */ |
| 1529 | conv.kw = ""; /* unused. */ |
| 1530 | conv.process = NULL; /* unused. */ |
| 1531 | conv.arg_mask = 0; /* unused. */ |
| 1532 | conv.val_args = NULL; /* unused. */ |
| 1533 | conv.out_type = SMP_T_STR; |
| 1534 | conv.private = (void *)(long)match; |
| 1535 | switch (match) { |
| 1536 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1537 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1538 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1539 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1540 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1541 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1542 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1543 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1544 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1545 | default: |
| 1546 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1547 | } |
| 1548 | |
| 1549 | /* fill fake args. */ |
| 1550 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1551 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1552 | args[1].type = ARGT_STOP; |
| 1553 | |
| 1554 | /* load the map. */ |
| 1555 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1556 | /* error case: we cant use luaL_error because we must |
| 1557 | * free the err variable. |
| 1558 | */ |
| 1559 | luaL_where(L, 1); |
| 1560 | lua_pushfstring(L, "'new': %s.", err); |
| 1561 | lua_concat(L, 2); |
| 1562 | free(err); |
| 1563 | WILL_LJMP(lua_error(L)); |
| 1564 | } |
| 1565 | |
| 1566 | /* create the lua object. */ |
| 1567 | lua_newtable(L); |
| 1568 | lua_pushlightuserdata(L, args[0].data.map); |
| 1569 | lua_rawseti(L, -2, 0); |
| 1570 | |
| 1571 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1572 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1573 | lua_setmetatable(L, -2); |
| 1574 | |
| 1575 | |
| 1576 | return 1; |
| 1577 | } |
| 1578 | |
| 1579 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1580 | { |
| 1581 | struct map_descriptor *desc; |
| 1582 | struct pattern *pat; |
| 1583 | struct sample smp; |
| 1584 | |
| 1585 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1586 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1587 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1588 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1589 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1590 | } |
| 1591 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1592 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1593 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1594 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry Fournier | 8e55384 | 2020-11-10 20:38:20 +0100 | [diff] [blame] | 1595 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1599 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1600 | if (str) |
| 1601 | lua_pushstring(L, ""); |
| 1602 | else |
| 1603 | lua_pushnil(L); |
| 1604 | return 1; |
| 1605 | } |
| 1606 | |
| 1607 | /* 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] | 1608 | 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] | 1609 | return 1; |
| 1610 | } |
| 1611 | |
| 1612 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1613 | { |
| 1614 | return _hlua_map_lookup(L, 0); |
| 1615 | } |
| 1616 | |
| 1617 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1618 | { |
| 1619 | return _hlua_map_lookup(L, 1); |
| 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * |
| 1624 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | * Class Socket |
| 1626 | * |
| 1627 | * |
| 1628 | */ |
| 1629 | |
| 1630 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1631 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1632 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | /* 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] | 1636 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1637 | * received. |
| 1638 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1639 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1640 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1641 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1642 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1643 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1644 | if (appctx->ctx.hlua_cosocket.die) { |
| 1645 | si_shutw(si); |
| 1646 | si_shutr(si); |
| 1647 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1648 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1649 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1650 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1651 | } |
| 1652 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1653 | /* If the connection object is not available, close all the |
| 1654 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1655 | */ |
| 1656 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1657 | si_shutw(si); |
| 1658 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1659 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1660 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1661 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1662 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1665 | /* If we cant write, wakeup the pending write signals. */ |
| 1666 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1667 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1668 | |
| 1669 | /* If we cant read, wakeup the pending read signals. */ |
| 1670 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1671 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1672 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1673 | /* if the connection is not estabkished, inform the stream that we want |
| 1674 | * to be notified whenever the connection completes. |
| 1675 | */ |
| 1676 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1677 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1678 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1679 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1680 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1681 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1682 | |
| 1683 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1684 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1686 | /* 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] | 1687 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1688 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | |
| 1690 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1691 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1692 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1693 | |
| 1694 | /* Some data were injected in the buffer, notify the stream |
| 1695 | * interface. |
| 1696 | */ |
| 1697 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1698 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1701 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1702 | */ |
| 1703 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1704 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | } |
| 1706 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1707 | /* This function is called when the "struct stream" is destroyed. |
| 1708 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1709 | * Wake all the pending signals. |
| 1710 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1711 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1713 | struct xref *peer; |
| 1714 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1715 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1716 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1717 | if (peer) |
| 1718 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1719 | |
| 1720 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1721 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1722 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1726 | * uses this object. If the stream does not exists, just quit. |
| 1727 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1728 | * pending signal can rest in the read and write lists. destroy |
| 1729 | * it. |
| 1730 | */ |
| 1731 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1732 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1733 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1734 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1735 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1736 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1737 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1738 | |
| 1739 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1740 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1741 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1743 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1744 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1745 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1746 | appctx->ctx.hlua_cosocket.die = 1; |
| 1747 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1748 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1749 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1750 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1755 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1756 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1757 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1758 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1759 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1760 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1761 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1762 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1763 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1764 | |
| 1765 | /* Check if we run on the same thread than the xreator thread. |
| 1766 | * We cannot access to the socket if the thread is different. |
| 1767 | */ |
| 1768 | if (socket->tid != tid) |
| 1769 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1770 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1771 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1772 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1773 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1774 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1775 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1776 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1777 | appctx->ctx.hlua_cosocket.die = 1; |
| 1778 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1779 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1780 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1781 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | return 0; |
| 1783 | } |
| 1784 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1785 | /* The close function calls close_helper. |
| 1786 | */ |
| 1787 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1788 | { |
| 1789 | MAY_LJMP(check_args(L, 1, "close")); |
| 1790 | return hlua_socket_close_helper(L); |
| 1791 | } |
| 1792 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | /* This Lua function assumes that the stack contain three parameters. |
| 1794 | * 1 - USERDATA containing a struct socket |
| 1795 | * 2 - INTEGER with values of the macro defined below |
| 1796 | * If the integer is -1, we must read at most one line. |
| 1797 | * If the integer is -2, we ust read all the data until the |
| 1798 | * end of the stream. |
| 1799 | * If the integer is positive value, we must read a number of |
| 1800 | * bytes corresponding to this value. |
| 1801 | */ |
| 1802 | #define HLSR_READ_LINE (-1) |
| 1803 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1804 | __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] | 1805 | { |
| 1806 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1807 | int wanted = lua_tointeger(L, 2); |
| 1808 | struct hlua *hlua = hlua_gethlua(L); |
| 1809 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1810 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1811 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1812 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1813 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1814 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1815 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1816 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1817 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1818 | struct stream_interface *si; |
| 1819 | struct stream *s; |
| 1820 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1821 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | |
| 1823 | /* Check if this lua stack is schedulable. */ |
| 1824 | if (!hlua || !hlua->task) |
| 1825 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1826 | "'frontend', 'backend' or 'task'")); |
| 1827 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1828 | /* Check if we run on the same thread than the xreator thread. |
| 1829 | * We cannot access to the socket if the thread is different. |
| 1830 | */ |
| 1831 | if (socket->tid != tid) |
| 1832 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1833 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1834 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1835 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1836 | if (!peer) |
| 1837 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1838 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1839 | si = appctx->owner; |
| 1840 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1842 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1844 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1845 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1846 | if (nblk < 0) /* Connection close. */ |
| 1847 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1848 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1850 | |
| 1851 | /* remove final \r\n. */ |
| 1852 | if (nblk == 1) { |
| 1853 | if (blk1[len1-1] == '\n') { |
| 1854 | len1--; |
| 1855 | skip_at_end++; |
| 1856 | if (blk1[len1-1] == '\r') { |
| 1857 | len1--; |
| 1858 | skip_at_end++; |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | else { |
| 1863 | if (blk2[len2-1] == '\n') { |
| 1864 | len2--; |
| 1865 | skip_at_end++; |
| 1866 | if (blk2[len2-1] == '\r') { |
| 1867 | len2--; |
| 1868 | skip_at_end++; |
| 1869 | } |
| 1870 | } |
| 1871 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1876 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | if (nblk < 0) /* Connection close. */ |
| 1878 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1879 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | goto connection_empty; |
| 1881 | } |
| 1882 | |
| 1883 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1885 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1886 | if (nblk < 0) /* Connection close. */ |
| 1887 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1888 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1889 | goto connection_empty; |
| 1890 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1891 | missing_bytes = wanted - socket->b.n; |
| 1892 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1894 | len1 = missing_bytes; |
| 1895 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1896 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | len = len1; |
| 1900 | |
| 1901 | luaL_addlstring(&socket->b, blk1, len1); |
| 1902 | if (nblk == 2) { |
| 1903 | len += len2; |
| 1904 | luaL_addlstring(&socket->b, blk2, len2); |
| 1905 | } |
| 1906 | |
| 1907 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1908 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | |
| 1910 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1911 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1912 | |
| 1913 | /* If the pattern reclaim to read all the data |
| 1914 | * in the connection, got out. |
| 1915 | */ |
| 1916 | if (wanted == HLSR_READ_ALL) |
| 1917 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1918 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | goto connection_empty; |
| 1920 | |
| 1921 | /* Return result. */ |
| 1922 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1923 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1924 | return 1; |
| 1925 | |
| 1926 | connection_closed: |
| 1927 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1928 | xref_unlock(&socket->xref, peer); |
| 1929 | |
| 1930 | no_peer: |
| 1931 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1932 | /* If the buffer containds data. */ |
| 1933 | if (socket->b.n > 0) { |
| 1934 | luaL_pushresult(&socket->b); |
| 1935 | return 1; |
| 1936 | } |
| 1937 | lua_pushnil(L); |
| 1938 | lua_pushstring(L, "connection closed."); |
| 1939 | return 2; |
| 1940 | |
| 1941 | connection_empty: |
| 1942 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1943 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1944 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1945 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1946 | } |
| 1947 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1948 | 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] | 1949 | return 0; |
| 1950 | } |
| 1951 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1952 | /* This Lua function gets two parameters. The first one can be string |
| 1953 | * or a number. If the string is "*l", the user requires one line. If |
| 1954 | * 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] | 1955 | * If the value is a number, the user require a number of bytes equal |
| 1956 | * to the value. The default value is "*l" (a line). |
| 1957 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1958 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | * integer takes this values: |
| 1960 | * -1 : read a line |
| 1961 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1962 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1963 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1964 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | * concatenated with the read data. |
| 1966 | */ |
| 1967 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1968 | { |
| 1969 | int wanted = HLSR_READ_LINE; |
| 1970 | const char *pattern; |
| 1971 | int type; |
| 1972 | char *error; |
| 1973 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1974 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1975 | |
| 1976 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1977 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1978 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1979 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1981 | /* Check if we run on the same thread than the xreator thread. |
| 1982 | * We cannot access to the socket if the thread is different. |
| 1983 | */ |
| 1984 | if (socket->tid != tid) |
| 1985 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1986 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1987 | /* check for pattern. */ |
| 1988 | if (lua_gettop(L) >= 2) { |
| 1989 | type = lua_type(L, 2); |
| 1990 | if (type == LUA_TSTRING) { |
| 1991 | pattern = lua_tostring(L, 2); |
| 1992 | if (strcmp(pattern, "*a") == 0) |
| 1993 | wanted = HLSR_READ_ALL; |
| 1994 | else if (strcmp(pattern, "*l") == 0) |
| 1995 | wanted = HLSR_READ_LINE; |
| 1996 | else { |
| 1997 | wanted = strtoll(pattern, &error, 10); |
| 1998 | if (*error != '\0') |
| 1999 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2000 | } |
| 2001 | } |
| 2002 | else if (type == LUA_TNUMBER) { |
| 2003 | wanted = lua_tointeger(L, 2); |
| 2004 | if (wanted < 0) |
| 2005 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | /* Set pattern. */ |
| 2010 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2011 | |
| 2012 | /* Check if we would replace the top by itself. */ |
| 2013 | if (lua_gettop(L) != 2) |
| 2014 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2016 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2017 | luaL_buffinit(L, &socket->b); |
| 2018 | |
| 2019 | /* Check prefix. */ |
| 2020 | if (lua_gettop(L) >= 3) { |
| 2021 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2022 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2023 | pattern = lua_tolstring(L, 3, &len); |
| 2024 | luaL_addlstring(&socket->b, pattern, len); |
| 2025 | } |
| 2026 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2027 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2031 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2033 | 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] | 2034 | { |
| 2035 | struct hlua_socket *socket; |
| 2036 | struct hlua *hlua = hlua_gethlua(L); |
| 2037 | struct appctx *appctx; |
| 2038 | size_t buf_len; |
| 2039 | const char *buf; |
| 2040 | int len; |
| 2041 | int send_len; |
| 2042 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2043 | struct xref *peer; |
| 2044 | struct stream_interface *si; |
| 2045 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | |
| 2047 | /* Check if this lua stack is schedulable. */ |
| 2048 | if (!hlua || !hlua->task) |
| 2049 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2050 | "'frontend', 'backend' or 'task'")); |
| 2051 | |
| 2052 | /* Get object */ |
| 2053 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2054 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2055 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2056 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2057 | /* Check if we run on the same thread than the xreator thread. |
| 2058 | * We cannot access to the socket if the thread is different. |
| 2059 | */ |
| 2060 | if (socket->tid != tid) |
| 2061 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2062 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2063 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2064 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2065 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2066 | lua_pushinteger(L, -1); |
| 2067 | return 1; |
| 2068 | } |
| 2069 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2070 | si = appctx->owner; |
| 2071 | s = si_strm(si); |
| 2072 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2074 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2075 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | lua_pushinteger(L, -1); |
| 2077 | return 1; |
| 2078 | } |
| 2079 | |
| 2080 | /* Update the input buffer data. */ |
| 2081 | buf += sent; |
| 2082 | send_len = buf_len - sent; |
| 2083 | |
| 2084 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2085 | if (sent >= buf_len) { |
| 2086 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2088 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2090 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2091 | * the request buffer if its not required. |
| 2092 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2093 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2094 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2095 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2096 | } |
| 2097 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2098 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2099 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2100 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2101 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2102 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2103 | |
| 2104 | /* send data */ |
| 2105 | if (len < send_len) |
| 2106 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2107 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | |
| 2109 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2110 | * the data" (-2) are not expected because the available length |
| 2111 | * is tested. |
| 2112 | * Other unknown error are also not expected. |
| 2113 | */ |
| 2114 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2115 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2116 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2117 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2118 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2119 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2120 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2121 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | return 1; |
| 2123 | } |
| 2124 | |
| 2125 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2126 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2127 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2128 | s->req.rex = TICK_ETERNITY; |
| 2129 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2130 | |
| 2131 | /* Update length sent. */ |
| 2132 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2133 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | |
| 2135 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2136 | if (sent + len >= buf_len) { |
| 2137 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2138 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2139 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | |
| 2141 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2142 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2143 | xref_unlock(&socket->xref, peer); |
| 2144 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2145 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2146 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2147 | 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] | 2148 | return 0; |
| 2149 | } |
| 2150 | |
| 2151 | /* This function initiate the send of data. It just check the input |
| 2152 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2153 | * 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] | 2154 | * "hlua_socket_write_yield" that can yield. |
| 2155 | * |
| 2156 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2157 | * the associated object. The second is a string buffer. The third is |
| 2158 | * a facultative integer that represents where is the buffer position |
| 2159 | * of the start of the data that can send. The first byte is the |
| 2160 | * position "1". The default value is "1". The fourth argument is a |
| 2161 | * facultative integer that represents where is the buffer position |
| 2162 | * of the end of the data that can send. The default is the last byte. |
| 2163 | */ |
| 2164 | static int hlua_socket_send(struct lua_State *L) |
| 2165 | { |
| 2166 | int i; |
| 2167 | int j; |
| 2168 | const char *buf; |
| 2169 | size_t buf_len; |
| 2170 | |
| 2171 | /* Check number of arguments. */ |
| 2172 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2173 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2174 | |
| 2175 | /* Get the string. */ |
| 2176 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2177 | |
| 2178 | /* Get and check j. */ |
| 2179 | if (lua_gettop(L) == 4) { |
| 2180 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2181 | if (j < 0) |
| 2182 | j = buf_len + j + 1; |
| 2183 | if (j > buf_len) |
| 2184 | j = buf_len + 1; |
| 2185 | lua_pop(L, 1); |
| 2186 | } |
| 2187 | else |
| 2188 | j = buf_len; |
| 2189 | |
| 2190 | /* Get and check i. */ |
| 2191 | if (lua_gettop(L) == 3) { |
| 2192 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2193 | if (i < 0) |
| 2194 | i = buf_len + i + 1; |
| 2195 | if (i > buf_len) |
| 2196 | i = buf_len + 1; |
| 2197 | lua_pop(L, 1); |
| 2198 | } else |
| 2199 | i = 1; |
| 2200 | |
| 2201 | /* Check bth i and j. */ |
| 2202 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2203 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | return 1; |
| 2205 | } |
| 2206 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2207 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2208 | return 1; |
| 2209 | } |
| 2210 | if (i == 0) |
| 2211 | i = 1; |
| 2212 | if (j == 0) |
| 2213 | j = 1; |
| 2214 | |
| 2215 | /* Pop the string. */ |
| 2216 | lua_pop(L, 1); |
| 2217 | |
| 2218 | /* Update the buffer length. */ |
| 2219 | buf += i - 1; |
| 2220 | buf_len = j - i + 1; |
| 2221 | lua_pushlstring(L, buf, buf_len); |
| 2222 | |
| 2223 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2224 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2225 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2226 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2227 | } |
| 2228 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2229 | #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] | 2230 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2231 | { |
| 2232 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2233 | int ret; |
| 2234 | int len; |
| 2235 | char *p; |
| 2236 | |
| 2237 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2238 | if (ret <= 0) { |
| 2239 | lua_pushnil(L); |
| 2240 | return 1; |
| 2241 | } |
| 2242 | |
| 2243 | if (ret == AF_UNIX) { |
| 2244 | lua_pushstring(L, buffer+1); |
| 2245 | return 1; |
| 2246 | } |
| 2247 | else if (ret == AF_INET6) { |
| 2248 | buffer[0] = '['; |
| 2249 | len = strlen(buffer); |
| 2250 | buffer[len] = ']'; |
| 2251 | len++; |
| 2252 | buffer[len] = ':'; |
| 2253 | len++; |
| 2254 | p = buffer; |
| 2255 | } |
| 2256 | else if (ret == AF_INET) { |
| 2257 | p = buffer + 1; |
| 2258 | len = strlen(p); |
| 2259 | p[len] = ':'; |
| 2260 | len++; |
| 2261 | } |
| 2262 | else { |
| 2263 | lua_pushnil(L); |
| 2264 | return 1; |
| 2265 | } |
| 2266 | |
| 2267 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2268 | lua_pushnil(L); |
| 2269 | return 1; |
| 2270 | } |
| 2271 | |
| 2272 | lua_pushstring(L, p); |
| 2273 | return 1; |
| 2274 | } |
| 2275 | |
| 2276 | /* Returns information about the peer of the connection. */ |
| 2277 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2278 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2279 | struct hlua_socket *socket; |
| 2280 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2281 | struct xref *peer; |
| 2282 | struct appctx *appctx; |
| 2283 | struct stream_interface *si; |
| 2284 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2285 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2286 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2288 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2289 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2291 | /* Check if we run on the same thread than the xreator thread. |
| 2292 | * We cannot access to the socket if the thread is different. |
| 2293 | */ |
| 2294 | if (socket->tid != tid) |
| 2295 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2296 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2297 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2299 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | lua_pushnil(L); |
| 2301 | return 1; |
| 2302 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2303 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2304 | si = appctx->owner; |
| 2305 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2307 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2309 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | lua_pushnil(L); |
| 2311 | return 1; |
| 2312 | } |
| 2313 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2314 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2316 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2317 | lua_pushnil(L); |
| 2318 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | } |
| 2320 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2321 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2322 | xref_unlock(&socket->xref, peer); |
| 2323 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | /* Returns information about my connection side. */ |
| 2327 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2328 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2329 | struct hlua_socket *socket; |
| 2330 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | struct appctx *appctx; |
| 2332 | struct xref *peer; |
| 2333 | struct stream_interface *si; |
| 2334 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2335 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2336 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2338 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2339 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2340 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2341 | /* Check if we run on the same thread than the xreator thread. |
| 2342 | * We cannot access to the socket if the thread is different. |
| 2343 | */ |
| 2344 | if (socket->tid != tid) |
| 2345 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2346 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2348 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2349 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | lua_pushnil(L); |
| 2351 | return 1; |
| 2352 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2353 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2354 | si = appctx->owner; |
| 2355 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2357 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | lua_pushnil(L); |
| 2361 | return 1; |
| 2362 | } |
| 2363 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2364 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2366 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2367 | lua_pushnil(L); |
| 2368 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2369 | } |
| 2370 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2371 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2372 | xref_unlock(&socket->xref, peer); |
| 2373 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2377 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | .obj_type = OBJ_TYPE_APPLET, |
| 2379 | .name = "<LUA_TCP>", |
| 2380 | .fct = hlua_socket_handler, |
| 2381 | .release = hlua_socket_release, |
| 2382 | }; |
| 2383 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2384 | __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] | 2385 | { |
| 2386 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2387 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2388 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2390 | struct stream_interface *si; |
| 2391 | struct stream *s; |
| 2392 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2393 | /* Check if we run on the same thread than the xreator thread. |
| 2394 | * We cannot access to the socket if the thread is different. |
| 2395 | */ |
| 2396 | if (socket->tid != tid) |
| 2397 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2398 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2399 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2400 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2401 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2402 | lua_pushnil(L); |
| 2403 | lua_pushstring(L, "Can't connect"); |
| 2404 | return 2; |
| 2405 | } |
| 2406 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2407 | si = appctx->owner; |
| 2408 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2410 | /* Check if we run on the same thread than the xreator thread. |
| 2411 | * We cannot access to the socket if the thread is different. |
| 2412 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2413 | if (socket->tid != tid) { |
| 2414 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2415 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2416 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2417 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2419 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2421 | lua_pushnil(L); |
| 2422 | lua_pushstring(L, "Can't connect"); |
| 2423 | return 2; |
| 2424 | } |
| 2425 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2426 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2427 | |
| 2428 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2429 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2430 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | lua_pushinteger(L, 1); |
| 2432 | return 1; |
| 2433 | } |
| 2434 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2435 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2436 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2437 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2438 | } |
| 2439 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2440 | 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] | 2441 | return 0; |
| 2442 | } |
| 2443 | |
| 2444 | /* This function fail or initite the connection. */ |
| 2445 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2446 | { |
| 2447 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2448 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | const char *ip; |
| 2450 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2451 | struct hlua *hlua; |
| 2452 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2453 | int low, high; |
| 2454 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2455 | struct xref *peer; |
| 2456 | struct stream_interface *si; |
| 2457 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2459 | if (lua_gettop(L) < 2) |
| 2460 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | |
| 2462 | /* Get args. */ |
| 2463 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2464 | |
| 2465 | /* Check if we run on the same thread than the xreator thread. |
| 2466 | * We cannot access to the socket if the thread is different. |
| 2467 | */ |
| 2468 | if (socket->tid != tid) |
| 2469 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2470 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2471 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2472 | if (lua_gettop(L) >= 3) { |
| 2473 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2474 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2476 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2477 | * that are not enclosed within square brackets. |
| 2478 | */ |
| 2479 | if (port > 0) { |
| 2480 | luaL_buffinit(L, &b); |
| 2481 | luaL_addstring(&b, ip); |
| 2482 | luaL_addchar(&b, ':'); |
| 2483 | luaL_pushresult(&b); |
| 2484 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2485 | } |
| 2486 | } |
| 2487 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2488 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2489 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2490 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2491 | lua_pushnil(L); |
| 2492 | return 1; |
| 2493 | } |
| 2494 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2495 | si = appctx->owner; |
| 2496 | s = si_strm(si); |
| 2497 | |
| 2498 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2499 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2500 | if (!conn) { |
| 2501 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2503 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2504 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2505 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2506 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2507 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2509 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2510 | if (!addr) { |
| 2511 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2512 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2513 | } |
| 2514 | if (low != high) { |
| 2515 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2516 | 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] | 2517 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2518 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | |
| 2520 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2521 | if (low == 0) { |
| 2522 | if (conn->addr.to.ss_family == AF_INET) { |
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_in *)&conn->addr.to)->sin_port = htons(port); |
| 2528 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2529 | if (port == -1) { |
| 2530 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2531 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2532 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2533 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2534 | } |
| 2535 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2536 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2537 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2538 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2539 | |
| 2540 | /* inform the stream that we want to be notified whenever the |
| 2541 | * connection completes. |
| 2542 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2543 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2544 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2545 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2546 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2547 | hlua->flags |= HLUA_MUST_GC; |
| 2548 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2550 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2551 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2552 | } |
| 2553 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2554 | |
| 2555 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2556 | /* Return yield waiting for connection. */ |
| 2557 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2558 | 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] | 2559 | |
| 2560 | return 0; |
| 2561 | } |
| 2562 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2563 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2564 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2565 | { |
| 2566 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2567 | struct xref *peer; |
| 2568 | struct appctx *appctx; |
| 2569 | struct stream_interface *si; |
| 2570 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2571 | |
| 2572 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2573 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2574 | |
| 2575 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2576 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2577 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2578 | lua_pushnil(L); |
| 2579 | return 1; |
| 2580 | } |
| 2581 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2582 | si = appctx->owner; |
| 2583 | s = si_strm(si); |
| 2584 | |
| 2585 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2586 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2588 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2589 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | |
| 2591 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2592 | { |
| 2593 | return 0; |
| 2594 | } |
| 2595 | |
| 2596 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2597 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2598 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2599 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2600 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2601 | struct xref *peer; |
| 2602 | struct appctx *appctx; |
| 2603 | struct stream_interface *si; |
| 2604 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2605 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2607 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2608 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2609 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2610 | /* convert the timeout to millis */ |
| 2611 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2613 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2614 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2615 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2616 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2617 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2618 | 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] | 2619 | |
| 2620 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2621 | if (tmout == 0) |
| 2622 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2623 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2624 | /* Check if we run on the same thread than the xreator thread. |
| 2625 | * We cannot access to the socket if the thread is different. |
| 2626 | */ |
| 2627 | if (socket->tid != tid) |
| 2628 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2629 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2630 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2631 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2632 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2633 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2634 | WILL_LJMP(lua_error(L)); |
| 2635 | return 0; |
| 2636 | } |
| 2637 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2638 | si = appctx->owner; |
| 2639 | s = si_strm(si); |
| 2640 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2641 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2642 | s->req.rto = tmout; |
| 2643 | s->req.wto = tmout; |
| 2644 | s->res.rto = tmout; |
| 2645 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2646 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2647 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2648 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2649 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2650 | |
| 2651 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2652 | task_queue(s->task); |
| 2653 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2654 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2655 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2656 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2657 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2661 | { |
| 2662 | struct hlua_socket *socket; |
| 2663 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2664 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2665 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | |
| 2667 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2668 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2669 | hlua_pusherror(L, "socket: full stack"); |
| 2670 | goto out_fail_conf; |
| 2671 | } |
| 2672 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2673 | /* Create the object: obj[0] = userdata. */ |
| 2674 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2676 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2678 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2679 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2680 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2681 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2682 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2683 | goto out_fail_conf; |
| 2684 | } |
| 2685 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2686 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2687 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2688 | lua_setmetatable(L, -2); |
| 2689 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2690 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2691 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2692 | if (!appctx) { |
| 2693 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2694 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2695 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2697 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2698 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2699 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2700 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2701 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2702 | /* Now create a session, task and stream for this applet */ |
| 2703 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2704 | if (!sess) { |
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_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | } |
| 2708 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2709 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2710 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2712 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | } |
| 2714 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2715 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2716 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2717 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2718 | /* Configure "right" stream interface. this "si" is used to connect |
| 2719 | * and retrieve data from the server. The connection is initialized |
| 2720 | * with the "struct server". |
| 2721 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2722 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2723 | |
| 2724 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2725 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2726 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2727 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2728 | return 1; |
| 2729 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2730 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2731 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2732 | out_fail_sess: |
| 2733 | appctx_free(appctx); |
| 2734 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2735 | WILL_LJMP(lua_error(L)); |
| 2736 | return 0; |
| 2737 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2738 | |
| 2739 | /* |
| 2740 | * |
| 2741 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2742 | * Class Channel |
| 2743 | * |
| 2744 | * |
| 2745 | */ |
| 2746 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2747 | /* This function is called before the Lua execution. It stores |
| 2748 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2749 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2750 | 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] | 2751 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2752 | c->mode = stream->be->mode; |
| 2753 | switch (c->mode) { |
| 2754 | case PR_MODE_HTTP: |
| 2755 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2756 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2757 | c->data.http.state = stream->txn->req.msg_state; |
| 2758 | else |
| 2759 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2760 | break; |
| 2761 | default: |
| 2762 | break; |
| 2763 | } |
| 2764 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2765 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2766 | /* This function is called after the Lua execution. it |
| 2767 | * returns true if the parser state is consistent, otherwise, |
| 2768 | * it return false. |
| 2769 | * |
| 2770 | * In HTTP mode, the parser state must be in the same state |
| 2771 | * or greater when we exit the function. Even if we do a |
| 2772 | * control yield. This prevent to break the HTTP message |
| 2773 | * from the Lua code. |
| 2774 | */ |
| 2775 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2776 | { |
| 2777 | if (c->mode != stream->be->mode) |
| 2778 | return 0; |
| 2779 | |
| 2780 | switch (c->mode) { |
| 2781 | case PR_MODE_HTTP: |
| 2782 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2783 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2784 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2785 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2786 | else |
| 2787 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2788 | default: |
| 2789 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2790 | } |
| 2791 | return 1; |
| 2792 | } |
| 2793 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2794 | /* Returns the struct hlua_channel join to the class channel in the |
| 2795 | * stack entry "ud" or throws an argument error. |
| 2796 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2797 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2798 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2799 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | } |
| 2801 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2802 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2803 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2804 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2805 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2806 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2807 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2808 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2809 | return 0; |
| 2810 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2811 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2812 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2813 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2814 | |
| 2815 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2816 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2817 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2818 | return 1; |
| 2819 | } |
| 2820 | |
| 2821 | /* Duplicate all the data present in the input channel and put it |
| 2822 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2823 | * the stack if the channel is closed and all the data are consumed, |
| 2824 | * returns 0 if no data are available, otherwise it returns the length |
| 2825 | * of the builded string. |
| 2826 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2827 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2828 | { |
| 2829 | char *blk1; |
| 2830 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2831 | size_t len1; |
| 2832 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2833 | int ret; |
| 2834 | luaL_Buffer b; |
| 2835 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2836 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2837 | if (unlikely(ret == 0)) |
| 2838 | return 0; |
| 2839 | |
| 2840 | if (unlikely(ret < 0)) { |
| 2841 | lua_pushnil(L); |
| 2842 | return -1; |
| 2843 | } |
| 2844 | |
| 2845 | luaL_buffinit(L, &b); |
| 2846 | luaL_addlstring(&b, blk1, len1); |
| 2847 | if (unlikely(ret == 2)) |
| 2848 | luaL_addlstring(&b, blk2, len2); |
| 2849 | luaL_pushresult(&b); |
| 2850 | |
| 2851 | if (unlikely(ret == 2)) |
| 2852 | return len1 + len2; |
| 2853 | return len1; |
| 2854 | } |
| 2855 | |
| 2856 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2857 | * a yield. This function keep the data in the buffer. |
| 2858 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2859 | __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] | 2860 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2861 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2862 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2863 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2864 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2865 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2866 | WILL_LJMP(lua_error(L)); |
| 2867 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2868 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2869 | 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] | 2870 | return 1; |
| 2871 | } |
| 2872 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2873 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2874 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2875 | { |
| 2876 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2877 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2878 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2879 | } |
| 2880 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2881 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2882 | * a yield. This function consumes the data in the buffer. It returns |
| 2883 | * a string containing the data or a nil pointer if no data are available |
| 2884 | * and the channel is closed. |
| 2885 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2886 | __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] | 2887 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2888 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2889 | int ret; |
| 2890 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2891 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2892 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2893 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2894 | WILL_LJMP(lua_error(L)); |
| 2895 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2896 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2898 | 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] | 2899 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | if (unlikely(ret == -1)) |
| 2901 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2902 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2903 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2904 | return 1; |
| 2905 | } |
| 2906 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2907 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2908 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2909 | { |
| 2910 | MAY_LJMP(check_args(L, 1, "get")); |
| 2911 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2912 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2913 | } |
| 2914 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | /* This functions consumes and returns one line. If the channel is closed, |
| 2916 | * 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] | 2917 | * 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] | 2918 | * value. |
| 2919 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2920 | __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] | 2921 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | char *blk1; |
| 2923 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2924 | size_t len1; |
| 2925 | size_t len2; |
| 2926 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2927 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | int ret; |
| 2929 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2930 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2931 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2932 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2933 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2934 | WILL_LJMP(lua_error(L)); |
| 2935 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2936 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2938 | 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] | 2939 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2940 | if (ret == -1) { |
| 2941 | lua_pushnil(L); |
| 2942 | return 1; |
| 2943 | } |
| 2944 | |
| 2945 | luaL_buffinit(L, &b); |
| 2946 | luaL_addlstring(&b, blk1, len1); |
| 2947 | len = len1; |
| 2948 | if (unlikely(ret == 2)) { |
| 2949 | luaL_addlstring(&b, blk2, len2); |
| 2950 | len += len2; |
| 2951 | } |
| 2952 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2953 | 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] | 2954 | return 1; |
| 2955 | } |
| 2956 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2957 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2958 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2959 | { |
| 2960 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2961 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2962 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2963 | } |
| 2964 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2965 | /* This function takes a string as input, and append it at the |
| 2966 | * input side of channel. If the data is too big, but a space |
| 2967 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2968 | * yields. If the data is bigger than the buffer, or if the |
| 2969 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2970 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2971 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2972 | __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] | 2973 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2974 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | size_t len; |
| 2976 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2977 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2978 | int ret; |
| 2979 | int max; |
| 2980 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2981 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2982 | WILL_LJMP(lua_error(L)); |
| 2983 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2984 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2985 | * the request buffer if its not required. |
| 2986 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2987 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2988 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2989 | 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] | 2990 | } |
| 2991 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2992 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | if (max > len - l) |
| 2994 | max = len - l; |
| 2995 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2996 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2997 | if (ret == -2 || ret == -3) { |
| 2998 | lua_pushinteger(L, -1); |
| 2999 | return 1; |
| 3000 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3001 | if (ret == -1) { |
| 3002 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3003 | 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] | 3004 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3005 | l += ret; |
| 3006 | lua_pop(L, 1); |
| 3007 | lua_pushinteger(L, l); |
| 3008 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3009 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3010 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3011 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | * in this case, we cannot add more data, so we cannot yield, |
| 3013 | * we return the amount of copyied data. |
| 3014 | */ |
| 3015 | return 1; |
| 3016 | } |
| 3017 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3018 | 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] | 3019 | return 1; |
| 3020 | } |
| 3021 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3022 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3023 | * 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] | 3024 | * buffer size is too little for the data. |
| 3025 | */ |
| 3026 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3027 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3028 | size_t len; |
| 3029 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3031 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3032 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3033 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | lua_pushinteger(L, 0); |
| 3035 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3036 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | } |
| 3038 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3039 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3040 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3041 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | * or -1 if the channel is closed or if the buffer size is too |
| 3043 | * little for the data. |
| 3044 | */ |
| 3045 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3046 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3047 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3048 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3049 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3050 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3051 | lua_pushinteger(L, 0); |
| 3052 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3053 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3054 | WILL_LJMP(lua_error(L)); |
| 3055 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3056 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3058 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3059 | } |
| 3060 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3061 | /* Append data in the output side of the buffer. This data is immediately |
| 3062 | * sent. The function returns the amount of data written. If the buffer |
| 3063 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3064 | * if the channel is closed. |
| 3065 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3066 | __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] | 3067 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3068 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | size_t len; |
| 3070 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3071 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3072 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3073 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3075 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3076 | WILL_LJMP(lua_error(L)); |
| 3077 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3078 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3079 | lua_pushinteger(L, -1); |
| 3080 | return 1; |
| 3081 | } |
| 3082 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3083 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3084 | * the request buffer if its not required. |
| 3085 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3086 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3087 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3088 | 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] | 3089 | } |
| 3090 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3091 | /* The written data will be immediately sent, so we can check |
| 3092 | * the available space without taking in account the reserve. |
| 3093 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3094 | * data, because the buffer will be flushed. |
| 3095 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3096 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3097 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3098 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3099 | * in this case, we cannot add more data, so we cannot yield, |
| 3100 | * we return the amount of copyied data. |
| 3101 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3102 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3103 | return 1; |
| 3104 | |
| 3105 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3106 | if (max > len - l) |
| 3107 | max = len - l; |
| 3108 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3109 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3110 | * detects a non contiguous buffer and realign it. |
| 3111 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3112 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3113 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3114 | |
| 3115 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3116 | 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] | 3117 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3118 | /* buffer replace considers that the input part is filled. |
| 3119 | * so, I must forward these new data in the output part. |
| 3120 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3121 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3122 | |
| 3123 | l += max; |
| 3124 | lua_pop(L, 1); |
| 3125 | lua_pushinteger(L, l); |
| 3126 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3127 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3128 | * in this case, we cannot add more data, so we cannot yield, |
| 3129 | * we return the amount of copyied data. |
| 3130 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3131 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3132 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3133 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3134 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3135 | if (l < len) { |
| 3136 | /* If we are waiting for space in the response buffer, we |
| 3137 | * must set the flag WAKERESWR. This flag required the task |
| 3138 | * wake up if any activity is detected on the response buffer. |
| 3139 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3140 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3141 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3142 | else |
| 3143 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3144 | 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] | 3145 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3146 | |
| 3147 | return 1; |
| 3148 | } |
| 3149 | |
| 3150 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3151 | * yield the LUA process, and resume it without checking the |
| 3152 | * input arguments. |
| 3153 | */ |
| 3154 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3155 | { |
| 3156 | MAY_LJMP(check_args(L, 2, "send")); |
| 3157 | lua_pushinteger(L, 0); |
| 3158 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3159 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | /* This function forward and amount of butes. The data pass from |
| 3163 | * the input side of the buffer to the output side, and can be |
| 3164 | * forwarded. This function never fails. |
| 3165 | * |
| 3166 | * The Lua function takes an amount of bytes to be forwarded in |
| 3167 | * imput. It returns the number of bytes forwarded. |
| 3168 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3169 | __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] | 3170 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3171 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3172 | int len; |
| 3173 | int l; |
| 3174 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3175 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3176 | |
| 3177 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3178 | |
| 3179 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3180 | WILL_LJMP(lua_error(L)); |
| 3181 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3182 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3183 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3184 | |
| 3185 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3186 | if (max > ci_data(chn)) |
| 3187 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3188 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | l += max; |
| 3190 | |
| 3191 | lua_pop(L, 1); |
| 3192 | lua_pushinteger(L, l); |
| 3193 | |
| 3194 | /* Check if it miss bytes to forward. */ |
| 3195 | if (l < len) { |
| 3196 | /* The the input channel or the output channel are closed, we |
| 3197 | * must return the amount of data forwarded. |
| 3198 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3199 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3200 | return 1; |
| 3201 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3202 | /* If we are waiting for space data in the response buffer, we |
| 3203 | * must set the flag WAKERESWR. This flag required the task |
| 3204 | * wake up if any activity is detected on the response buffer. |
| 3205 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3206 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3207 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3208 | else |
| 3209 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3210 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3211 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3212 | 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] | 3213 | } |
| 3214 | |
| 3215 | return 1; |
| 3216 | } |
| 3217 | |
| 3218 | /* Just check the input and prepare the stack for the previous |
| 3219 | * function "hlua_channel_forward_yield" |
| 3220 | */ |
| 3221 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3222 | { |
| 3223 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3224 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3225 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3226 | |
| 3227 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3228 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | /* Just returns the number of bytes available in the input |
| 3232 | * side of the buffer. This function never fails. |
| 3233 | */ |
| 3234 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3235 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3236 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3237 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3238 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3239 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3240 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3241 | struct htx *htx = htxbuf(&chn->buf); |
| 3242 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3243 | } |
| 3244 | else |
| 3245 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3246 | return 1; |
| 3247 | } |
| 3248 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3249 | /* Returns true if the channel is full. */ |
| 3250 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3251 | { |
| 3252 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3253 | |
| 3254 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3255 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0136ebb | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3256 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3257 | * applet). |
| 3258 | */ |
| 3259 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3260 | return 1; |
| 3261 | } |
| 3262 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3263 | /* Just returns the number of bytes available in the output |
| 3264 | * side of the buffer. This function never fails. |
| 3265 | */ |
| 3266 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3267 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3268 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3269 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3270 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3271 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3272 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3273 | return 1; |
| 3274 | } |
| 3275 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3276 | /* |
| 3277 | * |
| 3278 | * |
| 3279 | * Class Fetches |
| 3280 | * |
| 3281 | * |
| 3282 | */ |
| 3283 | |
| 3284 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3285 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3286 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3287 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3288 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3289 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | /* This function creates and push in the stack a fetch object according |
| 3293 | * with a current TXN. |
| 3294 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3295 | 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] | 3296 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3297 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3298 | |
| 3299 | /* Check stack size. */ |
| 3300 | if (!lua_checkstack(L, 3)) |
| 3301 | return 0; |
| 3302 | |
| 3303 | /* Create the object: obj[0] = userdata. |
| 3304 | * Note that the base of the Fetches object is the |
| 3305 | * transaction object. |
| 3306 | */ |
| 3307 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3308 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3309 | lua_rawseti(L, -2, 0); |
| 3310 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3311 | hsmp->s = txn->s; |
| 3312 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3313 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3314 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3315 | |
| 3316 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3317 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3318 | lua_setmetatable(L, -2); |
| 3319 | |
| 3320 | return 1; |
| 3321 | } |
| 3322 | |
| 3323 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3324 | * It uses closure argument to store the associated sample-fetch. It |
| 3325 | * returns only one argument or throws an error. An error is thrown |
| 3326 | * only if an error is encountered during the argument parsing. If |
| 3327 | * the "sample-fetch" function fails, nil is returned. |
| 3328 | */ |
| 3329 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3330 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3331 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3332 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3333 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3334 | int i; |
| 3335 | struct sample smp; |
| 3336 | |
| 3337 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3338 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3339 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3340 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3341 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3342 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3343 | /* Check execution authorization. */ |
| 3344 | if (f->use & SMP_USE_HTTP_ANY && |
| 3345 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3346 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3347 | "is not available in Lua services", f->kw); |
| 3348 | WILL_LJMP(lua_error(L)); |
| 3349 | } |
| 3350 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3351 | /* Get extra arguments. */ |
| 3352 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3353 | if (i >= ARGM_NBARGS) |
| 3354 | break; |
| 3355 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3356 | } |
| 3357 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3358 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3359 | |
| 3360 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3361 | 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] | 3362 | |
| 3363 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3364 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3365 | lua_pushfstring(L, "error in arguments"); |
| 3366 | WILL_LJMP(lua_error(L)); |
| 3367 | } |
| 3368 | |
| 3369 | /* Initialise the sample. */ |
| 3370 | memset(&smp, 0, sizeof(smp)); |
| 3371 | |
| 3372 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3373 | 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] | 3374 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3375 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3376 | lua_pushstring(L, ""); |
| 3377 | else |
| 3378 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3379 | return 1; |
| 3380 | } |
| 3381 | |
| 3382 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3383 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3384 | hlua_smp2lua_str(L, &smp); |
| 3385 | else |
| 3386 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3387 | return 1; |
| 3388 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3389 | |
| 3390 | /* |
| 3391 | * |
| 3392 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3393 | * Class Converters |
| 3394 | * |
| 3395 | * |
| 3396 | */ |
| 3397 | |
| 3398 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3399 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3400 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3401 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3402 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3403 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | /* This function creates and push in the stack a Converters object |
| 3407 | * according with a current TXN. |
| 3408 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3409 | 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] | 3410 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3411 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3412 | |
| 3413 | /* Check stack size. */ |
| 3414 | if (!lua_checkstack(L, 3)) |
| 3415 | return 0; |
| 3416 | |
| 3417 | /* Create the object: obj[0] = userdata. |
| 3418 | * Note that the base of the Converters object is the |
| 3419 | * same than the TXN object. |
| 3420 | */ |
| 3421 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3422 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3423 | lua_rawseti(L, -2, 0); |
| 3424 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3425 | hsmp->s = txn->s; |
| 3426 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3427 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3428 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3429 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3430 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3431 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3432 | lua_setmetatable(L, -2); |
| 3433 | |
| 3434 | return 1; |
| 3435 | } |
| 3436 | |
| 3437 | /* This function is an LUA binding. It is called with each converter. |
| 3438 | * It uses closure argument to store the associated converter. It |
| 3439 | * returns only one argument or throws an error. An error is thrown |
| 3440 | * only if an error is encountered during the argument parsing. If |
| 3441 | * the converter function function fails, nil is returned. |
| 3442 | */ |
| 3443 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3444 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3445 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3446 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3447 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3448 | int i; |
| 3449 | struct sample smp; |
| 3450 | |
| 3451 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3452 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3453 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3454 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3455 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3456 | |
| 3457 | /* Get extra arguments. */ |
| 3458 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3459 | if (i >= ARGM_NBARGS) |
| 3460 | break; |
| 3461 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3462 | } |
| 3463 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3464 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3465 | |
| 3466 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3467 | 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] | 3468 | |
| 3469 | /* Run the special args checker. */ |
| 3470 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3471 | hlua_pusherror(L, "error in arguments"); |
| 3472 | WILL_LJMP(lua_error(L)); |
| 3473 | } |
| 3474 | |
| 3475 | /* Initialise the sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3476 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3477 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3478 | hlua_pusherror(L, "error in the input argument"); |
| 3479 | WILL_LJMP(lua_error(L)); |
| 3480 | } |
| 3481 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3482 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3483 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3484 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3485 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3486 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3487 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3488 | WILL_LJMP(lua_error(L)); |
| 3489 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3490 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3491 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3492 | hlua_pusherror(L, "error during the input argument casting"); |
| 3493 | WILL_LJMP(lua_error(L)); |
| 3494 | } |
| 3495 | |
| 3496 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3497 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3498 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3499 | lua_pushstring(L, ""); |
| 3500 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3501 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3502 | return 1; |
| 3503 | } |
| 3504 | |
| 3505 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3506 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3507 | hlua_smp2lua_str(L, &smp); |
| 3508 | else |
| 3509 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3510 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3511 | } |
| 3512 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3513 | /* |
| 3514 | * |
| 3515 | * |
| 3516 | * Class AppletTCP |
| 3517 | * |
| 3518 | * |
| 3519 | */ |
| 3520 | |
| 3521 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3522 | * a class stream, otherwise it throws an error. |
| 3523 | */ |
| 3524 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3525 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3526 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | /* This function creates and push in the stack an Applet object |
| 3530 | * according with a current TXN. |
| 3531 | */ |
| 3532 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3533 | { |
| 3534 | struct hlua_appctx *appctx; |
| 3535 | struct stream_interface *si = ctx->owner; |
| 3536 | struct stream *s = si_strm(si); |
| 3537 | struct proxy *p = s->be; |
| 3538 | |
| 3539 | /* Check stack size. */ |
| 3540 | if (!lua_checkstack(L, 3)) |
| 3541 | return 0; |
| 3542 | |
| 3543 | /* Create the object: obj[0] = userdata. |
| 3544 | * Note that the base of the Converters object is the |
| 3545 | * same than the TXN object. |
| 3546 | */ |
| 3547 | lua_newtable(L); |
| 3548 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3549 | lua_rawseti(L, -2, 0); |
| 3550 | appctx->appctx = ctx; |
| 3551 | appctx->htxn.s = s; |
| 3552 | appctx->htxn.p = p; |
| 3553 | |
| 3554 | /* Create the "f" field that contains a list of fetches. */ |
| 3555 | lua_pushstring(L, "f"); |
| 3556 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3557 | return 0; |
| 3558 | lua_settable(L, -3); |
| 3559 | |
| 3560 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3561 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3562 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3563 | return 0; |
| 3564 | lua_settable(L, -3); |
| 3565 | |
| 3566 | /* Create the "c" field that contains a list of converters. */ |
| 3567 | lua_pushstring(L, "c"); |
| 3568 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3569 | return 0; |
| 3570 | lua_settable(L, -3); |
| 3571 | |
| 3572 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3573 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3574 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3575 | return 0; |
| 3576 | lua_settable(L, -3); |
| 3577 | |
| 3578 | /* Pop a class stream metatable and affect it to the table. */ |
| 3579 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3580 | lua_setmetatable(L, -2); |
| 3581 | |
| 3582 | return 1; |
| 3583 | } |
| 3584 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3585 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3586 | { |
| 3587 | struct hlua_appctx *appctx; |
| 3588 | struct stream *s; |
| 3589 | const char *name; |
| 3590 | size_t len; |
| 3591 | struct sample smp; |
| 3592 | |
| 3593 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3594 | |
| 3595 | /* It is useles to retrieve the stream, but this function |
| 3596 | * runs only in a stream context. |
| 3597 | */ |
| 3598 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3599 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3600 | s = appctx->htxn.s; |
| 3601 | |
| 3602 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3603 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3604 | hlua_lua2smp(L, 3, &smp); |
| 3605 | |
| 3606 | /* Store the sample in a variable. */ |
| 3607 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3608 | vars_set_by_name(name, len, &smp); |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
| 3612 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3613 | { |
| 3614 | struct hlua_appctx *appctx; |
| 3615 | struct stream *s; |
| 3616 | const char *name; |
| 3617 | size_t len; |
| 3618 | struct sample smp; |
| 3619 | |
| 3620 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3621 | |
| 3622 | /* It is useles to retrieve the stream, but this function |
| 3623 | * runs only in a stream context. |
| 3624 | */ |
| 3625 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3626 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3627 | s = appctx->htxn.s; |
| 3628 | |
| 3629 | /* Unset the variable. */ |
| 3630 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3631 | vars_unset_by_name(name, len, &smp); |
| 3632 | return 0; |
| 3633 | } |
| 3634 | |
| 3635 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3636 | { |
| 3637 | struct hlua_appctx *appctx; |
| 3638 | struct stream *s; |
| 3639 | const char *name; |
| 3640 | size_t len; |
| 3641 | struct sample smp; |
| 3642 | |
| 3643 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3644 | |
| 3645 | /* It is useles to retrieve the stream, but this function |
| 3646 | * runs only in a stream context. |
| 3647 | */ |
| 3648 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3649 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3650 | s = appctx->htxn.s; |
| 3651 | |
| 3652 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3653 | if (!vars_get_by_name(name, len, &smp)) { |
| 3654 | lua_pushnil(L); |
| 3655 | return 1; |
| 3656 | } |
| 3657 | |
| 3658 | return hlua_smp2lua(L, &smp); |
| 3659 | } |
| 3660 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3661 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3662 | { |
| 3663 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3664 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3665 | struct hlua *hlua; |
| 3666 | |
| 3667 | /* 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] | 3668 | if (!s->hlua) |
| 3669 | return 0; |
| 3670 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3671 | |
| 3672 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3673 | |
| 3674 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3675 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3676 | |
| 3677 | /* Get and store new value. */ |
| 3678 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3679 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3680 | |
| 3681 | return 0; |
| 3682 | } |
| 3683 | |
| 3684 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3685 | { |
| 3686 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3687 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3688 | struct hlua *hlua; |
| 3689 | |
| 3690 | /* 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] | 3691 | if (!s->hlua) { |
| 3692 | lua_pushnil(L); |
| 3693 | return 1; |
| 3694 | } |
| 3695 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3696 | |
| 3697 | /* Push configuration index in the stack. */ |
| 3698 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3699 | |
| 3700 | return 1; |
| 3701 | } |
| 3702 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3703 | /* If expected data not yet available, it returns a yield. This function |
| 3704 | * consumes the data in the buffer. It returns a string containing the |
| 3705 | * data. This string can be empty. |
| 3706 | */ |
| 3707 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3708 | { |
| 3709 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3710 | struct stream_interface *si = appctx->appctx->owner; |
| 3711 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3712 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3713 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3714 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3715 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3716 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3717 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3718 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3719 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3720 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3721 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3722 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3723 | 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] | 3724 | } |
| 3725 | |
| 3726 | /* End of data: commit the total strings and return. */ |
| 3727 | if (ret < 0) { |
| 3728 | luaL_pushresult(&appctx->b); |
| 3729 | return 1; |
| 3730 | } |
| 3731 | |
| 3732 | /* Ensure that the block 2 length is usable. */ |
| 3733 | if (ret == 1) |
| 3734 | len2 = 0; |
| 3735 | |
| 3736 | /* dont check the max length read and dont check. */ |
| 3737 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3738 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3739 | |
| 3740 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3741 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3742 | luaL_pushresult(&appctx->b); |
| 3743 | return 1; |
| 3744 | } |
| 3745 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3746 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3747 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3748 | { |
| 3749 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3750 | |
| 3751 | /* Initialise the string catenation. */ |
| 3752 | luaL_buffinit(L, &appctx->b); |
| 3753 | |
| 3754 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3755 | } |
| 3756 | |
| 3757 | /* If expected data not yet available, it returns a yield. This function |
| 3758 | * consumes the data in the buffer. It returns a string containing the |
| 3759 | * data. This string can be empty. |
| 3760 | */ |
| 3761 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3762 | { |
| 3763 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3764 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3765 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3766 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3767 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3768 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3769 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3770 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3771 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3772 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3773 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3774 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3775 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3776 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3777 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3778 | 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] | 3779 | } |
| 3780 | |
| 3781 | /* End of data: commit the total strings and return. */ |
| 3782 | if (ret < 0) { |
| 3783 | luaL_pushresult(&appctx->b); |
| 3784 | return 1; |
| 3785 | } |
| 3786 | |
| 3787 | /* Ensure that the block 2 length is usable. */ |
| 3788 | if (ret == 1) |
| 3789 | len2 = 0; |
| 3790 | |
| 3791 | if (len == -1) { |
| 3792 | |
| 3793 | /* If len == -1, catenate all the data avalaile and |
| 3794 | * yield because we want to get all the data until |
| 3795 | * the end of data stream. |
| 3796 | */ |
| 3797 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3798 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3799 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3800 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3801 | 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] | 3802 | |
| 3803 | } else { |
| 3804 | |
| 3805 | /* Copy the fisrt block caping to the length required. */ |
| 3806 | if (len1 > len) |
| 3807 | len1 = len; |
| 3808 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3809 | len -= len1; |
| 3810 | |
| 3811 | /* Copy the second block. */ |
| 3812 | if (len2 > len) |
| 3813 | len2 = len; |
| 3814 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3815 | len -= len2; |
| 3816 | |
| 3817 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3818 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3819 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3820 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3821 | if (len > 0) { |
| 3822 | lua_pushinteger(L, len); |
| 3823 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3824 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3825 | 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] | 3826 | } |
| 3827 | |
| 3828 | /* return the result. */ |
| 3829 | luaL_pushresult(&appctx->b); |
| 3830 | return 1; |
| 3831 | } |
| 3832 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3833 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3834 | hlua_pusherror(L, "Lua: internal error"); |
| 3835 | WILL_LJMP(lua_error(L)); |
| 3836 | return 0; |
| 3837 | } |
| 3838 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3839 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3840 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3841 | { |
| 3842 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3843 | int len = -1; |
| 3844 | |
| 3845 | if (lua_gettop(L) > 2) |
| 3846 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3847 | if (lua_gettop(L) >= 2) { |
| 3848 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3849 | lua_pop(L, 1); |
| 3850 | } |
| 3851 | |
| 3852 | /* Confirm or set the required length */ |
| 3853 | lua_pushinteger(L, len); |
| 3854 | |
| 3855 | /* Initialise the string catenation. */ |
| 3856 | luaL_buffinit(L, &appctx->b); |
| 3857 | |
| 3858 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3859 | } |
| 3860 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3861 | /* Append data in the output side of the buffer. This data is immediately |
| 3862 | * sent. The function returns the amount of data written. If the buffer |
| 3863 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3864 | * if the channel is closed. |
| 3865 | */ |
| 3866 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3867 | { |
| 3868 | size_t len; |
| 3869 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3870 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3871 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3872 | struct stream_interface *si = appctx->appctx->owner; |
| 3873 | struct channel *chn = si_ic(si); |
| 3874 | int max; |
| 3875 | |
| 3876 | /* Get the max amount of data which can write as input in the channel. */ |
| 3877 | max = channel_recv_max(chn); |
| 3878 | if (max > (len - l)) |
| 3879 | max = len - l; |
| 3880 | |
| 3881 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3882 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3883 | |
| 3884 | /* update counters. */ |
| 3885 | l += max; |
| 3886 | lua_pop(L, 1); |
| 3887 | lua_pushinteger(L, l); |
| 3888 | |
| 3889 | /* If some data is not send, declares the situation to the |
| 3890 | * applet, and returns a yield. |
| 3891 | */ |
| 3892 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3893 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3894 | 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] | 3895 | } |
| 3896 | |
| 3897 | return 1; |
| 3898 | } |
| 3899 | |
| 3900 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3901 | * yield the LUA process, and resume it without checking the |
| 3902 | * input arguments. |
| 3903 | */ |
| 3904 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3905 | { |
| 3906 | MAY_LJMP(check_args(L, 2, "send")); |
| 3907 | lua_pushinteger(L, 0); |
| 3908 | |
| 3909 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3910 | } |
| 3911 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3912 | /* |
| 3913 | * |
| 3914 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3915 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3916 | * |
| 3917 | * |
| 3918 | */ |
| 3919 | |
| 3920 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3921 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3922 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3923 | __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] | 3924 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3925 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3926 | } |
| 3927 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3928 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3929 | * according with a current TXN. |
| 3930 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3931 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3932 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3933 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3934 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3935 | struct stream_interface *si = ctx->owner; |
| 3936 | struct stream *s = si_strm(si); |
| 3937 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3938 | |
| 3939 | /* Check stack size. */ |
| 3940 | if (!lua_checkstack(L, 3)) |
| 3941 | return 0; |
| 3942 | |
| 3943 | /* Create the object: obj[0] = userdata. |
| 3944 | * Note that the base of the Converters object is the |
| 3945 | * same than the TXN object. |
| 3946 | */ |
| 3947 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3948 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3949 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3950 | appctx->appctx = ctx; |
| 3951 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3952 | 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] | 3953 | appctx->htxn.s = s; |
| 3954 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3955 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3956 | /* Create the "f" field that contains a list of fetches. */ |
| 3957 | lua_pushstring(L, "f"); |
| 3958 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3959 | return 0; |
| 3960 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3961 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3962 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3963 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3964 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3965 | return 0; |
| 3966 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3967 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3968 | /* Create the "c" field that contains a list of converters. */ |
| 3969 | lua_pushstring(L, "c"); |
| 3970 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3971 | return 0; |
| 3972 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3973 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3974 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3975 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3976 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3977 | return 0; |
| 3978 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3979 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3980 | if (IS_HTX_STRM(s)) { |
| 3981 | /* HTX version */ |
| 3982 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3983 | struct htx_blk *blk; |
| 3984 | struct htx_sl *sl; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3985 | struct ist path; |
| 3986 | unsigned long long len = 0; |
| 3987 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3988 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3989 | blk = htx_get_first_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3990 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3991 | sl = htx_get_blk_ptr(htx, blk); |
| 3992 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3993 | /* Stores the request method. */ |
| 3994 | lua_pushstring(L, "method"); |
| 3995 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3996 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3997 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3998 | /* Stores the http version. */ |
| 3999 | lua_pushstring(L, "version"); |
| 4000 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4001 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4002 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4003 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4004 | * the array on the top of the stack. |
| 4005 | */ |
| 4006 | lua_pushstring(L, "headers"); |
| 4007 | htxn.s = s; |
| 4008 | htxn.p = px; |
| 4009 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4010 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4011 | return 0; |
| 4012 | lua_settable(L, -3); |
| 4013 | |
| 4014 | path = http_get_path(htx_sl_req_uri(sl)); |
| 4015 | if (path.ptr) { |
| 4016 | char *p, *q, *end; |
| 4017 | |
| 4018 | p = path.ptr; |
| 4019 | end = path.ptr + path.len; |
| 4020 | q = p; |
| 4021 | while (q < end && *q != '?') |
| 4022 | q++; |
| 4023 | |
| 4024 | /* Stores the request path. */ |
| 4025 | lua_pushstring(L, "path"); |
| 4026 | lua_pushlstring(L, p, q - p); |
| 4027 | lua_settable(L, -3); |
| 4028 | |
| 4029 | /* Stores the query string. */ |
| 4030 | lua_pushstring(L, "qs"); |
| 4031 | if (*q == '?') |
| 4032 | q++; |
| 4033 | lua_pushlstring(L, q, end - q); |
| 4034 | lua_settable(L, -3); |
| 4035 | } |
| 4036 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4037 | 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] | 4038 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4039 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4040 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4041 | 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] | 4042 | break; |
| 4043 | if (type == HTX_BLK_DATA) |
| 4044 | len += htx_get_blksz(blk); |
| 4045 | } |
| 4046 | if (htx->extra != ULLONG_MAX) |
| 4047 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4048 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4049 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4050 | lua_pushstring(L, "length"); |
| 4051 | lua_pushinteger(L, len); |
| 4052 | lua_settable(L, -3); |
| 4053 | } |
| 4054 | else { |
| 4055 | /* Legacy HTTP version */ |
| 4056 | struct http_txn *txn = s->txn; |
| 4057 | const char *path; |
| 4058 | const char *end; |
| 4059 | const char *p; |
| 4060 | |
| 4061 | /* Stores the request method. */ |
| 4062 | lua_pushstring(L, "method"); |
| 4063 | 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] | 4064 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4065 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4066 | /* Stores the http version. */ |
| 4067 | lua_pushstring(L, "version"); |
| 4068 | 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] | 4069 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4070 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4071 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4072 | * the array on the top of the stack. |
| 4073 | */ |
| 4074 | lua_pushstring(L, "headers"); |
| 4075 | htxn.s = s; |
| 4076 | htxn.p = px; |
| 4077 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4078 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4079 | return 0; |
| 4080 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4081 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4082 | /* Get path and qs */ |
| 4083 | path = http_txn_get_path(txn); |
| 4084 | if (path) { |
| 4085 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4086 | p = path; |
| 4087 | while (p < end && *p != '?') |
| 4088 | p++; |
| 4089 | |
| 4090 | /* Stores the request path. */ |
| 4091 | lua_pushstring(L, "path"); |
| 4092 | lua_pushlstring(L, path, p - path); |
| 4093 | lua_settable(L, -3); |
| 4094 | |
| 4095 | /* Stores the query string. */ |
| 4096 | lua_pushstring(L, "qs"); |
| 4097 | if (*p == '?') |
| 4098 | p++; |
| 4099 | lua_pushlstring(L, p, end - p); |
| 4100 | lua_settable(L, -3); |
| 4101 | } |
| 4102 | |
| 4103 | /* Stores the request path. */ |
| 4104 | lua_pushstring(L, "length"); |
| 4105 | lua_pushinteger(L, txn->req.body_len); |
| 4106 | lua_settable(L, -3); |
| 4107 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4108 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4109 | /* Create an empty array of HTTP request headers. */ |
| 4110 | lua_pushstring(L, "response"); |
| 4111 | lua_newtable(L); |
| 4112 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4113 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4114 | /* Pop a class stream metatable and affect it to the table. */ |
| 4115 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4116 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4117 | |
| 4118 | return 1; |
| 4119 | } |
| 4120 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4121 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4122 | { |
| 4123 | struct hlua_appctx *appctx; |
| 4124 | struct stream *s; |
| 4125 | const char *name; |
| 4126 | size_t len; |
| 4127 | struct sample smp; |
| 4128 | |
| 4129 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4130 | |
| 4131 | /* It is useles to retrieve the stream, but this function |
| 4132 | * runs only in a stream context. |
| 4133 | */ |
| 4134 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4135 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4136 | s = appctx->htxn.s; |
| 4137 | |
| 4138 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4139 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4140 | hlua_lua2smp(L, 3, &smp); |
| 4141 | |
| 4142 | /* Store the sample in a variable. */ |
| 4143 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4144 | vars_set_by_name(name, len, &smp); |
| 4145 | return 0; |
| 4146 | } |
| 4147 | |
| 4148 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4149 | { |
| 4150 | struct hlua_appctx *appctx; |
| 4151 | struct stream *s; |
| 4152 | const char *name; |
| 4153 | size_t len; |
| 4154 | struct sample smp; |
| 4155 | |
| 4156 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4157 | |
| 4158 | /* It is useles to retrieve the stream, but this function |
| 4159 | * runs only in a stream context. |
| 4160 | */ |
| 4161 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4162 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4163 | s = appctx->htxn.s; |
| 4164 | |
| 4165 | /* Unset the variable. */ |
| 4166 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4167 | vars_unset_by_name(name, len, &smp); |
| 4168 | return 0; |
| 4169 | } |
| 4170 | |
| 4171 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4172 | { |
| 4173 | struct hlua_appctx *appctx; |
| 4174 | struct stream *s; |
| 4175 | const char *name; |
| 4176 | size_t len; |
| 4177 | struct sample smp; |
| 4178 | |
| 4179 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4180 | |
| 4181 | /* It is useles to retrieve the stream, but this function |
| 4182 | * runs only in a stream context. |
| 4183 | */ |
| 4184 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4185 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4186 | s = appctx->htxn.s; |
| 4187 | |
| 4188 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4189 | if (!vars_get_by_name(name, len, &smp)) { |
| 4190 | lua_pushnil(L); |
| 4191 | return 1; |
| 4192 | } |
| 4193 | |
| 4194 | return hlua_smp2lua(L, &smp); |
| 4195 | } |
| 4196 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4197 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4198 | { |
| 4199 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4200 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4201 | struct hlua *hlua; |
| 4202 | |
| 4203 | /* 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] | 4204 | if (!s->hlua) |
| 4205 | return 0; |
| 4206 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4207 | |
| 4208 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4209 | |
| 4210 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4211 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4212 | |
| 4213 | /* Get and store new value. */ |
| 4214 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4215 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4216 | |
| 4217 | return 0; |
| 4218 | } |
| 4219 | |
| 4220 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4221 | { |
| 4222 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4223 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4224 | struct hlua *hlua; |
| 4225 | |
| 4226 | /* 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] | 4227 | if (!s->hlua) { |
| 4228 | lua_pushnil(L); |
| 4229 | return 1; |
| 4230 | } |
| 4231 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4232 | |
| 4233 | /* Push configuration index in the stack. */ |
| 4234 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4235 | |
| 4236 | return 1; |
| 4237 | } |
| 4238 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4239 | /* If expected data not yet available, it returns a yield. This function |
| 4240 | * consumes the data in the buffer. It returns a string containing the |
| 4241 | * data. This string can be empty. |
| 4242 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4243 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4244 | { |
| 4245 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4246 | struct stream_interface *si = appctx->appctx->owner; |
| 4247 | struct channel *req = si_oc(si); |
| 4248 | struct htx *htx; |
| 4249 | struct htx_blk *blk; |
| 4250 | size_t count; |
| 4251 | int stop = 0; |
| 4252 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4253 | htx = htx_from_buf(&req->buf); |
| 4254 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4255 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4256 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4257 | while (count && !stop && blk) { |
| 4258 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4259 | uint32_t sz = htx_get_blksz(blk); |
| 4260 | struct ist v; |
| 4261 | uint32_t vlen; |
| 4262 | char *nl; |
| 4263 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4264 | if (type == HTX_BLK_EOM) { |
| 4265 | stop = 1; |
| 4266 | break; |
| 4267 | } |
| 4268 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4269 | vlen = sz; |
| 4270 | if (vlen > count) { |
| 4271 | if (type != HTX_BLK_DATA) |
| 4272 | break; |
| 4273 | vlen = count; |
| 4274 | } |
| 4275 | |
| 4276 | switch (type) { |
| 4277 | case HTX_BLK_UNUSED: |
| 4278 | break; |
| 4279 | |
| 4280 | case HTX_BLK_DATA: |
| 4281 | v = htx_get_blk_value(htx, blk); |
| 4282 | v.len = vlen; |
| 4283 | nl = istchr(v, '\n'); |
| 4284 | if (nl != NULL) { |
| 4285 | stop = 1; |
| 4286 | vlen = nl - v.ptr + 1; |
| 4287 | } |
| 4288 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4289 | break; |
| 4290 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4291 | case HTX_BLK_TLR: |
| 4292 | case HTX_BLK_EOM: |
| 4293 | stop = 1; |
| 4294 | break; |
| 4295 | |
| 4296 | default: |
| 4297 | break; |
| 4298 | } |
| 4299 | |
| 4300 | co_set_data(req, co_data(req) - vlen); |
| 4301 | count -= vlen; |
| 4302 | if (sz == vlen) |
| 4303 | blk = htx_remove_blk(htx, blk); |
| 4304 | else { |
| 4305 | htx_cut_data_blk(htx, blk, vlen); |
| 4306 | break; |
| 4307 | } |
| 4308 | } |
| 4309 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4310 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4311 | if (!stop) { |
| 4312 | si_cant_get(si); |
| 4313 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4314 | } |
| 4315 | |
| 4316 | /* return the result. */ |
| 4317 | luaL_pushresult(&appctx->b); |
| 4318 | return 1; |
| 4319 | } |
| 4320 | |
| 4321 | |
| 4322 | /* If expected data not yet available, it returns a yield. This function |
| 4323 | * consumes the data in the buffer. It returns a string containing the |
| 4324 | * data. This string can be empty. |
| 4325 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4326 | __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] | 4327 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4328 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4329 | struct stream_interface *si = appctx->appctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4330 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4331 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4332 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4333 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4334 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4335 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4336 | /* Check for the end of the data. */ |
| 4337 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4338 | luaL_pushresult(&appctx->b); |
| 4339 | return 1; |
| 4340 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4341 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4342 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4343 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4344 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4345 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4346 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4347 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4348 | 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] | 4349 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4350 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4351 | /* End of data: commit the total strings and return. */ |
| 4352 | if (ret < 0) { |
| 4353 | luaL_pushresult(&appctx->b); |
| 4354 | return 1; |
| 4355 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4356 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4357 | /* Ensure that the block 2 length is usable. */ |
| 4358 | if (ret == 1) |
| 4359 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4360 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4361 | /* Copy the fisrt block caping to the length required. */ |
| 4362 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4363 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4364 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4365 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4366 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4367 | /* Copy the second block. */ |
| 4368 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4369 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4370 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4371 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
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 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4374 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4375 | luaL_pushresult(&appctx->b); |
| 4376 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4379 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4380 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4381 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4382 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4383 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4384 | /* Initialise the string catenation. */ |
| 4385 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4386 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4387 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4388 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4389 | else |
| 4390 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4391 | } |
| 4392 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4393 | /* If expected data not yet available, it returns a yield. This function |
| 4394 | * consumes the data in the buffer. It returns a string containing the |
| 4395 | * data. This string can be empty. |
| 4396 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4397 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4398 | { |
| 4399 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4400 | struct stream_interface *si = appctx->appctx->owner; |
| 4401 | struct channel *req = si_oc(si); |
| 4402 | struct htx *htx; |
| 4403 | struct htx_blk *blk; |
| 4404 | size_t count; |
| 4405 | int len; |
| 4406 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4407 | htx = htx_from_buf(&req->buf); |
| 4408 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4409 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4410 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4411 | while (count && len && blk) { |
| 4412 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4413 | uint32_t sz = htx_get_blksz(blk); |
| 4414 | struct ist v; |
| 4415 | uint32_t vlen; |
| 4416 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4417 | if (type == HTX_BLK_EOM) { |
| 4418 | len = 0; |
| 4419 | break; |
| 4420 | } |
| 4421 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4422 | vlen = sz; |
| 4423 | if (len > 0 && vlen > len) |
| 4424 | vlen = len; |
| 4425 | if (vlen > count) { |
| 4426 | if (type != HTX_BLK_DATA) |
| 4427 | break; |
| 4428 | vlen = count; |
| 4429 | } |
| 4430 | |
| 4431 | switch (type) { |
| 4432 | case HTX_BLK_UNUSED: |
| 4433 | break; |
| 4434 | |
| 4435 | case HTX_BLK_DATA: |
| 4436 | v = htx_get_blk_value(htx, blk); |
| 4437 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4438 | break; |
| 4439 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4440 | case HTX_BLK_TLR: |
| 4441 | case HTX_BLK_EOM: |
| 4442 | len = 0; |
| 4443 | break; |
| 4444 | |
| 4445 | default: |
| 4446 | break; |
| 4447 | } |
| 4448 | |
| 4449 | co_set_data(req, co_data(req) - vlen); |
| 4450 | count -= vlen; |
| 4451 | if (len > 0) |
| 4452 | len -= vlen; |
| 4453 | if (sz == vlen) |
| 4454 | blk = htx_remove_blk(htx, blk); |
| 4455 | else { |
| 4456 | htx_cut_data_blk(htx, blk, vlen); |
| 4457 | break; |
| 4458 | } |
| 4459 | } |
| 4460 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4461 | htx_to_buf(htx, &req->buf); |
| 4462 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4463 | /* If we are no other data available, yield waiting for new data. */ |
| 4464 | if (len) { |
| 4465 | if (len > 0) { |
| 4466 | lua_pushinteger(L, len); |
| 4467 | lua_replace(L, 2); |
| 4468 | } |
| 4469 | si_cant_get(si); |
| 4470 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4471 | } |
| 4472 | |
| 4473 | /* return the result. */ |
| 4474 | luaL_pushresult(&appctx->b); |
| 4475 | return 1; |
| 4476 | } |
| 4477 | |
| 4478 | /* If expected data not yet available, it returns a yield. This function |
| 4479 | * consumes the data in the buffer. It returns a string containing the |
| 4480 | * data. This string can be empty. |
| 4481 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4482 | __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] | 4483 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4484 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4485 | struct stream_interface *si = appctx->appctx->owner; |
| 4486 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4487 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4488 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4489 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4490 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4491 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4492 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4493 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4494 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4495 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4496 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4497 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4498 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4499 | 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] | 4500 | } |
| 4501 | |
| 4502 | /* End of data: commit the total strings and return. */ |
| 4503 | if (ret < 0) { |
| 4504 | luaL_pushresult(&appctx->b); |
| 4505 | return 1; |
| 4506 | } |
| 4507 | |
| 4508 | /* Ensure that the block 2 length is usable. */ |
| 4509 | if (ret == 1) |
| 4510 | len2 = 0; |
| 4511 | |
| 4512 | /* Copy the fisrt block caping to the length required. */ |
| 4513 | if (len1 > len) |
| 4514 | len1 = len; |
| 4515 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4516 | len -= len1; |
| 4517 | |
| 4518 | /* Copy the second block. */ |
| 4519 | if (len2 > len) |
| 4520 | len2 = len; |
| 4521 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4522 | len -= len2; |
| 4523 | |
| 4524 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4525 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4526 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4527 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4528 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4529 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4530 | if (len > 0) { |
| 4531 | lua_pushinteger(L, len); |
| 4532 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4533 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4534 | 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] | 4535 | } |
| 4536 | |
| 4537 | /* return the result. */ |
| 4538 | luaL_pushresult(&appctx->b); |
| 4539 | return 1; |
| 4540 | } |
| 4541 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4542 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4543 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4544 | { |
| 4545 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4546 | int len = -1; |
| 4547 | |
| 4548 | /* Check arguments. */ |
| 4549 | if (lua_gettop(L) > 2) |
| 4550 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4551 | if (lua_gettop(L) >= 2) { |
| 4552 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4553 | lua_pop(L, 1); |
| 4554 | } |
| 4555 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4556 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4557 | /* HTX version */ |
| 4558 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4559 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4560 | /* Initialise the string catenation. */ |
| 4561 | luaL_buffinit(L, &appctx->b); |
| 4562 | |
| 4563 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4564 | } |
| 4565 | else { |
| 4566 | /* Legacy HTTP version */ |
| 4567 | /* Check the required length */ |
| 4568 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4569 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4570 | lua_pushinteger(L, len); |
| 4571 | |
| 4572 | /* Initialise the string catenation. */ |
| 4573 | luaL_buffinit(L, &appctx->b); |
| 4574 | |
| 4575 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4576 | } |
| 4577 | } |
| 4578 | |
| 4579 | /* Append data in the output side of the buffer. This data is immediately |
| 4580 | * sent. The function returns the amount of data written. If the buffer |
| 4581 | * cannot contain the data, the function yields. The function returns -1 |
| 4582 | * if the channel is closed. |
| 4583 | */ |
| 4584 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4585 | { |
| 4586 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4587 | struct stream_interface *si = appctx->appctx->owner; |
| 4588 | struct channel *res = si_ic(si); |
| 4589 | struct htx *htx = htx_from_buf(&res->buf); |
| 4590 | const char *data; |
| 4591 | size_t len; |
| 4592 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4593 | int max; |
| 4594 | |
Christopher Faulet | 4e96301 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4595 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4596 | if (!max) |
| 4597 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4598 | |
| 4599 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4600 | |
| 4601 | /* Get the max amount of data which can write as input in the channel. */ |
| 4602 | if (max > (len - l)) |
| 4603 | max = len - l; |
| 4604 | |
| 4605 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4606 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4607 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4608 | |
| 4609 | /* update counters. */ |
| 4610 | l += max; |
| 4611 | lua_pop(L, 1); |
| 4612 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4613 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4614 | /* If some data is not send, declares the situation to the |
| 4615 | * applet, and returns a yield. |
| 4616 | */ |
| 4617 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4618 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4619 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4620 | si_rx_room_blk(si); |
| 4621 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4622 | } |
| 4623 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4624 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4625 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4626 | } |
| 4627 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4628 | /* Append data in the output side of the buffer. This data is immediately |
| 4629 | * sent. The function returns the amount of data written. If the buffer |
| 4630 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4631 | * if the channel is closed. |
| 4632 | */ |
| 4633 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4634 | { |
| 4635 | size_t len; |
| 4636 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4637 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4638 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4639 | struct stream_interface *si = appctx->appctx->owner; |
| 4640 | struct channel *chn = si_ic(si); |
| 4641 | int max; |
| 4642 | |
| 4643 | /* Get the max amount of data which can write as input in the channel. */ |
| 4644 | max = channel_recv_max(chn); |
| 4645 | if (max > (len - l)) |
| 4646 | max = len - l; |
| 4647 | |
| 4648 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4649 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4650 | |
| 4651 | /* update counters. */ |
| 4652 | l += max; |
| 4653 | lua_pop(L, 1); |
| 4654 | lua_pushinteger(L, l); |
| 4655 | |
| 4656 | /* If some data is not send, declares the situation to the |
| 4657 | * applet, and returns a yield. |
| 4658 | */ |
| 4659 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4660 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4661 | 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] | 4662 | } |
| 4663 | |
| 4664 | return 1; |
| 4665 | } |
| 4666 | |
| 4667 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4668 | * yield the LUA process, and resume it without checking the |
| 4669 | * input arguments. |
| 4670 | */ |
| 4671 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4672 | { |
| 4673 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4674 | |
| 4675 | /* We want to send some data. Headers must be sent. */ |
| 4676 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4677 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4678 | WILL_LJMP(lua_error(L)); |
| 4679 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4680 | |
| 4681 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4682 | /* HTX version */ |
| 4683 | /* This interger is used for followinf the amount of data sent. */ |
| 4684 | lua_pushinteger(L, 0); |
| 4685 | |
| 4686 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4687 | } |
| 4688 | else { |
| 4689 | /* Legacy HTTP version */ |
| 4690 | size_t len; |
| 4691 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4692 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4693 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4694 | |
| 4695 | /* If transfer encoding chunked is selected, we surround the data |
| 4696 | * by chunk data. |
| 4697 | */ |
| 4698 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4699 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4700 | lua_pushfstring(L, "%s\r\n", hex); |
| 4701 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4702 | lua_pushstring(L, "\r\n"); |
| 4703 | lua_concat(L, 3); |
| 4704 | } |
| 4705 | |
| 4706 | /* This interger is used for followinf the amount of data sent. */ |
| 4707 | lua_pushinteger(L, 0); |
| 4708 | |
| 4709 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4710 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4714 | { |
| 4715 | const char *name; |
| 4716 | int ret; |
| 4717 | |
| 4718 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4719 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4720 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4721 | |
| 4722 | /* Push in the stack the "response" entry. */ |
| 4723 | ret = lua_getfield(L, 1, "response"); |
| 4724 | if (ret != LUA_TTABLE) { |
| 4725 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4726 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4727 | WILL_LJMP(lua_error(L)); |
| 4728 | } |
| 4729 | |
| 4730 | /* check if the header is already registered if it is not |
| 4731 | * the case, register it. |
| 4732 | */ |
| 4733 | ret = lua_getfield(L, -1, name); |
| 4734 | if (ret == LUA_TNIL) { |
| 4735 | |
| 4736 | /* Entry not found. */ |
| 4737 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4738 | |
| 4739 | /* Insert the new header name in the array in the top of the stack. |
| 4740 | * It left the new array in the top of the stack. |
| 4741 | */ |
| 4742 | lua_newtable(L); |
| 4743 | lua_pushvalue(L, 2); |
| 4744 | lua_pushvalue(L, -2); |
| 4745 | lua_settable(L, -4); |
| 4746 | |
| 4747 | } else if (ret != LUA_TTABLE) { |
| 4748 | |
| 4749 | /* corruption error. */ |
| 4750 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4751 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4752 | WILL_LJMP(lua_error(L)); |
| 4753 | } |
| 4754 | |
| 4755 | /* Now the top od thestack is an array of values. We push |
| 4756 | * the header value as new entry. |
| 4757 | */ |
| 4758 | lua_pushvalue(L, 3); |
| 4759 | ret = lua_rawlen(L, -2); |
| 4760 | lua_rawseti(L, -2, ret + 1); |
| 4761 | lua_pushboolean(L, 1); |
| 4762 | return 1; |
| 4763 | } |
| 4764 | |
| 4765 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4766 | { |
| 4767 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4768 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4769 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4770 | |
| 4771 | if (status < 100 || status > 599) { |
| 4772 | lua_pushboolean(L, 0); |
| 4773 | return 1; |
| 4774 | } |
| 4775 | |
| 4776 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4777 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | lua_pushboolean(L, 1); |
| 4779 | return 1; |
| 4780 | } |
| 4781 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4782 | |
| 4783 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4784 | { |
| 4785 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4786 | struct stream_interface *si = appctx->appctx->owner; |
| 4787 | struct channel *res = si_ic(si); |
| 4788 | struct htx *htx; |
| 4789 | struct htx_sl *sl; |
| 4790 | struct h1m h1m; |
| 4791 | const char *status, *reason; |
| 4792 | const char *name, *value; |
| 4793 | size_t nlen, vlen; |
| 4794 | unsigned int flags; |
| 4795 | |
| 4796 | /* Send the message at once. */ |
| 4797 | htx = htx_from_buf(&res->buf); |
| 4798 | h1m_init_res(&h1m); |
| 4799 | |
| 4800 | /* Use the same http version than the request. */ |
| 4801 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4802 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4803 | if (reason == NULL) |
| 4804 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4805 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4806 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4807 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4808 | } |
| 4809 | else { |
| 4810 | flags = HTX_SL_F_IS_RESP; |
| 4811 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4812 | } |
| 4813 | if (!sl) { |
| 4814 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4815 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4816 | WILL_LJMP(lua_error(L)); |
| 4817 | } |
| 4818 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4819 | |
| 4820 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4821 | lua_pushvalue(L, 0); |
| 4822 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4823 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4824 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4825 | WILL_LJMP(lua_error(L)); |
| 4826 | } |
| 4827 | |
| 4828 | /* Browse the list of headers. */ |
| 4829 | lua_pushnil(L); |
| 4830 | while(lua_next(L, -2) != 0) { |
| 4831 | /* We expect a string as -2. */ |
| 4832 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4833 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4834 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4835 | lua_typename(L, lua_type(L, -2))); |
| 4836 | WILL_LJMP(lua_error(L)); |
| 4837 | } |
| 4838 | name = lua_tolstring(L, -2, &nlen); |
| 4839 | |
| 4840 | /* We expect an array as -1. */ |
| 4841 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4842 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4843 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4844 | name, |
| 4845 | lua_typename(L, lua_type(L, -1))); |
| 4846 | WILL_LJMP(lua_error(L)); |
| 4847 | } |
| 4848 | |
| 4849 | /* Browse the table who is on the top of the stack. */ |
| 4850 | lua_pushnil(L); |
| 4851 | while(lua_next(L, -2) != 0) { |
| 4852 | int id; |
| 4853 | |
| 4854 | /* We expect a number as -2. */ |
| 4855 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4856 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4857 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4858 | name, |
| 4859 | lua_typename(L, lua_type(L, -2))); |
| 4860 | WILL_LJMP(lua_error(L)); |
| 4861 | } |
| 4862 | id = lua_tointeger(L, -2); |
| 4863 | |
| 4864 | /* We expect a string as -2. */ |
| 4865 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4866 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4867 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4868 | name, id, |
| 4869 | lua_typename(L, lua_type(L, -1))); |
| 4870 | WILL_LJMP(lua_error(L)); |
| 4871 | } |
| 4872 | value = lua_tolstring(L, -1, &vlen); |
| 4873 | |
| 4874 | /* Simple Protocol checks. */ |
| 4875 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4876 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4877 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4878 | struct ist v = ist2(value, vlen); |
| 4879 | int ret; |
| 4880 | |
| 4881 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4882 | if (ret < 0) { |
| 4883 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4884 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4885 | name); |
| 4886 | WILL_LJMP(lua_error(L)); |
| 4887 | } |
| 4888 | else if (ret == 0) |
| 4889 | goto next; /* Skip it */ |
| 4890 | } |
| 4891 | |
| 4892 | /* Add a new header */ |
| 4893 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4894 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4895 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4896 | name); |
| 4897 | WILL_LJMP(lua_error(L)); |
| 4898 | } |
| 4899 | next: |
| 4900 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4901 | lua_pop(L, 1); |
| 4902 | } |
| 4903 | |
| 4904 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4905 | lua_pop(L, 1); |
| 4906 | } |
| 4907 | |
| 4908 | if (h1m.flags & H1_MF_CHNK) |
| 4909 | h1m.flags &= ~H1_MF_CLEN; |
| 4910 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4911 | h1m.flags |= H1_MF_XFER_LEN; |
| 4912 | |
| 4913 | /* Uset HTX start-line flags */ |
| 4914 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4915 | flags |= HTX_SL_F_XFER_ENC; |
| 4916 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4917 | flags |= HTX_SL_F_XFER_LEN; |
| 4918 | if (h1m.flags & H1_MF_CHNK) |
| 4919 | flags |= HTX_SL_F_CHNK; |
| 4920 | else if (h1m.flags & H1_MF_CLEN) |
| 4921 | flags |= HTX_SL_F_CLEN; |
| 4922 | if (h1m.body_len == 0) |
| 4923 | flags |= HTX_SL_F_BODYLESS; |
| 4924 | } |
| 4925 | sl->flags |= flags; |
| 4926 | |
| 4927 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4928 | * and the status code implies the presence of a message body, we must |
| 4929 | * announce a transfer encoding chunked. This is required by haproxy |
| 4930 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4931 | * chunked itslef, don't do anything. |
| 4932 | */ |
| 4933 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4934 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4935 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4936 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4937 | /* Add a new header */ |
| 4938 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4939 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4940 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4941 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4942 | WILL_LJMP(lua_error(L)); |
| 4943 | } |
| 4944 | } |
| 4945 | |
| 4946 | /* Finalize headers. */ |
| 4947 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4948 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4949 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4950 | WILL_LJMP(lua_error(L)); |
| 4951 | } |
| 4952 | |
| 4953 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4954 | b_reset(&res->buf); |
| 4955 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4956 | WILL_LJMP(lua_error(L)); |
| 4957 | } |
| 4958 | |
| 4959 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4960 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4961 | |
| 4962 | /* Headers sent, set the flag. */ |
| 4963 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4964 | return 0; |
| 4965 | |
| 4966 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4967 | /* We will build the status line and the headers of the HTTP response. |
| 4968 | * We will try send at once if its not possible, we give back the hand |
| 4969 | * waiting for more room. |
| 4970 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4971 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4972 | { |
| 4973 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4974 | struct stream_interface *si = appctx->appctx->owner; |
| 4975 | struct channel *res = si_ic(si); |
| 4976 | |
| 4977 | if (co_data(res)) { |
| 4978 | si_rx_room_blk(si); |
| 4979 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 4980 | } |
| 4981 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 4982 | } |
| 4983 | |
| 4984 | |
| 4985 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 4986 | { |
| 4987 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 4988 | } |
| 4989 | |
| 4990 | /* We will build the status line and the headers of the HTTP response. |
| 4991 | * We will try send at once if its not possible, we give back the hand |
| 4992 | * waiting for more room. |
| 4993 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4994 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4995 | { |
| 4996 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4997 | struct stream_interface *si = appctx->appctx->owner; |
| 4998 | struct channel *chn = si_ic(si); |
| 4999 | int ret; |
| 5000 | size_t len; |
| 5001 | const char *msg; |
| 5002 | |
| 5003 | /* Get the message as the first argument on the stack. */ |
| 5004 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5005 | |
| 5006 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 5007 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5008 | |
| 5009 | /* if ret == -2 or -3 the channel closed or the message si too |
| 5010 | * big for the buffers. |
| 5011 | */ |
| 5012 | if (ret == -2 || ret == -3) { |
| 5013 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5014 | WILL_LJMP(lua_error(L)); |
| 5015 | } |
| 5016 | |
| 5017 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 5018 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 5019 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5020 | 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] | 5021 | } |
| 5022 | |
| 5023 | /* Headers sent, set the flag. */ |
| 5024 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 5025 | return 0; |
| 5026 | } |
| 5027 | |
| 5028 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 5029 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5030 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5031 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5032 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5033 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5034 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5035 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5036 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5037 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5038 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5039 | const char *reason; |
| 5040 | |
| 5041 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5042 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5043 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5044 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5045 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5046 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5047 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5048 | tmp = get_trash_chunk(); |
| 5049 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5050 | /* Use the same http version than the request. */ |
| 5051 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5052 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5053 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5054 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5055 | |
| 5056 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5057 | lua_pushvalue(L, 0); |
| 5058 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5059 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5060 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5061 | WILL_LJMP(lua_error(L)); |
| 5062 | } |
| 5063 | |
| 5064 | /* Browse the list of headers. */ |
| 5065 | lua_pushnil(L); |
| 5066 | while(lua_next(L, -2) != 0) { |
| 5067 | |
| 5068 | /* We expect a string as -2. */ |
| 5069 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5070 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5071 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5072 | lua_typename(L, lua_type(L, -2))); |
| 5073 | WILL_LJMP(lua_error(L)); |
| 5074 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5075 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5076 | |
| 5077 | /* We expect an array as -1. */ |
| 5078 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5079 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5080 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5081 | name, |
| 5082 | lua_typename(L, lua_type(L, -1))); |
| 5083 | WILL_LJMP(lua_error(L)); |
| 5084 | } |
| 5085 | |
| 5086 | /* Browse the table who is on the top of the stack. */ |
| 5087 | lua_pushnil(L); |
| 5088 | while(lua_next(L, -2) != 0) { |
| 5089 | |
| 5090 | /* We expect a number as -2. */ |
| 5091 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5092 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5093 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5094 | name, |
| 5095 | lua_typename(L, lua_type(L, -2))); |
| 5096 | WILL_LJMP(lua_error(L)); |
| 5097 | } |
| 5098 | id = lua_tointeger(L, -2); |
| 5099 | |
| 5100 | /* We expect a string as -2. */ |
| 5101 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5102 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5103 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5104 | name, id, |
| 5105 | lua_typename(L, lua_type(L, -1))); |
| 5106 | WILL_LJMP(lua_error(L)); |
| 5107 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5108 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5109 | |
| 5110 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5111 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5112 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5113 | tmp->data += name_len; |
| 5114 | tmp->area[tmp->data++] = ':'; |
| 5115 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5116 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5117 | memcpy(tmp->area + tmp->data, value, |
| 5118 | value_len); |
| 5119 | tmp->data += value_len; |
| 5120 | tmp->area[tmp->data++] = '\r'; |
| 5121 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5122 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5123 | |
| 5124 | /* Protocol checks. */ |
| 5125 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5126 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5127 | * is done without control. If it contains a bad value, |
| 5128 | * the content-length remains negative so that we can |
| 5129 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5130 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5131 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5132 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5133 | |
| 5134 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5135 | if (name_len == 17 && value_len == 7 && |
| 5136 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5137 | strcasecmp("chunked", value) == 0) |
| 5138 | hdr_chunked = 1; |
| 5139 | |
| 5140 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5141 | lua_pop(L, 1); |
| 5142 | } |
| 5143 | |
| 5144 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5145 | lua_pop(L, 1); |
| 5146 | } |
| 5147 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5148 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5149 | * and the status code implies the presence of a message body, we must |
| 5150 | * announce a transfer encoding chunked. This is required by haproxy |
| 5151 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5152 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5153 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5154 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5155 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5156 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5157 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5158 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5159 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5160 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5161 | } |
| 5162 | |
| 5163 | /* Finalize headers. */ |
| 5164 | chunk_appendf(tmp, "\r\n"); |
| 5165 | |
| 5166 | /* Remove the last entry and the array of headers */ |
| 5167 | lua_pop(L, 2); |
| 5168 | |
| 5169 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5170 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5171 | |
| 5172 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5173 | } |
| 5174 | |
| 5175 | /* |
| 5176 | * |
| 5177 | * |
| 5178 | * Class HTTP |
| 5179 | * |
| 5180 | * |
| 5181 | */ |
| 5182 | |
| 5183 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5184 | * a class stream, otherwise it throws an error. |
| 5185 | */ |
| 5186 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5187 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5188 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | /* This function creates and push in the stack a HTTP object |
| 5192 | * according with a current TXN. |
| 5193 | */ |
| 5194 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5195 | { |
| 5196 | struct hlua_txn *htxn; |
| 5197 | |
| 5198 | /* Check stack size. */ |
| 5199 | if (!lua_checkstack(L, 3)) |
| 5200 | return 0; |
| 5201 | |
| 5202 | /* Create the object: obj[0] = userdata. |
| 5203 | * Note that the base of the Converters object is the |
| 5204 | * same than the TXN object. |
| 5205 | */ |
| 5206 | lua_newtable(L); |
| 5207 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5208 | lua_rawseti(L, -2, 0); |
| 5209 | |
| 5210 | htxn->s = txn->s; |
| 5211 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5212 | htxn->dir = txn->dir; |
| 5213 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5214 | |
| 5215 | /* Pop a class stream metatable and affect it to the table. */ |
| 5216 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5217 | lua_setmetatable(L, -2); |
| 5218 | |
| 5219 | return 1; |
| 5220 | } |
| 5221 | |
| 5222 | /* This function creates ans returns an array of HTTP headers. |
| 5223 | * This function does not fails. It is used as wrapper with the |
| 5224 | * 2 following functions. |
| 5225 | */ |
| 5226 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5227 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5228 | /* Create the table. */ |
| 5229 | lua_newtable(L); |
| 5230 | |
| 5231 | if (!htxn->s->txn) |
| 5232 | return 1; |
| 5233 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5234 | if (IS_HTX_STRM(htxn->s)) { |
| 5235 | /* HTX version */ |
| 5236 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5237 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5238 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5239 | 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] | 5240 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5241 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5242 | struct ist n, v; |
| 5243 | int len; |
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 | if (type == HTX_BLK_HDR) { |
| 5246 | n = htx_get_blk_name(htx,blk); |
| 5247 | v = htx_get_blk_value(htx, blk); |
| 5248 | } |
| 5249 | else if (type == HTX_BLK_EOH) |
| 5250 | break; |
| 5251 | else |
| 5252 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5253 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5254 | /* Check for existing entry: |
| 5255 | * assume that the table is on the top of the stack, and |
| 5256 | * push the key in the stack, the function lua_gettable() |
| 5257 | * perform the lookup. |
| 5258 | */ |
| 5259 | lua_pushlstring(L, n.ptr, n.len); |
| 5260 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5261 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5262 | switch (lua_type(L, -1)) { |
| 5263 | case LUA_TNIL: |
| 5264 | /* Table not found, create it. */ |
| 5265 | lua_pop(L, 1); /* remove the nil value. */ |
| 5266 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5267 | lua_newtable(L); /* create and push empty table. */ |
| 5268 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5269 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5270 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5271 | break; |
| 5272 | |
| 5273 | case LUA_TTABLE: |
| 5274 | /* Entry found: push the value in the table. */ |
| 5275 | len = lua_rawlen(L, -1); |
| 5276 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5277 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5278 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5279 | break; |
| 5280 | |
| 5281 | default: |
| 5282 | /* Other cases are errors. */ |
| 5283 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5284 | WILL_LJMP(lua_error(L)); |
| 5285 | } |
| 5286 | } |
| 5287 | } |
| 5288 | else { |
| 5289 | /* Legacy HTTP version */ |
| 5290 | const char *cur_ptr, *cur_next, *p; |
| 5291 | int old_idx, cur_idx; |
| 5292 | struct hdr_idx_elem *cur_hdr; |
| 5293 | const char *hn, *hv; |
| 5294 | int hnl, hvl; |
| 5295 | const char *in; |
| 5296 | char *out; |
| 5297 | int len; |
| 5298 | |
| 5299 | /* Build array of headers. */ |
| 5300 | old_idx = 0; |
| 5301 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5302 | |
| 5303 | while (1) { |
| 5304 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5305 | if (!cur_idx) |
| 5306 | break; |
| 5307 | old_idx = cur_idx; |
| 5308 | |
| 5309 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5310 | cur_ptr = cur_next; |
| 5311 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5312 | |
| 5313 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5314 | * and the next header starts at cur_next. We'll check |
| 5315 | * this header in the list as well as against the default |
| 5316 | * rule. |
| 5317 | */ |
| 5318 | |
| 5319 | /* look for ': *'. */ |
| 5320 | hn = cur_ptr; |
| 5321 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5322 | if (p >= cur_ptr+cur_hdr->len) |
| 5323 | continue; |
| 5324 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5325 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5326 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5327 | p++; |
| 5328 | if (p >= cur_ptr+cur_hdr->len) |
| 5329 | continue; |
| 5330 | hv = p; |
| 5331 | hvl = cur_ptr+cur_hdr->len-p; |
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 | /* Lowercase the key. Don't check the size of trash, it have |
| 5334 | * the size of one buffer and the input data contains in one |
| 5335 | * buffer. |
| 5336 | */ |
| 5337 | out = trash.area; |
| 5338 | for (in=hn; in<hn+hnl; in++, out++) |
| 5339 | *out = tolower(*in); |
| 5340 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5341 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5342 | /* Check for existing entry: |
| 5343 | * assume that the table is on the top of the stack, and |
| 5344 | * push the key in the stack, the function lua_gettable() |
| 5345 | * perform the lookup. |
| 5346 | */ |
| 5347 | lua_pushlstring(L, trash.area, hnl); |
| 5348 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5349 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5350 | switch (lua_type(L, -1)) { |
| 5351 | case LUA_TNIL: |
| 5352 | /* Table not found, create it. */ |
| 5353 | lua_pop(L, 1); /* remove the nil value. */ |
| 5354 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5355 | lua_newtable(L); /* create and push empty table. */ |
| 5356 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5357 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5358 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5359 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5360 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5361 | case LUA_TTABLE: |
| 5362 | /* Entry found: push the value in the table. */ |
| 5363 | len = lua_rawlen(L, -1); |
| 5364 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5365 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5366 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5367 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5368 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5369 | default: |
| 5370 | /* Other cases are errors. */ |
| 5371 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5372 | WILL_LJMP(lua_error(L)); |
| 5373 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5374 | } |
| 5375 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5376 | return 1; |
| 5377 | } |
| 5378 | |
| 5379 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5380 | { |
| 5381 | struct hlua_txn *htxn; |
| 5382 | |
| 5383 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5384 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5385 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5386 | 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] | 5387 | WILL_LJMP(lua_error(L)); |
| 5388 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5389 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5390 | } |
| 5391 | |
| 5392 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5393 | { |
| 5394 | struct hlua_txn *htxn; |
| 5395 | |
| 5396 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5397 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5398 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5399 | 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] | 5400 | WILL_LJMP(lua_error(L)); |
| 5401 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5402 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5403 | } |
| 5404 | |
| 5405 | /* This function replace full header, or just a value in |
| 5406 | * the request or in the response. It is a wrapper fir the |
| 5407 | * 4 following functions. |
| 5408 | */ |
| 5409 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5410 | struct http_msg *msg, int action) |
| 5411 | { |
| 5412 | size_t name_len; |
| 5413 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5414 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5415 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5416 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5417 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5418 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5419 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5420 | |
Christopher Faulet | 5a8549e | 2019-06-17 13:36:06 +0200 | [diff] [blame] | 5421 | if (IS_HTX_STRM(htxn->s)) { |
| 5422 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5423 | |
| 5424 | htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action); |
| 5425 | } |
| 5426 | else |
| 5427 | 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] | 5428 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5429 | return 0; |
| 5430 | } |
| 5431 | |
| 5432 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5433 | { |
| 5434 | struct hlua_txn *htxn; |
| 5435 | |
| 5436 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5437 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5438 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5439 | 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] | 5440 | WILL_LJMP(lua_error(L)); |
| 5441 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5442 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5443 | } |
| 5444 | |
| 5445 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5446 | { |
| 5447 | struct hlua_txn *htxn; |
| 5448 | |
| 5449 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5450 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5451 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5452 | 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] | 5453 | WILL_LJMP(lua_error(L)); |
| 5454 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5455 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5456 | } |
| 5457 | |
| 5458 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5459 | { |
| 5460 | struct hlua_txn *htxn; |
| 5461 | |
| 5462 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5463 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5464 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5465 | 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] | 5466 | WILL_LJMP(lua_error(L)); |
| 5467 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5468 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5469 | } |
| 5470 | |
| 5471 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5472 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5473 | struct hlua_txn *htxn; |
| 5474 | |
| 5475 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5476 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5477 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5478 | 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] | 5479 | WILL_LJMP(lua_error(L)); |
| 5480 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5481 | 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] | 5482 | } |
| 5483 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5484 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5485 | * It is a wrapper for the 2 following functions. |
| 5486 | */ |
| 5487 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5488 | { |
| 5489 | size_t len; |
| 5490 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5491 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5492 | if (IS_HTX_STRM(htxn->s)) { |
| 5493 | /* HTX version */ |
| 5494 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5495 | struct http_hdr_ctx ctx; |
| 5496 | |
| 5497 | ctx.blk = NULL; |
| 5498 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5499 | http_remove_header(htx, &ctx); |
| 5500 | } |
| 5501 | else { |
| 5502 | /* Legacy HTTP version */ |
| 5503 | struct hdr_ctx ctx; |
| 5504 | struct http_txn *txn = htxn->s->txn; |
| 5505 | |
| 5506 | ctx.idx = 0; |
| 5507 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5508 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5509 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5510 | return 0; |
| 5511 | } |
| 5512 | |
| 5513 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5514 | { |
| 5515 | struct hlua_txn *htxn; |
| 5516 | |
| 5517 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5518 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5519 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5520 | 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] | 5521 | WILL_LJMP(lua_error(L)); |
| 5522 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5523 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5527 | { |
| 5528 | struct hlua_txn *htxn; |
| 5529 | |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5530 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5531 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5532 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5533 | 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] | 5534 | WILL_LJMP(lua_error(L)); |
| 5535 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5536 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | /* This function adds an header. It is a wrapper used by |
| 5540 | * the 2 following functions. |
| 5541 | */ |
| 5542 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5543 | { |
| 5544 | size_t name_len; |
| 5545 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5546 | size_t value_len; |
| 5547 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5548 | char *p; |
| 5549 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5550 | if (IS_HTX_STRM(htxn->s)) { |
| 5551 | /* HTX version */ |
| 5552 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5553 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5554 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5555 | ist2(value, value_len))); |
| 5556 | } |
| 5557 | else { |
| 5558 | /* Legacy HTTP version */ |
| 5559 | /* Check length. */ |
| 5560 | trash.data = value_len + name_len + 2; |
| 5561 | if (trash.data > trash.size) |
| 5562 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5563 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5564 | /* Creates the header string. */ |
| 5565 | p = trash.area; |
| 5566 | memcpy(p, name, name_len); |
| 5567 | p += name_len; |
| 5568 | *p = ':'; |
| 5569 | p++; |
| 5570 | *p = ' '; |
| 5571 | p++; |
| 5572 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5573 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5574 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5575 | trash.area, trash.data) != 0); |
| 5576 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5577 | return 0; |
| 5578 | } |
| 5579 | |
| 5580 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5581 | { |
| 5582 | struct hlua_txn *htxn; |
| 5583 | |
| 5584 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5585 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5586 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5587 | 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] | 5588 | WILL_LJMP(lua_error(L)); |
| 5589 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5590 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5591 | } |
| 5592 | |
| 5593 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5594 | { |
| 5595 | struct hlua_txn *htxn; |
| 5596 | |
| 5597 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5598 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5599 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5600 | 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] | 5601 | WILL_LJMP(lua_error(L)); |
| 5602 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5603 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5607 | { |
| 5608 | struct hlua_txn *htxn; |
| 5609 | |
| 5610 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5611 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5612 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5613 | 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] | 5614 | WILL_LJMP(lua_error(L)); |
| 5615 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5616 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5617 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5621 | { |
| 5622 | struct hlua_txn *htxn; |
| 5623 | |
| 5624 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5625 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5626 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5627 | 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] | 5628 | WILL_LJMP(lua_error(L)); |
| 5629 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5630 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5631 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5632 | } |
| 5633 | |
| 5634 | /* This function set the method. */ |
| 5635 | static int hlua_http_req_set_meth(lua_State *L) |
| 5636 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5637 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5638 | size_t name_len; |
| 5639 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5640 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5641 | 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] | 5642 | WILL_LJMP(lua_error(L)); |
| 5643 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5644 | 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] | 5645 | return 1; |
| 5646 | } |
| 5647 | |
| 5648 | /* This function set the method. */ |
| 5649 | static int hlua_http_req_set_path(lua_State *L) |
| 5650 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5651 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5652 | size_t name_len; |
| 5653 | 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] | 5654 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5655 | 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] | 5656 | WILL_LJMP(lua_error(L)); |
| 5657 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5658 | 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] | 5659 | return 1; |
| 5660 | } |
| 5661 | |
| 5662 | /* This function set the query-string. */ |
| 5663 | static int hlua_http_req_set_query(lua_State *L) |
| 5664 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5665 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5666 | size_t name_len; |
| 5667 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5668 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5669 | 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] | 5670 | WILL_LJMP(lua_error(L)); |
| 5671 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5672 | /* Check length. */ |
| 5673 | if (name_len > trash.size - 1) { |
| 5674 | lua_pushboolean(L, 0); |
| 5675 | return 1; |
| 5676 | } |
| 5677 | |
| 5678 | /* Add the mark question as prefix. */ |
| 5679 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5680 | trash.area[trash.data++] = '?'; |
| 5681 | memcpy(trash.area + trash.data, name, name_len); |
| 5682 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5683 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5684 | lua_pushboolean(L, |
| 5685 | 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] | 5686 | return 1; |
| 5687 | } |
| 5688 | |
| 5689 | /* This function set the uri. */ |
| 5690 | static int hlua_http_req_set_uri(lua_State *L) |
| 5691 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5692 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5693 | size_t name_len; |
| 5694 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5695 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5696 | 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] | 5697 | WILL_LJMP(lua_error(L)); |
| 5698 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5699 | 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] | 5700 | return 1; |
| 5701 | } |
| 5702 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5703 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5704 | static int hlua_http_res_set_status(lua_State *L) |
| 5705 | { |
| 5706 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5707 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5708 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5709 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5710 | 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] | 5711 | WILL_LJMP(lua_error(L)); |
| 5712 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5713 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5714 | return 0; |
| 5715 | } |
| 5716 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5717 | /* |
| 5718 | * |
| 5719 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5720 | * Class TXN |
| 5721 | * |
| 5722 | * |
| 5723 | */ |
| 5724 | |
| 5725 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5726 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5727 | */ |
| 5728 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5729 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5730 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5731 | } |
| 5732 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5733 | __LJMP static int hlua_set_var(lua_State *L) |
| 5734 | { |
| 5735 | struct hlua_txn *htxn; |
| 5736 | const char *name; |
| 5737 | size_t len; |
| 5738 | struct sample smp; |
| 5739 | |
| 5740 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5741 | |
| 5742 | /* It is useles to retrieve the stream, but this function |
| 5743 | * runs only in a stream context. |
| 5744 | */ |
| 5745 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5746 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5747 | |
| 5748 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5749 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5750 | hlua_lua2smp(L, 3, &smp); |
| 5751 | |
| 5752 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5753 | 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] | 5754 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5755 | return 0; |
| 5756 | } |
| 5757 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5758 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5759 | { |
| 5760 | struct hlua_txn *htxn; |
| 5761 | const char *name; |
| 5762 | size_t len; |
| 5763 | struct sample smp; |
| 5764 | |
| 5765 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5766 | |
| 5767 | /* It is useles to retrieve the stream, but this function |
| 5768 | * runs only in a stream context. |
| 5769 | */ |
| 5770 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5771 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5772 | |
| 5773 | /* Unset the variable. */ |
| 5774 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5775 | vars_unset_by_name(name, len, &smp); |
| 5776 | return 0; |
| 5777 | } |
| 5778 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5779 | __LJMP static int hlua_get_var(lua_State *L) |
| 5780 | { |
| 5781 | struct hlua_txn *htxn; |
| 5782 | const char *name; |
| 5783 | size_t len; |
| 5784 | struct sample smp; |
| 5785 | |
| 5786 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5787 | |
| 5788 | /* It is useles to retrieve the stream, but this function |
| 5789 | * runs only in a stream context. |
| 5790 | */ |
| 5791 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5792 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5793 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5794 | 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] | 5795 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5796 | lua_pushnil(L); |
| 5797 | return 1; |
| 5798 | } |
| 5799 | |
| 5800 | return hlua_smp2lua(L, &smp); |
| 5801 | } |
| 5802 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5803 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5804 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5805 | struct hlua *hlua; |
| 5806 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5807 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5808 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5809 | /* It is useles to retrieve the stream, but this function |
| 5810 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5811 | */ |
| 5812 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5813 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5814 | |
| 5815 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5816 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5817 | |
| 5818 | /* Get and store new value. */ |
| 5819 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5820 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5821 | |
| 5822 | return 0; |
| 5823 | } |
| 5824 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5825 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5826 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5827 | struct hlua *hlua; |
| 5828 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5829 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5830 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5831 | /* It is useles to retrieve the stream, but this function |
| 5832 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5833 | */ |
| 5834 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5835 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5836 | |
| 5837 | /* Push configuration index in the stack. */ |
| 5838 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5839 | |
| 5840 | return 1; |
| 5841 | } |
| 5842 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5843 | /* Create stack entry containing a class TXN. This function |
| 5844 | * return 0 if the stack does not contains free slots, |
| 5845 | * otherwise it returns 1. |
| 5846 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5847 | 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] | 5848 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5849 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5850 | |
| 5851 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5852 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5853 | return 0; |
| 5854 | |
| 5855 | /* NOTE: The allocation never fails. The failure |
| 5856 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5857 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5858 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5859 | /* Create the object: obj[0] = userdata. */ |
| 5860 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5861 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5862 | lua_rawseti(L, -2, 0); |
| 5863 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5864 | htxn->s = s; |
| 5865 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5866 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5867 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5868 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5869 | /* Create the "f" field that contains a list of fetches. */ |
| 5870 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5871 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5872 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5873 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5874 | |
| 5875 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5876 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5877 | 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] | 5878 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5879 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5880 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5881 | /* Create the "c" field that contains a list of converters. */ |
| 5882 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5883 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5884 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5885 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5886 | |
| 5887 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5888 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5889 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5890 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5891 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5892 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5893 | /* Create the "req" field that contains the request channel object. */ |
| 5894 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5895 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5896 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5897 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5898 | |
| 5899 | /* Create the "res" field that contains the response channel object. */ |
| 5900 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5901 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5902 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5903 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5904 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5905 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5906 | lua_pushstring(L, "http"); |
| 5907 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5908 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5909 | return 0; |
| 5910 | } |
| 5911 | else |
| 5912 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5913 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5914 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5915 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5916 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5917 | lua_setmetatable(L, -2); |
| 5918 | |
| 5919 | return 1; |
| 5920 | } |
| 5921 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5922 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5923 | { |
| 5924 | const char *msg; |
| 5925 | struct hlua_txn *htxn; |
| 5926 | |
| 5927 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5928 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5929 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5930 | |
| 5931 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5932 | return 0; |
| 5933 | } |
| 5934 | |
| 5935 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5936 | { |
| 5937 | int level; |
| 5938 | const char *msg; |
| 5939 | struct hlua_txn *htxn; |
| 5940 | |
| 5941 | MAY_LJMP(check_args(L, 3, "log")); |
| 5942 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5943 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5944 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5945 | |
| 5946 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5947 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5948 | |
| 5949 | hlua_sendlog(htxn->s->be, level, msg); |
| 5950 | return 0; |
| 5951 | } |
| 5952 | |
| 5953 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5954 | { |
| 5955 | const char *msg; |
| 5956 | struct hlua_txn *htxn; |
| 5957 | |
| 5958 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5959 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5960 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5961 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5962 | return 0; |
| 5963 | } |
| 5964 | |
| 5965 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5966 | { |
| 5967 | const char *msg; |
| 5968 | struct hlua_txn *htxn; |
| 5969 | |
| 5970 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5971 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5972 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5973 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5974 | return 0; |
| 5975 | } |
| 5976 | |
| 5977 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5978 | { |
| 5979 | const char *msg; |
| 5980 | struct hlua_txn *htxn; |
| 5981 | |
| 5982 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5983 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5984 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5985 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5986 | return 0; |
| 5987 | } |
| 5988 | |
| 5989 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5990 | { |
| 5991 | const char *msg; |
| 5992 | struct hlua_txn *htxn; |
| 5993 | |
| 5994 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5995 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5996 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5997 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5998 | return 0; |
| 5999 | } |
| 6000 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6001 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 6002 | { |
| 6003 | struct hlua_txn *htxn; |
| 6004 | int ll; |
| 6005 | |
| 6006 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 6007 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6008 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6009 | |
| 6010 | if (ll < 0 || ll > 7) |
| 6011 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 6012 | |
| 6013 | htxn->s->logs.level = ll; |
| 6014 | return 0; |
| 6015 | } |
| 6016 | |
| 6017 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 6018 | { |
| 6019 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6020 | int tos; |
| 6021 | |
| 6022 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 6023 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6024 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6025 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 6026 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6027 | return 0; |
| 6028 | } |
| 6029 | |
| 6030 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 6031 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6032 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6033 | int mark; |
| 6034 | |
| 6035 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 6036 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6037 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6038 | |
Lukas Tribus | b59291a | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 6039 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6040 | return 0; |
| 6041 | } |
| 6042 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6043 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6044 | { |
| 6045 | struct hlua_txn *htxn; |
| 6046 | |
| 6047 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6048 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6049 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6050 | return 0; |
| 6051 | } |
| 6052 | |
| 6053 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6054 | { |
| 6055 | struct hlua_txn *htxn; |
| 6056 | |
| 6057 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6058 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6059 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6060 | return 0; |
| 6061 | } |
| 6062 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6063 | /* This function is an Lua binding that send pending data |
| 6064 | * to the client, and close the stream interface. |
| 6065 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6066 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6067 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6068 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6069 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6070 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6071 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6072 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6073 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6074 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6075 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6076 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6077 | * session, so we just end the execution of the current |
| 6078 | * lua code. |
| 6079 | */ |
| 6080 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6081 | WILL_LJMP(hlua_done(L)); |
| 6082 | return 0; |
| 6083 | } |
| 6084 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6085 | ic = &htxn->s->req; |
| 6086 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6087 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6088 | if (IS_HTX_STRM(htxn->s)) { |
| 6089 | htxn->s->txn->status = 0; |
| 6090 | http_reply_and_close(htxn->s, 0, NULL); |
| 6091 | ic->analysers &= AN_REQ_FLT_END; |
| 6092 | oc->analysers &= AN_RES_FLT_END; |
| 6093 | } |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6094 | else { |
| 6095 | if (htxn->s->txn) { |
| 6096 | /* HTTP mode, let's stay in sync with the stream */ |
| 6097 | b_del(&ic->buf, htxn->s->txn->req.sov); |
| 6098 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6099 | htxn->s->txn->req.sov = 0; |
| 6100 | |
| 6101 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6102 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6103 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6104 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6105 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6106 | /* Note that if we want to support keep-alive, we need |
| 6107 | * to bypass the close/shutr_now calls below, but that |
| 6108 | * may only be done if the HTTP request was already |
| 6109 | * processed and the connection header is known (ie |
| 6110 | * not during TCP rules). |
| 6111 | */ |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6112 | } |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6113 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6114 | channel_auto_read(ic); |
| 6115 | channel_abort(ic); |
| 6116 | channel_auto_close(ic); |
| 6117 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6118 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6119 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 6120 | channel_auto_read(oc); |
| 6121 | channel_auto_close(oc); |
| 6122 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6123 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6124 | ic->analysers = 0; |
| 6125 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6126 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6127 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 6128 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 6129 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6130 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6131 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6132 | return 0; |
| 6133 | } |
| 6134 | |
| 6135 | __LJMP static int hlua_log(lua_State *L) |
| 6136 | { |
| 6137 | int level; |
| 6138 | const char *msg; |
| 6139 | |
| 6140 | MAY_LJMP(check_args(L, 2, "log")); |
| 6141 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6142 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6143 | |
| 6144 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6145 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6146 | |
| 6147 | hlua_sendlog(NULL, level, msg); |
| 6148 | return 0; |
| 6149 | } |
| 6150 | |
| 6151 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6152 | { |
| 6153 | const char *msg; |
| 6154 | |
| 6155 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6156 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6157 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6158 | return 0; |
| 6159 | } |
| 6160 | |
| 6161 | __LJMP static int hlua_log_info(lua_State *L) |
| 6162 | { |
| 6163 | const char *msg; |
| 6164 | |
| 6165 | MAY_LJMP(check_args(L, 1, "info")); |
| 6166 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6167 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6168 | return 0; |
| 6169 | } |
| 6170 | |
| 6171 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6172 | { |
| 6173 | const char *msg; |
| 6174 | |
| 6175 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6176 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6177 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6178 | return 0; |
| 6179 | } |
| 6180 | |
| 6181 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6182 | { |
| 6183 | const char *msg; |
| 6184 | |
| 6185 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6186 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6187 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6188 | return 0; |
| 6189 | } |
| 6190 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6191 | __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] | 6192 | { |
| 6193 | int wakeup_ms = lua_tointeger(L, -1); |
| 6194 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6195 | 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] | 6196 | return 0; |
| 6197 | } |
| 6198 | |
| 6199 | __LJMP static int hlua_sleep(lua_State *L) |
| 6200 | { |
| 6201 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6202 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6203 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6204 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6205 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6206 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6207 | wakeup_ms = tick_add(now_ms, delay); |
| 6208 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6209 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6210 | 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] | 6211 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6212 | } |
| 6213 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6214 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6215 | { |
| 6216 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6217 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6218 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6219 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6220 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6221 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6222 | wakeup_ms = tick_add(now_ms, delay); |
| 6223 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6224 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6225 | 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] | 6226 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6227 | } |
| 6228 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6229 | /* This functionis an LUA binding. it permits to give back |
| 6230 | * the hand at the HAProxy scheduler. It is used when the |
| 6231 | * LUA processing consumes a lot of time. |
| 6232 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6233 | __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] | 6234 | { |
| 6235 | return 0; |
| 6236 | } |
| 6237 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6238 | __LJMP static int hlua_yield(lua_State *L) |
| 6239 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6240 | 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] | 6241 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6242 | } |
| 6243 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6244 | /* This function change the nice of the currently executed |
| 6245 | * task. It is used set low or high priority at the current |
| 6246 | * task. |
| 6247 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6248 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6249 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6250 | struct hlua *hlua; |
| 6251 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6252 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6253 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6254 | hlua = hlua_gethlua(L); |
| 6255 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6256 | |
| 6257 | /* If he task is not set, I'm in a start mode. */ |
| 6258 | if (!hlua || !hlua->task) |
| 6259 | return 0; |
| 6260 | |
| 6261 | if (nice < -1024) |
| 6262 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6263 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6264 | nice = 1024; |
| 6265 | |
| 6266 | hlua->task->nice = nice; |
| 6267 | return 0; |
| 6268 | } |
| 6269 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6270 | /* 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] | 6271 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6272 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6273 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6274 | * |
| 6275 | * Task wrapper are longjmp safe because the only one Lua code |
| 6276 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6277 | */ |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6278 | 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] | 6279 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6280 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6281 | enum hlua_exec status; |
| 6282 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6283 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6284 | task_set_affinity(task, tid_bit); |
| 6285 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6286 | /* If it is the first call to the task, we must initialize the |
| 6287 | * execution timeouts. |
| 6288 | */ |
| 6289 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6290 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6291 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6292 | /* Execute the Lua code. */ |
| 6293 | status = hlua_ctx_resume(hlua, 1); |
| 6294 | |
| 6295 | switch (status) { |
| 6296 | /* finished or yield */ |
| 6297 | case HLUA_E_OK: |
| 6298 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6299 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6300 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6301 | break; |
| 6302 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6303 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6304 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6305 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6306 | break; |
| 6307 | |
| 6308 | /* finished with error. */ |
| 6309 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6310 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6311 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6312 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6313 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6314 | break; |
| 6315 | |
| 6316 | case HLUA_E_ERR: |
| 6317 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6318 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6319 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6320 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6321 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6322 | break; |
| 6323 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6324 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6325 | } |
| 6326 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6327 | /* This function is an LUA binding that register LUA function to be |
| 6328 | * executed after the HAProxy configuration parsing and before the |
| 6329 | * HAProxy scheduler starts. This function expect only one LUA |
| 6330 | * argument that is a function. This function returns nothing, but |
| 6331 | * throws if an error is encountered. |
| 6332 | */ |
| 6333 | __LJMP static int hlua_register_init(lua_State *L) |
| 6334 | { |
| 6335 | struct hlua_init_function *init; |
| 6336 | int ref; |
| 6337 | |
| 6338 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6339 | |
| 6340 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6341 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6342 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6343 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6344 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6345 | |
| 6346 | init->function_ref = ref; |
| 6347 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6348 | return 0; |
| 6349 | } |
| 6350 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6351 | /* This functio is an LUA binding. It permits to register a task |
| 6352 | * executed in parallel of the main HAroxy activity. The task is |
| 6353 | * created and it is set in the HAProxy scheduler. It can be called |
| 6354 | * from the "init" section, "post init" or during the runtime. |
| 6355 | * |
| 6356 | * Lua prototype: |
| 6357 | * |
| 6358 | * <none> core.register_task(<function>) |
| 6359 | */ |
| 6360 | static int hlua_register_task(lua_State *L) |
| 6361 | { |
| 6362 | struct hlua *hlua; |
| 6363 | struct task *task; |
| 6364 | int ref; |
| 6365 | |
| 6366 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6367 | |
| 6368 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6369 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6370 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6371 | if (!hlua) |
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 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6374 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6375 | if (!task) |
| 6376 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6377 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6378 | task->context = hlua; |
| 6379 | task->process = hlua_process_task; |
| 6380 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6381 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6382 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6383 | |
| 6384 | /* Restore the function in the stack. */ |
| 6385 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6386 | hlua->nargs = 0; |
| 6387 | |
| 6388 | /* Schedule task. */ |
| 6389 | task_schedule(task, now_ms); |
| 6390 | |
| 6391 | return 0; |
| 6392 | } |
| 6393 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6394 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6395 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6396 | * resume converters. |
| 6397 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6398 | 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] | 6399 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6400 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6401 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6402 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6403 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6404 | if (!stream) |
| 6405 | return 0; |
| 6406 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6407 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6408 | * Lua context can be not initialized. This behavior |
| 6409 | * permits to save performances because a systematic |
| 6410 | * Lua initialization cause 5% performances loss. |
| 6411 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6412 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6413 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6414 | if (!stream->hlua) { |
| 6415 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6416 | return 0; |
| 6417 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6418 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6419 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6420 | return 0; |
| 6421 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6422 | } |
| 6423 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6424 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6425 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6426 | |
| 6427 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6428 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6429 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6430 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6431 | else |
| 6432 | error = "critical error"; |
| 6433 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6434 | return 0; |
| 6435 | } |
| 6436 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6437 | /* Check stack available size. */ |
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 | } |
| 6443 | |
| 6444 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6445 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6446 | |
| 6447 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6448 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6449 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6450 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6451 | return 0; |
| 6452 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6453 | hlua_smp2lua(stream->hlua->T, smp); |
| 6454 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6455 | |
| 6456 | /* push keywords in the stack. */ |
| 6457 | if (arg_p) { |
| 6458 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6459 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6460 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6461 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6462 | return 0; |
| 6463 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6464 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6465 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6466 | } |
| 6467 | } |
| 6468 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6469 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6470 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6471 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6472 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6473 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6474 | } |
| 6475 | |
| 6476 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6477 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6478 | /* finished. */ |
| 6479 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6480 | /* If the stack is empty, the function fails. */ |
| 6481 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6482 | return 0; |
| 6483 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6484 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6485 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6486 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6487 | return 1; |
| 6488 | |
| 6489 | /* yield. */ |
| 6490 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6491 | 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] | 6492 | return 0; |
| 6493 | |
| 6494 | /* finished with error. */ |
| 6495 | case HLUA_E_ERRMSG: |
| 6496 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6497 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6498 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6499 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6500 | return 0; |
| 6501 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6502 | case HLUA_E_ETMOUT: |
| 6503 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6504 | return 0; |
| 6505 | |
| 6506 | case HLUA_E_NOMEM: |
| 6507 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6508 | return 0; |
| 6509 | |
| 6510 | case HLUA_E_YIELD: |
| 6511 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6512 | return 0; |
| 6513 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6514 | case HLUA_E_ERR: |
| 6515 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6516 | 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] | 6517 | |
| 6518 | default: |
| 6519 | return 0; |
| 6520 | } |
| 6521 | } |
| 6522 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6523 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6524 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6525 | * resume sample-fetches. This function will be called by the sample |
| 6526 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6527 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6528 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6529 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6530 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6531 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6532 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6533 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6534 | const struct buffer msg = { }; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6535 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6536 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6537 | if (!stream) |
| 6538 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6539 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6540 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6541 | * Lua context can be not initialized. This behavior |
| 6542 | * permits to save performances because a systematic |
| 6543 | * Lua initialization cause 5% performances loss. |
| 6544 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6545 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6546 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6547 | if (!stream->hlua) { |
| 6548 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6549 | return 0; |
| 6550 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6551 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6552 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6553 | return 0; |
| 6554 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6555 | } |
| 6556 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6557 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6558 | |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6559 | if (stream->be->mode == PR_MODE_HTTP) { |
| 6560 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 6561 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6562 | else |
| 6563 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6564 | } |
| 6565 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6566 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6567 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6568 | |
| 6569 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6570 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6571 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6572 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6573 | else |
| 6574 | error = "critical error"; |
| 6575 | 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] | 6576 | return 0; |
| 6577 | } |
| 6578 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6579 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6580 | if (!lua_checkstack(stream->hlua->T, 2)) { |
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 | } |
| 6585 | |
| 6586 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6587 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6588 | |
| 6589 | /* push arguments in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6590 | 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] | 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 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6596 | |
| 6597 | /* push keywords in the stack. */ |
| 6598 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6599 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6600 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6601 | 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] | 6602 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6603 | return 0; |
| 6604 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6605 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6606 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6607 | } |
| 6608 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6609 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6611 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6612 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6613 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6614 | } |
| 6615 | |
| 6616 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6617 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6618 | /* finished. */ |
| 6619 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6620 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6621 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6622 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6623 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6624 | /* If the stack is empty, the function fails. */ |
| 6625 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6626 | return 0; |
| 6627 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6628 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6629 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6630 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6631 | |
| 6632 | /* Set the end of execution flag. */ |
| 6633 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6634 | return 1; |
| 6635 | |
| 6636 | /* yield. */ |
| 6637 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6638 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6639 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6640 | 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] | 6641 | return 0; |
| 6642 | |
| 6643 | /* finished with error. */ |
| 6644 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6645 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6646 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6647 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6648 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6649 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6650 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6651 | return 0; |
| 6652 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6653 | case HLUA_E_ETMOUT: |
| 6654 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6655 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6656 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6657 | return 0; |
| 6658 | |
| 6659 | case HLUA_E_NOMEM: |
| 6660 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6661 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6662 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6663 | return 0; |
| 6664 | |
| 6665 | case HLUA_E_YIELD: |
| 6666 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6667 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6668 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6669 | return 0; |
| 6670 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6671 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6672 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6673 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6674 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6675 | 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] | 6676 | |
| 6677 | default: |
| 6678 | return 0; |
| 6679 | } |
| 6680 | } |
| 6681 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6682 | /* This function is an LUA binding used for registering |
| 6683 | * "sample-conv" functions. It expects a converter name used |
| 6684 | * in the haproxy configuration file, and an LUA function. |
| 6685 | */ |
| 6686 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6687 | { |
| 6688 | struct sample_conv_kw_list *sck; |
| 6689 | const char *name; |
| 6690 | int ref; |
| 6691 | int len; |
| 6692 | struct hlua_function *fcn; |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6693 | struct sample_conv *sc; |
| 6694 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6695 | |
| 6696 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6697 | |
| 6698 | /* First argument : converter name. */ |
| 6699 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6700 | |
| 6701 | /* Second argument : lua function. */ |
| 6702 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6703 | |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6704 | /* Check if the converter is already registered */ |
| 6705 | trash = get_trash_chunk(); |
| 6706 | chunk_printf(trash, "lua.%s", name); |
| 6707 | sc = find_sample_conv(trash->area, trash->data); |
| 6708 | if (sc != NULL) { |
| 6709 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6710 | "This will become a hard error in version 2.5.\n", name); |
| 6711 | } |
| 6712 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6713 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6714 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6715 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6716 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6717 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6718 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6719 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6720 | |
| 6721 | /* Fill fcn. */ |
| 6722 | fcn->name = strdup(name); |
| 6723 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6724 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6725 | fcn->function_ref = ref; |
| 6726 | |
| 6727 | /* List head */ |
| 6728 | sck->list.n = sck->list.p = NULL; |
| 6729 | |
| 6730 | /* converter keyword. */ |
| 6731 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6732 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6733 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6734 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6735 | |
| 6736 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6737 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6738 | 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] | 6739 | sck->kw[0].val_args = NULL; |
| 6740 | sck->kw[0].in_type = SMP_T_STR; |
| 6741 | sck->kw[0].out_type = SMP_T_STR; |
| 6742 | sck->kw[0].private = fcn; |
| 6743 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6744 | /* Register this new converter */ |
| 6745 | sample_register_convs(sck); |
| 6746 | |
| 6747 | return 0; |
| 6748 | } |
| 6749 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6750 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6751 | * "sample-fetch" functions. It expects a converter name used |
| 6752 | * in the haproxy configuration file, and an LUA function. |
| 6753 | */ |
| 6754 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6755 | { |
| 6756 | const char *name; |
| 6757 | int ref; |
| 6758 | int len; |
| 6759 | struct sample_fetch_kw_list *sfk; |
| 6760 | struct hlua_function *fcn; |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6761 | struct sample_fetch *sf; |
| 6762 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6763 | |
| 6764 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6765 | |
| 6766 | /* First argument : sample-fetch name. */ |
| 6767 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6768 | |
| 6769 | /* Second argument : lua function. */ |
| 6770 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6771 | |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6772 | /* Check if the sample-fetch is already registered */ |
| 6773 | trash = get_trash_chunk(); |
| 6774 | chunk_printf(trash, "lua.%s", name); |
| 6775 | sf = find_sample_fetch(trash->area, trash->data); |
| 6776 | if (sf != NULL) { |
| 6777 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6778 | "This will become a hard error in version 2.5.\n", name); |
| 6779 | } |
| 6780 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6781 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6782 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6783 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6784 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6785 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6786 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6787 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6788 | |
| 6789 | /* Fill fcn. */ |
| 6790 | fcn->name = strdup(name); |
| 6791 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6792 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6793 | fcn->function_ref = ref; |
| 6794 | |
| 6795 | /* List head */ |
| 6796 | sfk->list.n = sfk->list.p = NULL; |
| 6797 | |
| 6798 | /* sample-fetch keyword. */ |
| 6799 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6800 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6801 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6802 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6803 | |
| 6804 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6805 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6806 | 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] | 6807 | sfk->kw[0].val_args = NULL; |
| 6808 | sfk->kw[0].out_type = SMP_T_STR; |
| 6809 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6810 | sfk->kw[0].val = 0; |
| 6811 | sfk->kw[0].private = fcn; |
| 6812 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6813 | /* Register this new fetch. */ |
| 6814 | sample_register_fetches(sfk); |
| 6815 | |
| 6816 | return 0; |
| 6817 | } |
| 6818 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6819 | /* This function is a wrapper to execute each LUA function declared |
| 6820 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6821 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6822 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6823 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6824 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6825 | 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] | 6826 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6827 | { |
| 6828 | char **arg; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6829 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6830 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6831 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6832 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6833 | |
| 6834 | switch (rule->from) { |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6835 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6836 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6837 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6838 | 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] | 6839 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6840 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6841 | return ACT_RET_CONT; |
| 6842 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6843 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6844 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6845 | * Lua context can be not initialized. This behavior |
| 6846 | * permits to save performances because a systematic |
| 6847 | * Lua initialization cause 5% performances loss. |
| 6848 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6849 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6850 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6851 | if (!s->hlua) { |
| 6852 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6853 | rule->arg.hlua_rule->fcn.name); |
| 6854 | return ACT_RET_CONT; |
| 6855 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6856 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6857 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6858 | rule->arg.hlua_rule->fcn.name); |
| 6859 | return ACT_RET_CONT; |
| 6860 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6861 | } |
| 6862 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6863 | consistency_set(s, dir, &s->hlua->cons); |
| 6864 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6865 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6866 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6867 | |
| 6868 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6869 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6870 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6871 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6872 | else |
| 6873 | error = "critical error"; |
| 6874 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6875 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6876 | return ACT_RET_CONT; |
| 6877 | } |
| 6878 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6879 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6880 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6881 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6882 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6883 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6884 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6885 | } |
| 6886 | |
| 6887 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6888 | 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] | 6889 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6890 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6891 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6892 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6893 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6894 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6895 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6896 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6897 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6898 | |
| 6899 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6900 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6901 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6902 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6903 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6904 | RESET_SAFE_LJMP(s->hlua->T); |
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 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6907 | lua_pushstring(s->hlua->T, *arg); |
| 6908 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6909 | } |
| 6910 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6911 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6912 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6913 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6914 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6915 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6916 | } |
| 6917 | |
Christopher Faulet | 719ddc8 | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6918 | /* Always reset the analyse expiration timeout for the corresponding |
| 6919 | * channel in case the lua script yield, to be sure to not keep an |
| 6920 | * expired timeout. |
| 6921 | */ |
| 6922 | if (dir == SMP_OPT_DIR_REQ) |
| 6923 | s->req.analyse_exp = TICK_ETERNITY; |
| 6924 | else |
| 6925 | s->res.analyse_exp = TICK_ETERNITY; |
| 6926 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6927 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6928 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6929 | /* finished. */ |
| 6930 | case HLUA_E_OK: |
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 | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6935 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6936 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6937 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6938 | |
| 6939 | /* yield. */ |
| 6940 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6941 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6942 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | ea0b0e2 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6943 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6944 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6945 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6946 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6947 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6948 | /* Some actions can be wake up when a "write" event |
| 6949 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6950 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6951 | */ |
Christopher Faulet | b22f650 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6952 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6953 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6954 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6955 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6956 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6957 | * because HAProxy is not able to manipulate data, it |
| 6958 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6959 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6960 | |
| 6961 | /* finished with error. */ |
| 6962 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6963 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6964 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6965 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6966 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6967 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6968 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6969 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6970 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6971 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6972 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6973 | case HLUA_E_ETMOUT: |
| 6974 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6975 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6976 | return ACT_RET_ERR; |
| 6977 | } |
| 6978 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6979 | return 0; |
| 6980 | |
| 6981 | case HLUA_E_NOMEM: |
| 6982 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6983 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6984 | return ACT_RET_ERR; |
| 6985 | } |
| 6986 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6987 | return 0; |
| 6988 | |
| 6989 | case HLUA_E_YIELD: |
| 6990 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6991 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6992 | return ACT_RET_ERR; |
| 6993 | } |
| 6994 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6995 | rule->arg.hlua_rule->fcn.name); |
| 6996 | return 0; |
| 6997 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6998 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6999 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 7000 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 7001 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 7002 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7003 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7004 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7005 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7006 | |
| 7007 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 7008 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7009 | } |
| 7010 | } |
| 7011 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7012 | 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] | 7013 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7014 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7015 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7016 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7017 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7018 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7019 | } |
| 7020 | |
| 7021 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7022 | { |
| 7023 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7024 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7025 | struct task *task; |
| 7026 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7027 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7028 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7029 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7030 | if (!hlua) { |
| 7031 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 7032 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7033 | return 0; |
| 7034 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7035 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7036 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7037 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7038 | |
| 7039 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7040 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7041 | if (!task) { |
| 7042 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 7043 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7044 | return 0; |
| 7045 | } |
| 7046 | task->nice = 0; |
| 7047 | task->context = ctx; |
| 7048 | task->process = hlua_applet_wakeup; |
| 7049 | ctx->ctx.hlua_apptcp.task = task; |
| 7050 | |
| 7051 | /* In the execution wrappers linked with a stream, the |
| 7052 | * Lua context can be not initialized. This behavior |
| 7053 | * permits to save performances because a systematic |
| 7054 | * Lua initialization cause 5% performances loss. |
| 7055 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7056 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7057 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 7058 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7059 | return 0; |
| 7060 | } |
| 7061 | |
| 7062 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7063 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7064 | |
| 7065 | /* The following Lua calls can fail. */ |
| 7066 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7067 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7068 | error = lua_tostring(hlua->T, -1); |
| 7069 | else |
| 7070 | error = "critical error"; |
| 7071 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7072 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7073 | return 0; |
| 7074 | } |
| 7075 | |
| 7076 | /* Check stack available size. */ |
| 7077 | if (!lua_checkstack(hlua->T, 1)) { |
| 7078 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7079 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7080 | RESET_SAFE_LJMP(hlua->T); |
| 7081 | return 0; |
| 7082 | } |
| 7083 | |
| 7084 | /* Restore the function in the stack. */ |
| 7085 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7086 | |
| 7087 | /* Create and and push object stream in the stack. */ |
| 7088 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7089 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7090 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7091 | RESET_SAFE_LJMP(hlua->T); |
| 7092 | return 0; |
| 7093 | } |
| 7094 | hlua->nargs = 1; |
| 7095 | |
| 7096 | /* push keywords in the stack. */ |
| 7097 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7098 | if (!lua_checkstack(hlua->T, 1)) { |
| 7099 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7100 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7101 | RESET_SAFE_LJMP(hlua->T); |
| 7102 | return 0; |
| 7103 | } |
| 7104 | lua_pushstring(hlua->T, *arg); |
| 7105 | hlua->nargs++; |
| 7106 | } |
| 7107 | |
| 7108 | RESET_SAFE_LJMP(hlua->T); |
| 7109 | |
| 7110 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7111 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7112 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7113 | |
| 7114 | return 1; |
| 7115 | } |
| 7116 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7117 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7118 | { |
| 7119 | struct stream_interface *si = ctx->owner; |
| 7120 | struct stream *strm = si_strm(si); |
| 7121 | struct channel *res = si_ic(si); |
| 7122 | struct act_rule *rule = ctx->rule; |
| 7123 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7124 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7125 | |
| 7126 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7127 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7128 | /* eat the whole request */ |
| 7129 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7130 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7131 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7132 | |
| 7133 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7134 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7135 | return; |
| 7136 | |
| 7137 | /* Execute the function. */ |
| 7138 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7139 | /* finished. */ |
| 7140 | case HLUA_E_OK: |
| 7141 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7142 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7143 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7144 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7145 | res->flags |= CF_READ_NULL; |
| 7146 | si_shutr(si); |
| 7147 | return; |
| 7148 | |
| 7149 | /* yield. */ |
| 7150 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7151 | if (hlua->wake_time != TICK_ETERNITY) |
| 7152 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7153 | return; |
| 7154 | |
| 7155 | /* finished with error. */ |
| 7156 | case HLUA_E_ERRMSG: |
| 7157 | /* Display log. */ |
| 7158 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7159 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7160 | lua_pop(hlua->T, 1); |
| 7161 | goto error; |
| 7162 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7163 | case HLUA_E_ETMOUT: |
| 7164 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7165 | rule->arg.hlua_rule->fcn.name); |
| 7166 | goto error; |
| 7167 | |
| 7168 | case HLUA_E_NOMEM: |
| 7169 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7170 | rule->arg.hlua_rule->fcn.name); |
| 7171 | goto error; |
| 7172 | |
| 7173 | case HLUA_E_YIELD: /* unexpected */ |
| 7174 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7175 | rule->arg.hlua_rule->fcn.name); |
| 7176 | goto error; |
| 7177 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7178 | case HLUA_E_ERR: |
| 7179 | /* Display log. */ |
| 7180 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7181 | rule->arg.hlua_rule->fcn.name); |
| 7182 | goto error; |
| 7183 | |
| 7184 | default: |
| 7185 | goto error; |
| 7186 | } |
| 7187 | |
| 7188 | error: |
| 7189 | |
| 7190 | /* For all other cases, just close the stream. */ |
| 7191 | si_shutw(si); |
| 7192 | si_shutr(si); |
| 7193 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7194 | } |
| 7195 | |
| 7196 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7197 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7198 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7199 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7200 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7201 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7202 | } |
| 7203 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7204 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7205 | * an errors occurs and -1 if more data are required for initializing |
| 7206 | * the applet. |
| 7207 | */ |
| 7208 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7209 | { |
| 7210 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7211 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7212 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7213 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7214 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7215 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7216 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7217 | txn = strm->txn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7218 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7219 | /* We want two things in HTTP mode : |
| 7220 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7221 | * applet is released after each response ; |
| 7222 | * - enable request body transfer to the applet in order to resync |
| 7223 | * with the response body. |
| 7224 | */ |
| 7225 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7226 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7227 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7228 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7229 | if (!hlua) { |
| 7230 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7231 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7232 | return 0; |
| 7233 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7234 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7235 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7236 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7237 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7238 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7239 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7240 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7241 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7242 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7243 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7244 | if (!task) { |
| 7245 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7246 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7247 | return 0; |
| 7248 | } |
| 7249 | task->nice = 0; |
| 7250 | task->context = ctx; |
| 7251 | task->process = hlua_applet_wakeup; |
| 7252 | ctx->ctx.hlua_apphttp.task = task; |
| 7253 | |
| 7254 | /* In the execution wrappers linked with a stream, the |
| 7255 | * Lua context can be not initialized. This behavior |
| 7256 | * permits to save performances because a systematic |
| 7257 | * Lua initialization cause 5% performances loss. |
| 7258 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7259 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7260 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7261 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7262 | return 0; |
| 7263 | } |
| 7264 | |
| 7265 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7266 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7267 | |
| 7268 | /* The following Lua calls can fail. */ |
| 7269 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7270 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7271 | error = lua_tostring(hlua->T, -1); |
| 7272 | else |
| 7273 | error = "critical error"; |
| 7274 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7275 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7276 | return 0; |
| 7277 | } |
| 7278 | |
| 7279 | /* Check stack available size. */ |
| 7280 | if (!lua_checkstack(hlua->T, 1)) { |
| 7281 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7282 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7283 | RESET_SAFE_LJMP(hlua->T); |
| 7284 | return 0; |
| 7285 | } |
| 7286 | |
| 7287 | /* Restore the function in the stack. */ |
| 7288 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7289 | |
| 7290 | /* Create and and push object stream in the stack. */ |
| 7291 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7292 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7293 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7294 | RESET_SAFE_LJMP(hlua->T); |
| 7295 | return 0; |
| 7296 | } |
| 7297 | hlua->nargs = 1; |
| 7298 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7299 | /* push keywords in the stack. */ |
| 7300 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7301 | if (!lua_checkstack(hlua->T, 1)) { |
| 7302 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7303 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7304 | RESET_SAFE_LJMP(hlua->T); |
| 7305 | return 0; |
| 7306 | } |
| 7307 | lua_pushstring(hlua->T, *arg); |
| 7308 | hlua->nargs++; |
| 7309 | } |
| 7310 | |
| 7311 | RESET_SAFE_LJMP(hlua->T); |
| 7312 | |
| 7313 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7314 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7315 | |
| 7316 | return 1; |
| 7317 | } |
| 7318 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7319 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7320 | { |
| 7321 | struct stream_interface *si = ctx->owner; |
| 7322 | struct stream *strm = si_strm(si); |
| 7323 | struct channel *req = si_oc(si); |
| 7324 | struct channel *res = si_ic(si); |
| 7325 | struct act_rule *rule = ctx->rule; |
| 7326 | struct proxy *px = strm->be; |
| 7327 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7328 | struct htx *req_htx, *res_htx; |
| 7329 | |
| 7330 | res_htx = htx_from_buf(&res->buf); |
| 7331 | |
| 7332 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7333 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7334 | goto out; |
| 7335 | |
| 7336 | /* Check if the input buffer is avalaible. */ |
| 7337 | if (!b_size(&res->buf)) { |
| 7338 | si_rx_room_blk(si); |
| 7339 | goto out; |
| 7340 | } |
| 7341 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7342 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7343 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7344 | |
| 7345 | /* Set the currently running flag. */ |
| 7346 | if (!HLUA_IS_RUNNING(hlua) && |
| 7347 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7348 | struct htx_blk *blk; |
| 7349 | size_t count = co_data(req); |
| 7350 | |
| 7351 | if (!count) { |
| 7352 | si_cant_get(si); |
| 7353 | goto out; |
| 7354 | } |
| 7355 | |
| 7356 | /* We need to flush the request header. This left the body for |
| 7357 | * the Lua. |
| 7358 | */ |
| 7359 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7360 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7361 | while (count && blk) { |
| 7362 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7363 | uint32_t sz = htx_get_blksz(blk); |
| 7364 | |
| 7365 | if (sz > count) { |
| 7366 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7367 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7368 | goto out; |
| 7369 | } |
| 7370 | |
| 7371 | count -= sz; |
| 7372 | co_set_data(req, co_data(req) - sz); |
| 7373 | blk = htx_remove_blk(req_htx, blk); |
| 7374 | |
| 7375 | if (type == HTX_BLK_EOH) |
| 7376 | break; |
| 7377 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7378 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7379 | } |
| 7380 | |
| 7381 | /* Executes The applet if it is not done. */ |
| 7382 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7383 | |
| 7384 | /* Execute the function. */ |
| 7385 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7386 | /* finished. */ |
| 7387 | case HLUA_E_OK: |
| 7388 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7389 | break; |
| 7390 | |
| 7391 | /* yield. */ |
| 7392 | case HLUA_E_AGAIN: |
| 7393 | if (hlua->wake_time != TICK_ETERNITY) |
| 7394 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7395 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7396 | |
| 7397 | /* finished with error. */ |
| 7398 | case HLUA_E_ERRMSG: |
| 7399 | /* Display log. */ |
| 7400 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7401 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7402 | lua_pop(hlua->T, 1); |
| 7403 | goto error; |
| 7404 | |
| 7405 | case HLUA_E_ETMOUT: |
| 7406 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7407 | rule->arg.hlua_rule->fcn.name); |
| 7408 | goto error; |
| 7409 | |
| 7410 | case HLUA_E_NOMEM: |
| 7411 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7412 | rule->arg.hlua_rule->fcn.name); |
| 7413 | goto error; |
| 7414 | |
| 7415 | case HLUA_E_YIELD: /* unexpected */ |
| 7416 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7417 | rule->arg.hlua_rule->fcn.name); |
| 7418 | goto error; |
| 7419 | |
| 7420 | case HLUA_E_ERR: |
| 7421 | /* Display log. */ |
| 7422 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7423 | rule->arg.hlua_rule->fcn.name); |
| 7424 | goto error; |
| 7425 | |
| 7426 | default: |
| 7427 | goto error; |
| 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7432 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7433 | goto done; |
| 7434 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7435 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7436 | goto error; |
| 7437 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7438 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7439 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7440 | si_rx_room_blk(si); |
| 7441 | goto out; |
| 7442 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7443 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7444 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7445 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7446 | } |
| 7447 | |
| 7448 | done: |
| 7449 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7450 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7451 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7452 | si_shutr(si); |
| 7453 | } |
| 7454 | |
| 7455 | /* eat the whole request */ |
| 7456 | if (co_data(req)) { |
| 7457 | req_htx = htx_from_buf(&req->buf); |
| 7458 | co_htx_skip(req, req_htx, co_data(req)); |
| 7459 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7460 | } |
| 7461 | } |
| 7462 | |
| 7463 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7464 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7465 | return; |
| 7466 | |
| 7467 | error: |
| 7468 | |
| 7469 | /* If we are in HTTP mode, and we are not send any |
| 7470 | * data, return a 500 server error in best effort: |
| 7471 | * if there is no room available in the buffer, |
| 7472 | * just close the connection. |
| 7473 | */ |
| 7474 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7475 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7476 | |
| 7477 | channel_erase(res); |
| 7478 | res->buf.data = b_data(err); |
| 7479 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7480 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7481 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7482 | } |
| 7483 | if (!(strm->flags & SF_ERR_MASK)) |
| 7484 | strm->flags |= SF_ERR_RESOURCE; |
| 7485 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7486 | goto done; |
| 7487 | } |
| 7488 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7489 | void hlua_applet_http_fct(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7490 | { |
| 7491 | struct stream_interface *si = ctx->owner; |
| 7492 | struct stream *strm = si_strm(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7493 | struct channel *req = si_oc(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7494 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7495 | struct act_rule *rule = ctx->rule; |
| 7496 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7497 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7498 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7499 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7500 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7501 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7502 | int ret; |
| 7503 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7504 | if (IS_HTX_STRM(strm)) |
| 7505 | return hlua_applet_htx_fct(ctx); |
| 7506 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7507 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7508 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7509 | goto out; |
| 7510 | |
| 7511 | /* Check if the input buffer is avalaible. */ |
| 7512 | if (!b_size(&res->buf)) { |
| 7513 | si_rx_room_blk(si); |
| 7514 | goto out; |
| 7515 | } |
| 7516 | /* check that the output is not closed */ |
| 7517 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 7518 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7519 | |
| 7520 | /* Set the currently running flag. */ |
| 7521 | if (!HLUA_IS_RUNNING(hlua) && |
| 7522 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7523 | /* Store the max amount of bytes that we can read. */ |
| 7524 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7525 | |
| 7526 | /* We need to flush the request header. This left the body |
| 7527 | * for the Lua. |
| 7528 | */ |
| 7529 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7530 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7531 | ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7532 | if (ret == -1) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7533 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7534 | |
| 7535 | /* No data available, ask for more data. */ |
| 7536 | if (ret == 1) |
| 7537 | len2 = 0; |
| 7538 | if (ret == 0) |
| 7539 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7540 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7541 | si_cant_get(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7542 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7543 | } |
| 7544 | |
| 7545 | /* skip the requests bytes. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7546 | co_skip(req, strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7547 | } |
| 7548 | |
| 7549 | /* Executes The applet if it is not done. */ |
| 7550 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7551 | |
| 7552 | /* Execute the function. */ |
| 7553 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7554 | /* finished. */ |
| 7555 | case HLUA_E_OK: |
| 7556 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7557 | break; |
| 7558 | |
| 7559 | /* yield. */ |
| 7560 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7561 | if (hlua->wake_time != TICK_ETERNITY) |
| 7562 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7563 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7564 | |
| 7565 | /* finished with error. */ |
| 7566 | case HLUA_E_ERRMSG: |
| 7567 | /* Display log. */ |
| 7568 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7569 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7570 | lua_pop(hlua->T, 1); |
| 7571 | goto error; |
| 7572 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7573 | case HLUA_E_ETMOUT: |
| 7574 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7575 | rule->arg.hlua_rule->fcn.name); |
| 7576 | goto error; |
| 7577 | |
| 7578 | case HLUA_E_NOMEM: |
| 7579 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7580 | rule->arg.hlua_rule->fcn.name); |
| 7581 | goto error; |
| 7582 | |
| 7583 | case HLUA_E_YIELD: /* unexpected */ |
| 7584 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7585 | rule->arg.hlua_rule->fcn.name); |
| 7586 | goto error; |
| 7587 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7588 | case HLUA_E_ERR: |
| 7589 | /* Display log. */ |
| 7590 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7591 | rule->arg.hlua_rule->fcn.name); |
| 7592 | goto error; |
| 7593 | |
| 7594 | default: |
| 7595 | goto error; |
| 7596 | } |
| 7597 | } |
| 7598 | |
| 7599 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7600 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7601 | goto done; |
| 7602 | |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7603 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7604 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7605 | |
| 7606 | /* We must send the final chunk. */ |
| 7607 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7608 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7609 | |
| 7610 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7611 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7612 | |
| 7613 | /* critical error. */ |
| 7614 | if (ret == -2 || ret == -3) { |
| 7615 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7616 | rule->arg.hlua_rule->fcn.name); |
| 7617 | goto error; |
| 7618 | } |
| 7619 | |
| 7620 | /* no enough space error. */ |
| 7621 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7622 | si_rx_room_blk(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7623 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7624 | } |
| 7625 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7626 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7627 | ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7628 | } |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7629 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7630 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7631 | done: |
| 7632 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7633 | if (!(res->flags & CF_SHUTR)) { |
| 7634 | res->flags |= CF_READ_NULL; |
| 7635 | si_shutr(si); |
| 7636 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7637 | |
| 7638 | /* eat the whole request */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7639 | if (co_data(req)) |
| 7640 | co_skip(req, co_data(req)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7641 | } |
| 7642 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7643 | out: |
| 7644 | return; |
| 7645 | |
| 7646 | error: |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7647 | |
| 7648 | /* If we are in HTTP mode, and we are not send any |
| 7649 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7650 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7651 | * just close the connection. |
| 7652 | */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7653 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7654 | channel_erase(res); |
| 7655 | ci_putblk(res, error_500, strlen(error_500)); |
| 7656 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7657 | if (!(strm->flags & SF_ERR_MASK)) |
| 7658 | strm->flags |= SF_ERR_RESOURCE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7659 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7660 | goto done; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7661 | } |
| 7662 | |
| 7663 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7664 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7665 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7666 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7667 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7668 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7669 | } |
| 7670 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7671 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7672 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7673 | * |
| 7674 | * This function can fail with an abort() due to an Lua critical error. |
| 7675 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7676 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7677 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7678 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7679 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7680 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7681 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7682 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7683 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7684 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7685 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7686 | if (!rule->arg.hlua_rule) { |
| 7687 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7688 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7689 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7690 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7691 | /* Memory for arguments. */ |
| 7692 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7693 | if (!rule->arg.hlua_rule->args) { |
| 7694 | memprintf(err, "out of memory error"); |
| 7695 | return ACT_RET_PRS_ERR; |
| 7696 | } |
| 7697 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7698 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7699 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7700 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7701 | /* Expect some arguments */ |
| 7702 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7703 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7704 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7705 | return ACT_RET_PRS_ERR; |
| 7706 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7707 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7708 | if (!rule->arg.hlua_rule->args[i]) { |
| 7709 | memprintf(err, "out of memory error"); |
| 7710 | return ACT_RET_PRS_ERR; |
| 7711 | } |
| 7712 | (*cur_arg)++; |
| 7713 | } |
| 7714 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7715 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7716 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7717 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7718 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7719 | } |
| 7720 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7721 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7722 | struct act_rule *rule, char **err) |
| 7723 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7724 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7725 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7726 | /* HTTP applets are forbidden in tcp-request rules. |
| 7727 | * HTTP applet request requires everything initilized by |
| 7728 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7729 | * The applet will be immediately initilized, but its before |
| 7730 | * the call of this analyzer. |
| 7731 | */ |
| 7732 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7733 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7734 | return ACT_RET_PRS_ERR; |
| 7735 | } |
| 7736 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7737 | /* Memory for the rule. */ |
| 7738 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7739 | if (!rule->arg.hlua_rule) { |
| 7740 | memprintf(err, "out of memory error"); |
| 7741 | return ACT_RET_PRS_ERR; |
| 7742 | } |
| 7743 | |
| 7744 | /* Reference the Lua function and store the reference. */ |
| 7745 | rule->arg.hlua_rule->fcn = *fcn; |
| 7746 | |
| 7747 | /* TODO: later accept arguments. */ |
| 7748 | rule->arg.hlua_rule->args = NULL; |
| 7749 | |
| 7750 | /* Add applet pointer in the rule. */ |
| 7751 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7752 | rule->applet.name = fcn->name; |
| 7753 | rule->applet.init = hlua_applet_http_init; |
| 7754 | rule->applet.fct = hlua_applet_http_fct; |
| 7755 | rule->applet.release = hlua_applet_http_release; |
| 7756 | rule->applet.timeout = hlua_timeout_applet; |
| 7757 | |
| 7758 | return ACT_RET_PRS_OK; |
| 7759 | } |
| 7760 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7761 | /* This function is an LUA binding used for registering |
| 7762 | * "sample-conv" functions. It expects a converter name used |
| 7763 | * in the haproxy configuration file, and an LUA function. |
| 7764 | */ |
| 7765 | __LJMP static int hlua_register_action(lua_State *L) |
| 7766 | { |
| 7767 | struct action_kw_list *akl; |
| 7768 | const char *name; |
| 7769 | int ref; |
| 7770 | int len; |
| 7771 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7772 | int nargs; |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7773 | struct buffer *trash; |
| 7774 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7775 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7776 | /* Initialise the number of expected arguments at 0. */ |
| 7777 | nargs = 0; |
| 7778 | |
| 7779 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7780 | 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] | 7781 | |
| 7782 | /* First argument : converter name. */ |
| 7783 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7784 | |
| 7785 | /* Second argument : environment. */ |
| 7786 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7787 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7788 | |
| 7789 | /* Third argument : lua function. */ |
| 7790 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7791 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7792 | /* 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] | 7793 | if (lua_gettop(L) >= 4) |
| 7794 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7795 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7796 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7797 | lua_pushnil(L); |
| 7798 | while (lua_next(L, 2) != 0) { |
| 7799 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7800 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7801 | |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7802 | /* Check if action exists */ |
| 7803 | trash = get_trash_chunk(); |
| 7804 | chunk_printf(trash, "lua.%s", name); |
| 7805 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7806 | akw = tcp_req_cont_action(trash->area); |
| 7807 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7808 | akw = tcp_res_cont_action(trash->area); |
| 7809 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7810 | akw = action_http_req_custom(trash->area); |
| 7811 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7812 | akw = action_http_res_custom(trash->area); |
| 7813 | } else { |
| 7814 | akw = NULL; |
| 7815 | } |
| 7816 | if (akw != NULL) { |
| 7817 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7818 | "This will become a hard error in version 2.5.\n", name); |
| 7819 | } |
| 7820 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7821 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7822 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7823 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7824 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7825 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7826 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7827 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7828 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7829 | |
| 7830 | /* Fill fcn. */ |
| 7831 | fcn->name = strdup(name); |
| 7832 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7833 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7834 | fcn->function_ref = ref; |
| 7835 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7836 | /* Set the expected number od arguments. */ |
| 7837 | fcn->nargs = nargs; |
| 7838 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7839 | /* List head */ |
| 7840 | akl->list.n = akl->list.p = NULL; |
| 7841 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7842 | /* action keyword. */ |
| 7843 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7844 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7845 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7846 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7847 | |
| 7848 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7849 | |
| 7850 | akl->kw[0].match_pfx = 0; |
| 7851 | akl->kw[0].private = fcn; |
| 7852 | akl->kw[0].parse = action_register_lua; |
| 7853 | |
| 7854 | /* select the action registering point. */ |
| 7855 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7856 | tcp_req_cont_keywords_register(akl); |
| 7857 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7858 | tcp_res_cont_keywords_register(akl); |
| 7859 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7860 | http_req_keywords_register(akl); |
| 7861 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7862 | http_res_keywords_register(akl); |
| 7863 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7864 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7865 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7866 | "are expected.", lua_tostring(L, -1))); |
| 7867 | |
| 7868 | /* pop the environment string. */ |
| 7869 | lua_pop(L, 1); |
| 7870 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7871 | return ACT_RET_PRS_OK; |
| 7872 | } |
| 7873 | |
| 7874 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7875 | struct act_rule *rule, char **err) |
| 7876 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7877 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7878 | |
Christopher Faulet | eb70980 | 2019-04-11 22:04:08 +0200 | [diff] [blame] | 7879 | if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) { |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7880 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7881 | return ACT_RET_PRS_ERR; |
| 7882 | } |
| 7883 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7884 | /* Memory for the rule. */ |
| 7885 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7886 | if (!rule->arg.hlua_rule) { |
| 7887 | memprintf(err, "out of memory error"); |
| 7888 | return ACT_RET_PRS_ERR; |
| 7889 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7890 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7891 | /* Reference the Lua function and store the reference. */ |
| 7892 | rule->arg.hlua_rule->fcn = *fcn; |
| 7893 | |
| 7894 | /* TODO: later accept arguments. */ |
| 7895 | rule->arg.hlua_rule->args = NULL; |
| 7896 | |
| 7897 | /* Add applet pointer in the rule. */ |
| 7898 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7899 | rule->applet.name = fcn->name; |
| 7900 | rule->applet.init = hlua_applet_tcp_init; |
| 7901 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7902 | rule->applet.release = hlua_applet_tcp_release; |
| 7903 | rule->applet.timeout = hlua_timeout_applet; |
| 7904 | |
| 7905 | return 0; |
| 7906 | } |
| 7907 | |
| 7908 | /* This function is an LUA binding used for registering |
| 7909 | * "sample-conv" functions. It expects a converter name used |
| 7910 | * in the haproxy configuration file, and an LUA function. |
| 7911 | */ |
| 7912 | __LJMP static int hlua_register_service(lua_State *L) |
| 7913 | { |
| 7914 | struct action_kw_list *akl; |
| 7915 | const char *name; |
| 7916 | const char *env; |
| 7917 | int ref; |
| 7918 | int len; |
| 7919 | struct hlua_function *fcn; |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7920 | struct buffer *trash; |
| 7921 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7922 | |
| 7923 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7924 | |
| 7925 | /* First argument : converter name. */ |
| 7926 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7927 | |
| 7928 | /* Second argument : environment. */ |
| 7929 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7930 | |
| 7931 | /* Third argument : lua function. */ |
| 7932 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7933 | |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7934 | /* Check for service already registered */ |
| 7935 | trash = get_trash_chunk(); |
| 7936 | chunk_printf(trash, "lua.%s", name); |
| 7937 | akw = service_find(trash->area); |
| 7938 | if (akw != NULL) { |
| 7939 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7940 | "This will become a hard error in version 2.5.\n", name); |
| 7941 | } |
| 7942 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7943 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7944 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7945 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7946 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7947 | fcn = calloc(1, sizeof(*fcn)); |
| 7948 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7949 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7950 | |
| 7951 | /* Fill fcn. */ |
| 7952 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7953 | fcn->name = calloc(1, len); |
| 7954 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7955 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7956 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7957 | fcn->function_ref = ref; |
| 7958 | |
| 7959 | /* List head */ |
| 7960 | akl->list.n = akl->list.p = NULL; |
| 7961 | |
| 7962 | /* converter keyword. */ |
| 7963 | len = strlen("lua.") + strlen(name) + 1; |
| 7964 | akl->kw[0].kw = calloc(1, len); |
| 7965 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7966 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7967 | |
| 7968 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7969 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7970 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7971 | if (strcmp(env, "tcp") == 0) |
| 7972 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7973 | else if (strcmp(env, "http") == 0) |
| 7974 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7975 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7976 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7977 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7978 | |
| 7979 | akl->kw[0].match_pfx = 0; |
| 7980 | akl->kw[0].private = fcn; |
| 7981 | |
| 7982 | /* End of array. */ |
| 7983 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7984 | |
| 7985 | /* Register this new converter */ |
| 7986 | service_keywords_register(akl); |
| 7987 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7988 | return 0; |
| 7989 | } |
| 7990 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7991 | /* This function initialises Lua cli handler. It copies the |
| 7992 | * arguments in the Lua stack and create channel IO objects. |
| 7993 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7994 | 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] | 7995 | { |
| 7996 | struct hlua *hlua; |
| 7997 | struct hlua_function *fcn; |
| 7998 | int i; |
| 7999 | const char *error; |
| 8000 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8001 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8002 | appctx->ctx.hlua_cli.fcn = private; |
| 8003 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8004 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8005 | if (!hlua) { |
| 8006 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8007 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8008 | } |
| 8009 | HLUA_INIT(hlua); |
| 8010 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8011 | |
| 8012 | /* Create task used by signal to wakeup applets. |
| 8013 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 8014 | * applet_http. It is absolutely compatible. |
| 8015 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8016 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8017 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 8018 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8019 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8020 | } |
| 8021 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8022 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8023 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8024 | |
| 8025 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 8026 | 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] | 8027 | 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] | 8028 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8029 | } |
| 8030 | |
| 8031 | /* The following Lua calls can fail. */ |
| 8032 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 8033 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8034 | error = lua_tostring(hlua->T, -1); |
| 8035 | else |
| 8036 | error = "critical error"; |
| 8037 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 8038 | goto error; |
| 8039 | } |
| 8040 | |
| 8041 | /* Check stack available size. */ |
| 8042 | if (!lua_checkstack(hlua->T, 2)) { |
| 8043 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8044 | goto error; |
| 8045 | } |
| 8046 | |
| 8047 | /* Restore the function in the stack. */ |
| 8048 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 8049 | |
| 8050 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8051 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8052 | */ |
| 8053 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8054 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8055 | goto error; |
| 8056 | } |
| 8057 | hlua->nargs = 1; |
| 8058 | |
| 8059 | /* push keywords in the stack. */ |
| 8060 | for (i = 0; *args[i]; i++) { |
| 8061 | /* Check stack available size. */ |
| 8062 | if (!lua_checkstack(hlua->T, 1)) { |
| 8063 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8064 | goto error; |
| 8065 | } |
| 8066 | lua_pushstring(hlua->T, args[i]); |
| 8067 | hlua->nargs++; |
| 8068 | } |
| 8069 | |
| 8070 | /* We must initialize the execution timeouts. */ |
| 8071 | hlua->max_time = hlua_timeout_session; |
| 8072 | |
| 8073 | /* At this point the execution is safe. */ |
| 8074 | RESET_SAFE_LJMP(hlua->T); |
| 8075 | |
| 8076 | /* It's ok */ |
| 8077 | return 0; |
| 8078 | |
| 8079 | /* It's not ok. */ |
| 8080 | error: |
| 8081 | RESET_SAFE_LJMP(hlua->T); |
| 8082 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8083 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8084 | return 1; |
| 8085 | } |
| 8086 | |
| 8087 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8088 | { |
| 8089 | struct hlua *hlua; |
| 8090 | struct stream_interface *si; |
| 8091 | struct hlua_function *fcn; |
| 8092 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8093 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8094 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8095 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8096 | |
| 8097 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8098 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8099 | return 1; |
| 8100 | |
| 8101 | /* Execute the function. */ |
| 8102 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8103 | |
| 8104 | /* finished. */ |
| 8105 | case HLUA_E_OK: |
| 8106 | return 1; |
| 8107 | |
| 8108 | /* yield. */ |
| 8109 | case HLUA_E_AGAIN: |
| 8110 | /* We want write. */ |
| 8111 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8112 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8113 | /* Set the timeout. */ |
| 8114 | if (hlua->wake_time != TICK_ETERNITY) |
| 8115 | task_schedule(hlua->task, hlua->wake_time); |
| 8116 | return 0; |
| 8117 | |
| 8118 | /* finished with error. */ |
| 8119 | case HLUA_E_ERRMSG: |
| 8120 | /* Display log. */ |
| 8121 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8122 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8123 | lua_pop(hlua->T, 1); |
| 8124 | return 1; |
| 8125 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8126 | case HLUA_E_ETMOUT: |
| 8127 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8128 | fcn->name); |
| 8129 | return 1; |
| 8130 | |
| 8131 | case HLUA_E_NOMEM: |
| 8132 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8133 | fcn->name); |
| 8134 | return 1; |
| 8135 | |
| 8136 | case HLUA_E_YIELD: /* unexpected */ |
| 8137 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8138 | fcn->name); |
| 8139 | return 1; |
| 8140 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8141 | case HLUA_E_ERR: |
| 8142 | /* Display log. */ |
| 8143 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8144 | fcn->name); |
| 8145 | return 1; |
| 8146 | |
| 8147 | default: |
| 8148 | return 1; |
| 8149 | } |
| 8150 | |
| 8151 | return 1; |
| 8152 | } |
| 8153 | |
| 8154 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8155 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8156 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8157 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8158 | } |
| 8159 | |
| 8160 | /* This function is an LUA binding used for registering |
| 8161 | * new keywords in the cli. It expects a list of keywords |
| 8162 | * which are the "path". It is limited to 5 keywords. A |
| 8163 | * description of the command, a function to be executed |
| 8164 | * for the parsing and a function for io handlers. |
| 8165 | */ |
| 8166 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8167 | { |
| 8168 | struct cli_kw_list *cli_kws; |
| 8169 | const char *message; |
| 8170 | int ref_io; |
| 8171 | int len; |
| 8172 | struct hlua_function *fcn; |
| 8173 | int index; |
| 8174 | int i; |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8175 | struct buffer *trash; |
| 8176 | const char *kw[5]; |
| 8177 | struct cli_kw *cli_kw; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8178 | |
| 8179 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8180 | |
| 8181 | /* First argument : an array of maximum 5 keywords. */ |
| 8182 | if (!lua_istable(L, 1)) |
| 8183 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8184 | |
| 8185 | /* Second argument : string with contextual message. */ |
| 8186 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8187 | |
| 8188 | /* Third and fourth argument : lua function. */ |
| 8189 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8190 | |
Thierry Fournier | a9230f8 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8191 | /* Check for CLI service already registered */ |
| 8192 | trash = get_trash_chunk(); |
| 8193 | index = 0; |
| 8194 | lua_pushnil(L); |
| 8195 | memset(kw, 0, sizeof(kw)); |
| 8196 | while (lua_next(L, 1) != 0) { |
| 8197 | if (index >= CLI_PREFIX_KW_NB) |
| 8198 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8199 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8200 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8201 | kw[index] = lua_tostring(L, -1); |
| 8202 | if (index == 0) |
| 8203 | chunk_printf(trash, "%s", kw[index]); |
| 8204 | else |
| 8205 | chunk_appendf(trash, " %s", kw[index]); |
| 8206 | index++; |
| 8207 | lua_pop(L, 1); |
| 8208 | } |
| 8209 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8210 | if (cli_kw != NULL) { |
| 8211 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8212 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8213 | } |
| 8214 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8215 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8216 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8217 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8218 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8219 | fcn = calloc(1, sizeof(*fcn)); |
| 8220 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8221 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8222 | |
| 8223 | /* Fill path. */ |
| 8224 | index = 0; |
| 8225 | lua_pushnil(L); |
| 8226 | while(lua_next(L, 1) != 0) { |
| 8227 | if (index >= 5) |
| 8228 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8229 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8230 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8231 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8232 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8233 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8234 | index++; |
| 8235 | lua_pop(L, 1); |
| 8236 | } |
| 8237 | |
| 8238 | /* Copy help message. */ |
| 8239 | cli_kws->kw[0].usage = strdup(message); |
| 8240 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8241 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8242 | |
| 8243 | /* Fill fcn io handler. */ |
| 8244 | len = strlen("<lua.cli>") + 1; |
| 8245 | for (i = 0; i < index; i++) |
| 8246 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8247 | fcn->name = calloc(1, len); |
| 8248 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8249 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8250 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8251 | for (i = 0; i < index; i++) { |
| 8252 | strncat((char *)fcn->name, ".", len); |
| 8253 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8254 | } |
| 8255 | strncat((char *)fcn->name, ">", len); |
| 8256 | fcn->function_ref = ref_io; |
| 8257 | |
| 8258 | /* Fill last entries. */ |
| 8259 | cli_kws->kw[0].private = fcn; |
| 8260 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8261 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8262 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8263 | |
| 8264 | /* Register this new converter */ |
| 8265 | cli_register_kw(cli_kws); |
| 8266 | |
| 8267 | return 0; |
| 8268 | } |
| 8269 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8270 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8271 | struct proxy *defpx, const char *file, int line, |
| 8272 | char **err, unsigned int *timeout) |
| 8273 | { |
| 8274 | const char *error; |
| 8275 | |
| 8276 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8277 | if (error == PARSE_TIME_OVER) { |
| 8278 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8279 | args[1], args[0]); |
| 8280 | return -1; |
| 8281 | } |
| 8282 | else if (error == PARSE_TIME_UNDER) { |
| 8283 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8284 | args[1], args[0]); |
| 8285 | return -1; |
| 8286 | } |
| 8287 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8288 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8289 | return -1; |
| 8290 | } |
| 8291 | return 0; |
| 8292 | } |
| 8293 | |
| 8294 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8295 | struct proxy *defpx, const char *file, int line, |
| 8296 | char **err) |
| 8297 | { |
| 8298 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8299 | file, line, err, &hlua_timeout_session); |
| 8300 | } |
| 8301 | |
| 8302 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8303 | struct proxy *defpx, const char *file, int line, |
| 8304 | char **err) |
| 8305 | { |
| 8306 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8307 | file, line, err, &hlua_timeout_task); |
| 8308 | } |
| 8309 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8310 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8311 | struct proxy *defpx, const char *file, int line, |
| 8312 | char **err) |
| 8313 | { |
| 8314 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8315 | file, line, err, &hlua_timeout_applet); |
| 8316 | } |
| 8317 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8318 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8319 | struct proxy *defpx, const char *file, int line, |
| 8320 | char **err) |
| 8321 | { |
| 8322 | char *error; |
| 8323 | |
| 8324 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8325 | if (*error != '\0') { |
| 8326 | memprintf(err, "%s: invalid number", args[0]); |
| 8327 | return -1; |
| 8328 | } |
| 8329 | return 0; |
| 8330 | } |
| 8331 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8332 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8333 | struct proxy *defpx, const char *file, int line, |
| 8334 | char **err) |
| 8335 | { |
| 8336 | char *error; |
| 8337 | |
| 8338 | if (*(args[1]) == 0) { |
| 8339 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8340 | return -1; |
| 8341 | } |
| 8342 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8343 | if (*error != '\0') { |
| 8344 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8345 | return -1; |
| 8346 | } |
| 8347 | return 0; |
| 8348 | } |
| 8349 | |
| 8350 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8351 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8352 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8353 | * the main lua entry point. |
| 8354 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8355 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8356 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8357 | * |
| 8358 | * In some error case, LUA set an error message in top of the stack. This function |
| 8359 | * 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] | 8360 | * |
| 8361 | * This function can fail with an abort() due to an Lua critical error. |
| 8362 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8363 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8364 | */ |
| 8365 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8366 | struct proxy *defpx, const char *file, int line, |
| 8367 | char **err) |
| 8368 | { |
| 8369 | int error; |
| 8370 | |
Thierry Fournier | e667950 | 2020-11-29 01:06:24 +0100 | [diff] [blame] | 8371 | if (*(args[1]) == 0) { |
| 8372 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8373 | return -1; |
| 8374 | } |
| 8375 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8376 | /* Just load and compile the file. */ |
| 8377 | error = luaL_loadfile(gL.T, args[1]); |
| 8378 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8379 | 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] | 8380 | lua_pop(gL.T, 1); |
| 8381 | return -1; |
| 8382 | } |
| 8383 | |
| 8384 | /* If no syntax error where detected, execute the code. */ |
| 8385 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8386 | switch (error) { |
| 8387 | case LUA_OK: |
| 8388 | break; |
| 8389 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8390 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8391 | lua_pop(gL.T, 1); |
| 8392 | return -1; |
| 8393 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8394 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8395 | return -1; |
| 8396 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8397 | 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] | 8398 | lua_pop(gL.T, 1); |
| 8399 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8400 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8401 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8402 | 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] | 8403 | lua_pop(gL.T, 1); |
| 8404 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8405 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8406 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8407 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8408 | lua_pop(gL.T, 1); |
| 8409 | return -1; |
| 8410 | } |
| 8411 | |
| 8412 | return 0; |
| 8413 | } |
| 8414 | |
| 8415 | /* configuration keywords declaration */ |
| 8416 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8417 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8418 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8419 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8420 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8421 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8422 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8423 | { 0, NULL, NULL }, |
| 8424 | }}; |
| 8425 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8426 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8427 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8428 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8429 | /* This function can fail with an abort() due to an Lua critical error. |
| 8430 | * We are in the initialisation process of HAProxy, this abort() is |
| 8431 | * tolerated. |
| 8432 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8433 | int hlua_post_init() |
| 8434 | { |
| 8435 | struct hlua_init_function *init; |
| 8436 | const char *msg; |
| 8437 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8438 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8439 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8440 | /* Call post initialisation function in safe environement. */ |
| 8441 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8442 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8443 | error = lua_tostring(gL.T, -1); |
| 8444 | else |
| 8445 | error = "critical error"; |
| 8446 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8447 | exit(1); |
| 8448 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8449 | |
| 8450 | #if USE_OPENSSL |
| 8451 | /* Initialize SSL server. */ |
| 8452 | if (socket_ssl.xprt->prepare_srv) { |
| 8453 | int saved_used_backed = global.ssl_used_backend; |
| 8454 | // don't affect maxconn automatic computation |
| 8455 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8456 | global.ssl_used_backend = saved_used_backed; |
| 8457 | } |
| 8458 | #endif |
| 8459 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8460 | hlua_fcn_post_init(gL.T); |
| 8461 | RESET_SAFE_LJMP(gL.T); |
| 8462 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8463 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8464 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8465 | ret = hlua_ctx_resume(&gL, 0); |
| 8466 | switch (ret) { |
| 8467 | case HLUA_E_OK: |
| 8468 | lua_pop(gL.T, -1); |
Thierry Fournier | 65588c9 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8469 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8470 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8471 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8472 | return 0; |
| 8473 | case HLUA_E_ERRMSG: |
| 8474 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8475 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8476 | return 0; |
| 8477 | case HLUA_E_ERR: |
| 8478 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8479 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8480 | return 0; |
| 8481 | } |
| 8482 | } |
| 8483 | return 1; |
| 8484 | } |
| 8485 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8486 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8487 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8488 | * is the previously allocated size or the kind of object in case of a new |
| 8489 | * allocation. <nsize> is the requested new size. |
| 8490 | */ |
| 8491 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8492 | { |
| 8493 | struct hlua_mem_allocator *zone = ud; |
| 8494 | |
| 8495 | if (nsize == 0) { |
| 8496 | /* it's a free */ |
| 8497 | if (ptr) |
| 8498 | zone->allocated -= osize; |
| 8499 | free(ptr); |
| 8500 | return NULL; |
| 8501 | } |
| 8502 | |
| 8503 | if (!ptr) { |
| 8504 | /* it's a new allocation */ |
| 8505 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8506 | return NULL; |
| 8507 | |
| 8508 | ptr = malloc(nsize); |
| 8509 | if (ptr) |
| 8510 | zone->allocated += nsize; |
| 8511 | return ptr; |
| 8512 | } |
| 8513 | |
| 8514 | /* it's a realloc */ |
| 8515 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8516 | return NULL; |
| 8517 | |
| 8518 | ptr = realloc(ptr, nsize); |
| 8519 | if (ptr) |
| 8520 | zone->allocated += nsize - osize; |
| 8521 | return ptr; |
| 8522 | } |
| 8523 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8524 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8525 | * We are in the initialisation process of HAProxy, this abort() is |
| 8526 | * tolerated. |
| 8527 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8528 | void hlua_init(void) |
| 8529 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8530 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8531 | int idx; |
| 8532 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8533 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8534 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8535 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8536 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8537 | struct srv_kw *kw; |
| 8538 | int tmp_error; |
| 8539 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8540 | char *args[] = { /* SSL client configuration. */ |
| 8541 | "ssl", |
| 8542 | "verify", |
| 8543 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8544 | NULL |
| 8545 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8546 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8547 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8548 | /* Init main lua stack. */ |
| 8549 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8550 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8551 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8552 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8553 | hlua_sethlua(&gL); |
| 8554 | gL.Tref = LUA_REFNIL; |
| 8555 | gL.task = NULL; |
| 8556 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8557 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8558 | * the Lua function can fail with an abort. We are in the initialisation |
| 8559 | * process of HAProxy, this abort() is tolerated. |
| 8560 | */ |
| 8561 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8562 | /* Set safe environment for the initialisation. */ |
| 8563 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8564 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8565 | error_msg = lua_tostring(gL.T, -1); |
| 8566 | else |
| 8567 | error_msg = "critical error"; |
| 8568 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8569 | exit(1); |
| 8570 | } |
| 8571 | |
Thierry Fournier | a5d6258 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8572 | /* Initialise lua. */ |
| 8573 | luaL_openlibs(gL.T); |
| 8574 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8575 | /* |
| 8576 | * |
| 8577 | * Create "core" object. |
| 8578 | * |
| 8579 | */ |
| 8580 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8581 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8582 | lua_newtable(gL.T); |
| 8583 | |
| 8584 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8585 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8586 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8587 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8588 | /* Register special functions. */ |
| 8589 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8590 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8591 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8592 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8593 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8594 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8595 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8596 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8597 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8598 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8599 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8600 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8601 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8602 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8603 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8604 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8605 | hlua_class_function(gL.T, "log", hlua_log); |
| 8606 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8607 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8608 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8609 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8610 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8611 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8612 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8613 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8614 | |
| 8615 | /* |
| 8616 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8617 | * Register class Map |
| 8618 | * |
| 8619 | */ |
| 8620 | |
| 8621 | /* This table entry is the object "Map" base. */ |
| 8622 | lua_newtable(gL.T); |
| 8623 | |
| 8624 | /* register pattern types. */ |
| 8625 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8626 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8627 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8628 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8629 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8630 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8631 | |
| 8632 | /* register constructor. */ |
| 8633 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8634 | |
| 8635 | /* Create and fill the metatable. */ |
| 8636 | lua_newtable(gL.T); |
| 8637 | |
| 8638 | /* Create and fille the __index entry. */ |
| 8639 | lua_pushstring(gL.T, "__index"); |
| 8640 | lua_newtable(gL.T); |
| 8641 | |
| 8642 | /* Register . */ |
| 8643 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8644 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8645 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8646 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8647 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8648 | /* Register previous table in the registry with reference and named entry. |
| 8649 | * The function hlua_register_metatable() pops the stack, so we |
| 8650 | * previously create a copy of the table. |
| 8651 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8652 | 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] | 8653 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8654 | |
| 8655 | /* Assign the metatable to the mai Map object. */ |
| 8656 | lua_setmetatable(gL.T, -2); |
| 8657 | |
| 8658 | /* Set a name to the table. */ |
| 8659 | lua_setglobal(gL.T, "Map"); |
| 8660 | |
| 8661 | /* |
| 8662 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8663 | * Register class Channel |
| 8664 | * |
| 8665 | */ |
| 8666 | |
| 8667 | /* Create and fill the metatable. */ |
| 8668 | lua_newtable(gL.T); |
| 8669 | |
| 8670 | /* Create and fille the __index entry. */ |
| 8671 | lua_pushstring(gL.T, "__index"); |
| 8672 | lua_newtable(gL.T); |
| 8673 | |
| 8674 | /* Register . */ |
| 8675 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8676 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8677 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8678 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8679 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8680 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8681 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8682 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8683 | 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] | 8684 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8685 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8686 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8687 | |
| 8688 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8689 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8690 | |
| 8691 | /* |
| 8692 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8693 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8694 | * |
| 8695 | */ |
| 8696 | |
| 8697 | /* Create and fill the metatable. */ |
| 8698 | lua_newtable(gL.T); |
| 8699 | |
| 8700 | /* Create and fille the __index entry. */ |
| 8701 | lua_pushstring(gL.T, "__index"); |
| 8702 | lua_newtable(gL.T); |
| 8703 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8704 | /* Browse existing fetches and create the associated |
| 8705 | * object method. |
| 8706 | */ |
| 8707 | sf = NULL; |
| 8708 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8709 | |
| 8710 | /* Dont register the keywork if the arguments check function are |
| 8711 | * not safe during the runtime. |
| 8712 | */ |
| 8713 | if ((sf->val_args != NULL) && |
| 8714 | (sf->val_args != val_payload_lv) && |
| 8715 | (sf->val_args != val_hdr)) |
| 8716 | continue; |
| 8717 | |
| 8718 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8719 | * by an underscore. |
| 8720 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8721 | strncpy(trash.area, sf->kw, trash.size); |
| 8722 | trash.area[trash.size - 1] = '\0'; |
| 8723 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8724 | if (*p == '.' || *p == '-' || *p == '+') |
| 8725 | *p = '_'; |
| 8726 | |
| 8727 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8728 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8729 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8730 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8731 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8732 | } |
| 8733 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8734 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8735 | |
| 8736 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8737 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8738 | |
| 8739 | /* |
| 8740 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8741 | * Register class Converters |
| 8742 | * |
| 8743 | */ |
| 8744 | |
| 8745 | /* Create and fill the metatable. */ |
| 8746 | lua_newtable(gL.T); |
| 8747 | |
| 8748 | /* Create and fill the __index entry. */ |
| 8749 | lua_pushstring(gL.T, "__index"); |
| 8750 | lua_newtable(gL.T); |
| 8751 | |
| 8752 | /* Browse existing converters and create the associated |
| 8753 | * object method. |
| 8754 | */ |
| 8755 | sc = NULL; |
| 8756 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8757 | /* Dont register the keywork if the arguments check function are |
| 8758 | * not safe during the runtime. |
| 8759 | */ |
| 8760 | if (sc->val_args != NULL) |
| 8761 | continue; |
| 8762 | |
| 8763 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8764 | * by an underscore. |
| 8765 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8766 | strncpy(trash.area, sc->kw, trash.size); |
| 8767 | trash.area[trash.size - 1] = '\0'; |
| 8768 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8769 | if (*p == '.' || *p == '-' || *p == '+') |
| 8770 | *p = '_'; |
| 8771 | |
| 8772 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8773 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8774 | lua_pushlightuserdata(gL.T, sc); |
| 8775 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8776 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8777 | } |
| 8778 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8779 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8780 | |
| 8781 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8782 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8783 | |
| 8784 | /* |
| 8785 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8786 | * Register class HTTP |
| 8787 | * |
| 8788 | */ |
| 8789 | |
| 8790 | /* Create and fill the metatable. */ |
| 8791 | lua_newtable(gL.T); |
| 8792 | |
| 8793 | /* Create and fille the __index entry. */ |
| 8794 | lua_pushstring(gL.T, "__index"); |
| 8795 | lua_newtable(gL.T); |
| 8796 | |
| 8797 | /* Register Lua functions. */ |
| 8798 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8799 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8800 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8801 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8802 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8803 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8804 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8805 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8806 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8807 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8808 | |
| 8809 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8810 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8811 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8812 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8813 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8814 | 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] | 8815 | 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] | 8816 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8817 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +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_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8821 | |
| 8822 | /* |
| 8823 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8824 | * Register class AppletTCP |
| 8825 | * |
| 8826 | */ |
| 8827 | |
| 8828 | /* Create and fill the metatable. */ |
| 8829 | lua_newtable(gL.T); |
| 8830 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8831 | /* Create and fille the __index entry. */ |
| 8832 | lua_pushstring(gL.T, "__index"); |
| 8833 | lua_newtable(gL.T); |
| 8834 | |
| 8835 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8836 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8837 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8838 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8839 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8840 | 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] | 8841 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8842 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8843 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8844 | |
| 8845 | lua_settable(gL.T, -3); |
| 8846 | |
| 8847 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8848 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8849 | |
| 8850 | /* |
| 8851 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8852 | * Register class AppletHTTP |
| 8853 | * |
| 8854 | */ |
| 8855 | |
| 8856 | /* Create and fill the metatable. */ |
| 8857 | lua_newtable(gL.T); |
| 8858 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8859 | /* Create and fille the __index entry. */ |
| 8860 | lua_pushstring(gL.T, "__index"); |
| 8861 | lua_newtable(gL.T); |
| 8862 | |
| 8863 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8864 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8865 | 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] | 8866 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8867 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8868 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8869 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8870 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8871 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8872 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8873 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8874 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8875 | |
| 8876 | lua_settable(gL.T, -3); |
| 8877 | |
| 8878 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8879 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8880 | |
| 8881 | /* |
| 8882 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8883 | * Register class TXN |
| 8884 | * |
| 8885 | */ |
| 8886 | |
| 8887 | /* Create and fill the metatable. */ |
| 8888 | lua_newtable(gL.T); |
| 8889 | |
| 8890 | /* Create and fille the __index entry. */ |
| 8891 | lua_pushstring(gL.T, "__index"); |
| 8892 | lua_newtable(gL.T); |
| 8893 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8894 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8895 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8896 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8897 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8898 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8899 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8900 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8901 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8902 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8903 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8904 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8905 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8906 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8907 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8908 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8909 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8910 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8911 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8912 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8913 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8914 | |
| 8915 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8916 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8917 | |
| 8918 | /* |
| 8919 | * |
| 8920 | * Register class Socket |
| 8921 | * |
| 8922 | */ |
| 8923 | |
| 8924 | /* Create and fill the metatable. */ |
| 8925 | lua_newtable(gL.T); |
| 8926 | |
| 8927 | /* Create and fille the __index entry. */ |
| 8928 | lua_pushstring(gL.T, "__index"); |
| 8929 | lua_newtable(gL.T); |
| 8930 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8931 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8932 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8933 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8934 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8935 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8936 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8937 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8938 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8939 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8940 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8941 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8942 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8943 | 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] | 8944 | |
| 8945 | /* Register the garbage collector entry. */ |
| 8946 | lua_pushstring(gL.T, "__gc"); |
| 8947 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8948 | 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] | 8949 | |
| 8950 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8951 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8952 | |
| 8953 | /* Proxy and server configuration initialisation. */ |
| 8954 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8955 | init_new_proxy(&socket_proxy); |
| 8956 | socket_proxy.parent = NULL; |
| 8957 | socket_proxy.last_change = now.tv_sec; |
| 8958 | socket_proxy.id = "LUA-SOCKET"; |
| 8959 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8960 | socket_proxy.maxconn = 0; |
| 8961 | socket_proxy.accept = NULL; |
| 8962 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8963 | socket_proxy.srv = NULL; |
| 8964 | socket_proxy.conn_retries = 0; |
| 8965 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8966 | |
| 8967 | /* Init TCP server: unchanged parameters */ |
| 8968 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8969 | socket_tcp.next = NULL; |
| 8970 | socket_tcp.proxy = &socket_proxy; |
| 8971 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8972 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8973 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8974 | socket_tcp.priv_conns = NULL; |
| 8975 | socket_tcp.idle_conns = NULL; |
| 8976 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8977 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8978 | socket_tcp.last_change = 0; |
| 8979 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8980 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8981 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8982 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8983 | |
| 8984 | /* XXX: Copy default parameter from default server, |
| 8985 | * but the default server is not initialized. |
| 8986 | */ |
| 8987 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8988 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8989 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8990 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8991 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8992 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8993 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8994 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8995 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8996 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8997 | |
| 8998 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8999 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 9000 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 9001 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 9002 | socket_tcp.check.server = &socket_tcp; |
| 9003 | |
| 9004 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 9005 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 9006 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 9007 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 9008 | socket_tcp.agent.server = &socket_tcp; |
| 9009 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 9010 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9011 | |
| 9012 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9013 | /* Init TCP server: unchanged parameters */ |
| 9014 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 9015 | socket_ssl.next = NULL; |
| 9016 | socket_ssl.proxy = &socket_proxy; |
| 9017 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 9018 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 9019 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 9020 | socket_ssl.priv_conns = NULL; |
| 9021 | socket_ssl.idle_conns = NULL; |
| 9022 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 9023 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9024 | socket_ssl.last_change = 0; |
| 9025 | socket_ssl.id = "LUA-SSL-CONN"; |
| 9026 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9027 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9028 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 9029 | |
| 9030 | /* XXX: Copy default parameter from default server, |
| 9031 | * but the default server is not initialized. |
| 9032 | */ |
| 9033 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 9034 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 9035 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 9036 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 9037 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 9038 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 9039 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 9040 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 9041 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 9042 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 9043 | |
| 9044 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 9045 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 9046 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 9047 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 9048 | socket_ssl.check.server = &socket_ssl; |
| 9049 | |
| 9050 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 9051 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 9052 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 9053 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 9054 | socket_ssl.agent.server = &socket_ssl; |
| 9055 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 9056 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 9057 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9058 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 9059 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9060 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 9061 | /* |
| 9062 | * |
| 9063 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9064 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9065 | * features like client certificates and ssl_verify. |
| 9066 | * |
| 9067 | */ |
| 9068 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 9069 | if (tmp_error != 0) { |
| 9070 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9071 | abort(); /* This must be never arrives because the command line |
| 9072 | not editable by the user. */ |
| 9073 | } |
| 9074 | idx += kw->skip; |
| 9075 | } |
| 9076 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9077 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9078 | |
| 9079 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9080 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9081 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9082 | static void hlua_register_build_options(void) |
| 9083 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9084 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9085 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9086 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9087 | hap_register_build_opts(ptr, 1); |
| 9088 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9089 | |
| 9090 | INITCALL0(STG_REGISTER, hlua_register_build_options); |