Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 14 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 16 | #include <lauxlib.h> |
| 17 | #include <lua.h> |
| 18 | #include <lualib.h> |
| 19 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 20 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 21 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 24 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 26 | #include <haproxy/api.h> |
| 27 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 28 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 29 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 30 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 31 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 32 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 33 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 34 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 35 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 36 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 37 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 38 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 39 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 40 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 41 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 42 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 43 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 44 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 45 | #include <haproxy/payload.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 46 | #include <haproxy/proxy-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 47 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 48 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 49 | #include <haproxy/server-t.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 50 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 51 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 52 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 53 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 54 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 55 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 56 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 57 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 58 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 59 | #include <haproxy/xref.h> |
| 60 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 61 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 62 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 63 | * macro is used only for identifying the function that can |
| 64 | * not return because a longjmp is executed. |
| 65 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 66 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 67 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 68 | */ |
| 69 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 70 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 71 | #define MAY_LJMP(func) func |
| 72 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 73 | /* This couple of function executes securely some Lua calls outside of |
| 74 | * the lua runtime environment. Each Lua call can return a longjmp |
| 75 | * if it encounter a memory error. |
| 76 | * |
| 77 | * Lua documentation extract: |
| 78 | * |
| 79 | * If an error happens outside any protected environment, Lua calls |
| 80 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 81 | * exiting the host application. Your panic function can avoid this |
| 82 | * exit by never returning (e.g., doing a long jump to your own |
| 83 | * recovery point outside Lua). |
| 84 | * |
| 85 | * The panic function runs as if it were a message handler (see |
| 86 | * §2.3); in particular, the error message is at the top of the |
| 87 | * stack. However, there is no guarantee about stack space. To push |
| 88 | * anything on the stack, the panic function must first check the |
| 89 | * available space (see §4.2). |
| 90 | * |
| 91 | * We must check all the Lua entry point. This includes: |
| 92 | * - The include/proto/hlua.h exported functions |
| 93 | * - the task wrapper function |
| 94 | * - The action wrapper function |
| 95 | * - The converters wrapper function |
| 96 | * - The sample-fetch wrapper functions |
| 97 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 98 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 99 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 100 | * |
| 101 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 102 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 103 | * because they must be exists in the program stack when the longjmp |
| 104 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 105 | * |
| 106 | * Note that the Lua processing is not really thread safe. It provides |
| 107 | * heavy system which consists to add our own lock function in the Lua |
| 108 | * code and recompile the library. This system will probably not accepted |
| 109 | * by maintainers of various distribs. |
| 110 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 111 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 112 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 113 | * of function and a lua_unlock() at the end of the function. So I |
| 114 | * conclude that the Lua thread safe mode just perform a mutex around |
| 115 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 116 | * easier for distro maintainers. |
| 117 | * |
| 118 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 119 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 120 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 121 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 122 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 123 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 124 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 125 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 126 | |
| 127 | #define SET_SAFE_LJMP(__L) \ |
| 128 | ({ \ |
| 129 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 130 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 131 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 132 | lua_atpanic(__L, hlua_panic_safe); \ |
| 133 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 134 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 135 | } else { \ |
| 136 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 137 | ret = 1; \ |
| 138 | } \ |
| 139 | ret; \ |
| 140 | }) |
| 141 | |
| 142 | /* If we are the last function catching Lua errors, we |
| 143 | * must reset the panic function. |
| 144 | */ |
| 145 | #define RESET_SAFE_LJMP(__L) \ |
| 146 | do { \ |
| 147 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 148 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 149 | } while(0) |
| 150 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 151 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 152 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 153 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 154 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 155 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 156 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 157 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 158 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 159 | /* The main Lua execution context. */ |
Thierry Fournier | 92689e6 | 2020-11-28 11:21:25 +0100 | [diff] [blame] | 160 | struct { |
| 161 | lua_State *T; |
| 162 | } gL; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 163 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 164 | /* This is the memory pool containing struct lua for applets |
| 165 | * (including cli). |
| 166 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 167 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 168 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 169 | /* Used for Socket connection. */ |
| 170 | static struct proxy socket_proxy; |
| 171 | static struct server socket_tcp; |
| 172 | #ifdef USE_OPENSSL |
| 173 | static struct server socket_ssl; |
| 174 | #endif |
| 175 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 176 | /* List head of the function called at the initialisation time. */ |
| 177 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 178 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 179 | /* The following variables contains the reference of the different |
| 180 | * Lua classes. These references are useful for identify metadata |
| 181 | * associated with an object. |
| 182 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 183 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 184 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 185 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 186 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 187 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 188 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 189 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 190 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 191 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 192 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 193 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 194 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 195 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 196 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 197 | * because a task may remain alive during all the haproxy execution. |
| 198 | */ |
| 199 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 200 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 201 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 202 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 203 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 204 | * it is used for preventing infinite loops. |
| 205 | * |
| 206 | * I test the scheer with an infinite loop containing one incrementation |
| 207 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 208 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 209 | * to one interrupt each 10 000 instructions. |
| 210 | * |
| 211 | * configured | Number of |
| 212 | * instructions | loops executed |
| 213 | * between two | in milions |
| 214 | * forced yields | |
| 215 | * ---------------+--------------- |
| 216 | * 10 | 160 |
| 217 | * 500 | 670 |
| 218 | * 1000 | 680 |
| 219 | * 5000 | 700 |
| 220 | * 7000 | 700 |
| 221 | * 8000 | 700 |
| 222 | * 9000 | 710 <- ceil |
| 223 | * 10000 | 710 |
| 224 | * 100000 | 710 |
| 225 | * 1000000 | 710 |
| 226 | * |
| 227 | */ |
| 228 | static unsigned int hlua_nb_instruction = 10000; |
| 229 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 230 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 231 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 232 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 233 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 234 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 235 | */ |
| 236 | struct hlua_mem_allocator { |
| 237 | size_t allocated; |
| 238 | size_t limit; |
| 239 | }; |
| 240 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 241 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 242 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 243 | /* These functions converts types between HAProxy internal args or |
| 244 | * sample and LUA types. Another function permits to check if the |
| 245 | * LUA stack contains arguments according with an required ARG_T |
| 246 | * format. |
| 247 | */ |
| 248 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 249 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 250 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 251 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 252 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 253 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 254 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 255 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 256 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 257 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 258 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 259 | do { \ |
| 260 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 261 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 262 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 263 | } while (0) |
| 264 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 265 | /* Used to check an Lua function type in the stack. It creates and |
| 266 | * returns a reference of the function. This function throws an |
| 267 | * error if the rgument is not a "function". |
| 268 | */ |
| 269 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 270 | { |
| 271 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 272 | 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] | 273 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 274 | } |
| 275 | lua_pushvalue(L, argno); |
| 276 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 277 | } |
| 278 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 279 | /* Return the string that is of the top of the stack. */ |
| 280 | const char *hlua_get_top_error_string(lua_State *L) |
| 281 | { |
| 282 | if (lua_gettop(L) < 1) |
| 283 | return "unknown error"; |
| 284 | if (lua_type(L, -1) != LUA_TSTRING) |
| 285 | return "unknown error"; |
| 286 | return lua_tostring(L, -1); |
| 287 | } |
| 288 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 289 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 290 | { |
| 291 | lua_Debug ar; |
| 292 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 293 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 294 | int filled = 0; |
| 295 | |
| 296 | while (lua_getstack(L, level++, &ar)) { |
| 297 | |
| 298 | /* Add separator */ |
| 299 | if (filled) |
| 300 | chunk_appendf(msg, ", "); |
| 301 | filled = 1; |
| 302 | |
| 303 | /* Fill fields: |
| 304 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 305 | * 'l': fills in the field currentline; |
| 306 | * 'n': fills in the field name and namewhat; |
| 307 | * 't': fills in the field istailcall; |
| 308 | */ |
| 309 | lua_getinfo(L, "Slnt", &ar); |
| 310 | |
| 311 | /* Append code localisation */ |
| 312 | if (ar.currentline > 0) |
| 313 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 314 | else |
| 315 | chunk_appendf(msg, "%s ", ar.short_src); |
| 316 | |
| 317 | /* |
| 318 | * Get function name |
| 319 | * |
| 320 | * if namewhat is no empty, name is defined. |
| 321 | * what contains "Lua" for Lua function, "C" for C function, |
| 322 | * or "main" for main code. |
| 323 | */ |
| 324 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 325 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 326 | |
| 327 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 328 | chunk_appendf(msg, "main chunk"); |
| 329 | |
| 330 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 331 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 332 | |
| 333 | else /* nothing left... */ |
| 334 | chunk_appendf(msg, "?"); |
| 335 | |
| 336 | |
| 337 | /* Display tailed call */ |
| 338 | if (ar.istailcall) |
| 339 | chunk_appendf(msg, " ..."); |
| 340 | } |
| 341 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 342 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 346 | /* This function check the number of arguments available in the |
| 347 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 348 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 349 | */ |
| 350 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 351 | { |
| 352 | if (lua_gettop(L) == nb) |
| 353 | return; |
| 354 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 355 | } |
| 356 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 357 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 358 | * and the line number where the error is encountered. |
| 359 | */ |
| 360 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 361 | { |
| 362 | va_list argp; |
| 363 | va_start(argp, fmt); |
| 364 | luaL_where(L, 1); |
| 365 | lua_pushvfstring(L, fmt, argp); |
| 366 | va_end(argp); |
| 367 | lua_concat(L, 2); |
| 368 | return 1; |
| 369 | } |
| 370 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 371 | /* This functions is used with sample fetch and converters. It |
| 372 | * converts the HAProxy configuration argument in a lua stack |
| 373 | * values. |
| 374 | * |
| 375 | * It takes an array of "arg", and each entry of the array is |
| 376 | * converted and pushed in the LUA stack. |
| 377 | */ |
| 378 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 379 | { |
| 380 | switch (arg->type) { |
| 381 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 382 | case ARGT_TIME: |
| 383 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 384 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 385 | break; |
| 386 | |
| 387 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 388 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | break; |
| 390 | |
| 391 | case ARGT_IPV4: |
| 392 | case ARGT_IPV6: |
| 393 | case ARGT_MSK4: |
| 394 | case ARGT_MSK6: |
| 395 | case ARGT_FE: |
| 396 | case ARGT_BE: |
| 397 | case ARGT_TAB: |
| 398 | case ARGT_SRV: |
| 399 | case ARGT_USR: |
| 400 | case ARGT_MAP: |
| 401 | default: |
| 402 | lua_pushnil(L); |
| 403 | break; |
| 404 | } |
| 405 | return 1; |
| 406 | } |
| 407 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 408 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 409 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 410 | * with sample fetch wrappers. The input arguments are given to the |
| 411 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 412 | */ |
| 413 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 414 | { |
| 415 | switch (lua_type(L, ud)) { |
| 416 | |
| 417 | case LUA_TNUMBER: |
| 418 | case LUA_TBOOLEAN: |
| 419 | arg->type = ARGT_SINT; |
| 420 | arg->data.sint = lua_tointeger(L, ud); |
| 421 | break; |
| 422 | |
| 423 | case LUA_TSTRING: |
| 424 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 425 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 426 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 427 | arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 428 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 429 | break; |
| 430 | |
| 431 | case LUA_TUSERDATA: |
| 432 | case LUA_TNIL: |
| 433 | case LUA_TTABLE: |
| 434 | case LUA_TFUNCTION: |
| 435 | case LUA_TTHREAD: |
| 436 | case LUA_TLIGHTUSERDATA: |
| 437 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 438 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | /* the following functions are used to convert a struct sample |
| 445 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 446 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 447 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 448 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 450 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 451 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 453 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 454 | break; |
| 455 | |
| 456 | case SMP_T_BIN: |
| 457 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 458 | 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] | 459 | break; |
| 460 | |
| 461 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 462 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 463 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 464 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 465 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 466 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 467 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 468 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 469 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 470 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 471 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 472 | 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] | 473 | break; |
| 474 | default: |
| 475 | lua_pushnil(L); |
| 476 | break; |
| 477 | } |
| 478 | break; |
| 479 | |
| 480 | case SMP_T_IPV4: |
| 481 | case SMP_T_IPV6: |
| 482 | 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] | 483 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 484 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 485 | 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] | 486 | else |
| 487 | lua_pushnil(L); |
| 488 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 489 | default: |
| 490 | lua_pushnil(L); |
| 491 | break; |
| 492 | } |
| 493 | return 1; |
| 494 | } |
| 495 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 496 | /* the following functions are used to convert a struct sample |
| 497 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 498 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 499 | */ |
| 500 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 501 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 502 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 503 | |
| 504 | case SMP_T_BIN: |
| 505 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 506 | 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] | 507 | break; |
| 508 | |
| 509 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 510 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 511 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 512 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 513 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 514 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 515 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 516 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 517 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 518 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 519 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 520 | 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] | 521 | break; |
| 522 | default: |
| 523 | lua_pushstring(L, ""); |
| 524 | break; |
| 525 | } |
| 526 | break; |
| 527 | |
| 528 | case SMP_T_SINT: |
| 529 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 530 | case SMP_T_IPV4: |
| 531 | case SMP_T_IPV6: |
| 532 | 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] | 533 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 534 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 535 | 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] | 536 | else |
| 537 | lua_pushstring(L, ""); |
| 538 | break; |
| 539 | default: |
| 540 | lua_pushstring(L, ""); |
| 541 | break; |
| 542 | } |
| 543 | return 1; |
| 544 | } |
| 545 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 546 | /* the following functions are used to convert an Lua type in a |
| 547 | * struct sample. This is useful to provide data from a converter |
| 548 | * to the LUA code. |
| 549 | */ |
| 550 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 551 | { |
| 552 | switch (lua_type(L, ud)) { |
| 553 | |
| 554 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 555 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 556 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 557 | break; |
| 558 | |
| 559 | |
| 560 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 561 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 562 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 563 | break; |
| 564 | |
| 565 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 566 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 567 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 568 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 569 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 570 | smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 571 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 572 | break; |
| 573 | |
| 574 | case LUA_TUSERDATA: |
| 575 | case LUA_TNIL: |
| 576 | case LUA_TTABLE: |
| 577 | case LUA_TFUNCTION: |
| 578 | case LUA_TTHREAD: |
| 579 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 580 | case LUA_TNONE: |
| 581 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 582 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 583 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | return 1; |
| 587 | } |
| 588 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 589 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 590 | * 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] | 591 | * 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] | 592 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 593 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 594 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 596 | __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] | 597 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | { |
| 599 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 600 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 601 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 602 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 603 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 604 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 605 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 606 | |
| 607 | idx = 0; |
| 608 | min_arg = ARGM(mask); |
| 609 | mask >>= ARGM_BITS; |
| 610 | |
| 611 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 612 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 613 | |
| 614 | /* Check oversize. */ |
| 615 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 616 | msg = "Malformed argument mask"; |
| 617 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* Check for mandatory arguments. */ |
| 621 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 622 | if (idx < min_arg) { |
| 623 | |
| 624 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 625 | if (idx > 0) { |
| 626 | msg = "Mandatory argument expected"; |
| 627 | goto error; |
| 628 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 629 | |
| 630 | /* If first argument have a certain type, some default values |
| 631 | * may be used. See the function smp_resolve_args(). |
| 632 | */ |
| 633 | switch (mask & ARGT_MASK) { |
| 634 | |
| 635 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 636 | if (!(p->cap & PR_CAP_FE)) { |
| 637 | msg = "Mandatory argument expected"; |
| 638 | goto error; |
| 639 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 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: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 646 | if (!(p->cap & PR_CAP_BE)) { |
| 647 | msg = "Mandatory argument expected"; |
| 648 | goto error; |
| 649 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 650 | argp[idx].data.prx = p; |
| 651 | argp[idx].type = ARGT_BE; |
| 652 | argp[idx+1].type = ARGT_STOP; |
| 653 | break; |
| 654 | |
| 655 | case ARGT_TAB: |
| 656 | argp[idx].data.prx = p; |
| 657 | argp[idx].type = ARGT_TAB; |
| 658 | argp[idx+1].type = ARGT_STOP; |
| 659 | break; |
| 660 | |
| 661 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 662 | msg = "Mandatory argument expected"; |
| 663 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 664 | break; |
| 665 | } |
| 666 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 667 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 668 | } |
| 669 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 670 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 671 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 672 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 673 | msg = "Last argument expected"; |
| 674 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 678 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 679 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 682 | /* Convert some argument types. All string in argp[] are for not |
| 683 | * duplicated yet. |
| 684 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 685 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 686 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 687 | if (argp[idx].type != ARGT_SINT) { |
| 688 | msg = "integer expected"; |
| 689 | goto error; |
| 690 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 691 | argp[idx].type = ARGT_SINT; |
| 692 | break; |
| 693 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 694 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 695 | if (argp[idx].type != ARGT_SINT) { |
| 696 | msg = "integer expected"; |
| 697 | goto error; |
| 698 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 699 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 700 | break; |
| 701 | |
| 702 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 703 | if (argp[idx].type != ARGT_SINT) { |
| 704 | msg = "integer expected"; |
| 705 | goto error; |
| 706 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 707 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 708 | break; |
| 709 | |
| 710 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 711 | if (argp[idx].type != ARGT_STR) { |
| 712 | msg = "string expected"; |
| 713 | goto error; |
| 714 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 715 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 716 | if (!argp[idx].data.prx) { |
| 717 | msg = "frontend doesn't exist"; |
| 718 | goto error; |
| 719 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 720 | argp[idx].type = ARGT_FE; |
| 721 | break; |
| 722 | |
| 723 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 724 | if (argp[idx].type != ARGT_STR) { |
| 725 | msg = "string expected"; |
| 726 | goto error; |
| 727 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 728 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 729 | if (!argp[idx].data.prx) { |
| 730 | msg = "backend doesn't exist"; |
| 731 | goto error; |
| 732 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 733 | argp[idx].type = ARGT_BE; |
| 734 | break; |
| 735 | |
| 736 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 737 | if (argp[idx].type != ARGT_STR) { |
| 738 | msg = "string expected"; |
| 739 | goto error; |
| 740 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 741 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 742 | if (!argp[idx].data.t) { |
| 743 | msg = "table doesn't exist"; |
| 744 | goto error; |
| 745 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 746 | argp[idx].type = ARGT_TAB; |
| 747 | break; |
| 748 | |
| 749 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 750 | if (argp[idx].type != ARGT_STR) { |
| 751 | msg = "string expected"; |
| 752 | goto error; |
| 753 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 754 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 755 | if (sname) { |
| 756 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 757 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 758 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 759 | if (!px) { |
| 760 | msg = "backend doesn't exist"; |
| 761 | goto error; |
| 762 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | } |
| 764 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 765 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 766 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 767 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 768 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 769 | if (!argp[idx].data.srv) { |
| 770 | msg = "server doesn't exist"; |
| 771 | goto error; |
| 772 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 773 | argp[idx].type = ARGT_SRV; |
| 774 | break; |
| 775 | |
| 776 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 777 | if (argp[idx].type != ARGT_STR) { |
| 778 | msg = "string expected"; |
| 779 | goto error; |
| 780 | } |
| 781 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 782 | msg = "invalid IPv4 address"; |
| 783 | goto error; |
| 784 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 785 | argp[idx].type = ARGT_IPV4; |
| 786 | break; |
| 787 | |
| 788 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 789 | if (argp[idx].type == ARGT_SINT) |
| 790 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 791 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 792 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 793 | msg = "invalid IPv4 mask"; |
| 794 | goto error; |
| 795 | } |
| 796 | } |
| 797 | else { |
| 798 | msg = "integer or string expected"; |
| 799 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 800 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 801 | argp[idx].type = ARGT_MSK4; |
| 802 | break; |
| 803 | |
| 804 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 805 | if (argp[idx].type != ARGT_STR) { |
| 806 | msg = "string expected"; |
| 807 | goto error; |
| 808 | } |
| 809 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 810 | msg = "invalid IPv6 address"; |
| 811 | goto error; |
| 812 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 813 | argp[idx].type = ARGT_IPV6; |
| 814 | break; |
| 815 | |
| 816 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 817 | if (argp[idx].type == ARGT_SINT) |
| 818 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 819 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 820 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 821 | msg = "invalid IPv6 mask"; |
| 822 | goto error; |
| 823 | } |
| 824 | } |
| 825 | else { |
| 826 | msg = "integer or string expected"; |
| 827 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 828 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 829 | argp[idx].type = ARGT_MSK6; |
| 830 | break; |
| 831 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 832 | case ARGT_REG: |
| 833 | if (argp[idx].type != ARGT_STR) { |
| 834 | msg = "string expected"; |
| 835 | goto error; |
| 836 | } |
| 837 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 838 | if (!reg) { |
| 839 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 840 | argp[idx].data.str.area, err); |
| 841 | free(err); |
| 842 | goto error; |
| 843 | } |
| 844 | argp[idx].type = ARGT_REG; |
| 845 | argp[idx].data.reg = reg; |
| 846 | break; |
| 847 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 848 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 849 | if (argp[idx].type != ARGT_STR) { |
| 850 | msg = "string expected"; |
| 851 | goto error; |
| 852 | } |
| 853 | if (p->uri_auth && p->uri_auth->userlist && |
| 854 | !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area)) |
| 855 | ul = p->uri_auth->userlist; |
| 856 | else |
| 857 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 858 | |
| 859 | if (!ul) { |
| 860 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 861 | goto error; |
| 862 | } |
| 863 | argp[idx].type = ARGT_USR; |
| 864 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 865 | break; |
| 866 | |
| 867 | case ARGT_STR: |
| 868 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 869 | msg = "unable to duplicate string arg"; |
| 870 | goto error; |
| 871 | } |
| 872 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 873 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 874 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 875 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 876 | msg = "type not yet supported"; |
| 877 | goto error; |
| 878 | break; |
| 879 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /* Check for type of argument. */ |
| 883 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 884 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 885 | arg_type_names[(mask & ARGT_MASK)], |
| 886 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 887 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /* Next argument. */ |
| 891 | mask >>= ARGT_BITS; |
| 892 | idx++; |
| 893 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 894 | return 0; |
| 895 | |
| 896 | error: |
| 897 | for (i = 0; i < idx; i++) { |
| 898 | if (argp[i].type == ARGT_STR) |
| 899 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 900 | else if (argp[i].type == ARGT_REG) |
| 901 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 902 | } |
| 903 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 904 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 905 | } |
| 906 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 908 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 909 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 910 | * |
| 911 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 912 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 913 | */ |
| 914 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 915 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 916 | struct hlua **hlua = lua_getextraspace(L); |
| 917 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 918 | } |
| 919 | static inline void hlua_sethlua(struct hlua *hlua) |
| 920 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 921 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 922 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 923 | } |
| 924 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 925 | /* This function is used to send logs. It try to send on screen (stderr) |
| 926 | * and on the default syslog server. |
| 927 | */ |
| 928 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 929 | { |
| 930 | struct tm tm; |
| 931 | char *p; |
| 932 | |
| 933 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 934 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 935 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 936 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 937 | /* Break the message if exceed the buffer size. */ |
| 938 | *(p-4) = ' '; |
| 939 | *(p-3) = '.'; |
| 940 | *(p-2) = '.'; |
| 941 | *(p-1) = '.'; |
| 942 | break; |
| 943 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 944 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 945 | *p = *msg; |
| 946 | else |
| 947 | *p = '.'; |
| 948 | } |
| 949 | *p = '\0'; |
| 950 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 951 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 952 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 953 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 954 | return; |
| 955 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 956 | get_localtime(date.tv_sec, &tm); |
| 957 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 958 | 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] | 959 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 960 | fflush(stderr); |
| 961 | } |
| 962 | } |
| 963 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 964 | /* This function just ensure that the yield will be always |
| 965 | * returned with a timeout and permit to set some flags |
| 966 | */ |
| 967 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 968 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 969 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 970 | struct hlua *hlua; |
| 971 | |
| 972 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 973 | hlua = hlua_gethlua(L); |
| 974 | if (!hlua) { |
| 975 | return; |
| 976 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 977 | |
| 978 | /* Set the wake timeout. If timeout is required, we set |
| 979 | * the expiration time. |
| 980 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 981 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 982 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 983 | hlua->flags |= flags; |
| 984 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 985 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 986 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 989 | /* This function initialises the Lua environment stored in the stream. |
| 990 | * 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] | 991 | * 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] | 992 | * |
| 993 | * This function is particular. it initialises a new Lua thread. If the |
| 994 | * initialisation fails (example: out of memory error), the lua function |
| 995 | * throws an error (longjmp). |
| 996 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 997 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 998 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 999 | * threads appear, the safe environment set a lock to ensure only one |
| 1000 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1001 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1002 | * |
| 1003 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1004 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1005 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1006 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1007 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1008 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1009 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1010 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1011 | 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] | 1012 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1013 | if (!already_safe) { |
| 1014 | if (!SET_SAFE_LJMP(gL.T)) { |
| 1015 | lua->Tref = LUA_REFNIL; |
| 1016 | return 0; |
| 1017 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1018 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1019 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1020 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1021 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1022 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1023 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1024 | lua->T = lua_newthread(gL.T); |
| 1025 | if (!lua->T) { |
| 1026 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1027 | if (!already_safe) |
| 1028 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1029 | return 0; |
| 1030 | } |
| 1031 | hlua_sethlua(lua); |
| 1032 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1033 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1034 | if (!already_safe) |
| 1035 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1036 | return 1; |
| 1037 | } |
| 1038 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1039 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1040 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1041 | * is not freed. |
| 1042 | */ |
| 1043 | void hlua_ctx_destroy(struct hlua *lua) |
| 1044 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1045 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1046 | return; |
| 1047 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1048 | if (!lua->T) |
| 1049 | goto end; |
| 1050 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1051 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1052 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1053 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1054 | if (!SET_SAFE_LJMP(lua->T)) |
| 1055 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1056 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1057 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1058 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1059 | if (!SET_SAFE_LJMP(gL.T)) |
| 1060 | return; |
| 1061 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1062 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1063 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1064 | * without error, we run the GC on the thread pointer. Its freed all |
| 1065 | * the unused memory. |
| 1066 | * If the thread is finnish with an error or is currently yielded, |
| 1067 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1068 | * so e run the GC on the main thread. |
| 1069 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1070 | * the garbage collection. |
| 1071 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1072 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1073 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1074 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1075 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 1076 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1077 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1078 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1079 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1080 | |
| 1081 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1082 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* This function is used to restore the Lua context when a coroutine |
| 1086 | * fails. This function copy the common memory between old coroutine |
| 1087 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1088 | * replaced by the new coroutine. |
| 1089 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1090 | * as string error message and it is copied in the new stack. |
| 1091 | */ |
| 1092 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1093 | { |
| 1094 | lua_State *T; |
| 1095 | int new_ref; |
| 1096 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1097 | /* New Lua coroutine. */ |
| 1098 | T = lua_newthread(gL.T); |
| 1099 | if (!T) |
| 1100 | return 0; |
| 1101 | |
| 1102 | /* Copy last error message. */ |
| 1103 | if (keep_msg) |
| 1104 | lua_xmove(lua->T, T, 1); |
| 1105 | |
| 1106 | /* Copy data between the coroutines. */ |
| 1107 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1108 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1109 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1110 | |
| 1111 | /* Destroy old data. */ |
| 1112 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1113 | |
| 1114 | /* The thread is garbage collected by Lua. */ |
| 1115 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1116 | |
| 1117 | /* Fill the struct with the new coroutine values. */ |
| 1118 | lua->Mref = new_ref; |
| 1119 | lua->T = T; |
| 1120 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1121 | |
| 1122 | /* Set context. */ |
| 1123 | hlua_sethlua(lua); |
| 1124 | |
| 1125 | return 1; |
| 1126 | } |
| 1127 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1128 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1129 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1130 | struct hlua *hlua; |
| 1131 | |
| 1132 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1133 | hlua = hlua_gethlua(L); |
| 1134 | if (!hlua) |
| 1135 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1136 | |
| 1137 | /* Lua cannot yield when its returning from a function, |
| 1138 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1139 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1140 | */ |
| 1141 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1142 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1143 | return; |
| 1144 | } |
| 1145 | |
| 1146 | /* restore the interrupt condition. */ |
| 1147 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1148 | |
| 1149 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1150 | * If the state is not yieldable, trying yield causes an error. |
| 1151 | */ |
| 1152 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1153 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1154 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1155 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1156 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1157 | hlua->run_time += now_ms - hlua->start_time; |
| 1158 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1159 | lua_pushfstring(L, "execution timeout"); |
| 1160 | WILL_LJMP(lua_error(L)); |
| 1161 | } |
| 1162 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1163 | /* Update the start time. */ |
| 1164 | hlua->start_time = now_ms; |
| 1165 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1166 | /* Try to interrupt the process at the end of the current |
| 1167 | * unyieldable function. |
| 1168 | */ |
| 1169 | 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] | 1170 | } |
| 1171 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | /* This function start or resumes the Lua stack execution. If the flag |
| 1173 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1174 | * The function return an error. |
| 1175 | * |
| 1176 | * The function can returns 4 values: |
| 1177 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1178 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1179 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1180 | * - 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] | 1181 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1182 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1184 | * 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] | 1185 | * LUA code. |
| 1186 | */ |
| 1187 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1188 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1189 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1190 | int nres; |
| 1191 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1192 | int ret; |
| 1193 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1194 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1195 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1196 | /* Initialise run time counter. */ |
| 1197 | if (!HLUA_IS_RUNNING(lua)) |
| 1198 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1199 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1200 | /* Lock the whole Lua execution. This lock must be before the |
| 1201 | * label "resume_execution". |
| 1202 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1203 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1204 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1205 | resume_execution: |
| 1206 | |
| 1207 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1208 | * instructions. it is used for preventing infinite loops. |
| 1209 | */ |
| 1210 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1211 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1212 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1213 | HLUA_SET_RUN(lua); |
| 1214 | HLUA_CLR_CTRLYIELD(lua); |
| 1215 | HLUA_CLR_WAKERESWR(lua); |
| 1216 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1217 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1218 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1219 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1220 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1221 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1222 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1223 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1224 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1225 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1226 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1227 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1228 | switch (ret) { |
| 1229 | |
| 1230 | case LUA_OK: |
| 1231 | ret = HLUA_E_OK; |
| 1232 | break; |
| 1233 | |
| 1234 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1235 | /* Check if the execution timeout is expired. It it is the case, we |
| 1236 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1237 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1238 | tv_update_date(0, 1); |
| 1239 | lua->run_time += now_ms - lua->start_time; |
| 1240 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1241 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1242 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1243 | break; |
| 1244 | } |
| 1245 | /* Process the forced yield. if the general yield is not allowed or |
| 1246 | * if no task were associated this the current Lua execution |
| 1247 | * coroutine, we resume the execution. Else we want to return in the |
| 1248 | * scheduler and we want to be waked up again, to continue the |
| 1249 | * current Lua execution. So we schedule our own task. |
| 1250 | */ |
| 1251 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1252 | if (!yield_allowed || !lua->task) |
| 1253 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1254 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1255 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1256 | if (!yield_allowed) { |
| 1257 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1258 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1259 | break; |
| 1260 | } |
| 1261 | ret = HLUA_E_AGAIN; |
| 1262 | break; |
| 1263 | |
| 1264 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1265 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1266 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1267 | * because the errors ares the only one mean to return immediately |
| 1268 | * from and lua execution. |
| 1269 | */ |
| 1270 | if (lua->flags & HLUA_EXIT) { |
| 1271 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1272 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1273 | break; |
| 1274 | } |
| 1275 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1276 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1277 | if (!lua_checkstack(lua->T, 1)) { |
| 1278 | ret = HLUA_E_ERR; |
| 1279 | break; |
| 1280 | } |
| 1281 | msg = lua_tostring(lua->T, -1); |
| 1282 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1283 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1284 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1285 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1286 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1287 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1288 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1289 | ret = HLUA_E_ERRMSG; |
| 1290 | break; |
| 1291 | |
| 1292 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1293 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1294 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1295 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1296 | break; |
| 1297 | |
| 1298 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1299 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1300 | if (!lua_checkstack(lua->T, 1)) { |
| 1301 | ret = HLUA_E_ERR; |
| 1302 | break; |
| 1303 | } |
| 1304 | msg = lua_tostring(lua->T, -1); |
| 1305 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1306 | lua_pop(lua->T, 1); |
| 1307 | if (msg) |
| 1308 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1309 | else |
| 1310 | lua_pushfstring(lua->T, "message handler error"); |
| 1311 | ret = HLUA_E_ERRMSG; |
| 1312 | break; |
| 1313 | |
| 1314 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1315 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1316 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1317 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1318 | break; |
| 1319 | } |
| 1320 | |
| 1321 | switch (ret) { |
| 1322 | case HLUA_E_AGAIN: |
| 1323 | break; |
| 1324 | |
| 1325 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1326 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1327 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1328 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1329 | break; |
| 1330 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1331 | case HLUA_E_ETMOUT: |
| 1332 | case HLUA_E_NOMEM: |
| 1333 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1334 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1335 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1336 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1337 | hlua_ctx_renew(lua, 0); |
| 1338 | break; |
| 1339 | |
| 1340 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1341 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1342 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1343 | break; |
| 1344 | } |
| 1345 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1346 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1347 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1348 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1349 | return ret; |
| 1350 | } |
| 1351 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1352 | /* This function exit the current code. */ |
| 1353 | __LJMP static int hlua_done(lua_State *L) |
| 1354 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1355 | struct hlua *hlua; |
| 1356 | |
| 1357 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1358 | hlua = hlua_gethlua(L); |
| 1359 | if (!hlua) |
| 1360 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1361 | |
| 1362 | hlua->flags |= HLUA_EXIT; |
| 1363 | WILL_LJMP(lua_error(L)); |
| 1364 | |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1368 | /* This function is an LUA binding. It provides a function |
| 1369 | * for deleting ACL from a referenced ACL file. |
| 1370 | */ |
| 1371 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1372 | { |
| 1373 | const char *name; |
| 1374 | const char *key; |
| 1375 | struct pat_ref *ref; |
| 1376 | |
| 1377 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1378 | |
| 1379 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1380 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1381 | |
| 1382 | ref = pat_ref_lookup(name); |
| 1383 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1384 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1385 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1386 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1387 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1388 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | /* This function is an LUA binding. It provides a function |
| 1393 | * for deleting map entry from a referenced map file. |
| 1394 | */ |
| 1395 | static int hlua_del_map(lua_State *L) |
| 1396 | { |
| 1397 | const char *name; |
| 1398 | const char *key; |
| 1399 | struct pat_ref *ref; |
| 1400 | |
| 1401 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1402 | |
| 1403 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1404 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1405 | |
| 1406 | ref = pat_ref_lookup(name); |
| 1407 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1408 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1409 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1410 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1411 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1412 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | /* This function is an LUA binding. It provides a function |
| 1417 | * for adding ACL pattern from a referenced ACL file. |
| 1418 | */ |
| 1419 | static int hlua_add_acl(lua_State *L) |
| 1420 | { |
| 1421 | const char *name; |
| 1422 | const char *key; |
| 1423 | struct pat_ref *ref; |
| 1424 | |
| 1425 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1426 | |
| 1427 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1428 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1429 | |
| 1430 | ref = pat_ref_lookup(name); |
| 1431 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1432 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1433 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1434 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1435 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1436 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1437 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1438 | return 0; |
| 1439 | } |
| 1440 | |
| 1441 | /* This function is an LUA binding. It provides a function |
| 1442 | * for setting map pattern and sample from a referenced map |
| 1443 | * file. |
| 1444 | */ |
| 1445 | static int hlua_set_map(lua_State *L) |
| 1446 | { |
| 1447 | const char *name; |
| 1448 | const char *key; |
| 1449 | const char *value; |
| 1450 | struct pat_ref *ref; |
| 1451 | |
| 1452 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1453 | |
| 1454 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1455 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1456 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1457 | |
| 1458 | ref = pat_ref_lookup(name); |
| 1459 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1460 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1461 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1462 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1463 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1464 | pat_ref_set(ref, key, value, NULL); |
| 1465 | else |
| 1466 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1467 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1468 | return 0; |
| 1469 | } |
| 1470 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1471 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1472 | * an integer or user data. This data is associated with a metatable. This |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1473 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1474 | * the name of the object (_G[<name>] = <metable> ). |
| 1475 | * |
| 1476 | * A metable is a table that modify the standard behavior of a standard |
| 1477 | * access to the associated data. The entries of this new metatable are |
| 1478 | * defined as is: |
| 1479 | * |
| 1480 | * http://lua-users.org/wiki/MetatableEvents |
| 1481 | * |
| 1482 | * __index |
| 1483 | * |
| 1484 | * we access an absent field in a table, the result is nil. This is |
| 1485 | * true, but it is not the whole truth. Actually, such access triggers |
| 1486 | * the interpreter to look for an __index metamethod: If there is no |
| 1487 | * such method, as usually happens, then the access results in nil; |
| 1488 | * otherwise, the metamethod will provide the result. |
| 1489 | * |
| 1490 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1491 | * the key does not appear in the table, but the metatable has an __index |
| 1492 | * property: |
| 1493 | * |
| 1494 | * - if the value is a function, the function is called, passing in the |
| 1495 | * table and the key; the return value of that function is returned as |
| 1496 | * the result. |
| 1497 | * |
| 1498 | * - if the value is another table, the value of the key in that table is |
| 1499 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1500 | * table's metatable has an __index property, then it continues on up) |
| 1501 | * |
| 1502 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1503 | * |
| 1504 | * http://www.lua.org/pil/13.4.1.html |
| 1505 | * |
| 1506 | * __newindex |
| 1507 | * |
| 1508 | * Like __index, but control property assignment. |
| 1509 | * |
| 1510 | * __mode - Control weak references. A string value with one or both |
| 1511 | * of the characters 'k' and 'v' which specifies that the the |
| 1512 | * keys and/or values in the table are weak references. |
| 1513 | * |
| 1514 | * __call - Treat a table like a function. When a table is followed by |
| 1515 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1516 | * a __call key pointing to a function, that function is invoked |
| 1517 | * (passing any specified arguments) and the return value is |
| 1518 | * returned. |
| 1519 | * |
| 1520 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1521 | * called, if the metatable for myTable has a __metatable |
| 1522 | * key, the value of that key is returned instead of the |
| 1523 | * actual metatable. |
| 1524 | * |
| 1525 | * __tostring - Control string representation. When the builtin |
| 1526 | * "tostring( myTable )" function is called, if the metatable |
| 1527 | * for myTable has a __tostring property set to a function, |
| 1528 | * that function is invoked (passing myTable to it) and the |
| 1529 | * return value is used as the string representation. |
| 1530 | * |
| 1531 | * __len - Control table length. When the table length is requested using |
| 1532 | * the length operator ( '#' ), if the metatable for myTable has |
| 1533 | * a __len key pointing to a function, that function is invoked |
| 1534 | * (passing myTable to it) and the return value used as the value |
| 1535 | * of "#myTable". |
| 1536 | * |
| 1537 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1538 | * collected, if the metatable has a __gc field pointing to a |
| 1539 | * function, that function is first invoked, passing the userdata |
| 1540 | * to it. The __gc metamethod is not called for tables. |
| 1541 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1542 | * |
| 1543 | * Special metamethods for redefining standard operators: |
| 1544 | * http://www.lua.org/pil/13.1.html |
| 1545 | * |
| 1546 | * __add "+" |
| 1547 | * __sub "-" |
| 1548 | * __mul "*" |
| 1549 | * __div "/" |
| 1550 | * __unm "!" |
| 1551 | * __pow "^" |
| 1552 | * __concat ".." |
| 1553 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1554 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1555 | * http://www.lua.org/pil/13.2.html |
| 1556 | * |
| 1557 | * __eq "==" |
| 1558 | * __lt "<" |
| 1559 | * __le "<=" |
| 1560 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1561 | |
| 1562 | /* |
| 1563 | * |
| 1564 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1565 | * Class Map |
| 1566 | * |
| 1567 | * |
| 1568 | */ |
| 1569 | |
| 1570 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1571 | * a class session, otherwise it throws an error. |
| 1572 | */ |
| 1573 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1574 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1575 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /* This function is the map constructor. It don't need |
| 1579 | * the class Map object. It creates and return a new Map |
| 1580 | * object. It must be called only during "body" or "init" |
| 1581 | * context because it process some filesystem accesses. |
| 1582 | */ |
| 1583 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1584 | { |
| 1585 | const char *fn; |
| 1586 | int match = PAT_MATCH_STR; |
| 1587 | struct sample_conv conv; |
| 1588 | const char *file = ""; |
| 1589 | int line = 0; |
| 1590 | lua_Debug ar; |
| 1591 | char *err = NULL; |
| 1592 | struct arg args[2]; |
| 1593 | |
| 1594 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1595 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1596 | |
| 1597 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1598 | |
| 1599 | if (lua_gettop(L) >= 2) { |
| 1600 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1601 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1602 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1603 | } |
| 1604 | |
| 1605 | /* Get Lua filename and line number. */ |
| 1606 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1607 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1608 | if (ar.currentline > 0) { /* is there info? */ |
| 1609 | file = ar.short_src; |
| 1610 | line = ar.currentline; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | /* fill fake sample_conv struct. */ |
| 1615 | conv.kw = ""; /* unused. */ |
| 1616 | conv.process = NULL; /* unused. */ |
| 1617 | conv.arg_mask = 0; /* unused. */ |
| 1618 | conv.val_args = NULL; /* unused. */ |
| 1619 | conv.out_type = SMP_T_STR; |
| 1620 | conv.private = (void *)(long)match; |
| 1621 | switch (match) { |
| 1622 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1623 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1624 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1625 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1626 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1627 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1628 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1629 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1630 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1631 | default: |
| 1632 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1633 | } |
| 1634 | |
| 1635 | /* fill fake args. */ |
| 1636 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1637 | args[0].data.str.area = strdup(fn); |
| 1638 | args[0].data.str.data = strlen(fn); |
| 1639 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1640 | args[1].type = ARGT_STOP; |
| 1641 | |
| 1642 | /* load the map. */ |
| 1643 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1644 | /* error case: we cant use luaL_error because we must |
| 1645 | * free the err variable. |
| 1646 | */ |
| 1647 | luaL_where(L, 1); |
| 1648 | lua_pushfstring(L, "'new': %s.", err); |
| 1649 | lua_concat(L, 2); |
| 1650 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1651 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1652 | WILL_LJMP(lua_error(L)); |
| 1653 | } |
| 1654 | |
| 1655 | /* create the lua object. */ |
| 1656 | lua_newtable(L); |
| 1657 | lua_pushlightuserdata(L, args[0].data.map); |
| 1658 | lua_rawseti(L, -2, 0); |
| 1659 | |
| 1660 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1661 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1662 | lua_setmetatable(L, -2); |
| 1663 | |
| 1664 | |
| 1665 | return 1; |
| 1666 | } |
| 1667 | |
| 1668 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1669 | { |
| 1670 | struct map_descriptor *desc; |
| 1671 | struct pattern *pat; |
| 1672 | struct sample smp; |
| 1673 | |
| 1674 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1675 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1676 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1677 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1678 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1679 | } |
| 1680 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1681 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1682 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1683 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry Fournier | 91dc0c0 | 2020-11-10 20:38:20 +0100 | [diff] [blame] | 1684 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1688 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1689 | if (str) |
| 1690 | lua_pushstring(L, ""); |
| 1691 | else |
| 1692 | lua_pushnil(L); |
| 1693 | return 1; |
| 1694 | } |
| 1695 | |
| 1696 | /* 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] | 1697 | 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] | 1698 | return 1; |
| 1699 | } |
| 1700 | |
| 1701 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1702 | { |
| 1703 | return _hlua_map_lookup(L, 0); |
| 1704 | } |
| 1705 | |
| 1706 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1707 | { |
| 1708 | return _hlua_map_lookup(L, 1); |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | * |
| 1713 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | * Class Socket |
| 1715 | * |
| 1716 | * |
| 1717 | */ |
| 1718 | |
| 1719 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1720 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1721 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | /* 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] | 1725 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1726 | * received. |
| 1727 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1728 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1729 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1730 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1731 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1732 | if (appctx->ctx.hlua_cosocket.die) { |
| 1733 | si_shutw(si); |
| 1734 | si_shutr(si); |
| 1735 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1736 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1737 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1738 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1739 | } |
| 1740 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1741 | /* If we cant write, wakeup the pending write signals. */ |
| 1742 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1743 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1744 | |
| 1745 | /* If we cant read, wakeup the pending read signals. */ |
| 1746 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1747 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1748 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1749 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1750 | * to be notified whenever the connection completes. |
| 1751 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1752 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1753 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1754 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1755 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1756 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1757 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1758 | |
| 1759 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1760 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1761 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1762 | /* 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] | 1763 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1764 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1765 | |
| 1766 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1767 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1768 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1769 | |
| 1770 | /* Some data were injected in the buffer, notify the stream |
| 1771 | * interface. |
| 1772 | */ |
| 1773 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1774 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1775 | |
| 1776 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1777 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1778 | */ |
| 1779 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1780 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1781 | } |
| 1782 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1783 | /* This function is called when the "struct stream" is destroyed. |
| 1784 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1785 | * Wake all the pending signals. |
| 1786 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1787 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1788 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1789 | struct xref *peer; |
| 1790 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1792 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1793 | if (peer) |
| 1794 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1795 | |
| 1796 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1797 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1798 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1802 | * uses this object. If the stream does not exists, just quit. |
| 1803 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1804 | * pending signal can rest in the read and write lists. destroy |
| 1805 | * it. |
| 1806 | */ |
| 1807 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1808 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1809 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1810 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1811 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1812 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1813 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1814 | |
| 1815 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1816 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1817 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1818 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1819 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1821 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1822 | appctx->ctx.hlua_cosocket.die = 1; |
| 1823 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1824 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1825 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1826 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | return 0; |
| 1828 | } |
| 1829 | |
| 1830 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1831 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1833 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1834 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1835 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1837 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1838 | struct hlua *hlua; |
| 1839 | |
| 1840 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1841 | hlua = hlua_gethlua(L); |
| 1842 | if (!hlua) |
| 1843 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1844 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1845 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1846 | |
| 1847 | /* Check if we run on the same thread than the xreator thread. |
| 1848 | * We cannot access to the socket if the thread is different. |
| 1849 | */ |
| 1850 | if (socket->tid != tid) |
| 1851 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1852 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1853 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1854 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1856 | |
| 1857 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1858 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1859 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1860 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1861 | appctx->ctx.hlua_cosocket.die = 1; |
| 1862 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1863 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1864 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1865 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | return 0; |
| 1867 | } |
| 1868 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1869 | /* The close function calls close_helper. |
| 1870 | */ |
| 1871 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1872 | { |
| 1873 | MAY_LJMP(check_args(L, 1, "close")); |
| 1874 | return hlua_socket_close_helper(L); |
| 1875 | } |
| 1876 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | /* This Lua function assumes that the stack contain three parameters. |
| 1878 | * 1 - USERDATA containing a struct socket |
| 1879 | * 2 - INTEGER with values of the macro defined below |
| 1880 | * If the integer is -1, we must read at most one line. |
| 1881 | * If the integer is -2, we ust read all the data until the |
| 1882 | * end of the stream. |
| 1883 | * If the integer is positive value, we must read a number of |
| 1884 | * bytes corresponding to this value. |
| 1885 | */ |
| 1886 | #define HLSR_READ_LINE (-1) |
| 1887 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1888 | __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] | 1889 | { |
| 1890 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1891 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1892 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1894 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1896 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1897 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1898 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1899 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1900 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1901 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1902 | struct stream_interface *si; |
| 1903 | struct stream *s; |
| 1904 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1905 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1907 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1908 | hlua = hlua_gethlua(L); |
| 1909 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | /* Check if this lua stack is schedulable. */ |
| 1911 | if (!hlua || !hlua->task) |
| 1912 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1913 | "'frontend', 'backend' or 'task'")); |
| 1914 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1915 | /* Check if we run on the same thread than the xreator thread. |
| 1916 | * We cannot access to the socket if the thread is different. |
| 1917 | */ |
| 1918 | if (socket->tid != tid) |
| 1919 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1920 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1921 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1922 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1923 | if (!peer) |
| 1924 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1925 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1926 | si = appctx->owner; |
| 1927 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1929 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1931 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1932 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | if (nblk < 0) /* Connection close. */ |
| 1934 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1935 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1937 | |
| 1938 | /* remove final \r\n. */ |
| 1939 | if (nblk == 1) { |
| 1940 | if (blk1[len1-1] == '\n') { |
| 1941 | len1--; |
| 1942 | skip_at_end++; |
| 1943 | if (blk1[len1-1] == '\r') { |
| 1944 | len1--; |
| 1945 | skip_at_end++; |
| 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | else { |
| 1950 | if (blk2[len2-1] == '\n') { |
| 1951 | len2--; |
| 1952 | skip_at_end++; |
| 1953 | if (blk2[len2-1] == '\r') { |
| 1954 | len2--; |
| 1955 | skip_at_end++; |
| 1956 | } |
| 1957 | } |
| 1958 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1963 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1964 | if (nblk < 0) /* Connection close. */ |
| 1965 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1966 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | goto connection_empty; |
| 1968 | } |
| 1969 | |
| 1970 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1972 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | if (nblk < 0) /* Connection close. */ |
| 1974 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1975 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | goto connection_empty; |
| 1977 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1978 | missing_bytes = wanted - socket->b.n; |
| 1979 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1981 | len1 = missing_bytes; |
| 1982 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1983 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | len = len1; |
| 1987 | |
| 1988 | luaL_addlstring(&socket->b, blk1, len1); |
| 1989 | if (nblk == 2) { |
| 1990 | len += len2; |
| 1991 | luaL_addlstring(&socket->b, blk2, len2); |
| 1992 | } |
| 1993 | |
| 1994 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1995 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | |
| 1997 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1998 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | |
| 2000 | /* If the pattern reclaim to read all the data |
| 2001 | * in the connection, got out. |
| 2002 | */ |
| 2003 | if (wanted == HLSR_READ_ALL) |
| 2004 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2005 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | goto connection_empty; |
| 2007 | |
| 2008 | /* Return result. */ |
| 2009 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2010 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | return 1; |
| 2012 | |
| 2013 | connection_closed: |
| 2014 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2015 | xref_unlock(&socket->xref, peer); |
| 2016 | |
| 2017 | no_peer: |
| 2018 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2019 | /* If the buffer containds data. */ |
| 2020 | if (socket->b.n > 0) { |
| 2021 | luaL_pushresult(&socket->b); |
| 2022 | return 1; |
| 2023 | } |
| 2024 | lua_pushnil(L); |
| 2025 | lua_pushstring(L, "connection closed."); |
| 2026 | return 2; |
| 2027 | |
| 2028 | connection_empty: |
| 2029 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2030 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2031 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2033 | } |
| 2034 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2035 | 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] | 2036 | return 0; |
| 2037 | } |
| 2038 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2039 | /* This Lua function gets two parameters. The first one can be string |
| 2040 | * or a number. If the string is "*l", the user requires one line. If |
| 2041 | * 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] | 2042 | * If the value is a number, the user require a number of bytes equal |
| 2043 | * to the value. The default value is "*l" (a line). |
| 2044 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2045 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | * integer takes this values: |
| 2047 | * -1 : read a line |
| 2048 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2049 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2051 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2052 | * concatenated with the read data. |
| 2053 | */ |
| 2054 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2055 | { |
| 2056 | int wanted = HLSR_READ_LINE; |
| 2057 | const char *pattern; |
| 2058 | int type; |
| 2059 | char *error; |
| 2060 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2061 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | |
| 2063 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2064 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2065 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2066 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2068 | /* Check if we run on the same thread than the xreator thread. |
| 2069 | * We cannot access to the socket if the thread is different. |
| 2070 | */ |
| 2071 | if (socket->tid != tid) |
| 2072 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2073 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | /* check for pattern. */ |
| 2075 | if (lua_gettop(L) >= 2) { |
| 2076 | type = lua_type(L, 2); |
| 2077 | if (type == LUA_TSTRING) { |
| 2078 | pattern = lua_tostring(L, 2); |
| 2079 | if (strcmp(pattern, "*a") == 0) |
| 2080 | wanted = HLSR_READ_ALL; |
| 2081 | else if (strcmp(pattern, "*l") == 0) |
| 2082 | wanted = HLSR_READ_LINE; |
| 2083 | else { |
| 2084 | wanted = strtoll(pattern, &error, 10); |
| 2085 | if (*error != '\0') |
| 2086 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2087 | } |
| 2088 | } |
| 2089 | else if (type == LUA_TNUMBER) { |
| 2090 | wanted = lua_tointeger(L, 2); |
| 2091 | if (wanted < 0) |
| 2092 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | /* Set pattern. */ |
| 2097 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2098 | |
| 2099 | /* Check if we would replace the top by itself. */ |
| 2100 | if (lua_gettop(L) != 2) |
| 2101 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2102 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2103 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2104 | luaL_buffinit(L, &socket->b); |
| 2105 | |
| 2106 | /* Check prefix. */ |
| 2107 | if (lua_gettop(L) >= 3) { |
| 2108 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2109 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2110 | pattern = lua_tolstring(L, 3, &len); |
| 2111 | luaL_addlstring(&socket->b, pattern, len); |
| 2112 | } |
| 2113 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2114 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2118 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2119 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2120 | 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] | 2121 | { |
| 2122 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2123 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2124 | struct appctx *appctx; |
| 2125 | size_t buf_len; |
| 2126 | const char *buf; |
| 2127 | int len; |
| 2128 | int send_len; |
| 2129 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2130 | struct xref *peer; |
| 2131 | struct stream_interface *si; |
| 2132 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2133 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2134 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2135 | hlua = hlua_gethlua(L); |
| 2136 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | /* Check if this lua stack is schedulable. */ |
| 2138 | if (!hlua || !hlua->task) |
| 2139 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2140 | "'frontend', 'backend' or 'task'")); |
| 2141 | |
| 2142 | /* Get object */ |
| 2143 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2144 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2145 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2147 | /* Check if we run on the same thread than the xreator thread. |
| 2148 | * We cannot access to the socket if the thread is different. |
| 2149 | */ |
| 2150 | if (socket->tid != tid) |
| 2151 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2152 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2153 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2154 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2155 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2156 | lua_pushinteger(L, -1); |
| 2157 | return 1; |
| 2158 | } |
| 2159 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2160 | si = appctx->owner; |
| 2161 | s = si_strm(si); |
| 2162 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2164 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2165 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | lua_pushinteger(L, -1); |
| 2167 | return 1; |
| 2168 | } |
| 2169 | |
| 2170 | /* Update the input buffer data. */ |
| 2171 | buf += sent; |
| 2172 | send_len = buf_len - sent; |
| 2173 | |
| 2174 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2175 | if (sent >= buf_len) { |
| 2176 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2178 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2179 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2180 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2181 | * the request buffer if its not required. |
| 2182 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2183 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2184 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2185 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2186 | } |
| 2187 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2188 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2189 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2190 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2191 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2192 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2193 | |
| 2194 | /* send data */ |
| 2195 | if (len < send_len) |
| 2196 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2197 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | |
| 2199 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2200 | * the data" (-2) are not expected because the available length |
| 2201 | * is tested. |
| 2202 | * Other unknown error are also not expected. |
| 2203 | */ |
| 2204 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2205 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2206 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2207 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2208 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2209 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2210 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2211 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2212 | return 1; |
| 2213 | } |
| 2214 | |
| 2215 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2216 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2217 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2218 | s->req.rex = TICK_ETERNITY; |
| 2219 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | |
| 2221 | /* Update length sent. */ |
| 2222 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2223 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | |
| 2225 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2226 | if (sent + len >= buf_len) { |
| 2227 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2229 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | |
| 2231 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2232 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2233 | xref_unlock(&socket->xref, peer); |
| 2234 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2235 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2236 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2237 | 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] | 2238 | return 0; |
| 2239 | } |
| 2240 | |
| 2241 | /* This function initiate the send of data. It just check the input |
| 2242 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2243 | * 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] | 2244 | * "hlua_socket_write_yield" that can yield. |
| 2245 | * |
| 2246 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2247 | * the associated object. The second is a string buffer. The third is |
| 2248 | * a facultative integer that represents where is the buffer position |
| 2249 | * of the start of the data that can send. The first byte is the |
| 2250 | * position "1". The default value is "1". The fourth argument is a |
| 2251 | * facultative integer that represents where is the buffer position |
| 2252 | * of the end of the data that can send. The default is the last byte. |
| 2253 | */ |
| 2254 | static int hlua_socket_send(struct lua_State *L) |
| 2255 | { |
| 2256 | int i; |
| 2257 | int j; |
| 2258 | const char *buf; |
| 2259 | size_t buf_len; |
| 2260 | |
| 2261 | /* Check number of arguments. */ |
| 2262 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2263 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2264 | |
| 2265 | /* Get the string. */ |
| 2266 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2267 | |
| 2268 | /* Get and check j. */ |
| 2269 | if (lua_gettop(L) == 4) { |
| 2270 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2271 | if (j < 0) |
| 2272 | j = buf_len + j + 1; |
| 2273 | if (j > buf_len) |
| 2274 | j = buf_len + 1; |
| 2275 | lua_pop(L, 1); |
| 2276 | } |
| 2277 | else |
| 2278 | j = buf_len; |
| 2279 | |
| 2280 | /* Get and check i. */ |
| 2281 | if (lua_gettop(L) == 3) { |
| 2282 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2283 | if (i < 0) |
| 2284 | i = buf_len + i + 1; |
| 2285 | if (i > buf_len) |
| 2286 | i = buf_len + 1; |
| 2287 | lua_pop(L, 1); |
| 2288 | } else |
| 2289 | i = 1; |
| 2290 | |
| 2291 | /* Check bth i and j. */ |
| 2292 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2293 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | return 1; |
| 2295 | } |
| 2296 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2297 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | return 1; |
| 2299 | } |
| 2300 | if (i == 0) |
| 2301 | i = 1; |
| 2302 | if (j == 0) |
| 2303 | j = 1; |
| 2304 | |
| 2305 | /* Pop the string. */ |
| 2306 | lua_pop(L, 1); |
| 2307 | |
| 2308 | /* Update the buffer length. */ |
| 2309 | buf += i - 1; |
| 2310 | buf_len = j - i + 1; |
| 2311 | lua_pushlstring(L, buf, buf_len); |
| 2312 | |
| 2313 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2314 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2316 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2319 | #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] | 2320 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2321 | { |
| 2322 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2323 | int ret; |
| 2324 | int len; |
| 2325 | char *p; |
| 2326 | |
| 2327 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2328 | if (ret <= 0) { |
| 2329 | lua_pushnil(L); |
| 2330 | return 1; |
| 2331 | } |
| 2332 | |
| 2333 | if (ret == AF_UNIX) { |
| 2334 | lua_pushstring(L, buffer+1); |
| 2335 | return 1; |
| 2336 | } |
| 2337 | else if (ret == AF_INET6) { |
| 2338 | buffer[0] = '['; |
| 2339 | len = strlen(buffer); |
| 2340 | buffer[len] = ']'; |
| 2341 | len++; |
| 2342 | buffer[len] = ':'; |
| 2343 | len++; |
| 2344 | p = buffer; |
| 2345 | } |
| 2346 | else if (ret == AF_INET) { |
| 2347 | p = buffer + 1; |
| 2348 | len = strlen(p); |
| 2349 | p[len] = ':'; |
| 2350 | len++; |
| 2351 | } |
| 2352 | else { |
| 2353 | lua_pushnil(L); |
| 2354 | return 1; |
| 2355 | } |
| 2356 | |
| 2357 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2358 | lua_pushnil(L); |
| 2359 | return 1; |
| 2360 | } |
| 2361 | |
| 2362 | lua_pushstring(L, p); |
| 2363 | return 1; |
| 2364 | } |
| 2365 | |
| 2366 | /* Returns information about the peer of the connection. */ |
| 2367 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2368 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2369 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2370 | struct xref *peer; |
| 2371 | struct appctx *appctx; |
| 2372 | struct stream_interface *si; |
| 2373 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2374 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2375 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2377 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2378 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2379 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2380 | /* Check if we run on the same thread than the xreator thread. |
| 2381 | * We cannot access to the socket if the thread is different. |
| 2382 | */ |
| 2383 | if (socket->tid != tid) |
| 2384 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2385 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2386 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2387 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2388 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | lua_pushnil(L); |
| 2390 | return 1; |
| 2391 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2392 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2393 | si = appctx->owner; |
| 2394 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2396 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2397 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2398 | lua_pushnil(L); |
| 2399 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | } |
| 2401 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2402 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2403 | xref_unlock(&socket->xref, peer); |
| 2404 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | /* Returns information about my connection side. */ |
| 2408 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2409 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2410 | struct hlua_socket *socket; |
| 2411 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2412 | struct appctx *appctx; |
| 2413 | struct xref *peer; |
| 2414 | struct stream_interface *si; |
| 2415 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2416 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2417 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2419 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2420 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2421 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2422 | /* Check if we run on the same thread than the xreator thread. |
| 2423 | * We cannot access to the socket if the thread is different. |
| 2424 | */ |
| 2425 | if (socket->tid != tid) |
| 2426 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2427 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2428 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2429 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2430 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | lua_pushnil(L); |
| 2432 | return 1; |
| 2433 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2435 | si = appctx->owner; |
| 2436 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2437 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2438 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2439 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2440 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2441 | lua_pushnil(L); |
| 2442 | return 1; |
| 2443 | } |
| 2444 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2445 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | xref_unlock(&socket->xref, peer); |
| 2447 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2451 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2452 | .obj_type = OBJ_TYPE_APPLET, |
| 2453 | .name = "<LUA_TCP>", |
| 2454 | .fct = hlua_socket_handler, |
| 2455 | .release = hlua_socket_release, |
| 2456 | }; |
| 2457 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2458 | __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] | 2459 | { |
| 2460 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2461 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2462 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2463 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2464 | struct stream_interface *si; |
| 2465 | struct stream *s; |
| 2466 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2467 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2468 | hlua = hlua_gethlua(L); |
| 2469 | if (!hlua) |
| 2470 | return 0; |
| 2471 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2472 | /* Check if we run on the same thread than the xreator thread. |
| 2473 | * We cannot access to the socket if the thread is different. |
| 2474 | */ |
| 2475 | if (socket->tid != tid) |
| 2476 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2477 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2478 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2479 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2480 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2481 | lua_pushnil(L); |
| 2482 | lua_pushstring(L, "Can't connect"); |
| 2483 | return 2; |
| 2484 | } |
| 2485 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2486 | si = appctx->owner; |
| 2487 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2489 | /* Check if we run on the same thread than the xreator thread. |
| 2490 | * We cannot access to the socket if the thread is different. |
| 2491 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2492 | if (socket->tid != tid) { |
| 2493 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2494 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2495 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2496 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2497 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2498 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2499 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | lua_pushnil(L); |
| 2501 | lua_pushstring(L, "Can't connect"); |
| 2502 | return 2; |
| 2503 | } |
| 2504 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2505 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | |
| 2507 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2508 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2509 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2510 | lua_pushinteger(L, 1); |
| 2511 | return 1; |
| 2512 | } |
| 2513 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2514 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2515 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2517 | } |
| 2518 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2519 | 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] | 2520 | return 0; |
| 2521 | } |
| 2522 | |
| 2523 | /* This function fail or initite the connection. */ |
| 2524 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2525 | { |
| 2526 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2527 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2529 | struct hlua *hlua; |
| 2530 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2531 | int low, high; |
| 2532 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2533 | struct xref *peer; |
| 2534 | struct stream_interface *si; |
| 2535 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2536 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2537 | if (lua_gettop(L) < 2) |
| 2538 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2539 | |
| 2540 | /* Get args. */ |
| 2541 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2542 | |
| 2543 | /* Check if we run on the same thread than the xreator thread. |
| 2544 | * We cannot access to the socket if the thread is different. |
| 2545 | */ |
| 2546 | if (socket->tid != tid) |
| 2547 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2548 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2550 | if (lua_gettop(L) >= 3) { |
| 2551 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2552 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2553 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2554 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2555 | * that are not enclosed within square brackets. |
| 2556 | */ |
| 2557 | if (port > 0) { |
| 2558 | luaL_buffinit(L, &b); |
| 2559 | luaL_addstring(&b, ip); |
| 2560 | luaL_addchar(&b, ':'); |
| 2561 | luaL_pushresult(&b); |
| 2562 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2563 | } |
| 2564 | } |
| 2565 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2566 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2567 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2568 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2569 | lua_pushnil(L); |
| 2570 | return 1; |
| 2571 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2572 | |
| 2573 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2574 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2575 | if (!addr) { |
| 2576 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2577 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2578 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2579 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2581 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2582 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2583 | if (port == -1) { |
| 2584 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2585 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2586 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2587 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2588 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2589 | if (port == -1) { |
| 2590 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2591 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2592 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2593 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2594 | } |
| 2595 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2596 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2597 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2598 | si = appctx->owner; |
| 2599 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2600 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2601 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2602 | xref_unlock(&socket->xref, peer); |
| 2603 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2604 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2605 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2607 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2608 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2609 | if (!hlua) |
| 2610 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2611 | |
| 2612 | /* inform the stream that we want to be notified whenever the |
| 2613 | * connection completes. |
| 2614 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2615 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2616 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2617 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2618 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2619 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2620 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2621 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2622 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2623 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2624 | } |
| 2625 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2626 | |
| 2627 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2628 | /* Return yield waiting for connection. */ |
| 2629 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2630 | 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] | 2631 | |
| 2632 | return 0; |
| 2633 | } |
| 2634 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2635 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2637 | { |
| 2638 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | struct xref *peer; |
| 2640 | struct appctx *appctx; |
| 2641 | struct stream_interface *si; |
| 2642 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2643 | |
| 2644 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2645 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2646 | |
| 2647 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2648 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2649 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2650 | lua_pushnil(L); |
| 2651 | return 1; |
| 2652 | } |
| 2653 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2654 | si = appctx->owner; |
| 2655 | s = si_strm(si); |
| 2656 | |
| 2657 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2658 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2659 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2660 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2661 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | |
| 2663 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2664 | { |
| 2665 | return 0; |
| 2666 | } |
| 2667 | |
| 2668 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2669 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2670 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2671 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2672 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2673 | struct xref *peer; |
| 2674 | struct appctx *appctx; |
| 2675 | struct stream_interface *si; |
| 2676 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2677 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2678 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2679 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2680 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2681 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2682 | /* convert the timeout to millis */ |
| 2683 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2684 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2685 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2686 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2687 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2688 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2689 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2690 | 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] | 2691 | |
| 2692 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2693 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2694 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2695 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2696 | /* Check if we run on the same thread than the xreator thread. |
| 2697 | * We cannot access to the socket if the thread is different. |
| 2698 | */ |
| 2699 | if (socket->tid != tid) |
| 2700 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2701 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2702 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2703 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2704 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2705 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2706 | WILL_LJMP(lua_error(L)); |
| 2707 | return 0; |
| 2708 | } |
| 2709 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2710 | si = appctx->owner; |
| 2711 | s = si_strm(si); |
| 2712 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2713 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2714 | s->req.rto = tmout; |
| 2715 | s->req.wto = tmout; |
| 2716 | s->res.rto = tmout; |
| 2717 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2718 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2719 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2720 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2721 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2722 | |
| 2723 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2724 | task_queue(s->task); |
| 2725 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2726 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2727 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2728 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2729 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2733 | { |
| 2734 | struct hlua_socket *socket; |
| 2735 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2736 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2737 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2738 | |
| 2739 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2740 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2741 | hlua_pusherror(L, "socket: full stack"); |
| 2742 | goto out_fail_conf; |
| 2743 | } |
| 2744 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2745 | /* Create the object: obj[0] = userdata. */ |
| 2746 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2747 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2748 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2749 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2750 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2751 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2752 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2753 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2754 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2755 | goto out_fail_conf; |
| 2756 | } |
| 2757 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2758 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2759 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2760 | lua_setmetatable(L, -2); |
| 2761 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2762 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2763 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2764 | if (!appctx) { |
| 2765 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2766 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2767 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2768 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2769 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2770 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2771 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2772 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2773 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2774 | /* Now create a session, task and stream for this applet */ |
| 2775 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2776 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2777 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2778 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2779 | } |
| 2780 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2781 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2782 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2783 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2784 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2785 | } |
| 2786 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2787 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2788 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2789 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2790 | /* Configure "right" stream interface. this "si" is used to connect |
| 2791 | * and retrieve data from the server. The connection is initialized |
| 2792 | * with the "struct server". |
| 2793 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2794 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2795 | |
| 2796 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2797 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2798 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2799 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2800 | return 1; |
| 2801 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2802 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2803 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2804 | out_fail_sess: |
| 2805 | appctx_free(appctx); |
| 2806 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2807 | WILL_LJMP(lua_error(L)); |
| 2808 | return 0; |
| 2809 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2810 | |
| 2811 | /* |
| 2812 | * |
| 2813 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2814 | * Class Channel |
| 2815 | * |
| 2816 | * |
| 2817 | */ |
| 2818 | |
| 2819 | /* Returns the struct hlua_channel join to the class channel in the |
| 2820 | * stack entry "ud" or throws an argument error. |
| 2821 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2822 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2823 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2824 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2825 | } |
| 2826 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2827 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2828 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2829 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2830 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2831 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2832 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2833 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2834 | return 0; |
| 2835 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2836 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2837 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2838 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2839 | |
| 2840 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2841 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2842 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2843 | return 1; |
| 2844 | } |
| 2845 | |
| 2846 | /* Duplicate all the data present in the input channel and put it |
| 2847 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2848 | * the stack if the channel is closed and all the data are consumed, |
| 2849 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2850 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2851 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2852 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2853 | { |
| 2854 | char *blk1; |
| 2855 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2856 | size_t len1; |
| 2857 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2858 | int ret; |
| 2859 | luaL_Buffer b; |
| 2860 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2861 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2862 | if (unlikely(ret == 0)) |
| 2863 | return 0; |
| 2864 | |
| 2865 | if (unlikely(ret < 0)) { |
| 2866 | lua_pushnil(L); |
| 2867 | return -1; |
| 2868 | } |
| 2869 | |
| 2870 | luaL_buffinit(L, &b); |
| 2871 | luaL_addlstring(&b, blk1, len1); |
| 2872 | if (unlikely(ret == 2)) |
| 2873 | luaL_addlstring(&b, blk2, len2); |
| 2874 | luaL_pushresult(&b); |
| 2875 | |
| 2876 | if (unlikely(ret == 2)) |
| 2877 | return len1 + len2; |
| 2878 | return len1; |
| 2879 | } |
| 2880 | |
| 2881 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2882 | * a yield. This function keep the data in the buffer. |
| 2883 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2884 | __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] | 2885 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2886 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2887 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2888 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2889 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2890 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2891 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2892 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2893 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2894 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2895 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2896 | 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] | 2897 | return 1; |
| 2898 | } |
| 2899 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2900 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2901 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2902 | { |
| 2903 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2904 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2905 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2906 | } |
| 2907 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2909 | * a yield. This function consumes the data in the buffer. It returns |
| 2910 | * a string containing the data or a nil pointer if no data are available |
| 2911 | * and the channel is closed. |
| 2912 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2913 | __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] | 2914 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2915 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2916 | int ret; |
| 2917 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2918 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2919 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2920 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2921 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2922 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2923 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2924 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2925 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2926 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2927 | 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] | 2928 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2929 | if (unlikely(ret == -1)) |
| 2930 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2932 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2933 | return 1; |
| 2934 | } |
| 2935 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2936 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2937 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2938 | { |
| 2939 | MAY_LJMP(check_args(L, 1, "get")); |
| 2940 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2941 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2942 | } |
| 2943 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2944 | /* This functions consumes and returns one line. If the channel is closed, |
| 2945 | * 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] | 2946 | * 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] | 2947 | * value. |
| 2948 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2949 | __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] | 2950 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2951 | char *blk1; |
| 2952 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2953 | size_t len1; |
| 2954 | size_t len2; |
| 2955 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2956 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2957 | int ret; |
| 2958 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2959 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2960 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2961 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2962 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2963 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2964 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2965 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2966 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2967 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2969 | 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] | 2970 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2971 | if (ret == -1) { |
| 2972 | lua_pushnil(L); |
| 2973 | return 1; |
| 2974 | } |
| 2975 | |
| 2976 | luaL_buffinit(L, &b); |
| 2977 | luaL_addlstring(&b, blk1, len1); |
| 2978 | len = len1; |
| 2979 | if (unlikely(ret == 2)) { |
| 2980 | luaL_addlstring(&b, blk2, len2); |
| 2981 | len += len2; |
| 2982 | } |
| 2983 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2984 | 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] | 2985 | return 1; |
| 2986 | } |
| 2987 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2988 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2989 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2990 | { |
| 2991 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2992 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2993 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2994 | } |
| 2995 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | /* This function takes a string as input, and append it at the |
| 2997 | * input side of channel. If the data is too big, but a space |
| 2998 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2999 | * yields. If the data is bigger than the buffer, or if the |
| 3000 | * channel is closed, it returns -1. Otherwise, it returns the |
| 3001 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3003 | __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] | 3004 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3005 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | size_t len; |
| 3007 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3008 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3009 | int ret; |
| 3010 | int max; |
| 3011 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3012 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3013 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3014 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3015 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3016 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3017 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3018 | * the request buffer if its not required. |
| 3019 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3020 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3021 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3022 | 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] | 3023 | } |
| 3024 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3025 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | if (max > len - l) |
| 3027 | max = len - l; |
| 3028 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3029 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | if (ret == -2 || ret == -3) { |
| 3031 | lua_pushinteger(L, -1); |
| 3032 | return 1; |
| 3033 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3034 | if (ret == -1) { |
| 3035 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3036 | 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] | 3037 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3038 | l += ret; |
| 3039 | lua_pop(L, 1); |
| 3040 | lua_pushinteger(L, l); |
| 3041 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3042 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3043 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3044 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3045 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3046 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3047 | */ |
| 3048 | return 1; |
| 3049 | } |
| 3050 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3051 | 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] | 3052 | return 1; |
| 3053 | } |
| 3054 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3055 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3056 | * 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] | 3057 | * buffer size is too little for the data. |
| 3058 | */ |
| 3059 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3060 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3061 | size_t len; |
| 3062 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3064 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3065 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3066 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | lua_pushinteger(L, 0); |
| 3068 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3069 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3070 | } |
| 3071 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3072 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3074 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3075 | * or -1 if the channel is closed or if the buffer size is too |
| 3076 | * little for the data. |
| 3077 | */ |
| 3078 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3079 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3080 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3081 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3082 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3083 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3084 | lua_pushinteger(L, 0); |
| 3085 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3086 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3087 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3088 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3089 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3090 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3091 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3092 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3093 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3094 | } |
| 3095 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3096 | /* Append data in the output side of the buffer. This data is immediately |
| 3097 | * sent. The function returns the amount of data written. If the buffer |
| 3098 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | * if the channel is closed. |
| 3100 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3101 | __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] | 3102 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3103 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3104 | size_t len; |
| 3105 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3106 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3107 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3108 | struct hlua *hlua; |
| 3109 | |
| 3110 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3111 | hlua = hlua_gethlua(L); |
| 3112 | if (!hlua) { |
| 3113 | lua_pushnil(L); |
| 3114 | return 1; |
| 3115 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3117 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3118 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3119 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3120 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3121 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3122 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | lua_pushinteger(L, -1); |
| 3124 | return 1; |
| 3125 | } |
| 3126 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3127 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3128 | * the request buffer if its not required. |
| 3129 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3130 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3131 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3132 | 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] | 3133 | } |
| 3134 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3135 | /* The written data will be immediately sent, so we can check |
| 3136 | * the available space without taking in account the reserve. |
| 3137 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3138 | * data, because the buffer will be flushed. |
| 3139 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3140 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3141 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3142 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3143 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3144 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3145 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3146 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3147 | return 1; |
| 3148 | |
| 3149 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3150 | if (max > len - l) |
| 3151 | max = len - l; |
| 3152 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3153 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3154 | * detects a non contiguous buffer and realign it. |
| 3155 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3156 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3157 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3158 | |
| 3159 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3160 | 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] | 3161 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | /* buffer replace considers that the input part is filled. |
| 3163 | * so, I must forward these new data in the output part. |
| 3164 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3165 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3166 | |
| 3167 | l += max; |
| 3168 | lua_pop(L, 1); |
| 3169 | lua_pushinteger(L, l); |
| 3170 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3171 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3172 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3173 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3174 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3175 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3176 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3177 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3178 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3179 | if (l < len) { |
| 3180 | /* If we are waiting for space in the response buffer, we |
| 3181 | * must set the flag WAKERESWR. This flag required the task |
| 3182 | * wake up if any activity is detected on the response buffer. |
| 3183 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3184 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3185 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3186 | else |
| 3187 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3188 | 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] | 3189 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3190 | |
| 3191 | return 1; |
| 3192 | } |
| 3193 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3194 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3195 | * yield the LUA process, and resume it without checking the |
| 3196 | * input arguments. |
| 3197 | */ |
| 3198 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3199 | { |
| 3200 | MAY_LJMP(check_args(L, 2, "send")); |
| 3201 | lua_pushinteger(L, 0); |
| 3202 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3203 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | /* This function forward and amount of butes. The data pass from |
| 3207 | * the input side of the buffer to the output side, and can be |
| 3208 | * forwarded. This function never fails. |
| 3209 | * |
| 3210 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3211 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3212 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3213 | __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] | 3214 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3215 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3216 | int len; |
| 3217 | int l; |
| 3218 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3219 | struct hlua *hlua; |
| 3220 | |
| 3221 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3222 | hlua = hlua_gethlua(L); |
| 3223 | if (!hlua) |
| 3224 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3225 | |
| 3226 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3227 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3228 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3229 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3230 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3231 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3232 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3233 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3234 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3235 | |
| 3236 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3237 | if (max > ci_data(chn)) |
| 3238 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3239 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3240 | l += max; |
| 3241 | |
| 3242 | lua_pop(L, 1); |
| 3243 | lua_pushinteger(L, l); |
| 3244 | |
| 3245 | /* Check if it miss bytes to forward. */ |
| 3246 | if (l < len) { |
| 3247 | /* The the input channel or the output channel are closed, we |
| 3248 | * must return the amount of data forwarded. |
| 3249 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3250 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3251 | return 1; |
| 3252 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3253 | /* If we are waiting for space data in the response buffer, we |
| 3254 | * must set the flag WAKERESWR. This flag required the task |
| 3255 | * wake up if any activity is detected on the response buffer. |
| 3256 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3257 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3258 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3259 | else |
| 3260 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3261 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3262 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3263 | 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] | 3264 | } |
| 3265 | |
| 3266 | return 1; |
| 3267 | } |
| 3268 | |
| 3269 | /* Just check the input and prepare the stack for the previous |
| 3270 | * function "hlua_channel_forward_yield" |
| 3271 | */ |
| 3272 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3273 | { |
| 3274 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3275 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3276 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3277 | |
| 3278 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3279 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | /* Just returns the number of bytes available in the input |
| 3283 | * side of the buffer. This function never fails. |
| 3284 | */ |
| 3285 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3286 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3287 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3288 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3289 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3290 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3291 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3292 | struct htx *htx = htxbuf(&chn->buf); |
| 3293 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3294 | } |
| 3295 | else |
| 3296 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3297 | return 1; |
| 3298 | } |
| 3299 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3300 | /* Returns true if the channel is full. */ |
| 3301 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3302 | { |
| 3303 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3304 | |
| 3305 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3306 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3307 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3308 | * applet). |
| 3309 | */ |
| 3310 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3311 | return 1; |
| 3312 | } |
| 3313 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3314 | /* Returns true if the channel is the response channel. */ |
| 3315 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3316 | { |
| 3317 | struct channel *chn; |
| 3318 | |
| 3319 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3320 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3321 | |
| 3322 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3323 | return 1; |
| 3324 | } |
| 3325 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3326 | /* Just returns the number of bytes available in the output |
| 3327 | * side of the buffer. This function never fails. |
| 3328 | */ |
| 3329 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3330 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3331 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3332 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3333 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3334 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3335 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3336 | return 1; |
| 3337 | } |
| 3338 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3339 | /* |
| 3340 | * |
| 3341 | * |
| 3342 | * Class Fetches |
| 3343 | * |
| 3344 | * |
| 3345 | */ |
| 3346 | |
| 3347 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3348 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3349 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3350 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3351 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3352 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | /* This function creates and push in the stack a fetch object according |
| 3356 | * with a current TXN. |
| 3357 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3358 | 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] | 3359 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3360 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3361 | |
| 3362 | /* Check stack size. */ |
| 3363 | if (!lua_checkstack(L, 3)) |
| 3364 | return 0; |
| 3365 | |
| 3366 | /* Create the object: obj[0] = userdata. |
| 3367 | * Note that the base of the Fetches object is the |
| 3368 | * transaction object. |
| 3369 | */ |
| 3370 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3371 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3372 | lua_rawseti(L, -2, 0); |
| 3373 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3374 | hsmp->s = txn->s; |
| 3375 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3376 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3377 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3378 | |
| 3379 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3380 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3381 | lua_setmetatable(L, -2); |
| 3382 | |
| 3383 | return 1; |
| 3384 | } |
| 3385 | |
| 3386 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3387 | * It uses closure argument to store the associated sample-fetch. It |
| 3388 | * returns only one argument or throws an error. An error is thrown |
| 3389 | * only if an error is encountered during the argument parsing. If |
| 3390 | * the "sample-fetch" function fails, nil is returned. |
| 3391 | */ |
| 3392 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3393 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3394 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3395 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3396 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3397 | int i; |
| 3398 | struct sample smp; |
| 3399 | |
| 3400 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3401 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3402 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3403 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3404 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3405 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3406 | /* Check execution authorization. */ |
| 3407 | if (f->use & SMP_USE_HTTP_ANY && |
| 3408 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3409 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3410 | "is not available in Lua services", f->kw); |
| 3411 | WILL_LJMP(lua_error(L)); |
| 3412 | } |
| 3413 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3414 | /* Get extra arguments. */ |
| 3415 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3416 | if (i >= ARGM_NBARGS) |
| 3417 | break; |
| 3418 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3419 | } |
| 3420 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3421 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3422 | |
| 3423 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3424 | 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] | 3425 | |
| 3426 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3427 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3428 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3429 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | /* Initialise the sample. */ |
| 3433 | memset(&smp, 0, sizeof(smp)); |
| 3434 | |
| 3435 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3436 | 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] | 3437 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3438 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3439 | lua_pushstring(L, ""); |
| 3440 | else |
| 3441 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3442 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3446 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3447 | hlua_smp2lua_str(L, &smp); |
| 3448 | else |
| 3449 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3450 | |
| 3451 | end: |
| 3452 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3453 | if (args[i].type == ARGT_STR) |
| 3454 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3455 | else if (args[i].type == ARGT_REG) |
| 3456 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3457 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3458 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3459 | |
| 3460 | error: |
| 3461 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3462 | if (args[i].type == ARGT_STR) |
| 3463 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3464 | else if (args[i].type == ARGT_REG) |
| 3465 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3466 | } |
| 3467 | WILL_LJMP(lua_error(L)); |
| 3468 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3469 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3470 | |
| 3471 | /* |
| 3472 | * |
| 3473 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3474 | * Class Converters |
| 3475 | * |
| 3476 | * |
| 3477 | */ |
| 3478 | |
| 3479 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3480 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3481 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3482 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3483 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3484 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | /* This function creates and push in the stack a Converters object |
| 3488 | * according with a current TXN. |
| 3489 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3490 | 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] | 3491 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3492 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3493 | |
| 3494 | /* Check stack size. */ |
| 3495 | if (!lua_checkstack(L, 3)) |
| 3496 | return 0; |
| 3497 | |
| 3498 | /* Create the object: obj[0] = userdata. |
| 3499 | * Note that the base of the Converters object is the |
| 3500 | * same than the TXN object. |
| 3501 | */ |
| 3502 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3503 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3504 | lua_rawseti(L, -2, 0); |
| 3505 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3506 | hsmp->s = txn->s; |
| 3507 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3508 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3509 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3510 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3511 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3512 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3513 | lua_setmetatable(L, -2); |
| 3514 | |
| 3515 | return 1; |
| 3516 | } |
| 3517 | |
| 3518 | /* This function is an LUA binding. It is called with each converter. |
| 3519 | * It uses closure argument to store the associated converter. It |
| 3520 | * returns only one argument or throws an error. An error is thrown |
| 3521 | * only if an error is encountered during the argument parsing. If |
| 3522 | * the converter function function fails, nil is returned. |
| 3523 | */ |
| 3524 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3525 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3526 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3527 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3528 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3529 | int i; |
| 3530 | struct sample smp; |
| 3531 | |
| 3532 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3533 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3534 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3535 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3536 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3537 | |
| 3538 | /* Get extra arguments. */ |
| 3539 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3540 | if (i >= ARGM_NBARGS) |
| 3541 | break; |
| 3542 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3543 | } |
| 3544 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3545 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3546 | |
| 3547 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3548 | 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] | 3549 | |
| 3550 | /* Run the special args checker. */ |
| 3551 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3552 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3553 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3557 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3558 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3559 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3560 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3561 | } |
| 3562 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3563 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3564 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3565 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3566 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3567 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3568 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3569 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3570 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3571 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3572 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3573 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3574 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3578 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3579 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3580 | lua_pushstring(L, ""); |
| 3581 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3582 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3583 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3587 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3588 | hlua_smp2lua_str(L, &smp); |
| 3589 | else |
| 3590 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3591 | end: |
| 3592 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3593 | if (args[i].type == ARGT_STR) |
| 3594 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3595 | else if (args[i].type == ARGT_REG) |
| 3596 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3597 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3598 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3599 | |
| 3600 | error: |
| 3601 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3602 | if (args[i].type == ARGT_STR) |
| 3603 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3604 | else if (args[i].type == ARGT_REG) |
| 3605 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3606 | } |
| 3607 | WILL_LJMP(lua_error(L)); |
| 3608 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3609 | } |
| 3610 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3611 | /* |
| 3612 | * |
| 3613 | * |
| 3614 | * Class AppletTCP |
| 3615 | * |
| 3616 | * |
| 3617 | */ |
| 3618 | |
| 3619 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3620 | * a class stream, otherwise it throws an error. |
| 3621 | */ |
| 3622 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3623 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3624 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | /* This function creates and push in the stack an Applet object |
| 3628 | * according with a current TXN. |
| 3629 | */ |
| 3630 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3631 | { |
| 3632 | struct hlua_appctx *appctx; |
| 3633 | struct stream_interface *si = ctx->owner; |
| 3634 | struct stream *s = si_strm(si); |
| 3635 | struct proxy *p = s->be; |
| 3636 | |
| 3637 | /* Check stack size. */ |
| 3638 | if (!lua_checkstack(L, 3)) |
| 3639 | return 0; |
| 3640 | |
| 3641 | /* Create the object: obj[0] = userdata. |
| 3642 | * Note that the base of the Converters object is the |
| 3643 | * same than the TXN object. |
| 3644 | */ |
| 3645 | lua_newtable(L); |
| 3646 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3647 | lua_rawseti(L, -2, 0); |
| 3648 | appctx->appctx = ctx; |
| 3649 | appctx->htxn.s = s; |
| 3650 | appctx->htxn.p = p; |
| 3651 | |
| 3652 | /* Create the "f" field that contains a list of fetches. */ |
| 3653 | lua_pushstring(L, "f"); |
| 3654 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3655 | return 0; |
| 3656 | lua_settable(L, -3); |
| 3657 | |
| 3658 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3659 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3660 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3661 | return 0; |
| 3662 | lua_settable(L, -3); |
| 3663 | |
| 3664 | /* Create the "c" field that contains a list of converters. */ |
| 3665 | lua_pushstring(L, "c"); |
| 3666 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3667 | return 0; |
| 3668 | lua_settable(L, -3); |
| 3669 | |
| 3670 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3671 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3672 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3673 | return 0; |
| 3674 | lua_settable(L, -3); |
| 3675 | |
| 3676 | /* Pop a class stream metatable and affect it to the table. */ |
| 3677 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3678 | lua_setmetatable(L, -2); |
| 3679 | |
| 3680 | return 1; |
| 3681 | } |
| 3682 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3683 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3684 | { |
| 3685 | struct hlua_appctx *appctx; |
| 3686 | struct stream *s; |
| 3687 | const char *name; |
| 3688 | size_t len; |
| 3689 | struct sample smp; |
| 3690 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3691 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3692 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3693 | |
| 3694 | /* It is useles to retrieve the stream, but this function |
| 3695 | * runs only in a stream context. |
| 3696 | */ |
| 3697 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3698 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3699 | s = appctx->htxn.s; |
| 3700 | |
| 3701 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3702 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3703 | hlua_lua2smp(L, 3, &smp); |
| 3704 | |
| 3705 | /* Store the sample in a variable. */ |
| 3706 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3707 | |
| 3708 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3709 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3710 | else |
| 3711 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3712 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3713 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3717 | { |
| 3718 | struct hlua_appctx *appctx; |
| 3719 | struct stream *s; |
| 3720 | const char *name; |
| 3721 | size_t len; |
| 3722 | struct sample smp; |
| 3723 | |
| 3724 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3725 | |
| 3726 | /* It is useles to retrieve the stream, but this function |
| 3727 | * runs only in a stream context. |
| 3728 | */ |
| 3729 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3730 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3731 | s = appctx->htxn.s; |
| 3732 | |
| 3733 | /* Unset the variable. */ |
| 3734 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3735 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3736 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3740 | { |
| 3741 | struct hlua_appctx *appctx; |
| 3742 | struct stream *s; |
| 3743 | const char *name; |
| 3744 | size_t len; |
| 3745 | struct sample smp; |
| 3746 | |
| 3747 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3748 | |
| 3749 | /* It is useles to retrieve the stream, but this function |
| 3750 | * runs only in a stream context. |
| 3751 | */ |
| 3752 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3753 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3754 | s = appctx->htxn.s; |
| 3755 | |
| 3756 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3757 | if (!vars_get_by_name(name, len, &smp)) { |
| 3758 | lua_pushnil(L); |
| 3759 | return 1; |
| 3760 | } |
| 3761 | |
| 3762 | return hlua_smp2lua(L, &smp); |
| 3763 | } |
| 3764 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3765 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3766 | { |
| 3767 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3768 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3769 | struct hlua *hlua; |
| 3770 | |
| 3771 | /* 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] | 3772 | if (!s->hlua) |
| 3773 | return 0; |
| 3774 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3775 | |
| 3776 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3777 | |
| 3778 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3779 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3780 | |
| 3781 | /* Get and store new value. */ |
| 3782 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3783 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3784 | |
| 3785 | return 0; |
| 3786 | } |
| 3787 | |
| 3788 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3789 | { |
| 3790 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3791 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3792 | struct hlua *hlua; |
| 3793 | |
| 3794 | /* 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] | 3795 | if (!s->hlua) { |
| 3796 | lua_pushnil(L); |
| 3797 | return 1; |
| 3798 | } |
| 3799 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3800 | |
| 3801 | /* Push configuration index in the stack. */ |
| 3802 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3803 | |
| 3804 | return 1; |
| 3805 | } |
| 3806 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3807 | /* If expected data not yet available, it returns a yield. This function |
| 3808 | * consumes the data in the buffer. It returns a string containing the |
| 3809 | * data. This string can be empty. |
| 3810 | */ |
| 3811 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3812 | { |
| 3813 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3814 | struct stream_interface *si = appctx->appctx->owner; |
| 3815 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3816 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3817 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3818 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3819 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3820 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3821 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3822 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3823 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3824 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3825 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3826 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3827 | 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] | 3828 | } |
| 3829 | |
| 3830 | /* End of data: commit the total strings and return. */ |
| 3831 | if (ret < 0) { |
| 3832 | luaL_pushresult(&appctx->b); |
| 3833 | return 1; |
| 3834 | } |
| 3835 | |
| 3836 | /* Ensure that the block 2 length is usable. */ |
| 3837 | if (ret == 1) |
| 3838 | len2 = 0; |
| 3839 | |
| 3840 | /* dont check the max length read and dont check. */ |
| 3841 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3842 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3843 | |
| 3844 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3845 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3846 | luaL_pushresult(&appctx->b); |
| 3847 | return 1; |
| 3848 | } |
| 3849 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3850 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3851 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3852 | { |
| 3853 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3854 | |
| 3855 | /* Initialise the string catenation. */ |
| 3856 | luaL_buffinit(L, &appctx->b); |
| 3857 | |
| 3858 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3859 | } |
| 3860 | |
| 3861 | /* If expected data not yet available, it returns a yield. This function |
| 3862 | * consumes the data in the buffer. It returns a string containing the |
| 3863 | * data. This string can be empty. |
| 3864 | */ |
| 3865 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3866 | { |
| 3867 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3868 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3869 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3870 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3871 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3872 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3873 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3874 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3875 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3876 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3877 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3878 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3879 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3880 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3881 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3882 | 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] | 3883 | } |
| 3884 | |
| 3885 | /* End of data: commit the total strings and return. */ |
| 3886 | if (ret < 0) { |
| 3887 | luaL_pushresult(&appctx->b); |
| 3888 | return 1; |
| 3889 | } |
| 3890 | |
| 3891 | /* Ensure that the block 2 length is usable. */ |
| 3892 | if (ret == 1) |
| 3893 | len2 = 0; |
| 3894 | |
| 3895 | if (len == -1) { |
| 3896 | |
| 3897 | /* If len == -1, catenate all the data avalaile and |
| 3898 | * yield because we want to get all the data until |
| 3899 | * the end of data stream. |
| 3900 | */ |
| 3901 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3902 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3903 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3904 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3905 | 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] | 3906 | |
| 3907 | } else { |
| 3908 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3909 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3910 | if (len1 > len) |
| 3911 | len1 = len; |
| 3912 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3913 | len -= len1; |
| 3914 | |
| 3915 | /* Copy the second block. */ |
| 3916 | if (len2 > len) |
| 3917 | len2 = len; |
| 3918 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3919 | len -= len2; |
| 3920 | |
| 3921 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3922 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3923 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3924 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3925 | if (len > 0) { |
| 3926 | lua_pushinteger(L, len); |
| 3927 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3928 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3929 | 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] | 3930 | } |
| 3931 | |
| 3932 | /* return the result. */ |
| 3933 | luaL_pushresult(&appctx->b); |
| 3934 | return 1; |
| 3935 | } |
| 3936 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3937 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3938 | hlua_pusherror(L, "Lua: internal error"); |
| 3939 | WILL_LJMP(lua_error(L)); |
| 3940 | return 0; |
| 3941 | } |
| 3942 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3943 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3944 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3945 | { |
| 3946 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3947 | int len = -1; |
| 3948 | |
| 3949 | if (lua_gettop(L) > 2) |
| 3950 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3951 | if (lua_gettop(L) >= 2) { |
| 3952 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3953 | lua_pop(L, 1); |
| 3954 | } |
| 3955 | |
| 3956 | /* Confirm or set the required length */ |
| 3957 | lua_pushinteger(L, len); |
| 3958 | |
| 3959 | /* Initialise the string catenation. */ |
| 3960 | luaL_buffinit(L, &appctx->b); |
| 3961 | |
| 3962 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3963 | } |
| 3964 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3965 | /* Append data in the output side of the buffer. This data is immediately |
| 3966 | * sent. The function returns the amount of data written. If the buffer |
| 3967 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3968 | * if the channel is closed. |
| 3969 | */ |
| 3970 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3971 | { |
| 3972 | size_t len; |
| 3973 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3974 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3975 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3976 | struct stream_interface *si = appctx->appctx->owner; |
| 3977 | struct channel *chn = si_ic(si); |
| 3978 | int max; |
| 3979 | |
| 3980 | /* Get the max amount of data which can write as input in the channel. */ |
| 3981 | max = channel_recv_max(chn); |
| 3982 | if (max > (len - l)) |
| 3983 | max = len - l; |
| 3984 | |
| 3985 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3986 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3987 | |
| 3988 | /* update counters. */ |
| 3989 | l += max; |
| 3990 | lua_pop(L, 1); |
| 3991 | lua_pushinteger(L, l); |
| 3992 | |
| 3993 | /* If some data is not send, declares the situation to the |
| 3994 | * applet, and returns a yield. |
| 3995 | */ |
| 3996 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3997 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3998 | 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] | 3999 | } |
| 4000 | |
| 4001 | return 1; |
| 4002 | } |
| 4003 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4004 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4005 | * yield the LUA process, and resume it without checking the |
| 4006 | * input arguments. |
| 4007 | */ |
| 4008 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4009 | { |
| 4010 | MAY_LJMP(check_args(L, 2, "send")); |
| 4011 | lua_pushinteger(L, 0); |
| 4012 | |
| 4013 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4014 | } |
| 4015 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4016 | /* |
| 4017 | * |
| 4018 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4019 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4020 | * |
| 4021 | * |
| 4022 | */ |
| 4023 | |
| 4024 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4025 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4026 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4027 | __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] | 4028 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4029 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4030 | } |
| 4031 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4032 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4033 | * according with a current TXN. |
| 4034 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4035 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4036 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4037 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4038 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4039 | struct stream_interface *si = ctx->owner; |
| 4040 | struct stream *s = si_strm(si); |
| 4041 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4042 | struct htx *htx; |
| 4043 | struct htx_blk *blk; |
| 4044 | struct htx_sl *sl; |
| 4045 | struct ist path; |
| 4046 | unsigned long long len = 0; |
| 4047 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4048 | |
| 4049 | /* Check stack size. */ |
| 4050 | if (!lua_checkstack(L, 3)) |
| 4051 | return 0; |
| 4052 | |
| 4053 | /* Create the object: obj[0] = userdata. |
| 4054 | * Note that the base of the Converters object is the |
| 4055 | * same than the TXN object. |
| 4056 | */ |
| 4057 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4058 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4059 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4060 | appctx->appctx = ctx; |
| 4061 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4062 | 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] | 4063 | appctx->htxn.s = s; |
| 4064 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4065 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4066 | /* Create the "f" field that contains a list of fetches. */ |
| 4067 | lua_pushstring(L, "f"); |
| 4068 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4069 | return 0; |
| 4070 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4071 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4072 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4073 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4074 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4075 | return 0; |
| 4076 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4077 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4078 | /* Create the "c" field that contains a list of converters. */ |
| 4079 | lua_pushstring(L, "c"); |
| 4080 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4081 | return 0; |
| 4082 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4083 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4084 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4085 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4086 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4087 | return 0; |
| 4088 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4089 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4090 | htx = htxbuf(&s->req.buf); |
| 4091 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4092 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4093 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4094 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4095 | /* Stores the request method. */ |
| 4096 | lua_pushstring(L, "method"); |
| 4097 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4098 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4099 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4100 | /* Stores the http version. */ |
| 4101 | lua_pushstring(L, "version"); |
| 4102 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4103 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4104 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4105 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4106 | * the array on the top of the stack. |
| 4107 | */ |
| 4108 | lua_pushstring(L, "headers"); |
| 4109 | htxn.s = s; |
| 4110 | htxn.p = px; |
| 4111 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4112 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4113 | return 0; |
| 4114 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4115 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4116 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4117 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4118 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4119 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4120 | p = path.ptr; |
| 4121 | end = path.ptr + path.len; |
| 4122 | q = p; |
| 4123 | while (q < end && *q != '?') |
| 4124 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4125 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4126 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4127 | lua_pushstring(L, "path"); |
| 4128 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4129 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4130 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4131 | /* Stores the query string. */ |
| 4132 | lua_pushstring(L, "qs"); |
| 4133 | if (*q == '?') |
| 4134 | q++; |
| 4135 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4136 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4137 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4138 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4139 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4140 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4141 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4142 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4143 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 4144 | break; |
| 4145 | if (type == HTX_BLK_DATA) |
| 4146 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4147 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4148 | if (htx->extra != ULLONG_MAX) |
| 4149 | len += htx->extra; |
| 4150 | |
| 4151 | /* Stores the request path. */ |
| 4152 | lua_pushstring(L, "length"); |
| 4153 | lua_pushinteger(L, len); |
| 4154 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4155 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4156 | /* Create an empty array of HTTP request headers. */ |
| 4157 | lua_pushstring(L, "response"); |
| 4158 | lua_newtable(L); |
| 4159 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4160 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4161 | /* Pop a class stream metatable and affect it to the table. */ |
| 4162 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4163 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4164 | |
| 4165 | return 1; |
| 4166 | } |
| 4167 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4168 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4169 | { |
| 4170 | struct hlua_appctx *appctx; |
| 4171 | struct stream *s; |
| 4172 | const char *name; |
| 4173 | size_t len; |
| 4174 | struct sample smp; |
| 4175 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4176 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4177 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4178 | |
| 4179 | /* It is useles to retrieve the stream, but this function |
| 4180 | * runs only in a stream context. |
| 4181 | */ |
| 4182 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4183 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4184 | s = appctx->htxn.s; |
| 4185 | |
| 4186 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4187 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4188 | hlua_lua2smp(L, 3, &smp); |
| 4189 | |
| 4190 | /* Store the sample in a variable. */ |
| 4191 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4192 | |
| 4193 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4194 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4195 | else |
| 4196 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4197 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4198 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4202 | { |
| 4203 | struct hlua_appctx *appctx; |
| 4204 | struct stream *s; |
| 4205 | const char *name; |
| 4206 | size_t len; |
| 4207 | struct sample smp; |
| 4208 | |
| 4209 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4210 | |
| 4211 | /* It is useles to retrieve the stream, but this function |
| 4212 | * runs only in a stream context. |
| 4213 | */ |
| 4214 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4215 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4216 | s = appctx->htxn.s; |
| 4217 | |
| 4218 | /* Unset the variable. */ |
| 4219 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4220 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4221 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4225 | { |
| 4226 | struct hlua_appctx *appctx; |
| 4227 | struct stream *s; |
| 4228 | const char *name; |
| 4229 | size_t len; |
| 4230 | struct sample smp; |
| 4231 | |
| 4232 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4233 | |
| 4234 | /* It is useles to retrieve the stream, but this function |
| 4235 | * runs only in a stream context. |
| 4236 | */ |
| 4237 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4238 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4239 | s = appctx->htxn.s; |
| 4240 | |
| 4241 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4242 | if (!vars_get_by_name(name, len, &smp)) { |
| 4243 | lua_pushnil(L); |
| 4244 | return 1; |
| 4245 | } |
| 4246 | |
| 4247 | return hlua_smp2lua(L, &smp); |
| 4248 | } |
| 4249 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4250 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4251 | { |
| 4252 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4253 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4254 | struct hlua *hlua; |
| 4255 | |
| 4256 | /* 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] | 4257 | if (!s->hlua) |
| 4258 | return 0; |
| 4259 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4260 | |
| 4261 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4262 | |
| 4263 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4264 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4265 | |
| 4266 | /* Get and store new value. */ |
| 4267 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4268 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4269 | |
| 4270 | return 0; |
| 4271 | } |
| 4272 | |
| 4273 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4274 | { |
| 4275 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4276 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4277 | struct hlua *hlua; |
| 4278 | |
| 4279 | /* 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] | 4280 | if (!s->hlua) { |
| 4281 | lua_pushnil(L); |
| 4282 | return 1; |
| 4283 | } |
| 4284 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4285 | |
| 4286 | /* Push configuration index in the stack. */ |
| 4287 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4288 | |
| 4289 | return 1; |
| 4290 | } |
| 4291 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4292 | /* If expected data not yet available, it returns a yield. This function |
| 4293 | * consumes the data in the buffer. It returns a string containing the |
| 4294 | * data. This string can be empty. |
| 4295 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4296 | __LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4297 | { |
| 4298 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4299 | struct stream_interface *si = appctx->appctx->owner; |
| 4300 | struct channel *req = si_oc(si); |
| 4301 | struct htx *htx; |
| 4302 | struct htx_blk *blk; |
| 4303 | size_t count; |
| 4304 | int stop = 0; |
| 4305 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4306 | htx = htx_from_buf(&req->buf); |
| 4307 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4308 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4309 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4310 | while (count && !stop && blk) { |
| 4311 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4312 | uint32_t sz = htx_get_blksz(blk); |
| 4313 | struct ist v; |
| 4314 | uint32_t vlen; |
| 4315 | char *nl; |
| 4316 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4317 | if (type == HTX_BLK_EOM) { |
| 4318 | stop = 1; |
| 4319 | break; |
| 4320 | } |
| 4321 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4322 | vlen = sz; |
| 4323 | if (vlen > count) { |
| 4324 | if (type != HTX_BLK_DATA) |
| 4325 | break; |
| 4326 | vlen = count; |
| 4327 | } |
| 4328 | |
| 4329 | switch (type) { |
| 4330 | case HTX_BLK_UNUSED: |
| 4331 | break; |
| 4332 | |
| 4333 | case HTX_BLK_DATA: |
| 4334 | v = htx_get_blk_value(htx, blk); |
| 4335 | v.len = vlen; |
| 4336 | nl = istchr(v, '\n'); |
| 4337 | if (nl != NULL) { |
| 4338 | stop = 1; |
| 4339 | vlen = nl - v.ptr + 1; |
| 4340 | } |
| 4341 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4342 | break; |
| 4343 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4344 | case HTX_BLK_TLR: |
| 4345 | case HTX_BLK_EOM: |
| 4346 | stop = 1; |
| 4347 | break; |
| 4348 | |
| 4349 | default: |
| 4350 | break; |
| 4351 | } |
| 4352 | |
| 4353 | co_set_data(req, co_data(req) - vlen); |
| 4354 | count -= vlen; |
| 4355 | if (sz == vlen) |
| 4356 | blk = htx_remove_blk(htx, blk); |
| 4357 | else { |
| 4358 | htx_cut_data_blk(htx, blk, vlen); |
| 4359 | break; |
| 4360 | } |
| 4361 | } |
| 4362 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4363 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4364 | if (!stop) { |
| 4365 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4366 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | /* return the result. */ |
| 4370 | luaL_pushresult(&appctx->b); |
| 4371 | return 1; |
| 4372 | } |
| 4373 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4374 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4375 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4376 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4377 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4378 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4379 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4380 | /* Initialise the string catenation. */ |
| 4381 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4382 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4383 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4384 | } |
| 4385 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4386 | /* If expected data not yet available, it returns a yield. This function |
| 4387 | * consumes the data in the buffer. It returns a string containing the |
| 4388 | * data. This string can be empty. |
| 4389 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4390 | __LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4391 | { |
| 4392 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4393 | struct stream_interface *si = appctx->appctx->owner; |
| 4394 | struct channel *req = si_oc(si); |
| 4395 | struct htx *htx; |
| 4396 | struct htx_blk *blk; |
| 4397 | size_t count; |
| 4398 | int len; |
| 4399 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4400 | htx = htx_from_buf(&req->buf); |
| 4401 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4402 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4403 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4404 | while (count && len && blk) { |
| 4405 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4406 | uint32_t sz = htx_get_blksz(blk); |
| 4407 | struct ist v; |
| 4408 | uint32_t vlen; |
| 4409 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4410 | if (type == HTX_BLK_EOM) { |
| 4411 | len = 0; |
| 4412 | break; |
| 4413 | } |
| 4414 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4415 | vlen = sz; |
| 4416 | if (len > 0 && vlen > len) |
| 4417 | vlen = len; |
| 4418 | if (vlen > count) { |
| 4419 | if (type != HTX_BLK_DATA) |
| 4420 | break; |
| 4421 | vlen = count; |
| 4422 | } |
| 4423 | |
| 4424 | switch (type) { |
| 4425 | case HTX_BLK_UNUSED: |
| 4426 | break; |
| 4427 | |
| 4428 | case HTX_BLK_DATA: |
| 4429 | v = htx_get_blk_value(htx, blk); |
| 4430 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4431 | break; |
| 4432 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4433 | case HTX_BLK_TLR: |
| 4434 | case HTX_BLK_EOM: |
| 4435 | len = 0; |
| 4436 | break; |
| 4437 | |
| 4438 | default: |
| 4439 | break; |
| 4440 | } |
| 4441 | |
| 4442 | co_set_data(req, co_data(req) - vlen); |
| 4443 | count -= vlen; |
| 4444 | if (len > 0) |
| 4445 | len -= vlen; |
| 4446 | if (sz == vlen) |
| 4447 | blk = htx_remove_blk(htx, blk); |
| 4448 | else { |
| 4449 | htx_cut_data_blk(htx, blk, vlen); |
| 4450 | break; |
| 4451 | } |
| 4452 | } |
| 4453 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4454 | htx_to_buf(htx, &req->buf); |
| 4455 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4456 | /* If we are no other data available, yield waiting for new data. */ |
| 4457 | if (len) { |
| 4458 | if (len > 0) { |
| 4459 | lua_pushinteger(L, len); |
| 4460 | lua_replace(L, 2); |
| 4461 | } |
| 4462 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4463 | 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] | 4464 | } |
| 4465 | |
| 4466 | /* return the result. */ |
| 4467 | luaL_pushresult(&appctx->b); |
| 4468 | return 1; |
| 4469 | } |
| 4470 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4471 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4472 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4473 | { |
| 4474 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4475 | int len = -1; |
| 4476 | |
| 4477 | /* Check arguments. */ |
| 4478 | if (lua_gettop(L) > 2) |
| 4479 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4480 | if (lua_gettop(L) >= 2) { |
| 4481 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4482 | lua_pop(L, 1); |
| 4483 | } |
| 4484 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4485 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4486 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4487 | /* Initialise the string catenation. */ |
| 4488 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4489 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4490 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | /* Append data in the output side of the buffer. This data is immediately |
| 4494 | * sent. The function returns the amount of data written. If the buffer |
| 4495 | * cannot contain the data, the function yields. The function returns -1 |
| 4496 | * if the channel is closed. |
| 4497 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4498 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4499 | { |
| 4500 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4501 | struct stream_interface *si = appctx->appctx->owner; |
| 4502 | struct channel *res = si_ic(si); |
| 4503 | struct htx *htx = htx_from_buf(&res->buf); |
| 4504 | const char *data; |
| 4505 | size_t len; |
| 4506 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4507 | int max; |
| 4508 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4509 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4510 | if (!max) |
| 4511 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4512 | |
| 4513 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4514 | |
| 4515 | /* Get the max amount of data which can write as input in the channel. */ |
| 4516 | if (max > (len - l)) |
| 4517 | max = len - l; |
| 4518 | |
| 4519 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4520 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4521 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4522 | |
| 4523 | /* update counters. */ |
| 4524 | l += max; |
| 4525 | lua_pop(L, 1); |
| 4526 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4527 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4528 | /* If some data is not send, declares the situation to the |
| 4529 | * applet, and returns a yield. |
| 4530 | */ |
| 4531 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4532 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4533 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4534 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4535 | 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] | 4536 | } |
| 4537 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4538 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4539 | return 1; |
| 4540 | } |
| 4541 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4542 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4543 | * yield the LUA process, and resume it without checking the |
| 4544 | * input arguments. |
| 4545 | */ |
| 4546 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4547 | { |
| 4548 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4549 | |
| 4550 | /* We want to send some data. Headers must be sent. */ |
| 4551 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4552 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4553 | WILL_LJMP(lua_error(L)); |
| 4554 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4555 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4556 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4557 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4558 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4559 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4563 | { |
| 4564 | const char *name; |
| 4565 | int ret; |
| 4566 | |
| 4567 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4568 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4569 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4570 | |
| 4571 | /* Push in the stack the "response" entry. */ |
| 4572 | ret = lua_getfield(L, 1, "response"); |
| 4573 | if (ret != LUA_TTABLE) { |
| 4574 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4575 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4576 | WILL_LJMP(lua_error(L)); |
| 4577 | } |
| 4578 | |
| 4579 | /* check if the header is already registered if it is not |
| 4580 | * the case, register it. |
| 4581 | */ |
| 4582 | ret = lua_getfield(L, -1, name); |
| 4583 | if (ret == LUA_TNIL) { |
| 4584 | |
| 4585 | /* Entry not found. */ |
| 4586 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4587 | |
| 4588 | /* Insert the new header name in the array in the top of the stack. |
| 4589 | * It left the new array in the top of the stack. |
| 4590 | */ |
| 4591 | lua_newtable(L); |
| 4592 | lua_pushvalue(L, 2); |
| 4593 | lua_pushvalue(L, -2); |
| 4594 | lua_settable(L, -4); |
| 4595 | |
| 4596 | } else if (ret != LUA_TTABLE) { |
| 4597 | |
| 4598 | /* corruption error. */ |
| 4599 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4600 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4601 | WILL_LJMP(lua_error(L)); |
| 4602 | } |
| 4603 | |
| 4604 | /* Now the top od thestack is an array of values. We push |
| 4605 | * the header value as new entry. |
| 4606 | */ |
| 4607 | lua_pushvalue(L, 3); |
| 4608 | ret = lua_rawlen(L, -2); |
| 4609 | lua_rawseti(L, -2, ret + 1); |
| 4610 | lua_pushboolean(L, 1); |
| 4611 | return 1; |
| 4612 | } |
| 4613 | |
| 4614 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4615 | { |
| 4616 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4617 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4618 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4619 | |
| 4620 | if (status < 100 || status > 599) { |
| 4621 | lua_pushboolean(L, 0); |
| 4622 | return 1; |
| 4623 | } |
| 4624 | |
| 4625 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4626 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4627 | lua_pushboolean(L, 1); |
| 4628 | return 1; |
| 4629 | } |
| 4630 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4631 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4632 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4633 | { |
| 4634 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4635 | struct stream_interface *si = appctx->appctx->owner; |
| 4636 | struct channel *res = si_ic(si); |
| 4637 | struct htx *htx; |
| 4638 | struct htx_sl *sl; |
| 4639 | struct h1m h1m; |
| 4640 | const char *status, *reason; |
| 4641 | const char *name, *value; |
| 4642 | size_t nlen, vlen; |
| 4643 | unsigned int flags; |
| 4644 | |
| 4645 | /* Send the message at once. */ |
| 4646 | htx = htx_from_buf(&res->buf); |
| 4647 | h1m_init_res(&h1m); |
| 4648 | |
| 4649 | /* Use the same http version than the request. */ |
| 4650 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4651 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4652 | if (reason == NULL) |
| 4653 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4654 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4655 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4656 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4657 | } |
| 4658 | else { |
| 4659 | flags = HTX_SL_F_IS_RESP; |
| 4660 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4661 | } |
| 4662 | if (!sl) { |
| 4663 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4664 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4665 | WILL_LJMP(lua_error(L)); |
| 4666 | } |
| 4667 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4668 | |
| 4669 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4670 | lua_pushvalue(L, 0); |
| 4671 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4672 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4673 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4674 | WILL_LJMP(lua_error(L)); |
| 4675 | } |
| 4676 | |
| 4677 | /* Browse the list of headers. */ |
| 4678 | lua_pushnil(L); |
| 4679 | while(lua_next(L, -2) != 0) { |
| 4680 | /* We expect a string as -2. */ |
| 4681 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4682 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4683 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4684 | lua_typename(L, lua_type(L, -2))); |
| 4685 | WILL_LJMP(lua_error(L)); |
| 4686 | } |
| 4687 | name = lua_tolstring(L, -2, &nlen); |
| 4688 | |
| 4689 | /* We expect an array as -1. */ |
| 4690 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4691 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4692 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4693 | name, |
| 4694 | lua_typename(L, lua_type(L, -1))); |
| 4695 | WILL_LJMP(lua_error(L)); |
| 4696 | } |
| 4697 | |
| 4698 | /* Browse the table who is on the top of the stack. */ |
| 4699 | lua_pushnil(L); |
| 4700 | while(lua_next(L, -2) != 0) { |
| 4701 | int id; |
| 4702 | |
| 4703 | /* We expect a number as -2. */ |
| 4704 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4705 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4706 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4707 | name, |
| 4708 | lua_typename(L, lua_type(L, -2))); |
| 4709 | WILL_LJMP(lua_error(L)); |
| 4710 | } |
| 4711 | id = lua_tointeger(L, -2); |
| 4712 | |
| 4713 | /* We expect a string as -2. */ |
| 4714 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4715 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4716 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4717 | name, id, |
| 4718 | lua_typename(L, lua_type(L, -1))); |
| 4719 | WILL_LJMP(lua_error(L)); |
| 4720 | } |
| 4721 | value = lua_tolstring(L, -1, &vlen); |
| 4722 | |
| 4723 | /* Simple Protocol checks. */ |
| 4724 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4725 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4726 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4727 | struct ist v = ist2(value, vlen); |
| 4728 | int ret; |
| 4729 | |
| 4730 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4731 | if (ret < 0) { |
| 4732 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4733 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4734 | name); |
| 4735 | WILL_LJMP(lua_error(L)); |
| 4736 | } |
| 4737 | else if (ret == 0) |
| 4738 | goto next; /* Skip it */ |
| 4739 | } |
| 4740 | |
| 4741 | /* Add a new header */ |
| 4742 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4743 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4744 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4745 | name); |
| 4746 | WILL_LJMP(lua_error(L)); |
| 4747 | } |
| 4748 | next: |
| 4749 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4750 | lua_pop(L, 1); |
| 4751 | } |
| 4752 | |
| 4753 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4754 | lua_pop(L, 1); |
| 4755 | } |
| 4756 | |
| 4757 | if (h1m.flags & H1_MF_CHNK) |
| 4758 | h1m.flags &= ~H1_MF_CLEN; |
| 4759 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4760 | h1m.flags |= H1_MF_XFER_LEN; |
| 4761 | |
| 4762 | /* Uset HTX start-line flags */ |
| 4763 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4764 | flags |= HTX_SL_F_XFER_ENC; |
| 4765 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4766 | flags |= HTX_SL_F_XFER_LEN; |
| 4767 | if (h1m.flags & H1_MF_CHNK) |
| 4768 | flags |= HTX_SL_F_CHNK; |
| 4769 | else if (h1m.flags & H1_MF_CLEN) |
| 4770 | flags |= HTX_SL_F_CLEN; |
| 4771 | if (h1m.body_len == 0) |
| 4772 | flags |= HTX_SL_F_BODYLESS; |
| 4773 | } |
| 4774 | sl->flags |= flags; |
| 4775 | |
| 4776 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4777 | * and the status code implies the presence of a message body, we must |
| 4778 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4779 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4780 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4781 | */ |
| 4782 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4783 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4784 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4785 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4786 | /* Add a new header */ |
| 4787 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4788 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4789 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4790 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4791 | WILL_LJMP(lua_error(L)); |
| 4792 | } |
| 4793 | } |
| 4794 | |
| 4795 | /* Finalize headers. */ |
| 4796 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4797 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4798 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4799 | WILL_LJMP(lua_error(L)); |
| 4800 | } |
| 4801 | |
| 4802 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4803 | b_reset(&res->buf); |
| 4804 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4805 | WILL_LJMP(lua_error(L)); |
| 4806 | } |
| 4807 | |
| 4808 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4809 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4810 | |
| 4811 | /* Headers sent, set the flag. */ |
| 4812 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4813 | return 0; |
| 4814 | |
| 4815 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4816 | /* We will build the status line and the headers of the HTTP response. |
| 4817 | * We will try send at once if its not possible, we give back the hand |
| 4818 | * waiting for more room. |
| 4819 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4820 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4821 | { |
| 4822 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4823 | struct stream_interface *si = appctx->appctx->owner; |
| 4824 | struct channel *res = si_ic(si); |
| 4825 | |
| 4826 | if (co_data(res)) { |
| 4827 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4828 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4829 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4830 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4834 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4835 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4836 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4837 | } |
| 4838 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4839 | /* |
| 4840 | * |
| 4841 | * |
| 4842 | * Class HTTP |
| 4843 | * |
| 4844 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4845 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4846 | |
| 4847 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4848 | * a class stream, otherwise it throws an error. |
| 4849 | */ |
| 4850 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4851 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4852 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4853 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4854 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4855 | /* This function creates and push in the stack a HTTP object |
| 4856 | * according with a current TXN. |
| 4857 | */ |
| 4858 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4859 | { |
| 4860 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4861 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4862 | /* Check stack size. */ |
| 4863 | if (!lua_checkstack(L, 3)) |
| 4864 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4865 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4866 | /* Create the object: obj[0] = userdata. |
| 4867 | * Note that the base of the Converters object is the |
| 4868 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4869 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4870 | lua_newtable(L); |
| 4871 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4872 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4873 | |
| 4874 | htxn->s = txn->s; |
| 4875 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4876 | htxn->dir = txn->dir; |
| 4877 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4878 | |
| 4879 | /* Pop a class stream metatable and affect it to the table. */ |
| 4880 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4881 | lua_setmetatable(L, -2); |
| 4882 | |
| 4883 | return 1; |
| 4884 | } |
| 4885 | |
| 4886 | /* This function creates ans returns an array of HTTP headers. |
| 4887 | * This function does not fails. It is used as wrapper with the |
| 4888 | * 2 following functions. |
| 4889 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4890 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4891 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4892 | struct htx *htx; |
| 4893 | int32_t pos; |
| 4894 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4895 | /* Create the table. */ |
| 4896 | lua_newtable(L); |
| 4897 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4898 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4899 | htx = htxbuf(&msg->chn->buf); |
| 4900 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4901 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4902 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4903 | struct ist n, v; |
| 4904 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4905 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4906 | if (type == HTX_BLK_HDR) { |
| 4907 | n = htx_get_blk_name(htx,blk); |
| 4908 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4909 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4910 | else if (type == HTX_BLK_EOH) |
| 4911 | break; |
| 4912 | else |
| 4913 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4914 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4915 | /* Check for existing entry: |
| 4916 | * assume that the table is on the top of the stack, and |
| 4917 | * push the key in the stack, the function lua_gettable() |
| 4918 | * perform the lookup. |
| 4919 | */ |
| 4920 | lua_pushlstring(L, n.ptr, n.len); |
| 4921 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4922 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4923 | switch (lua_type(L, -1)) { |
| 4924 | case LUA_TNIL: |
| 4925 | /* Table not found, create it. */ |
| 4926 | lua_pop(L, 1); /* remove the nil value. */ |
| 4927 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4928 | lua_newtable(L); /* create and push empty table. */ |
| 4929 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4930 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4931 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4932 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4933 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4934 | case LUA_TTABLE: |
| 4935 | /* Entry found: push the value in the table. */ |
| 4936 | len = lua_rawlen(L, -1); |
| 4937 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4938 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4939 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4940 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4941 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4942 | default: |
| 4943 | /* Other cases are errors. */ |
| 4944 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4945 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4946 | } |
| 4947 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4948 | return 1; |
| 4949 | } |
| 4950 | |
| 4951 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4952 | { |
| 4953 | struct hlua_txn *htxn; |
| 4954 | |
| 4955 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4956 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4957 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4958 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4959 | WILL_LJMP(lua_error(L)); |
| 4960 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4961 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4965 | { |
| 4966 | struct hlua_txn *htxn; |
| 4967 | |
| 4968 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4969 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4970 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4971 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4972 | WILL_LJMP(lua_error(L)); |
| 4973 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4974 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4975 | } |
| 4976 | |
| 4977 | /* This function replace full header, or just a value in |
| 4978 | * the request or in the response. It is a wrapper fir the |
| 4979 | * 4 following functions. |
| 4980 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4981 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4982 | { |
| 4983 | size_t name_len; |
| 4984 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4985 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4986 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4987 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4988 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4989 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4990 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4991 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4992 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4993 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4994 | http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4995 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4996 | return 0; |
| 4997 | } |
| 4998 | |
| 4999 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5000 | { |
| 5001 | struct hlua_txn *htxn; |
| 5002 | |
| 5003 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5004 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5005 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5006 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5007 | WILL_LJMP(lua_error(L)); |
| 5008 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5009 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5010 | } |
| 5011 | |
| 5012 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5013 | { |
| 5014 | struct hlua_txn *htxn; |
| 5015 | |
| 5016 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5017 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5018 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5019 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5020 | WILL_LJMP(lua_error(L)); |
| 5021 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5022 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5026 | { |
| 5027 | struct hlua_txn *htxn; |
| 5028 | |
| 5029 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5030 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5031 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5032 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5033 | WILL_LJMP(lua_error(L)); |
| 5034 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5035 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5036 | } |
| 5037 | |
| 5038 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5039 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5040 | struct hlua_txn *htxn; |
| 5041 | |
| 5042 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5043 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5044 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5045 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5046 | WILL_LJMP(lua_error(L)); |
| 5047 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5048 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5049 | } |
| 5050 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5051 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5052 | * It is a wrapper for the 2 following functions. |
| 5053 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5054 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5055 | { |
| 5056 | size_t len; |
| 5057 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5058 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5059 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5060 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5061 | ctx.blk = NULL; |
| 5062 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5063 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5064 | return 0; |
| 5065 | } |
| 5066 | |
| 5067 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5068 | { |
| 5069 | struct hlua_txn *htxn; |
| 5070 | |
| 5071 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5072 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5073 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5074 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5075 | WILL_LJMP(lua_error(L)); |
| 5076 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5077 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5081 | { |
| 5082 | struct hlua_txn *htxn; |
| 5083 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5084 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5085 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5086 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5087 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5088 | WILL_LJMP(lua_error(L)); |
| 5089 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5090 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5091 | } |
| 5092 | |
| 5093 | /* This function adds an header. It is a wrapper used by |
| 5094 | * the 2 following functions. |
| 5095 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5096 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5097 | { |
| 5098 | size_t name_len; |
| 5099 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5100 | size_t value_len; |
| 5101 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5102 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5103 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5104 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5105 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5106 | return 0; |
| 5107 | } |
| 5108 | |
| 5109 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5110 | { |
| 5111 | struct hlua_txn *htxn; |
| 5112 | |
| 5113 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5114 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5115 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5116 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5117 | WILL_LJMP(lua_error(L)); |
| 5118 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5119 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5123 | { |
| 5124 | struct hlua_txn *htxn; |
| 5125 | |
| 5126 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5127 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5128 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5129 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5130 | WILL_LJMP(lua_error(L)); |
| 5131 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5132 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5136 | { |
| 5137 | struct hlua_txn *htxn; |
| 5138 | |
| 5139 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5140 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5141 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5142 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5143 | WILL_LJMP(lua_error(L)); |
| 5144 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5145 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5146 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5147 | } |
| 5148 | |
| 5149 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5150 | { |
| 5151 | struct hlua_txn *htxn; |
| 5152 | |
| 5153 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5154 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5155 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5156 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5157 | WILL_LJMP(lua_error(L)); |
| 5158 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5159 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5160 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | /* This function set the method. */ |
| 5164 | static int hlua_http_req_set_meth(lua_State *L) |
| 5165 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5166 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5167 | size_t name_len; |
| 5168 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5169 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5170 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5171 | WILL_LJMP(lua_error(L)); |
| 5172 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5173 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5174 | return 1; |
| 5175 | } |
| 5176 | |
| 5177 | /* This function set the method. */ |
| 5178 | static int hlua_http_req_set_path(lua_State *L) |
| 5179 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5180 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5181 | size_t name_len; |
| 5182 | 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] | 5183 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5184 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5185 | WILL_LJMP(lua_error(L)); |
| 5186 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5187 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5188 | return 1; |
| 5189 | } |
| 5190 | |
| 5191 | /* This function set the query-string. */ |
| 5192 | static int hlua_http_req_set_query(lua_State *L) |
| 5193 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5194 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5195 | size_t name_len; |
| 5196 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5197 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5198 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5199 | WILL_LJMP(lua_error(L)); |
| 5200 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5201 | /* Check length. */ |
| 5202 | if (name_len > trash.size - 1) { |
| 5203 | lua_pushboolean(L, 0); |
| 5204 | return 1; |
| 5205 | } |
| 5206 | |
| 5207 | /* Add the mark question as prefix. */ |
| 5208 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5209 | trash.area[trash.data++] = '?'; |
| 5210 | memcpy(trash.area + trash.data, name, name_len); |
| 5211 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5212 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5213 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5214 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5215 | return 1; |
| 5216 | } |
| 5217 | |
| 5218 | /* This function set the uri. */ |
| 5219 | static int hlua_http_req_set_uri(lua_State *L) |
| 5220 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5221 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5222 | size_t name_len; |
| 5223 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5224 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5225 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5226 | WILL_LJMP(lua_error(L)); |
| 5227 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5228 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5229 | return 1; |
| 5230 | } |
| 5231 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5232 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5233 | static int hlua_http_res_set_status(lua_State *L) |
| 5234 | { |
| 5235 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5236 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5237 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5238 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5239 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5240 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5241 | WILL_LJMP(lua_error(L)); |
| 5242 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5243 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5244 | return 0; |
| 5245 | } |
| 5246 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5247 | /* |
| 5248 | * |
| 5249 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5250 | * Class TXN |
| 5251 | * |
| 5252 | * |
| 5253 | */ |
| 5254 | |
| 5255 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5256 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5257 | */ |
| 5258 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5259 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5260 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5261 | } |
| 5262 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5263 | __LJMP static int hlua_set_var(lua_State *L) |
| 5264 | { |
| 5265 | struct hlua_txn *htxn; |
| 5266 | const char *name; |
| 5267 | size_t len; |
| 5268 | struct sample smp; |
| 5269 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5270 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5271 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5272 | |
| 5273 | /* It is useles to retrieve the stream, but this function |
| 5274 | * runs only in a stream context. |
| 5275 | */ |
| 5276 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5277 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5278 | |
| 5279 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5280 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5281 | hlua_lua2smp(L, 3, &smp); |
| 5282 | |
| 5283 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5284 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5285 | |
| 5286 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5287 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5288 | else |
| 5289 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5290 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5291 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5292 | } |
| 5293 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5294 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5295 | { |
| 5296 | struct hlua_txn *htxn; |
| 5297 | const char *name; |
| 5298 | size_t len; |
| 5299 | struct sample smp; |
| 5300 | |
| 5301 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5302 | |
| 5303 | /* It is useles to retrieve the stream, but this function |
| 5304 | * runs only in a stream context. |
| 5305 | */ |
| 5306 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5307 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5308 | |
| 5309 | /* Unset the variable. */ |
| 5310 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5311 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5312 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5313 | } |
| 5314 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5315 | __LJMP static int hlua_get_var(lua_State *L) |
| 5316 | { |
| 5317 | struct hlua_txn *htxn; |
| 5318 | const char *name; |
| 5319 | size_t len; |
| 5320 | struct sample smp; |
| 5321 | |
| 5322 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5323 | |
| 5324 | /* It is useles to retrieve the stream, but this function |
| 5325 | * runs only in a stream context. |
| 5326 | */ |
| 5327 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5328 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5329 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5330 | 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] | 5331 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5332 | lua_pushnil(L); |
| 5333 | return 1; |
| 5334 | } |
| 5335 | |
| 5336 | return hlua_smp2lua(L, &smp); |
| 5337 | } |
| 5338 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5339 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5340 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5341 | struct hlua *hlua; |
| 5342 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5343 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5344 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5345 | /* It is useles to retrieve the stream, but this function |
| 5346 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5347 | */ |
| 5348 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5349 | |
| 5350 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5351 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5352 | if (!hlua) |
| 5353 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5354 | |
| 5355 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5356 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5357 | |
| 5358 | /* Get and store new value. */ |
| 5359 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5360 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5361 | |
| 5362 | return 0; |
| 5363 | } |
| 5364 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5365 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5366 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5367 | struct hlua *hlua; |
| 5368 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5369 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5370 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5371 | /* It is useles to retrieve the stream, but this function |
| 5372 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5373 | */ |
| 5374 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5375 | |
| 5376 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5377 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5378 | if (!hlua) { |
| 5379 | lua_pushnil(L); |
| 5380 | return 1; |
| 5381 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5382 | |
| 5383 | /* Push configuration index in the stack. */ |
| 5384 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5385 | |
| 5386 | return 1; |
| 5387 | } |
| 5388 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5389 | /* Create stack entry containing a class TXN. This function |
| 5390 | * return 0 if the stack does not contains free slots, |
| 5391 | * otherwise it returns 1. |
| 5392 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5393 | 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] | 5394 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5395 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5396 | |
| 5397 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5398 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5399 | return 0; |
| 5400 | |
| 5401 | /* NOTE: The allocation never fails. The failure |
| 5402 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5403 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5404 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5405 | /* Create the object: obj[0] = userdata. */ |
| 5406 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5407 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5408 | lua_rawseti(L, -2, 0); |
| 5409 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5410 | htxn->s = s; |
| 5411 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5412 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5413 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5414 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5415 | /* Create the "f" field that contains a list of fetches. */ |
| 5416 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5417 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5418 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5419 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5420 | |
| 5421 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5422 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5423 | 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] | 5424 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5425 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5426 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5427 | /* Create the "c" field that contains a list of converters. */ |
| 5428 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5429 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5430 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5431 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5432 | |
| 5433 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5434 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5435 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5436 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5437 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5438 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5439 | /* Create the "req" field that contains the request channel object. */ |
| 5440 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5441 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5442 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5443 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5444 | |
| 5445 | /* Create the "res" field that contains the response channel object. */ |
| 5446 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5447 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5448 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5449 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5450 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5451 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5452 | lua_pushstring(L, "http"); |
| 5453 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5454 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5455 | return 0; |
| 5456 | } |
| 5457 | else |
| 5458 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5459 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5460 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5461 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5462 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5463 | lua_setmetatable(L, -2); |
| 5464 | |
| 5465 | return 1; |
| 5466 | } |
| 5467 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5468 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5469 | { |
| 5470 | const char *msg; |
| 5471 | struct hlua_txn *htxn; |
| 5472 | |
| 5473 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5474 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5475 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5476 | |
| 5477 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5478 | return 0; |
| 5479 | } |
| 5480 | |
| 5481 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5482 | { |
| 5483 | int level; |
| 5484 | const char *msg; |
| 5485 | struct hlua_txn *htxn; |
| 5486 | |
| 5487 | MAY_LJMP(check_args(L, 3, "log")); |
| 5488 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5489 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5490 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5491 | |
| 5492 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5493 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5494 | |
| 5495 | hlua_sendlog(htxn->s->be, level, msg); |
| 5496 | return 0; |
| 5497 | } |
| 5498 | |
| 5499 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5500 | { |
| 5501 | const char *msg; |
| 5502 | struct hlua_txn *htxn; |
| 5503 | |
| 5504 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5505 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5506 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5507 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5508 | return 0; |
| 5509 | } |
| 5510 | |
| 5511 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5512 | { |
| 5513 | const char *msg; |
| 5514 | struct hlua_txn *htxn; |
| 5515 | |
| 5516 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5517 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5518 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5519 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5520 | return 0; |
| 5521 | } |
| 5522 | |
| 5523 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5524 | { |
| 5525 | const char *msg; |
| 5526 | struct hlua_txn *htxn; |
| 5527 | |
| 5528 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5529 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5530 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5531 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5532 | return 0; |
| 5533 | } |
| 5534 | |
| 5535 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5536 | { |
| 5537 | const char *msg; |
| 5538 | struct hlua_txn *htxn; |
| 5539 | |
| 5540 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5541 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5542 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5543 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5544 | return 0; |
| 5545 | } |
| 5546 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5547 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5548 | { |
| 5549 | struct hlua_txn *htxn; |
| 5550 | int ll; |
| 5551 | |
| 5552 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5553 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5554 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5555 | |
| 5556 | if (ll < 0 || ll > 7) |
| 5557 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5558 | |
| 5559 | htxn->s->logs.level = ll; |
| 5560 | return 0; |
| 5561 | } |
| 5562 | |
| 5563 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5564 | { |
| 5565 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5566 | int tos; |
| 5567 | |
| 5568 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5569 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5570 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5571 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5572 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5573 | return 0; |
| 5574 | } |
| 5575 | |
| 5576 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5577 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5578 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5579 | int mark; |
| 5580 | |
| 5581 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5582 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5583 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5584 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5585 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5586 | return 0; |
| 5587 | } |
| 5588 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5589 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5590 | { |
| 5591 | struct hlua_txn *htxn; |
| 5592 | |
| 5593 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5594 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5595 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5596 | return 0; |
| 5597 | } |
| 5598 | |
| 5599 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5600 | { |
| 5601 | struct hlua_txn *htxn; |
| 5602 | |
| 5603 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5604 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5605 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5606 | return 0; |
| 5607 | } |
| 5608 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5609 | /* Forward the Reply object to the client. This function converts the reply in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5610 | * HTX an push it to into the response channel. It is response to forward the |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5611 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5612 | * error. The Reply must be on top of the stack. |
| 5613 | */ |
| 5614 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5615 | { |
| 5616 | struct htx *htx; |
| 5617 | struct htx_sl *sl; |
| 5618 | struct h1m h1m; |
| 5619 | const char *status, *reason, *body; |
| 5620 | size_t status_len, reason_len, body_len; |
| 5621 | int ret, code, flags; |
| 5622 | |
| 5623 | code = 200; |
| 5624 | status = "200"; |
| 5625 | status_len = 3; |
| 5626 | ret = lua_getfield(L, -1, "status"); |
| 5627 | if (ret == LUA_TNUMBER) { |
| 5628 | code = lua_tointeger(L, -1); |
| 5629 | status = lua_tolstring(L, -1, &status_len); |
| 5630 | } |
| 5631 | lua_pop(L, 1); |
| 5632 | |
| 5633 | reason = http_get_reason(code); |
| 5634 | reason_len = strlen(reason); |
| 5635 | ret = lua_getfield(L, -1, "reason"); |
| 5636 | if (ret == LUA_TSTRING) |
| 5637 | reason = lua_tolstring(L, -1, &reason_len); |
| 5638 | lua_pop(L, 1); |
| 5639 | |
| 5640 | body = NULL; |
| 5641 | body_len = 0; |
| 5642 | ret = lua_getfield(L, -1, "body"); |
| 5643 | if (ret == LUA_TSTRING) |
| 5644 | body = lua_tolstring(L, -1, &body_len); |
| 5645 | lua_pop(L, 1); |
| 5646 | |
| 5647 | /* Prepare the response before inserting the headers */ |
| 5648 | h1m_init_res(&h1m); |
| 5649 | htx = htx_from_buf(&s->res.buf); |
| 5650 | channel_htx_truncate(&s->res, htx); |
| 5651 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5652 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5653 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5654 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5655 | } |
| 5656 | else { |
| 5657 | flags = HTX_SL_F_IS_RESP; |
| 5658 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5659 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5660 | } |
| 5661 | if (!sl) |
| 5662 | goto fail; |
| 5663 | sl->info.res.status = code; |
| 5664 | |
| 5665 | /* Push in the stack the "headers" entry. */ |
| 5666 | ret = lua_getfield(L, -1, "headers"); |
| 5667 | if (ret != LUA_TTABLE) |
| 5668 | goto skip_headers; |
| 5669 | |
| 5670 | lua_pushnil(L); |
| 5671 | while (lua_next(L, -2) != 0) { |
| 5672 | struct ist name, value; |
| 5673 | const char *n, *v; |
| 5674 | size_t nlen, vlen; |
| 5675 | |
| 5676 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5677 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5678 | goto next_hdr; |
| 5679 | } |
| 5680 | |
| 5681 | n = lua_tolstring(L, -2, &nlen); |
| 5682 | name = ist2(n, nlen); |
| 5683 | if (isteqi(name, ist("content-length"))) { |
| 5684 | /* Always skip content-length header. It will be added |
| 5685 | * later with the correct len |
| 5686 | */ |
| 5687 | goto next_hdr; |
| 5688 | } |
| 5689 | |
| 5690 | /* Loop on header's values */ |
| 5691 | lua_pushnil(L); |
| 5692 | while (lua_next(L, -2)) { |
| 5693 | if (!lua_isstring(L, -1)) { |
| 5694 | /* Skip the value if it is not a string */ |
| 5695 | goto next_value; |
| 5696 | } |
| 5697 | |
| 5698 | v = lua_tolstring(L, -1, &vlen); |
| 5699 | value = ist2(v, vlen); |
| 5700 | |
| 5701 | if (isteqi(name, ist("transfer-encoding"))) |
| 5702 | h1_parse_xfer_enc_header(&h1m, value); |
| 5703 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5704 | goto fail; |
| 5705 | |
| 5706 | next_value: |
| 5707 | lua_pop(L, 1); |
| 5708 | } |
| 5709 | |
| 5710 | next_hdr: |
| 5711 | lua_pop(L, 1); |
| 5712 | } |
| 5713 | skip_headers: |
| 5714 | lua_pop(L, 1); |
| 5715 | |
| 5716 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5717 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5718 | const char *clen = ultoa(body_len); |
| 5719 | |
| 5720 | h1m.flags |= H1_MF_CLEN; |
| 5721 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5722 | goto fail; |
| 5723 | } |
| 5724 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5725 | h1m.flags |= H1_MF_XFER_LEN; |
| 5726 | |
| 5727 | /* Update HTX start-line flags */ |
| 5728 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5729 | flags |= HTX_SL_F_XFER_ENC; |
| 5730 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5731 | flags |= HTX_SL_F_XFER_LEN; |
| 5732 | if (h1m.flags & H1_MF_CHNK) |
| 5733 | flags |= HTX_SL_F_CHNK; |
| 5734 | else if (h1m.flags & H1_MF_CLEN) |
| 5735 | flags |= HTX_SL_F_CLEN; |
| 5736 | if (h1m.body_len == 0) |
| 5737 | flags |= HTX_SL_F_BODYLESS; |
| 5738 | } |
| 5739 | sl->flags |= flags; |
| 5740 | |
| 5741 | |
| 5742 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5743 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5744 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5745 | goto fail; |
| 5746 | |
| 5747 | /* Now, forward the response and terminate the transaction */ |
| 5748 | s->txn->status = code; |
| 5749 | htx_to_buf(htx, &s->res.buf); |
| 5750 | if (!http_forward_proxy_resp(s, 1)) |
| 5751 | goto fail; |
| 5752 | |
| 5753 | return 1; |
| 5754 | |
| 5755 | fail: |
| 5756 | channel_htx_truncate(&s->res, htx); |
| 5757 | return 0; |
| 5758 | } |
| 5759 | |
| 5760 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5761 | * processing is just aborted. Nothing is returned to the client and all |
| 5762 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5763 | * is forwarded to the client before terminating the transaction. On success, |
| 5764 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5765 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5766 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5767 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5768 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5769 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5770 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5771 | struct stream *s; |
| 5772 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5773 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5774 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5775 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5776 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5777 | * just end the execution of the current lua code. */ |
| 5778 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5779 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5780 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5781 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5782 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5783 | struct channel *req = &s->req; |
| 5784 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5785 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5786 | channel_auto_read(req); |
| 5787 | channel_abort(req); |
| 5788 | channel_auto_close(req); |
| 5789 | channel_erase(req); |
| 5790 | |
| 5791 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5792 | channel_auto_read(res); |
| 5793 | channel_auto_close(res); |
| 5794 | channel_shutr_now(res); |
| 5795 | |
| 5796 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5797 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5798 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5799 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5800 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5801 | /* No reply or invalid reply */ |
| 5802 | s->txn->status = 0; |
| 5803 | http_reply_and_close(s, 0, NULL); |
| 5804 | } |
| 5805 | else { |
| 5806 | /* Remove extra args to have the reply on top of the stack */ |
| 5807 | if (lua_gettop(L) > 2) |
| 5808 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5809 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5810 | if (!hlua_txn_forward_reply(L, s)) { |
| 5811 | if (!(s->flags & SF_ERR_MASK)) |
| 5812 | s->flags |= SF_ERR_PRXCOND; |
| 5813 | lua_pushinteger(L, ACT_RET_ERR); |
| 5814 | WILL_LJMP(hlua_done(L)); |
| 5815 | return 0; /* Never reached */ |
| 5816 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5817 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5818 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5819 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5820 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5821 | /* let's log the request time */ |
| 5822 | s->logs.tv_request = now; |
| 5823 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5824 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5825 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5826 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5827 | done: |
| 5828 | if (!(s->flags & SF_ERR_MASK)) |
| 5829 | s->flags |= SF_ERR_LOCAL; |
| 5830 | if (!(s->flags & SF_FINST_MASK)) |
| 5831 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5832 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5833 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5834 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5835 | return 0; |
| 5836 | } |
| 5837 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5838 | /* |
| 5839 | * |
| 5840 | * |
| 5841 | * Class REPLY |
| 5842 | * |
| 5843 | * |
| 5844 | */ |
| 5845 | |
| 5846 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5847 | * free slots, the function fails and returns 0; |
| 5848 | */ |
| 5849 | static int hlua_txn_reply_new(lua_State *L) |
| 5850 | { |
| 5851 | struct hlua_txn *htxn; |
| 5852 | const char *reason, *body = NULL; |
| 5853 | int ret, status; |
| 5854 | |
| 5855 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5856 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5857 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5858 | WILL_LJMP(lua_error(L)); |
| 5859 | } |
| 5860 | |
| 5861 | /* Default value */ |
| 5862 | status = 200; |
| 5863 | reason = http_get_reason(status); |
| 5864 | |
| 5865 | if (lua_istable(L, 2)) { |
| 5866 | /* load status and reason from the table argument at index 2 */ |
| 5867 | ret = lua_getfield(L, 2, "status"); |
| 5868 | if (ret == LUA_TNIL) |
| 5869 | goto reason; |
| 5870 | else if (ret != LUA_TNUMBER) { |
| 5871 | /* invalid status: ignore the reason */ |
| 5872 | goto body; |
| 5873 | } |
| 5874 | status = lua_tointeger(L, -1); |
| 5875 | |
| 5876 | reason: |
| 5877 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5878 | ret = lua_getfield(L, 2, "reason"); |
| 5879 | if (ret == LUA_TSTRING) |
| 5880 | reason = lua_tostring(L, -1); |
| 5881 | |
| 5882 | body: |
| 5883 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5884 | ret = lua_getfield(L, 2, "body"); |
| 5885 | if (ret == LUA_TSTRING) |
| 5886 | body = lua_tostring(L, -1); |
| 5887 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5888 | } |
| 5889 | |
| 5890 | /* Create the Reply table */ |
| 5891 | lua_newtable(L); |
| 5892 | |
| 5893 | /* Add status element */ |
| 5894 | lua_pushstring(L, "status"); |
| 5895 | lua_pushinteger(L, status); |
| 5896 | lua_settable(L, -3); |
| 5897 | |
| 5898 | /* Add reason element */ |
| 5899 | reason = http_get_reason(status); |
| 5900 | lua_pushstring(L, "reason"); |
| 5901 | lua_pushstring(L, reason); |
| 5902 | lua_settable(L, -3); |
| 5903 | |
| 5904 | /* Add body element, nil if undefined */ |
| 5905 | lua_pushstring(L, "body"); |
| 5906 | if (body) |
| 5907 | lua_pushstring(L, body); |
| 5908 | else |
| 5909 | lua_pushnil(L); |
| 5910 | lua_settable(L, -3); |
| 5911 | |
| 5912 | /* Add headers element */ |
| 5913 | lua_pushstring(L, "headers"); |
| 5914 | lua_newtable(L); |
| 5915 | |
| 5916 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5917 | if (lua_istable(L, 2)) { |
| 5918 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5919 | ret = lua_getfield(L, 2, "headers"); |
| 5920 | if (ret == LUA_TTABLE) { |
| 5921 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5922 | lua_pushnil(L); |
| 5923 | while (lua_next(L, -2) != 0) { |
| 5924 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5925 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5926 | /* invalid value type, skip it */ |
| 5927 | lua_pop(L, 1); |
| 5928 | continue; |
| 5929 | } |
| 5930 | |
| 5931 | |
| 5932 | /* Duplicate the key and swap it with the value. */ |
| 5933 | lua_pushvalue(L, -2); |
| 5934 | lua_insert(L, -2); |
| 5935 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5936 | |
| 5937 | lua_newtable(L); |
| 5938 | lua_insert(L, -2); |
| 5939 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5940 | |
| 5941 | if (lua_isstring(L, -1)) { |
| 5942 | /* push the value in the inner table */ |
| 5943 | lua_rawseti(L, -2, 1); |
| 5944 | } |
| 5945 | else { /* table */ |
| 5946 | lua_pushnil(L); |
| 5947 | while (lua_next(L, -2) != 0) { |
| 5948 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5949 | if (!lua_isstring(L, -1)) { |
| 5950 | /* invalid value type, skip it*/ |
| 5951 | lua_pop(L, 1); |
| 5952 | continue; |
| 5953 | } |
| 5954 | /* push the value in the inner table */ |
| 5955 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5956 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5957 | } |
| 5958 | lua_pop(L, 1); |
| 5959 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5960 | } |
| 5961 | |
| 5962 | /* push (k,v) on the stack in the headers table: |
| 5963 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5964 | */ |
| 5965 | lua_settable(L, -5); |
| 5966 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5967 | } |
| 5968 | } |
| 5969 | lua_pop(L, 1); |
| 5970 | } |
| 5971 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5972 | lua_settable(L, -3); |
| 5973 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5974 | |
| 5975 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5976 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5977 | lua_setmetatable(L, -2); |
| 5978 | return 1; |
| 5979 | } |
| 5980 | |
| 5981 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5982 | * provided, the default one corresponding to the status code is used. |
| 5983 | */ |
| 5984 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5985 | { |
| 5986 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5987 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5988 | |
| 5989 | /* First argument (self) must be a table */ |
| 5990 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5991 | |
| 5992 | if (status < 100 || status > 599) { |
| 5993 | lua_pushboolean(L, 0); |
| 5994 | return 1; |
| 5995 | } |
| 5996 | if (!reason) |
| 5997 | reason = http_get_reason(status); |
| 5998 | |
| 5999 | lua_pushinteger(L, status); |
| 6000 | lua_setfield(L, 1, "status"); |
| 6001 | |
| 6002 | lua_pushstring(L, reason); |
| 6003 | lua_setfield(L, 1, "reason"); |
| 6004 | |
| 6005 | lua_pushboolean(L, 1); |
| 6006 | return 1; |
| 6007 | } |
| 6008 | |
| 6009 | /* Add a header into the reply object. Each header name is associated to an |
| 6010 | * array of values in the "headers" table. If the header name is not found, a |
| 6011 | * new entry is created. |
| 6012 | */ |
| 6013 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6014 | { |
| 6015 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6016 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6017 | int ret; |
| 6018 | |
| 6019 | /* First argument (self) must be a table */ |
| 6020 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6021 | |
| 6022 | /* Push in the stack the "headers" entry. */ |
| 6023 | ret = lua_getfield(L, 1, "headers"); |
| 6024 | if (ret != LUA_TTABLE) { |
| 6025 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6026 | WILL_LJMP(lua_error(L)); |
| 6027 | } |
| 6028 | |
| 6029 | /* check if the header is already registered. If not, register it. */ |
| 6030 | ret = lua_getfield(L, -1, name); |
| 6031 | if (ret == LUA_TNIL) { |
| 6032 | /* Entry not found. */ |
| 6033 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6034 | |
| 6035 | /* Insert the new header name in the array in the top of the stack. |
| 6036 | * It left the new array in the top of the stack. |
| 6037 | */ |
| 6038 | lua_newtable(L); |
| 6039 | lua_pushstring(L, name); |
| 6040 | lua_pushvalue(L, -2); |
| 6041 | lua_settable(L, -4); |
| 6042 | } |
| 6043 | else if (ret != LUA_TTABLE) { |
| 6044 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6045 | WILL_LJMP(lua_error(L)); |
| 6046 | } |
| 6047 | |
| 6048 | /* Now the top od thestack is an array of values. We push |
| 6049 | * the header value as new entry. |
| 6050 | */ |
| 6051 | lua_pushstring(L, value); |
| 6052 | ret = lua_rawlen(L, -2); |
| 6053 | lua_rawseti(L, -2, ret + 1); |
| 6054 | |
| 6055 | lua_pushboolean(L, 1); |
| 6056 | return 1; |
| 6057 | } |
| 6058 | |
| 6059 | /* Remove all occurrences of a given header name. */ |
| 6060 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6061 | { |
| 6062 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6063 | int ret; |
| 6064 | |
| 6065 | /* First argument (self) must be a table */ |
| 6066 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6067 | |
| 6068 | /* Push in the stack the "headers" entry. */ |
| 6069 | ret = lua_getfield(L, 1, "headers"); |
| 6070 | if (ret != LUA_TTABLE) { |
| 6071 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6072 | WILL_LJMP(lua_error(L)); |
| 6073 | } |
| 6074 | |
| 6075 | lua_pushstring(L, name); |
| 6076 | lua_pushnil(L); |
| 6077 | lua_settable(L, -3); |
| 6078 | |
| 6079 | lua_pushboolean(L, 1); |
| 6080 | return 1; |
| 6081 | } |
| 6082 | |
| 6083 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6084 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6085 | { |
| 6086 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6087 | |
| 6088 | /* First argument (self) must be a table */ |
| 6089 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6090 | |
| 6091 | lua_pushstring(L, payload); |
| 6092 | lua_setfield(L, 1, "body"); |
| 6093 | |
| 6094 | lua_pushboolean(L, 1); |
| 6095 | return 1; |
| 6096 | } |
| 6097 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6098 | __LJMP static int hlua_log(lua_State *L) |
| 6099 | { |
| 6100 | int level; |
| 6101 | const char *msg; |
| 6102 | |
| 6103 | MAY_LJMP(check_args(L, 2, "log")); |
| 6104 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6105 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6106 | |
| 6107 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6108 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6109 | |
| 6110 | hlua_sendlog(NULL, level, msg); |
| 6111 | return 0; |
| 6112 | } |
| 6113 | |
| 6114 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6115 | { |
| 6116 | const char *msg; |
| 6117 | |
| 6118 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6119 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6120 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6121 | return 0; |
| 6122 | } |
| 6123 | |
| 6124 | __LJMP static int hlua_log_info(lua_State *L) |
| 6125 | { |
| 6126 | const char *msg; |
| 6127 | |
| 6128 | MAY_LJMP(check_args(L, 1, "info")); |
| 6129 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6130 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6131 | return 0; |
| 6132 | } |
| 6133 | |
| 6134 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6135 | { |
| 6136 | const char *msg; |
| 6137 | |
| 6138 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6139 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6140 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6141 | return 0; |
| 6142 | } |
| 6143 | |
| 6144 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6145 | { |
| 6146 | const char *msg; |
| 6147 | |
| 6148 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6149 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6150 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6151 | return 0; |
| 6152 | } |
| 6153 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6154 | __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] | 6155 | { |
| 6156 | int wakeup_ms = lua_tointeger(L, -1); |
| 6157 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6158 | 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] | 6159 | return 0; |
| 6160 | } |
| 6161 | |
| 6162 | __LJMP static int hlua_sleep(lua_State *L) |
| 6163 | { |
| 6164 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6165 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6166 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6167 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6168 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6169 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6170 | wakeup_ms = tick_add(now_ms, delay); |
| 6171 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6172 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6173 | 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] | 6174 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6175 | } |
| 6176 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6177 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6178 | { |
| 6179 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6180 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6181 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6182 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6183 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6184 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6185 | wakeup_ms = tick_add(now_ms, delay); |
| 6186 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6187 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6188 | 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] | 6189 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6190 | } |
| 6191 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6192 | /* This functionis an LUA binding. it permits to give back |
| 6193 | * the hand at the HAProxy scheduler. It is used when the |
| 6194 | * LUA processing consumes a lot of time. |
| 6195 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6196 | __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] | 6197 | { |
| 6198 | return 0; |
| 6199 | } |
| 6200 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6201 | __LJMP static int hlua_yield(lua_State *L) |
| 6202 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6203 | 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] | 6204 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6205 | } |
| 6206 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6207 | /* This function change the nice of the currently executed |
| 6208 | * task. It is used set low or high priority at the current |
| 6209 | * task. |
| 6210 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6211 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6212 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6213 | struct hlua *hlua; |
| 6214 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6215 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6216 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6217 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6218 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6219 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6220 | hlua = hlua_gethlua(L); |
| 6221 | |
| 6222 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6223 | if (!hlua || !hlua->task) |
| 6224 | return 0; |
| 6225 | |
| 6226 | if (nice < -1024) |
| 6227 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6228 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6229 | nice = 1024; |
| 6230 | |
| 6231 | hlua->task->nice = nice; |
| 6232 | return 0; |
| 6233 | } |
| 6234 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6235 | /* 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] | 6236 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6237 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6238 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6239 | * |
| 6240 | * Task wrapper are longjmp safe because the only one Lua code |
| 6241 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6242 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6243 | 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] | 6244 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6245 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6246 | enum hlua_exec status; |
| 6247 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6248 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6249 | task_set_affinity(task, tid_bit); |
| 6250 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6251 | /* If it is the first call to the task, we must initialize the |
| 6252 | * execution timeouts. |
| 6253 | */ |
| 6254 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6255 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6256 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6257 | /* Execute the Lua code. */ |
| 6258 | status = hlua_ctx_resume(hlua, 1); |
| 6259 | |
| 6260 | switch (status) { |
| 6261 | /* finished or yield */ |
| 6262 | case HLUA_E_OK: |
| 6263 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6264 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6265 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6266 | break; |
| 6267 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6268 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6269 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6270 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6271 | break; |
| 6272 | |
| 6273 | /* finished with error. */ |
| 6274 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6275 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6276 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6277 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6278 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6279 | break; |
| 6280 | |
| 6281 | case HLUA_E_ERR: |
| 6282 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6283 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6284 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6285 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6286 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6287 | break; |
| 6288 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6289 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6290 | } |
| 6291 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6292 | /* This function is an LUA binding that register LUA function to be |
| 6293 | * executed after the HAProxy configuration parsing and before the |
| 6294 | * HAProxy scheduler starts. This function expect only one LUA |
| 6295 | * argument that is a function. This function returns nothing, but |
| 6296 | * throws if an error is encountered. |
| 6297 | */ |
| 6298 | __LJMP static int hlua_register_init(lua_State *L) |
| 6299 | { |
| 6300 | struct hlua_init_function *init; |
| 6301 | int ref; |
| 6302 | |
| 6303 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6304 | |
| 6305 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6306 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6307 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6308 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6309 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6310 | |
| 6311 | init->function_ref = ref; |
| 6312 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6313 | return 0; |
| 6314 | } |
| 6315 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6316 | /* This functio is an LUA binding. It permits to register a task |
| 6317 | * executed in parallel of the main HAroxy activity. The task is |
| 6318 | * created and it is set in the HAProxy scheduler. It can be called |
| 6319 | * from the "init" section, "post init" or during the runtime. |
| 6320 | * |
| 6321 | * Lua prototype: |
| 6322 | * |
| 6323 | * <none> core.register_task(<function>) |
| 6324 | */ |
| 6325 | static int hlua_register_task(lua_State *L) |
| 6326 | { |
| 6327 | struct hlua *hlua; |
| 6328 | struct task *task; |
| 6329 | int ref; |
| 6330 | |
| 6331 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6332 | |
| 6333 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6334 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6335 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6336 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6337 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6338 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6339 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6340 | if (!task) |
| 6341 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6342 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6343 | task->context = hlua; |
| 6344 | task->process = hlua_process_task; |
| 6345 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6346 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6347 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6348 | |
| 6349 | /* Restore the function in the stack. */ |
| 6350 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6351 | hlua->nargs = 0; |
| 6352 | |
| 6353 | /* Schedule task. */ |
| 6354 | task_schedule(task, now_ms); |
| 6355 | |
| 6356 | return 0; |
| 6357 | } |
| 6358 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6359 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6360 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6361 | * resume converters. |
| 6362 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6363 | 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] | 6364 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6365 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6366 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6367 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6368 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6369 | if (!stream) |
| 6370 | return 0; |
| 6371 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6372 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6373 | * Lua context can be not initialized. This behavior |
| 6374 | * permits to save performances because a systematic |
| 6375 | * Lua initialization cause 5% performances loss. |
| 6376 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6377 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6378 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6379 | if (!stream->hlua) { |
| 6380 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6381 | return 0; |
| 6382 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6383 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6384 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6385 | return 0; |
| 6386 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6387 | } |
| 6388 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6389 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6390 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6391 | |
| 6392 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6393 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6394 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6395 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6396 | else |
| 6397 | error = "critical error"; |
| 6398 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6399 | return 0; |
| 6400 | } |
| 6401 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6402 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6403 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6404 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6405 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6406 | return 0; |
| 6407 | } |
| 6408 | |
| 6409 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6410 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6411 | |
| 6412 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6413 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6414 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6415 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6416 | return 0; |
| 6417 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6418 | hlua_smp2lua(stream->hlua->T, smp); |
| 6419 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6420 | |
| 6421 | /* push keywords in the stack. */ |
| 6422 | if (arg_p) { |
| 6423 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6424 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6425 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6426 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6427 | return 0; |
| 6428 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6429 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6430 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6431 | } |
| 6432 | } |
| 6433 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6434 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6435 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6436 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6437 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6438 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6442 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6443 | /* finished. */ |
| 6444 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6445 | /* If the stack is empty, the function fails. */ |
| 6446 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6447 | return 0; |
| 6448 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6449 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6450 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6451 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6452 | return 1; |
| 6453 | |
| 6454 | /* yield. */ |
| 6455 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6456 | 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] | 6457 | return 0; |
| 6458 | |
| 6459 | /* finished with error. */ |
| 6460 | case HLUA_E_ERRMSG: |
| 6461 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6462 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6463 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6464 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6465 | return 0; |
| 6466 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6467 | case HLUA_E_ETMOUT: |
| 6468 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6469 | return 0; |
| 6470 | |
| 6471 | case HLUA_E_NOMEM: |
| 6472 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6473 | return 0; |
| 6474 | |
| 6475 | case HLUA_E_YIELD: |
| 6476 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6477 | return 0; |
| 6478 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6479 | case HLUA_E_ERR: |
| 6480 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6481 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6482 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6483 | |
| 6484 | default: |
| 6485 | return 0; |
| 6486 | } |
| 6487 | } |
| 6488 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6489 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6490 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6491 | * resume sample-fetches. This function will be called by the sample |
| 6492 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6493 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6494 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6495 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6496 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6497 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6498 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6499 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6500 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6501 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6502 | if (!stream) |
| 6503 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6504 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6505 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6506 | * Lua context can be not initialized. This behavior |
| 6507 | * permits to save performances because a systematic |
| 6508 | * Lua initialization cause 5% performances loss. |
| 6509 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6510 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6511 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6512 | if (!stream->hlua) { |
| 6513 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6514 | return 0; |
| 6515 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6516 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6517 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6518 | return 0; |
| 6519 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6520 | } |
| 6521 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6522 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6523 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6524 | |
| 6525 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6526 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6527 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6528 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6529 | else |
| 6530 | error = "critical error"; |
| 6531 | 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] | 6532 | return 0; |
| 6533 | } |
| 6534 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6535 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6536 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6537 | 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] | 6538 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6539 | return 0; |
| 6540 | } |
| 6541 | |
| 6542 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6543 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6544 | |
| 6545 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6546 | 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] | 6547 | 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] | 6548 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6549 | return 0; |
| 6550 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6551 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6552 | |
| 6553 | /* push keywords in the stack. */ |
| 6554 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6555 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6556 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6557 | 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] | 6558 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6559 | return 0; |
| 6560 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6561 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6562 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6563 | } |
| 6564 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6565 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6566 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6567 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6568 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6569 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6570 | } |
| 6571 | |
| 6572 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6573 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6574 | /* finished. */ |
| 6575 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6576 | /* If the stack is empty, the function fails. */ |
| 6577 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6578 | return 0; |
| 6579 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6580 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6581 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6582 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6583 | |
| 6584 | /* Set the end of execution flag. */ |
| 6585 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6586 | return 1; |
| 6587 | |
| 6588 | /* yield. */ |
| 6589 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6590 | 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] | 6591 | return 0; |
| 6592 | |
| 6593 | /* finished with error. */ |
| 6594 | case HLUA_E_ERRMSG: |
| 6595 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6596 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6597 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6598 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6599 | return 0; |
| 6600 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6601 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6602 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6603 | return 0; |
| 6604 | |
| 6605 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6606 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6607 | return 0; |
| 6608 | |
| 6609 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6610 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6611 | return 0; |
| 6612 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6613 | case HLUA_E_ERR: |
| 6614 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6615 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6616 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6617 | |
| 6618 | default: |
| 6619 | return 0; |
| 6620 | } |
| 6621 | } |
| 6622 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6623 | /* This function is an LUA binding used for registering |
| 6624 | * "sample-conv" functions. It expects a converter name used |
| 6625 | * in the haproxy configuration file, and an LUA function. |
| 6626 | */ |
| 6627 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6628 | { |
| 6629 | struct sample_conv_kw_list *sck; |
| 6630 | const char *name; |
| 6631 | int ref; |
| 6632 | int len; |
| 6633 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6634 | struct sample_conv *sc; |
| 6635 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6636 | |
| 6637 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6638 | |
| 6639 | /* First argument : converter name. */ |
| 6640 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6641 | |
| 6642 | /* Second argument : lua function. */ |
| 6643 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6644 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6645 | /* Check if the converter is already registered */ |
| 6646 | trash = get_trash_chunk(); |
| 6647 | chunk_printf(trash, "lua.%s", name); |
| 6648 | sc = find_sample_conv(trash->area, trash->data); |
| 6649 | if (sc != NULL) { |
| 6650 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6651 | "This will become a hard error in version 2.5.\n", name); |
| 6652 | } |
| 6653 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6654 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6655 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6656 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6657 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6658 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6659 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6660 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6661 | |
| 6662 | /* Fill fcn. */ |
| 6663 | fcn->name = strdup(name); |
| 6664 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6665 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6666 | fcn->function_ref = ref; |
| 6667 | |
| 6668 | /* List head */ |
| 6669 | sck->list.n = sck->list.p = NULL; |
| 6670 | |
| 6671 | /* converter keyword. */ |
| 6672 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6673 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6674 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6675 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6676 | |
| 6677 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6678 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6679 | 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] | 6680 | sck->kw[0].val_args = NULL; |
| 6681 | sck->kw[0].in_type = SMP_T_STR; |
| 6682 | sck->kw[0].out_type = SMP_T_STR; |
| 6683 | sck->kw[0].private = fcn; |
| 6684 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6685 | /* Register this new converter */ |
| 6686 | sample_register_convs(sck); |
| 6687 | |
| 6688 | return 0; |
| 6689 | } |
| 6690 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6691 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6692 | * "sample-fetch" functions. It expects a converter name used |
| 6693 | * in the haproxy configuration file, and an LUA function. |
| 6694 | */ |
| 6695 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6696 | { |
| 6697 | const char *name; |
| 6698 | int ref; |
| 6699 | int len; |
| 6700 | struct sample_fetch_kw_list *sfk; |
| 6701 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6702 | struct sample_fetch *sf; |
| 6703 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6704 | |
| 6705 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6706 | |
| 6707 | /* First argument : sample-fetch name. */ |
| 6708 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6709 | |
| 6710 | /* Second argument : lua function. */ |
| 6711 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6712 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6713 | /* Check if the sample-fetch is already registered */ |
| 6714 | trash = get_trash_chunk(); |
| 6715 | chunk_printf(trash, "lua.%s", name); |
| 6716 | sf = find_sample_fetch(trash->area, trash->data); |
| 6717 | if (sf != NULL) { |
| 6718 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6719 | "This will become a hard error in version 2.5.\n", name); |
| 6720 | } |
| 6721 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6722 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6723 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6724 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6725 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6726 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6727 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6728 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6729 | |
| 6730 | /* Fill fcn. */ |
| 6731 | fcn->name = strdup(name); |
| 6732 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6733 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6734 | fcn->function_ref = ref; |
| 6735 | |
| 6736 | /* List head */ |
| 6737 | sfk->list.n = sfk->list.p = NULL; |
| 6738 | |
| 6739 | /* sample-fetch keyword. */ |
| 6740 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6741 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6742 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6743 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6744 | |
| 6745 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6746 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6747 | 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] | 6748 | sfk->kw[0].val_args = NULL; |
| 6749 | sfk->kw[0].out_type = SMP_T_STR; |
| 6750 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6751 | sfk->kw[0].val = 0; |
| 6752 | sfk->kw[0].private = fcn; |
| 6753 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6754 | /* Register this new fetch. */ |
| 6755 | sample_register_fetches(sfk); |
| 6756 | |
| 6757 | return 0; |
| 6758 | } |
| 6759 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6760 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6761 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6762 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6763 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6764 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6765 | unsigned int delay; |
| 6766 | unsigned int wakeup_ms; |
| 6767 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6768 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6769 | hlua = hlua_gethlua(L); |
| 6770 | if (!hlua) { |
| 6771 | return 0; |
| 6772 | } |
| 6773 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6774 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6775 | |
| 6776 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6777 | wakeup_ms = tick_add(now_ms, delay); |
| 6778 | hlua->wake_time = wakeup_ms; |
| 6779 | return 0; |
| 6780 | } |
| 6781 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6782 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6783 | * wrapper during the initialisation period. This function may return any |
| 6784 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6785 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6786 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6787 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6788 | 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] | 6789 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6790 | { |
| 6791 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6792 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6793 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6794 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6795 | |
| 6796 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6797 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6798 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6799 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6800 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6801 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6802 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6803 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6804 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6805 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6806 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6807 | * Lua context can be not initialized. This behavior |
| 6808 | * permits to save performances because a systematic |
| 6809 | * Lua initialization cause 5% performances loss. |
| 6810 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6811 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6812 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6813 | if (!s->hlua) { |
| 6814 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6815 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6816 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6817 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6818 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6819 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6820 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6821 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6822 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6823 | } |
| 6824 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6825 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6826 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6827 | |
| 6828 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6829 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6830 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6831 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6832 | else |
| 6833 | error = "critical error"; |
| 6834 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6835 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6836 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6837 | } |
| 6838 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6839 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6840 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6841 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6842 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6843 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6844 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6845 | } |
| 6846 | |
| 6847 | /* Restore the function in the stack. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6848 | 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] | 6849 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6850 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6851 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6852 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6853 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6854 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6855 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6856 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6857 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6858 | |
| 6859 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6860 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6861 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6862 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6863 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6864 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6865 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6866 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6867 | lua_pushstring(s->hlua->T, *arg); |
| 6868 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6869 | } |
| 6870 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6871 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6872 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6873 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6874 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6875 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6879 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6880 | /* finished. */ |
| 6881 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6882 | /* Catch the return value */ |
| 6883 | if (lua_gettop(s->hlua->T) > 0) |
| 6884 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6885 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6886 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6887 | if (act_ret == ACT_RET_YIELD) { |
| 6888 | if (flags & ACT_OPT_FINAL) |
| 6889 | goto err_yield; |
| 6890 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6891 | if (dir == SMP_OPT_DIR_REQ) |
| 6892 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6893 | s->hlua->wake_time); |
| 6894 | else |
| 6895 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6896 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6897 | } |
| 6898 | goto end; |
| 6899 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6900 | /* yield. */ |
| 6901 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6902 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6903 | if (dir == SMP_OPT_DIR_REQ) |
| 6904 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6905 | s->hlua->wake_time); |
| 6906 | else |
| 6907 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6908 | s->hlua->wake_time); |
| 6909 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6910 | /* Some actions can be wake up when a "write" event |
| 6911 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6912 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6913 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6914 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6915 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6916 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6917 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6918 | act_ret = ACT_RET_YIELD; |
| 6919 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6920 | |
| 6921 | /* finished with error. */ |
| 6922 | case HLUA_E_ERRMSG: |
| 6923 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6924 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6925 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6926 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6927 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6928 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6929 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6930 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6931 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6932 | |
| 6933 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6934 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6935 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6936 | |
| 6937 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6938 | err_yield: |
| 6939 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6940 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6941 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6942 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6943 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6944 | case HLUA_E_ERR: |
| 6945 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6946 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6947 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6948 | |
| 6949 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6950 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6951 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6952 | |
| 6953 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6954 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6955 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6956 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6957 | } |
| 6958 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6959 | 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] | 6960 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6961 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6962 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6963 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6964 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6965 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6966 | } |
| 6967 | |
| 6968 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6969 | { |
| 6970 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6971 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6972 | struct task *task; |
| 6973 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6974 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6975 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6976 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6977 | if (!hlua) { |
| 6978 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6979 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6980 | return 0; |
| 6981 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6982 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6983 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6984 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6985 | |
| 6986 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6987 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6988 | if (!task) { |
| 6989 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6990 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6991 | return 0; |
| 6992 | } |
| 6993 | task->nice = 0; |
| 6994 | task->context = ctx; |
| 6995 | task->process = hlua_applet_wakeup; |
| 6996 | ctx->ctx.hlua_apptcp.task = task; |
| 6997 | |
| 6998 | /* In the execution wrappers linked with a stream, the |
| 6999 | * Lua context can be not initialized. This behavior |
| 7000 | * permits to save performances because a systematic |
| 7001 | * Lua initialization cause 5% performances loss. |
| 7002 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7003 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7004 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7005 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7006 | return 0; |
| 7007 | } |
| 7008 | |
| 7009 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7010 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7011 | |
| 7012 | /* The following Lua calls can fail. */ |
| 7013 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7014 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7015 | error = lua_tostring(hlua->T, -1); |
| 7016 | else |
| 7017 | error = "critical error"; |
| 7018 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7019 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7020 | return 0; |
| 7021 | } |
| 7022 | |
| 7023 | /* Check stack available size. */ |
| 7024 | if (!lua_checkstack(hlua->T, 1)) { |
| 7025 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7026 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7027 | RESET_SAFE_LJMP(hlua->T); |
| 7028 | return 0; |
| 7029 | } |
| 7030 | |
| 7031 | /* Restore the function in the stack. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7032 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7033 | |
| 7034 | /* Create and and push object stream in the stack. */ |
| 7035 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7036 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7037 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7038 | RESET_SAFE_LJMP(hlua->T); |
| 7039 | return 0; |
| 7040 | } |
| 7041 | hlua->nargs = 1; |
| 7042 | |
| 7043 | /* push keywords in the stack. */ |
| 7044 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7045 | if (!lua_checkstack(hlua->T, 1)) { |
| 7046 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7047 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7048 | RESET_SAFE_LJMP(hlua->T); |
| 7049 | return 0; |
| 7050 | } |
| 7051 | lua_pushstring(hlua->T, *arg); |
| 7052 | hlua->nargs++; |
| 7053 | } |
| 7054 | |
| 7055 | RESET_SAFE_LJMP(hlua->T); |
| 7056 | |
| 7057 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7058 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7059 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7060 | |
| 7061 | return 1; |
| 7062 | } |
| 7063 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7064 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7065 | { |
| 7066 | struct stream_interface *si = ctx->owner; |
| 7067 | struct stream *strm = si_strm(si); |
| 7068 | struct channel *res = si_ic(si); |
| 7069 | struct act_rule *rule = ctx->rule; |
| 7070 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7071 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7072 | |
| 7073 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7074 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7075 | /* eat the whole request */ |
| 7076 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7077 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7078 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7079 | |
| 7080 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7081 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7082 | return; |
| 7083 | |
| 7084 | /* Execute the function. */ |
| 7085 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7086 | /* finished. */ |
| 7087 | case HLUA_E_OK: |
| 7088 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7089 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7090 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7091 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7092 | res->flags |= CF_READ_NULL; |
| 7093 | si_shutr(si); |
| 7094 | return; |
| 7095 | |
| 7096 | /* yield. */ |
| 7097 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7098 | if (hlua->wake_time != TICK_ETERNITY) |
| 7099 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7100 | return; |
| 7101 | |
| 7102 | /* finished with error. */ |
| 7103 | case HLUA_E_ERRMSG: |
| 7104 | /* Display log. */ |
| 7105 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7106 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7107 | lua_pop(hlua->T, 1); |
| 7108 | goto error; |
| 7109 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7110 | case HLUA_E_ETMOUT: |
| 7111 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7112 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7113 | goto error; |
| 7114 | |
| 7115 | case HLUA_E_NOMEM: |
| 7116 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7117 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7118 | goto error; |
| 7119 | |
| 7120 | case HLUA_E_YIELD: /* unexpected */ |
| 7121 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7122 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7123 | goto error; |
| 7124 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7125 | case HLUA_E_ERR: |
| 7126 | /* Display log. */ |
| 7127 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7128 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7129 | goto error; |
| 7130 | |
| 7131 | default: |
| 7132 | goto error; |
| 7133 | } |
| 7134 | |
| 7135 | error: |
| 7136 | |
| 7137 | /* For all other cases, just close the stream. */ |
| 7138 | si_shutw(si); |
| 7139 | si_shutr(si); |
| 7140 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7141 | } |
| 7142 | |
| 7143 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7144 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7145 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7146 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7147 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7148 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7149 | } |
| 7150 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7151 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7152 | * an errors occurs and -1 if more data are required for initializing |
| 7153 | * the applet. |
| 7154 | */ |
| 7155 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7156 | { |
| 7157 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7158 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7159 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7160 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7161 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7162 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7163 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7164 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7165 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7166 | if (!hlua) { |
| 7167 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7168 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7169 | return 0; |
| 7170 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7171 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7172 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7173 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7174 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7175 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7176 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7177 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7178 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7179 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7180 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7181 | if (!task) { |
| 7182 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7183 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7184 | return 0; |
| 7185 | } |
| 7186 | task->nice = 0; |
| 7187 | task->context = ctx; |
| 7188 | task->process = hlua_applet_wakeup; |
| 7189 | ctx->ctx.hlua_apphttp.task = task; |
| 7190 | |
| 7191 | /* In the execution wrappers linked with a stream, the |
| 7192 | * Lua context can be not initialized. This behavior |
| 7193 | * permits to save performances because a systematic |
| 7194 | * Lua initialization cause 5% performances loss. |
| 7195 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7196 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7197 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7198 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7199 | return 0; |
| 7200 | } |
| 7201 | |
| 7202 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7203 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7204 | |
| 7205 | /* The following Lua calls can fail. */ |
| 7206 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7207 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7208 | error = lua_tostring(hlua->T, -1); |
| 7209 | else |
| 7210 | error = "critical error"; |
| 7211 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7212 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7213 | return 0; |
| 7214 | } |
| 7215 | |
| 7216 | /* Check stack available size. */ |
| 7217 | if (!lua_checkstack(hlua->T, 1)) { |
| 7218 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7219 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7220 | RESET_SAFE_LJMP(hlua->T); |
| 7221 | return 0; |
| 7222 | } |
| 7223 | |
| 7224 | /* Restore the function in the stack. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7225 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7226 | |
| 7227 | /* Create and and push object stream in the stack. */ |
| 7228 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7229 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7230 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7231 | RESET_SAFE_LJMP(hlua->T); |
| 7232 | return 0; |
| 7233 | } |
| 7234 | hlua->nargs = 1; |
| 7235 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7236 | /* push keywords in the stack. */ |
| 7237 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7238 | if (!lua_checkstack(hlua->T, 1)) { |
| 7239 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7240 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7241 | RESET_SAFE_LJMP(hlua->T); |
| 7242 | return 0; |
| 7243 | } |
| 7244 | lua_pushstring(hlua->T, *arg); |
| 7245 | hlua->nargs++; |
| 7246 | } |
| 7247 | |
| 7248 | RESET_SAFE_LJMP(hlua->T); |
| 7249 | |
| 7250 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7251 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7252 | |
| 7253 | return 1; |
| 7254 | } |
| 7255 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7256 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7257 | { |
| 7258 | struct stream_interface *si = ctx->owner; |
| 7259 | struct stream *strm = si_strm(si); |
| 7260 | struct channel *req = si_oc(si); |
| 7261 | struct channel *res = si_ic(si); |
| 7262 | struct act_rule *rule = ctx->rule; |
| 7263 | struct proxy *px = strm->be; |
| 7264 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7265 | struct htx *req_htx, *res_htx; |
| 7266 | |
| 7267 | res_htx = htx_from_buf(&res->buf); |
| 7268 | |
| 7269 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7270 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7271 | goto out; |
| 7272 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7273 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7274 | if (!b_size(&res->buf)) { |
| 7275 | si_rx_room_blk(si); |
| 7276 | goto out; |
| 7277 | } |
| 7278 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7279 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7280 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7281 | |
| 7282 | /* Set the currently running flag. */ |
| 7283 | if (!HLUA_IS_RUNNING(hlua) && |
| 7284 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7285 | struct htx_blk *blk; |
| 7286 | size_t count = co_data(req); |
| 7287 | |
| 7288 | if (!count) { |
| 7289 | si_cant_get(si); |
| 7290 | goto out; |
| 7291 | } |
| 7292 | |
| 7293 | /* We need to flush the request header. This left the body for |
| 7294 | * the Lua. |
| 7295 | */ |
| 7296 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7297 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7298 | while (count && blk) { |
| 7299 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7300 | uint32_t sz = htx_get_blksz(blk); |
| 7301 | |
| 7302 | if (sz > count) { |
| 7303 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7304 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7305 | goto out; |
| 7306 | } |
| 7307 | |
| 7308 | count -= sz; |
| 7309 | co_set_data(req, co_data(req) - sz); |
| 7310 | blk = htx_remove_blk(req_htx, blk); |
| 7311 | |
| 7312 | if (type == HTX_BLK_EOH) |
| 7313 | break; |
| 7314 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7315 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7316 | } |
| 7317 | |
| 7318 | /* Executes The applet if it is not done. */ |
| 7319 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7320 | |
| 7321 | /* Execute the function. */ |
| 7322 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7323 | /* finished. */ |
| 7324 | case HLUA_E_OK: |
| 7325 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7326 | break; |
| 7327 | |
| 7328 | /* yield. */ |
| 7329 | case HLUA_E_AGAIN: |
| 7330 | if (hlua->wake_time != TICK_ETERNITY) |
| 7331 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7332 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7333 | |
| 7334 | /* finished with error. */ |
| 7335 | case HLUA_E_ERRMSG: |
| 7336 | /* Display log. */ |
| 7337 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7338 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7339 | lua_pop(hlua->T, 1); |
| 7340 | goto error; |
| 7341 | |
| 7342 | case HLUA_E_ETMOUT: |
| 7343 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7344 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7345 | goto error; |
| 7346 | |
| 7347 | case HLUA_E_NOMEM: |
| 7348 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7349 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7350 | goto error; |
| 7351 | |
| 7352 | case HLUA_E_YIELD: /* unexpected */ |
| 7353 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7354 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7355 | goto error; |
| 7356 | |
| 7357 | case HLUA_E_ERR: |
| 7358 | /* Display log. */ |
| 7359 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7360 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7361 | goto error; |
| 7362 | |
| 7363 | default: |
| 7364 | goto error; |
| 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7369 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7370 | goto done; |
| 7371 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7372 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7373 | goto error; |
| 7374 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7375 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7376 | res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7377 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7378 | si_rx_room_blk(si); |
| 7379 | goto out; |
| 7380 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7381 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7382 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7383 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7384 | } |
| 7385 | |
| 7386 | done: |
| 7387 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7388 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7389 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7390 | si_shutr(si); |
| 7391 | } |
| 7392 | |
| 7393 | /* eat the whole request */ |
| 7394 | if (co_data(req)) { |
| 7395 | req_htx = htx_from_buf(&req->buf); |
| 7396 | co_htx_skip(req, req_htx, co_data(req)); |
| 7397 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7398 | } |
| 7399 | } |
| 7400 | |
| 7401 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7402 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7403 | return; |
| 7404 | |
| 7405 | error: |
| 7406 | |
| 7407 | /* If we are in HTTP mode, and we are not send any |
| 7408 | * data, return a 500 server error in best effort: |
| 7409 | * if there is no room available in the buffer, |
| 7410 | * just close the connection. |
| 7411 | */ |
| 7412 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7413 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7414 | |
| 7415 | channel_erase(res); |
| 7416 | res->buf.data = b_data(err); |
| 7417 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7418 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7419 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7420 | } |
| 7421 | if (!(strm->flags & SF_ERR_MASK)) |
| 7422 | strm->flags |= SF_ERR_RESOURCE; |
| 7423 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7424 | goto done; |
| 7425 | } |
| 7426 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7427 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7428 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7429 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7430 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7431 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7432 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7433 | } |
| 7434 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7435 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7436 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7437 | * |
| 7438 | * This function can fail with an abort() due to an Lua critical error. |
| 7439 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7440 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7441 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7442 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7443 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7444 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7445 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7446 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7447 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7448 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7449 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7450 | if (!rule->arg.hlua_rule) { |
| 7451 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7452 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7453 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7454 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7455 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7456 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7457 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7458 | if (!rule->arg.hlua_rule->args) { |
| 7459 | memprintf(err, "out of memory error"); |
| 7460 | return ACT_RET_PRS_ERR; |
| 7461 | } |
| 7462 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7463 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7464 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7465 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7466 | /* Expect some arguments */ |
| 7467 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7468 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7469 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7470 | return ACT_RET_PRS_ERR; |
| 7471 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7472 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7473 | if (!rule->arg.hlua_rule->args[i]) { |
| 7474 | memprintf(err, "out of memory error"); |
| 7475 | return ACT_RET_PRS_ERR; |
| 7476 | } |
| 7477 | (*cur_arg)++; |
| 7478 | } |
| 7479 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7480 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7481 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7482 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7483 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7484 | } |
| 7485 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7486 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7487 | struct act_rule *rule, char **err) |
| 7488 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7489 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7490 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7491 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7492 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7493 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7494 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7495 | * the call of this analyzer. |
| 7496 | */ |
| 7497 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7498 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7499 | return ACT_RET_PRS_ERR; |
| 7500 | } |
| 7501 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7502 | /* Memory for the rule. */ |
| 7503 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7504 | if (!rule->arg.hlua_rule) { |
| 7505 | memprintf(err, "out of memory error"); |
| 7506 | return ACT_RET_PRS_ERR; |
| 7507 | } |
| 7508 | |
| 7509 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7510 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7511 | |
| 7512 | /* TODO: later accept arguments. */ |
| 7513 | rule->arg.hlua_rule->args = NULL; |
| 7514 | |
| 7515 | /* Add applet pointer in the rule. */ |
| 7516 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7517 | rule->applet.name = fcn->name; |
| 7518 | rule->applet.init = hlua_applet_http_init; |
| 7519 | rule->applet.fct = hlua_applet_http_fct; |
| 7520 | rule->applet.release = hlua_applet_http_release; |
| 7521 | rule->applet.timeout = hlua_timeout_applet; |
| 7522 | |
| 7523 | return ACT_RET_PRS_OK; |
| 7524 | } |
| 7525 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7526 | /* This function is an LUA binding used for registering |
| 7527 | * "sample-conv" functions. It expects a converter name used |
| 7528 | * in the haproxy configuration file, and an LUA function. |
| 7529 | */ |
| 7530 | __LJMP static int hlua_register_action(lua_State *L) |
| 7531 | { |
| 7532 | struct action_kw_list *akl; |
| 7533 | const char *name; |
| 7534 | int ref; |
| 7535 | int len; |
| 7536 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7537 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7538 | struct buffer *trash; |
| 7539 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7540 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7541 | /* Initialise the number of expected arguments at 0. */ |
| 7542 | nargs = 0; |
| 7543 | |
| 7544 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7545 | 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] | 7546 | |
| 7547 | /* First argument : converter name. */ |
| 7548 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7549 | |
| 7550 | /* Second argument : environment. */ |
| 7551 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7552 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7553 | |
| 7554 | /* Third argument : lua function. */ |
| 7555 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7556 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7557 | /* 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] | 7558 | if (lua_gettop(L) >= 4) |
| 7559 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7560 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7561 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7562 | lua_pushnil(L); |
| 7563 | while (lua_next(L, 2) != 0) { |
| 7564 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7565 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7566 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7567 | /* Check if action exists */ |
| 7568 | trash = get_trash_chunk(); |
| 7569 | chunk_printf(trash, "lua.%s", name); |
| 7570 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7571 | akw = tcp_req_cont_action(trash->area); |
| 7572 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7573 | akw = tcp_res_cont_action(trash->area); |
| 7574 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7575 | akw = action_http_req_custom(trash->area); |
| 7576 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7577 | akw = action_http_res_custom(trash->area); |
| 7578 | } else { |
| 7579 | akw = NULL; |
| 7580 | } |
| 7581 | if (akw != NULL) { |
| 7582 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7583 | "This will become a hard error in version 2.5.\n", name); |
| 7584 | } |
| 7585 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7586 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7587 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7588 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7589 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7590 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7591 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7592 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7593 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7594 | |
| 7595 | /* Fill fcn. */ |
| 7596 | fcn->name = strdup(name); |
| 7597 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7598 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7599 | fcn->function_ref = ref; |
| 7600 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7601 | /* Set the expected number od arguments. */ |
| 7602 | fcn->nargs = nargs; |
| 7603 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7604 | /* List head */ |
| 7605 | akl->list.n = akl->list.p = NULL; |
| 7606 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7607 | /* action keyword. */ |
| 7608 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7609 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7610 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7611 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7612 | |
| 7613 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7614 | |
| 7615 | akl->kw[0].match_pfx = 0; |
| 7616 | akl->kw[0].private = fcn; |
| 7617 | akl->kw[0].parse = action_register_lua; |
| 7618 | |
| 7619 | /* select the action registering point. */ |
| 7620 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7621 | tcp_req_cont_keywords_register(akl); |
| 7622 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7623 | tcp_res_cont_keywords_register(akl); |
| 7624 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7625 | http_req_keywords_register(akl); |
| 7626 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7627 | http_res_keywords_register(akl); |
| 7628 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7629 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7630 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7631 | "are expected.", lua_tostring(L, -1))); |
| 7632 | |
| 7633 | /* pop the environment string. */ |
| 7634 | lua_pop(L, 1); |
| 7635 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7636 | return ACT_RET_PRS_OK; |
| 7637 | } |
| 7638 | |
| 7639 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7640 | struct act_rule *rule, char **err) |
| 7641 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7642 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7643 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7644 | if (px->mode == PR_MODE_HTTP) { |
| 7645 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7646 | return ACT_RET_PRS_ERR; |
| 7647 | } |
| 7648 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7649 | /* Memory for the rule. */ |
| 7650 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7651 | if (!rule->arg.hlua_rule) { |
| 7652 | memprintf(err, "out of memory error"); |
| 7653 | return ACT_RET_PRS_ERR; |
| 7654 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7655 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7656 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7657 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7658 | |
| 7659 | /* TODO: later accept arguments. */ |
| 7660 | rule->arg.hlua_rule->args = NULL; |
| 7661 | |
| 7662 | /* Add applet pointer in the rule. */ |
| 7663 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7664 | rule->applet.name = fcn->name; |
| 7665 | rule->applet.init = hlua_applet_tcp_init; |
| 7666 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7667 | rule->applet.release = hlua_applet_tcp_release; |
| 7668 | rule->applet.timeout = hlua_timeout_applet; |
| 7669 | |
| 7670 | return 0; |
| 7671 | } |
| 7672 | |
| 7673 | /* This function is an LUA binding used for registering |
| 7674 | * "sample-conv" functions. It expects a converter name used |
| 7675 | * in the haproxy configuration file, and an LUA function. |
| 7676 | */ |
| 7677 | __LJMP static int hlua_register_service(lua_State *L) |
| 7678 | { |
| 7679 | struct action_kw_list *akl; |
| 7680 | const char *name; |
| 7681 | const char *env; |
| 7682 | int ref; |
| 7683 | int len; |
| 7684 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7685 | struct buffer *trash; |
| 7686 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7687 | |
| 7688 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7689 | |
| 7690 | /* First argument : converter name. */ |
| 7691 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7692 | |
| 7693 | /* Second argument : environment. */ |
| 7694 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7695 | |
| 7696 | /* Third argument : lua function. */ |
| 7697 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7698 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7699 | /* Check for service already registered */ |
| 7700 | trash = get_trash_chunk(); |
| 7701 | chunk_printf(trash, "lua.%s", name); |
| 7702 | akw = service_find(trash->area); |
| 7703 | if (akw != NULL) { |
| 7704 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7705 | "This will become a hard error in version 2.5.\n", name); |
| 7706 | } |
| 7707 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7708 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7709 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7710 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7711 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7712 | fcn = calloc(1, sizeof(*fcn)); |
| 7713 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7714 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7715 | |
| 7716 | /* Fill fcn. */ |
| 7717 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7718 | fcn->name = calloc(1, len); |
| 7719 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7720 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7721 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7722 | fcn->function_ref = ref; |
| 7723 | |
| 7724 | /* List head */ |
| 7725 | akl->list.n = akl->list.p = NULL; |
| 7726 | |
| 7727 | /* converter keyword. */ |
| 7728 | len = strlen("lua.") + strlen(name) + 1; |
| 7729 | akl->kw[0].kw = calloc(1, len); |
| 7730 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7731 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7732 | |
| 7733 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7734 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7735 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7736 | if (strcmp(env, "tcp") == 0) |
| 7737 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7738 | else if (strcmp(env, "http") == 0) |
| 7739 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7740 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7741 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7742 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7743 | |
| 7744 | akl->kw[0].match_pfx = 0; |
| 7745 | akl->kw[0].private = fcn; |
| 7746 | |
| 7747 | /* End of array. */ |
| 7748 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7749 | |
| 7750 | /* Register this new converter */ |
| 7751 | service_keywords_register(akl); |
| 7752 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7753 | return 0; |
| 7754 | } |
| 7755 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7756 | /* This function initialises Lua cli handler. It copies the |
| 7757 | * arguments in the Lua stack and create channel IO objects. |
| 7758 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7759 | 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] | 7760 | { |
| 7761 | struct hlua *hlua; |
| 7762 | struct hlua_function *fcn; |
| 7763 | int i; |
| 7764 | const char *error; |
| 7765 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7766 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7767 | appctx->ctx.hlua_cli.fcn = private; |
| 7768 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7769 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7770 | if (!hlua) { |
| 7771 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7772 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7773 | } |
| 7774 | HLUA_INIT(hlua); |
| 7775 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7776 | |
| 7777 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7778 | * We use the same wakeup function than the Lua applet_tcp and |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7779 | * applet_http. It is absolutely compatible. |
| 7780 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7781 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7782 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7783 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7784 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7785 | } |
| 7786 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7787 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7788 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7789 | |
| 7790 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7791 | 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] | 7792 | 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] | 7793 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7794 | } |
| 7795 | |
| 7796 | /* The following Lua calls can fail. */ |
| 7797 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7798 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7799 | error = lua_tostring(hlua->T, -1); |
| 7800 | else |
| 7801 | error = "critical error"; |
| 7802 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7803 | goto error; |
| 7804 | } |
| 7805 | |
| 7806 | /* Check stack available size. */ |
| 7807 | if (!lua_checkstack(hlua->T, 2)) { |
| 7808 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7809 | goto error; |
| 7810 | } |
| 7811 | |
| 7812 | /* Restore the function in the stack. */ |
| 7813 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7814 | |
| 7815 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7816 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7817 | */ |
| 7818 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7819 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7820 | goto error; |
| 7821 | } |
| 7822 | hlua->nargs = 1; |
| 7823 | |
| 7824 | /* push keywords in the stack. */ |
| 7825 | for (i = 0; *args[i]; i++) { |
| 7826 | /* Check stack available size. */ |
| 7827 | if (!lua_checkstack(hlua->T, 1)) { |
| 7828 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7829 | goto error; |
| 7830 | } |
| 7831 | lua_pushstring(hlua->T, args[i]); |
| 7832 | hlua->nargs++; |
| 7833 | } |
| 7834 | |
| 7835 | /* We must initialize the execution timeouts. */ |
| 7836 | hlua->max_time = hlua_timeout_session; |
| 7837 | |
| 7838 | /* At this point the execution is safe. */ |
| 7839 | RESET_SAFE_LJMP(hlua->T); |
| 7840 | |
| 7841 | /* It's ok */ |
| 7842 | return 0; |
| 7843 | |
| 7844 | /* It's not ok. */ |
| 7845 | error: |
| 7846 | RESET_SAFE_LJMP(hlua->T); |
| 7847 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7848 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7849 | return 1; |
| 7850 | } |
| 7851 | |
| 7852 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7853 | { |
| 7854 | struct hlua *hlua; |
| 7855 | struct stream_interface *si; |
| 7856 | struct hlua_function *fcn; |
| 7857 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7858 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7859 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7860 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7861 | |
| 7862 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7863 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7864 | return 1; |
| 7865 | |
| 7866 | /* Execute the function. */ |
| 7867 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7868 | |
| 7869 | /* finished. */ |
| 7870 | case HLUA_E_OK: |
| 7871 | return 1; |
| 7872 | |
| 7873 | /* yield. */ |
| 7874 | case HLUA_E_AGAIN: |
| 7875 | /* We want write. */ |
| 7876 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7877 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7878 | /* Set the timeout. */ |
| 7879 | if (hlua->wake_time != TICK_ETERNITY) |
| 7880 | task_schedule(hlua->task, hlua->wake_time); |
| 7881 | return 0; |
| 7882 | |
| 7883 | /* finished with error. */ |
| 7884 | case HLUA_E_ERRMSG: |
| 7885 | /* Display log. */ |
| 7886 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7887 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7888 | lua_pop(hlua->T, 1); |
| 7889 | return 1; |
| 7890 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7891 | case HLUA_E_ETMOUT: |
| 7892 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7893 | fcn->name); |
| 7894 | return 1; |
| 7895 | |
| 7896 | case HLUA_E_NOMEM: |
| 7897 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7898 | fcn->name); |
| 7899 | return 1; |
| 7900 | |
| 7901 | case HLUA_E_YIELD: /* unexpected */ |
| 7902 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7903 | fcn->name); |
| 7904 | return 1; |
| 7905 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7906 | case HLUA_E_ERR: |
| 7907 | /* Display log. */ |
| 7908 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7909 | fcn->name); |
| 7910 | return 1; |
| 7911 | |
| 7912 | default: |
| 7913 | return 1; |
| 7914 | } |
| 7915 | |
| 7916 | return 1; |
| 7917 | } |
| 7918 | |
| 7919 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7920 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7921 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7922 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7923 | } |
| 7924 | |
| 7925 | /* This function is an LUA binding used for registering |
| 7926 | * new keywords in the cli. It expects a list of keywords |
| 7927 | * which are the "path". It is limited to 5 keywords. A |
| 7928 | * description of the command, a function to be executed |
| 7929 | * for the parsing and a function for io handlers. |
| 7930 | */ |
| 7931 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7932 | { |
| 7933 | struct cli_kw_list *cli_kws; |
| 7934 | const char *message; |
| 7935 | int ref_io; |
| 7936 | int len; |
| 7937 | struct hlua_function *fcn; |
| 7938 | int index; |
| 7939 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7940 | struct buffer *trash; |
| 7941 | const char *kw[5]; |
| 7942 | struct cli_kw *cli_kw; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7943 | |
| 7944 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7945 | |
| 7946 | /* First argument : an array of maximum 5 keywords. */ |
| 7947 | if (!lua_istable(L, 1)) |
| 7948 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7949 | |
| 7950 | /* Second argument : string with contextual message. */ |
| 7951 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7952 | |
| 7953 | /* Third and fourth argument : lua function. */ |
| 7954 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7955 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7956 | /* Check for CLI service already registered */ |
| 7957 | trash = get_trash_chunk(); |
| 7958 | index = 0; |
| 7959 | lua_pushnil(L); |
| 7960 | memset(kw, 0, sizeof(kw)); |
| 7961 | while (lua_next(L, 1) != 0) { |
| 7962 | if (index >= CLI_PREFIX_KW_NB) |
| 7963 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7964 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7965 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7966 | kw[index] = lua_tostring(L, -1); |
| 7967 | if (index == 0) |
| 7968 | chunk_printf(trash, "%s", kw[index]); |
| 7969 | else |
| 7970 | chunk_appendf(trash, " %s", kw[index]); |
| 7971 | index++; |
| 7972 | lua_pop(L, 1); |
| 7973 | } |
| 7974 | cli_kw = cli_find_kw_exact((char **)kw); |
| 7975 | if (cli_kw != NULL) { |
| 7976 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 7977 | "This will become a hard error in version 2.5.\n", trash->area); |
| 7978 | } |
| 7979 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7980 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7981 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7982 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7983 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7984 | fcn = calloc(1, sizeof(*fcn)); |
| 7985 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7986 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7987 | |
| 7988 | /* Fill path. */ |
| 7989 | index = 0; |
| 7990 | lua_pushnil(L); |
| 7991 | while(lua_next(L, 1) != 0) { |
| 7992 | if (index >= 5) |
| 7993 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7994 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7995 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7996 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7997 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7998 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7999 | index++; |
| 8000 | lua_pop(L, 1); |
| 8001 | } |
| 8002 | |
| 8003 | /* Copy help message. */ |
| 8004 | cli_kws->kw[0].usage = strdup(message); |
| 8005 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8006 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8007 | |
| 8008 | /* Fill fcn io handler. */ |
| 8009 | len = strlen("<lua.cli>") + 1; |
| 8010 | for (i = 0; i < index; i++) |
| 8011 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8012 | fcn->name = calloc(1, len); |
| 8013 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8014 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8015 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8016 | for (i = 0; i < index; i++) { |
| 8017 | strncat((char *)fcn->name, ".", len); |
| 8018 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8019 | } |
| 8020 | strncat((char *)fcn->name, ">", len); |
| 8021 | fcn->function_ref = ref_io; |
| 8022 | |
| 8023 | /* Fill last entries. */ |
| 8024 | cli_kws->kw[0].private = fcn; |
| 8025 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8026 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8027 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8028 | |
| 8029 | /* Register this new converter */ |
| 8030 | cli_register_kw(cli_kws); |
| 8031 | |
| 8032 | return 0; |
| 8033 | } |
| 8034 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8035 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8036 | struct proxy *defpx, const char *file, int line, |
| 8037 | char **err, unsigned int *timeout) |
| 8038 | { |
| 8039 | const char *error; |
| 8040 | |
| 8041 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8042 | if (error == PARSE_TIME_OVER) { |
| 8043 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8044 | args[1], args[0]); |
| 8045 | return -1; |
| 8046 | } |
| 8047 | else if (error == PARSE_TIME_UNDER) { |
| 8048 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8049 | args[1], args[0]); |
| 8050 | return -1; |
| 8051 | } |
| 8052 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8053 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8054 | return -1; |
| 8055 | } |
| 8056 | return 0; |
| 8057 | } |
| 8058 | |
| 8059 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8060 | struct proxy *defpx, const char *file, int line, |
| 8061 | char **err) |
| 8062 | { |
| 8063 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8064 | file, line, err, &hlua_timeout_session); |
| 8065 | } |
| 8066 | |
| 8067 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8068 | struct proxy *defpx, const char *file, int line, |
| 8069 | char **err) |
| 8070 | { |
| 8071 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8072 | file, line, err, &hlua_timeout_task); |
| 8073 | } |
| 8074 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8075 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8076 | struct proxy *defpx, const char *file, int line, |
| 8077 | char **err) |
| 8078 | { |
| 8079 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8080 | file, line, err, &hlua_timeout_applet); |
| 8081 | } |
| 8082 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8083 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8084 | struct proxy *defpx, const char *file, int line, |
| 8085 | char **err) |
| 8086 | { |
| 8087 | char *error; |
| 8088 | |
| 8089 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8090 | if (*error != '\0') { |
| 8091 | memprintf(err, "%s: invalid number", args[0]); |
| 8092 | return -1; |
| 8093 | } |
| 8094 | return 0; |
| 8095 | } |
| 8096 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8097 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8098 | struct proxy *defpx, const char *file, int line, |
| 8099 | char **err) |
| 8100 | { |
| 8101 | char *error; |
| 8102 | |
| 8103 | if (*(args[1]) == 0) { |
| 8104 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8105 | return -1; |
| 8106 | } |
| 8107 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8108 | if (*error != '\0') { |
| 8109 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8110 | return -1; |
| 8111 | } |
| 8112 | return 0; |
| 8113 | } |
| 8114 | |
| 8115 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8116 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8117 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8118 | * the main lua entry point. |
| 8119 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8120 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8121 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8122 | * |
| 8123 | * In some error case, LUA set an error message in top of the stack. This function |
| 8124 | * 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] | 8125 | * |
| 8126 | * This function can fail with an abort() due to an Lua critical error. |
| 8127 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8128 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8129 | */ |
| 8130 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8131 | struct proxy *defpx, const char *file, int line, |
| 8132 | char **err) |
| 8133 | { |
| 8134 | int error; |
| 8135 | |
Thierry Fournier | 77a8894 | 2020-11-29 01:06:24 +0100 | [diff] [blame] | 8136 | if (*(args[1]) == 0) { |
| 8137 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8138 | return -1; |
| 8139 | } |
| 8140 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8141 | /* Just load and compile the file. */ |
| 8142 | error = luaL_loadfile(gL.T, args[1]); |
| 8143 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8144 | 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] | 8145 | lua_pop(gL.T, 1); |
| 8146 | return -1; |
| 8147 | } |
| 8148 | |
| 8149 | /* If no syntax error where detected, execute the code. */ |
| 8150 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8151 | switch (error) { |
| 8152 | case LUA_OK: |
| 8153 | break; |
| 8154 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8155 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8156 | lua_pop(gL.T, 1); |
| 8157 | return -1; |
| 8158 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8159 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8160 | return -1; |
| 8161 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8162 | 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] | 8163 | lua_pop(gL.T, 1); |
| 8164 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8165 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8166 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8167 | 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] | 8168 | lua_pop(gL.T, 1); |
| 8169 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8170 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8171 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8172 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8173 | lua_pop(gL.T, 1); |
| 8174 | return -1; |
| 8175 | } |
| 8176 | |
| 8177 | return 0; |
| 8178 | } |
| 8179 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8180 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8181 | * in the given <ctx>. |
| 8182 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8183 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8184 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8185 | lua_getglobal(L, "package"); /* push package variable */ |
| 8186 | lua_pushstring(L, path); /* push given path */ |
| 8187 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8188 | lua_getfield(L, -3, type); /* push old path */ |
| 8189 | lua_concat(L, 3); /* concatenate to new path */ |
| 8190 | lua_setfield(L, -2, type); /* store new path */ |
| 8191 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8192 | |
| 8193 | return 0; |
| 8194 | } |
| 8195 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8196 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 8197 | struct proxy *defpx, const char *file, int line, |
| 8198 | char **err) |
| 8199 | { |
| 8200 | char *path; |
| 8201 | char *type = "path"; |
| 8202 | if (too_many_args(2, args, err, NULL)) { |
| 8203 | return -1; |
| 8204 | } |
| 8205 | |
| 8206 | if (!(*args[1])) { |
| 8207 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 8208 | return -1; |
| 8209 | } |
| 8210 | path = args[1]; |
| 8211 | |
| 8212 | if (*args[2]) { |
| 8213 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8214 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 8215 | return -1; |
| 8216 | } |
| 8217 | type = args[2]; |
| 8218 | } |
| 8219 | |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8220 | return hlua_prepend_path(gL.T, type, path); |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8221 | } |
| 8222 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8223 | /* configuration keywords declaration */ |
| 8224 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8225 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8226 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8227 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8228 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8229 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8230 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8231 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8232 | { 0, NULL, NULL }, |
| 8233 | }}; |
| 8234 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8235 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8236 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8237 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8238 | /* This function can fail with an abort() due to an Lua critical error. |
| 8239 | * We are in the initialisation process of HAProxy, this abort() is |
| 8240 | * tolerated. |
| 8241 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8242 | int hlua_post_init() |
| 8243 | { |
| 8244 | struct hlua_init_function *init; |
| 8245 | const char *msg; |
| 8246 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8247 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8248 | const char *kind; |
| 8249 | const char *trace; |
| 8250 | int return_status = 1; |
| 8251 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8252 | int nres; |
| 8253 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8254 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8255 | /* disable memory limit checks if limit is not set */ |
| 8256 | if (!hlua_global_allocator.limit) |
| 8257 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8258 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8259 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8260 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8261 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8262 | error = lua_tostring(gL.T, -1); |
| 8263 | else |
| 8264 | error = "critical error"; |
| 8265 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8266 | exit(1); |
| 8267 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8268 | |
| 8269 | #if USE_OPENSSL |
| 8270 | /* Initialize SSL server. */ |
| 8271 | if (socket_ssl.xprt->prepare_srv) { |
| 8272 | int saved_used_backed = global.ssl_used_backend; |
| 8273 | // don't affect maxconn automatic computation |
| 8274 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8275 | global.ssl_used_backend = saved_used_backed; |
| 8276 | } |
| 8277 | #endif |
| 8278 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8279 | hlua_fcn_post_init(gL.T); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8280 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8281 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8282 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8283 | |
| 8284 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8285 | ret = lua_resume(gL.T, gL.T, 0, &nres); |
| 8286 | #else |
| 8287 | ret = lua_resume(gL.T, gL.T, 0); |
| 8288 | #endif |
| 8289 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8290 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8291 | |
| 8292 | case LUA_OK: |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8293 | lua_pop(gL.T, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8294 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8295 | |
| 8296 | case LUA_ERRERR: |
| 8297 | kind = "message handler error"; |
| 8298 | /* Fall thru */ |
| 8299 | case LUA_ERRRUN: |
| 8300 | if (!kind) |
| 8301 | kind = "runtime error"; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8302 | msg = lua_tostring(gL.T, -1); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8303 | lua_settop(gL.T, 0); /* Empty the stack. */ |
| 8304 | lua_pop(gL.T, 1); |
| 8305 | trace = hlua_traceback(gL.T); |
| 8306 | if (msg) |
| 8307 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8308 | else |
| 8309 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8310 | return_status = 0; |
| 8311 | break; |
| 8312 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8313 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8314 | /* Unknown error */ |
| 8315 | kind = "Unknown error"; |
| 8316 | /* Fall thru */ |
| 8317 | case LUA_YIELD: |
| 8318 | /* yield is not configured at this step, this state doesn't happen */ |
| 8319 | if (!kind) |
| 8320 | kind = "yield not allowed"; |
| 8321 | /* Fall thru */ |
| 8322 | case LUA_ERRMEM: |
| 8323 | if (!kind) |
| 8324 | kind = "out of memory error"; |
| 8325 | lua_settop(gL.T, 0); |
| 8326 | lua_pop(gL.T, 1); |
| 8327 | trace = hlua_traceback(gL.T); |
| 8328 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8329 | return_status = 0; |
| 8330 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8331 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8332 | if (!return_status) |
| 8333 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8334 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8335 | RESET_SAFE_LJMP(gL.T); |
| 8336 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8337 | } |
| 8338 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8339 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8340 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8341 | * is the previously allocated size or the kind of object in case of a new |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8342 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8343 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8344 | * zero. This one verifies that the limits are respected but is optimized |
| 8345 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8346 | */ |
| 8347 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8348 | { |
| 8349 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8350 | size_t limit, old, new; |
| 8351 | |
| 8352 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8353 | * for accounting anymore. |
| 8354 | */ |
| 8355 | if (likely(~zone->limit == 0)) |
| 8356 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8357 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8358 | if (!ptr) |
| 8359 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8360 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8361 | /* enforce strict limits across all threads */ |
| 8362 | limit = zone->limit; |
| 8363 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8364 | do { |
| 8365 | new = old + nsize - osize; |
| 8366 | if (unlikely(nsize && limit && new > limit)) |
| 8367 | return NULL; |
| 8368 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8369 | |
| 8370 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8371 | |
| 8372 | if (unlikely(!ptr && nsize)) // failed |
| 8373 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8374 | |
| 8375 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8376 | return ptr; |
| 8377 | } |
| 8378 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8379 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8380 | * We are in the initialisation process of HAProxy, this abort() is |
| 8381 | * tolerated. |
| 8382 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8383 | lua_State *hlua_init_state(void) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8384 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8385 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8386 | int idx; |
| 8387 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8388 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8389 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8390 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8391 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8392 | lua_State *L; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8393 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8394 | struct srv_kw *kw; |
| 8395 | int tmp_error; |
| 8396 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8397 | char *args[] = { /* SSL client configuration. */ |
| 8398 | "ssl", |
| 8399 | "verify", |
| 8400 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8401 | NULL |
| 8402 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8403 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8404 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8405 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8406 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8407 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8408 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8409 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8410 | *context = NULL; |
| 8411 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8412 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8413 | * the Lua function can fail with an abort. We are in the initialisation |
| 8414 | * process of HAProxy, this abort() is tolerated. |
| 8415 | */ |
| 8416 | |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8417 | /* Set safe environment for the initialisation. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8418 | if (!SET_SAFE_LJMP(L)) { |
| 8419 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8420 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8421 | else |
| 8422 | error_msg = "critical error"; |
| 8423 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8424 | exit(1); |
| 8425 | } |
| 8426 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8427 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8428 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8429 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8430 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8431 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8432 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8433 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8434 | #endif |
| 8435 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8436 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8437 | #endif |
| 8438 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8439 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8440 | |
| 8441 | /* |
| 8442 | * |
| 8443 | * Create "core" object. |
| 8444 | * |
| 8445 | */ |
| 8446 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8447 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8448 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8449 | |
| 8450 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8451 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8452 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8453 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8454 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8455 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8456 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8457 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8458 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8459 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8460 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8461 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8462 | hlua_class_function(L, "yield", hlua_yield); |
| 8463 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8464 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8465 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8466 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8467 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8468 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8469 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8470 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8471 | hlua_class_function(L, "log", hlua_log); |
| 8472 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8473 | hlua_class_function(L, "Info", hlua_log_info); |
| 8474 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8475 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8476 | hlua_class_function(L, "done", hlua_done); |
| 8477 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8478 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8479 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8480 | |
| 8481 | /* |
| 8482 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8483 | * Create "act" object. |
| 8484 | * |
| 8485 | */ |
| 8486 | |
| 8487 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8488 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8489 | |
| 8490 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8491 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8492 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8493 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8494 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8495 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8496 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8497 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8498 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8499 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8500 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8501 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8502 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8503 | |
| 8504 | /* |
| 8505 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8506 | * Register class Map |
| 8507 | * |
| 8508 | */ |
| 8509 | |
| 8510 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8511 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8512 | |
| 8513 | /* register pattern types. */ |
| 8514 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8515 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8516 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8517 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8518 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8519 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8520 | |
| 8521 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8522 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8523 | |
| 8524 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8525 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8526 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8527 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8528 | lua_pushstring(L, "__index"); |
| 8529 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8530 | |
| 8531 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8532 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8533 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8534 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8535 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8536 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8537 | /* Register previous table in the registry with reference and named entry. |
| 8538 | * The function hlua_register_metatable() pops the stack, so we |
| 8539 | * previously create a copy of the table. |
| 8540 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8541 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8542 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8543 | |
| 8544 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8545 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8546 | |
| 8547 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8548 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8549 | |
| 8550 | /* |
| 8551 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8552 | * Register class Channel |
| 8553 | * |
| 8554 | */ |
| 8555 | |
| 8556 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8557 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8558 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8559 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8560 | lua_pushstring(L, "__index"); |
| 8561 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8562 | |
| 8563 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8564 | hlua_class_function(L, "get", hlua_channel_get); |
| 8565 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8566 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8567 | hlua_class_function(L, "set", hlua_channel_set); |
| 8568 | hlua_class_function(L, "append", hlua_channel_append); |
| 8569 | hlua_class_function(L, "send", hlua_channel_send); |
| 8570 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8571 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8572 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8573 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8574 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8575 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8576 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8577 | |
| 8578 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8579 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8580 | |
| 8581 | /* |
| 8582 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8583 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8584 | * |
| 8585 | */ |
| 8586 | |
| 8587 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8588 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8589 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8590 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8591 | lua_pushstring(L, "__index"); |
| 8592 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8593 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8594 | /* Browse existing fetches and create the associated |
| 8595 | * object method. |
| 8596 | */ |
| 8597 | sf = NULL; |
| 8598 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8599 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8600 | * by an underscore. |
| 8601 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8602 | strncpy(trash.area, sf->kw, trash.size); |
| 8603 | trash.area[trash.size - 1] = '\0'; |
| 8604 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8605 | if (*p == '.' || *p == '-' || *p == '+') |
| 8606 | *p = '_'; |
| 8607 | |
| 8608 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8609 | lua_pushstring(L, trash.area); |
| 8610 | lua_pushlightuserdata(L, sf); |
| 8611 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8612 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8613 | } |
| 8614 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8615 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8616 | |
| 8617 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8618 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8619 | |
| 8620 | /* |
| 8621 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8622 | * Register class Converters |
| 8623 | * |
| 8624 | */ |
| 8625 | |
| 8626 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8627 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8628 | |
| 8629 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8630 | lua_pushstring(L, "__index"); |
| 8631 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8632 | |
| 8633 | /* Browse existing converters and create the associated |
| 8634 | * object method. |
| 8635 | */ |
| 8636 | sc = NULL; |
| 8637 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8638 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8639 | * by an underscore. |
| 8640 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8641 | strncpy(trash.area, sc->kw, trash.size); |
| 8642 | trash.area[trash.size - 1] = '\0'; |
| 8643 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8644 | if (*p == '.' || *p == '-' || *p == '+') |
| 8645 | *p = '_'; |
| 8646 | |
| 8647 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8648 | lua_pushstring(L, trash.area); |
| 8649 | lua_pushlightuserdata(L, sc); |
| 8650 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 8651 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8652 | } |
| 8653 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8654 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8655 | |
| 8656 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8657 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8658 | |
| 8659 | /* |
| 8660 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8661 | * Register class HTTP |
| 8662 | * |
| 8663 | */ |
| 8664 | |
| 8665 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8666 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8667 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8668 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8669 | lua_pushstring(L, "__index"); |
| 8670 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8671 | |
| 8672 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8673 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 8674 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 8675 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 8676 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 8677 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 8678 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 8679 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 8680 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 8681 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 8682 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8683 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8684 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 8685 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 8686 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 8687 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 8688 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 8689 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 8690 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8691 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8692 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8693 | |
| 8694 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8695 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8696 | |
| 8697 | /* |
| 8698 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8699 | * Register class AppletTCP |
| 8700 | * |
| 8701 | */ |
| 8702 | |
| 8703 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8704 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8705 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8706 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8707 | lua_pushstring(L, "__index"); |
| 8708 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8709 | |
| 8710 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8711 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 8712 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 8713 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 8714 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 8715 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 8716 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 8717 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 8718 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8719 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8720 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8721 | |
| 8722 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8723 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8724 | |
| 8725 | /* |
| 8726 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8727 | * Register class AppletHTTP |
| 8728 | * |
| 8729 | */ |
| 8730 | |
| 8731 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8732 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8733 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8734 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8735 | lua_pushstring(L, "__index"); |
| 8736 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8737 | |
| 8738 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8739 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 8740 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 8741 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 8742 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 8743 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 8744 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 8745 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 8746 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 8747 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 8748 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 8749 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8750 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8751 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8752 | |
| 8753 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8754 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8755 | |
| 8756 | /* |
| 8757 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8758 | * Register class TXN |
| 8759 | * |
| 8760 | */ |
| 8761 | |
| 8762 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8763 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8764 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8765 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8766 | lua_pushstring(L, "__index"); |
| 8767 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8768 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8769 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8770 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 8771 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 8772 | hlua_class_function(L, "set_var", hlua_set_var); |
| 8773 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 8774 | hlua_class_function(L, "get_var", hlua_get_var); |
| 8775 | hlua_class_function(L, "done", hlua_txn_done); |
| 8776 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 8777 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 8778 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 8779 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 8780 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 8781 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8782 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 8783 | hlua_class_function(L, "log", hlua_txn_log); |
| 8784 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 8785 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 8786 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 8787 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8788 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8789 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8790 | |
| 8791 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8792 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8793 | |
| 8794 | /* |
| 8795 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8796 | * Register class reply |
| 8797 | * |
| 8798 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8799 | lua_newtable(L); |
| 8800 | lua_pushstring(L, "__index"); |
| 8801 | lua_newtable(L); |
| 8802 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 8803 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 8804 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 8805 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 8806 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 8807 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8808 | |
| 8809 | |
| 8810 | /* |
| 8811 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8812 | * Register class Socket |
| 8813 | * |
| 8814 | */ |
| 8815 | |
| 8816 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8817 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8818 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8819 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8820 | lua_pushstring(L, "__index"); |
| 8821 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8822 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8823 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8824 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8825 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8826 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 8827 | hlua_class_function(L, "send", hlua_socket_send); |
| 8828 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 8829 | hlua_class_function(L, "close", hlua_socket_close); |
| 8830 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 8831 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 8832 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 8833 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8834 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8835 | lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8836 | |
| 8837 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8838 | lua_pushstring(L, "__gc"); |
| 8839 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 8840 | lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8841 | |
| 8842 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8843 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8844 | |
| 8845 | /* Proxy and server configuration initialisation. */ |
| 8846 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8847 | init_new_proxy(&socket_proxy); |
| 8848 | socket_proxy.parent = NULL; |
| 8849 | socket_proxy.last_change = now.tv_sec; |
| 8850 | socket_proxy.id = "LUA-SOCKET"; |
| 8851 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8852 | socket_proxy.maxconn = 0; |
| 8853 | socket_proxy.accept = NULL; |
| 8854 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8855 | socket_proxy.srv = NULL; |
| 8856 | socket_proxy.conn_retries = 0; |
| 8857 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8858 | |
| 8859 | /* Init TCP server: unchanged parameters */ |
| 8860 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8861 | socket_tcp.next = NULL; |
| 8862 | socket_tcp.proxy = &socket_proxy; |
| 8863 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8864 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8865 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8866 | socket_tcp.idle_conns = NULL; |
| 8867 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8868 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8869 | socket_tcp.last_change = 0; |
| 8870 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8871 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8872 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8873 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8874 | |
| 8875 | /* XXX: Copy default parameter from default server, |
| 8876 | * but the default server is not initialized. |
| 8877 | */ |
| 8878 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8879 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8880 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8881 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8882 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8883 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8884 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8885 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8886 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8887 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8888 | |
| 8889 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8890 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8891 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8892 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8893 | socket_tcp.check.server = &socket_tcp; |
| 8894 | |
| 8895 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8896 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8897 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8898 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8899 | socket_tcp.agent.server = &socket_tcp; |
| 8900 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8901 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8902 | |
| 8903 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8904 | /* Init TCP server: unchanged parameters */ |
| 8905 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8906 | socket_ssl.next = NULL; |
| 8907 | socket_ssl.proxy = &socket_proxy; |
| 8908 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8909 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8910 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8911 | socket_ssl.idle_conns = NULL; |
| 8912 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8913 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8914 | socket_ssl.last_change = 0; |
| 8915 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8916 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8917 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8918 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8919 | |
| 8920 | /* XXX: Copy default parameter from default server, |
| 8921 | * but the default server is not initialized. |
| 8922 | */ |
| 8923 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8924 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8925 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8926 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8927 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8928 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8929 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8930 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8931 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8932 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8933 | |
| 8934 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8935 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8936 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8937 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8938 | socket_ssl.check.server = &socket_ssl; |
| 8939 | |
| 8940 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8941 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8942 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8943 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8944 | socket_ssl.agent.server = &socket_ssl; |
| 8945 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8946 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8947 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8948 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8949 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8950 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8951 | /* |
| 8952 | * |
| 8953 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8954 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8955 | * features like client certificates and ssl_verify. |
| 8956 | * |
| 8957 | */ |
| 8958 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8959 | if (tmp_error != 0) { |
| 8960 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8961 | abort(); /* This must be never arrives because the command line |
| 8962 | not editable by the user. */ |
| 8963 | } |
| 8964 | idx += kw->skip; |
| 8965 | } |
| 8966 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8967 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8968 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame^] | 8969 | RESET_SAFE_LJMP(L); |
| 8970 | |
| 8971 | return L; |
| 8972 | } |
| 8973 | |
| 8974 | void hlua_init(void) { |
| 8975 | gL.T = hlua_init_state(); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8976 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8977 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8978 | static void hlua_deinit() |
| 8979 | { |
| 8980 | lua_close(gL.T); |
| 8981 | } |
| 8982 | |
| 8983 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8984 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8985 | static void hlua_register_build_options(void) |
| 8986 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8987 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8988 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8989 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8990 | hap_register_build_opts(ptr, 1); |
| 8991 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8992 | |
| 8993 | INITCALL0(STG_REGISTER, hlua_register_build_options); |