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. */ |
| 160 | struct hlua gL; |
| 161 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 162 | /* This is the memory pool containing struct lua for applets |
| 163 | * (including cli). |
| 164 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 165 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 166 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 167 | /* Used for Socket connection. */ |
| 168 | static struct proxy socket_proxy; |
| 169 | static struct server socket_tcp; |
| 170 | #ifdef USE_OPENSSL |
| 171 | static struct server socket_ssl; |
| 172 | #endif |
| 173 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 174 | /* List head of the function called at the initialisation time. */ |
| 175 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 176 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 177 | /* The following variables contains the reference of the different |
| 178 | * Lua classes. These references are useful for identify metadata |
| 179 | * associated with an object. |
| 180 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 181 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 182 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 183 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 184 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 185 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 186 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 187 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 188 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 189 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 190 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 191 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 192 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 193 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 194 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 195 | * because a task may remain alive during all the haproxy execution. |
| 196 | */ |
| 197 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 198 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 199 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 200 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 201 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 202 | * it is used for preventing infinite loops. |
| 203 | * |
| 204 | * I test the scheer with an infinite loop containing one incrementation |
| 205 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 206 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 207 | * to one interrupt each 10 000 instructions. |
| 208 | * |
| 209 | * configured | Number of |
| 210 | * instructions | loops executed |
| 211 | * between two | in milions |
| 212 | * forced yields | |
| 213 | * ---------------+--------------- |
| 214 | * 10 | 160 |
| 215 | * 500 | 670 |
| 216 | * 1000 | 680 |
| 217 | * 5000 | 700 |
| 218 | * 7000 | 700 |
| 219 | * 8000 | 700 |
| 220 | * 9000 | 710 <- ceil |
| 221 | * 10000 | 710 |
| 222 | * 100000 | 710 |
| 223 | * 1000000 | 710 |
| 224 | * |
| 225 | */ |
| 226 | static unsigned int hlua_nb_instruction = 10000; |
| 227 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 228 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 229 | * be enforced on any memory allocation. |
| 230 | */ |
| 231 | struct hlua_mem_allocator { |
| 232 | size_t allocated; |
| 233 | size_t limit; |
| 234 | }; |
| 235 | |
| 236 | static struct hlua_mem_allocator hlua_global_allocator; |
| 237 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 238 | /* These functions converts types between HAProxy internal args or |
| 239 | * sample and LUA types. Another function permits to check if the |
| 240 | * LUA stack contains arguments according with an required ARG_T |
| 241 | * format. |
| 242 | */ |
| 243 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 244 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 245 | __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] | 246 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 247 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 248 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 249 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 250 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 251 | __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] | 252 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 253 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 254 | do { \ |
| 255 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 256 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 257 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 258 | } while (0) |
| 259 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 260 | /* Used to check an Lua function type in the stack. It creates and |
| 261 | * returns a reference of the function. This function throws an |
| 262 | * error if the rgument is not a "function". |
| 263 | */ |
| 264 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 265 | { |
| 266 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 267 | 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] | 268 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 269 | } |
| 270 | lua_pushvalue(L, argno); |
| 271 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 272 | } |
| 273 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 274 | /* Return the string that is of the top of the stack. */ |
| 275 | const char *hlua_get_top_error_string(lua_State *L) |
| 276 | { |
| 277 | if (lua_gettop(L) < 1) |
| 278 | return "unknown error"; |
| 279 | if (lua_type(L, -1) != LUA_TSTRING) |
| 280 | return "unknown error"; |
| 281 | return lua_tostring(L, -1); |
| 282 | } |
| 283 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 284 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 285 | { |
| 286 | lua_Debug ar; |
| 287 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 288 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 289 | int filled = 0; |
| 290 | |
| 291 | while (lua_getstack(L, level++, &ar)) { |
| 292 | |
| 293 | /* Add separator */ |
| 294 | if (filled) |
| 295 | chunk_appendf(msg, ", "); |
| 296 | filled = 1; |
| 297 | |
| 298 | /* Fill fields: |
| 299 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 300 | * 'l': fills in the field currentline; |
| 301 | * 'n': fills in the field name and namewhat; |
| 302 | * 't': fills in the field istailcall; |
| 303 | */ |
| 304 | lua_getinfo(L, "Slnt", &ar); |
| 305 | |
| 306 | /* Append code localisation */ |
| 307 | if (ar.currentline > 0) |
| 308 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 309 | else |
| 310 | chunk_appendf(msg, "%s ", ar.short_src); |
| 311 | |
| 312 | /* |
| 313 | * Get function name |
| 314 | * |
| 315 | * if namewhat is no empty, name is defined. |
| 316 | * what contains "Lua" for Lua function, "C" for C function, |
| 317 | * or "main" for main code. |
| 318 | */ |
| 319 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 320 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 321 | |
| 322 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 323 | chunk_appendf(msg, "main chunk"); |
| 324 | |
| 325 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 326 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 327 | |
| 328 | else /* nothing left... */ |
| 329 | chunk_appendf(msg, "?"); |
| 330 | |
| 331 | |
| 332 | /* Display tailed call */ |
| 333 | if (ar.istailcall) |
| 334 | chunk_appendf(msg, " ..."); |
| 335 | } |
| 336 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 337 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 341 | /* This function check the number of arguments available in the |
| 342 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 343 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 344 | */ |
| 345 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 346 | { |
| 347 | if (lua_gettop(L) == nb) |
| 348 | return; |
| 349 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 350 | } |
| 351 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 352 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 353 | * and the line number where the error is encountered. |
| 354 | */ |
| 355 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 356 | { |
| 357 | va_list argp; |
| 358 | va_start(argp, fmt); |
| 359 | luaL_where(L, 1); |
| 360 | lua_pushvfstring(L, fmt, argp); |
| 361 | va_end(argp); |
| 362 | lua_concat(L, 2); |
| 363 | return 1; |
| 364 | } |
| 365 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 366 | /* This functions is used with sample fetch and converters. It |
| 367 | * converts the HAProxy configuration argument in a lua stack |
| 368 | * values. |
| 369 | * |
| 370 | * It takes an array of "arg", and each entry of the array is |
| 371 | * converted and pushed in the LUA stack. |
| 372 | */ |
| 373 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 374 | { |
| 375 | switch (arg->type) { |
| 376 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 377 | case ARGT_TIME: |
| 378 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 379 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 380 | break; |
| 381 | |
| 382 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 383 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 384 | break; |
| 385 | |
| 386 | case ARGT_IPV4: |
| 387 | case ARGT_IPV6: |
| 388 | case ARGT_MSK4: |
| 389 | case ARGT_MSK6: |
| 390 | case ARGT_FE: |
| 391 | case ARGT_BE: |
| 392 | case ARGT_TAB: |
| 393 | case ARGT_SRV: |
| 394 | case ARGT_USR: |
| 395 | case ARGT_MAP: |
| 396 | default: |
| 397 | lua_pushnil(L); |
| 398 | break; |
| 399 | } |
| 400 | return 1; |
| 401 | } |
| 402 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 403 | /* 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] | 404 | * 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] | 405 | * with sample fetch wrappers. The input arguments are given to the |
| 406 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 407 | */ |
| 408 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 409 | { |
| 410 | switch (lua_type(L, ud)) { |
| 411 | |
| 412 | case LUA_TNUMBER: |
| 413 | case LUA_TBOOLEAN: |
| 414 | arg->type = ARGT_SINT; |
| 415 | arg->data.sint = lua_tointeger(L, ud); |
| 416 | break; |
| 417 | |
| 418 | case LUA_TSTRING: |
| 419 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 420 | 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] | 421 | /* 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] | 422 | 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] | 423 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | case LUA_TUSERDATA: |
| 427 | case LUA_TNIL: |
| 428 | case LUA_TTABLE: |
| 429 | case LUA_TFUNCTION: |
| 430 | case LUA_TTHREAD: |
| 431 | case LUA_TLIGHTUSERDATA: |
| 432 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 433 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 434 | break; |
| 435 | } |
| 436 | return 1; |
| 437 | } |
| 438 | |
| 439 | /* the following functions are used to convert a struct sample |
| 440 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 441 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 442 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 443 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 444 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 445 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 447 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 448 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | break; |
| 450 | |
| 451 | case SMP_T_BIN: |
| 452 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 453 | 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] | 454 | break; |
| 455 | |
| 456 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 457 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 459 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 460 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 461 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 462 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 463 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 464 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 465 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 466 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 467 | 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] | 468 | break; |
| 469 | default: |
| 470 | lua_pushnil(L); |
| 471 | break; |
| 472 | } |
| 473 | break; |
| 474 | |
| 475 | case SMP_T_IPV4: |
| 476 | case SMP_T_IPV6: |
| 477 | 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] | 478 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 479 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 480 | 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] | 481 | else |
| 482 | lua_pushnil(L); |
| 483 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 484 | default: |
| 485 | lua_pushnil(L); |
| 486 | break; |
| 487 | } |
| 488 | return 1; |
| 489 | } |
| 490 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 491 | /* the following functions are used to convert a struct sample |
| 492 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 493 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 494 | */ |
| 495 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 496 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 497 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 498 | |
| 499 | case SMP_T_BIN: |
| 500 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 501 | 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] | 502 | break; |
| 503 | |
| 504 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 505 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 506 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 507 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 508 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 509 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 510 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 511 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 512 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 513 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 514 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 515 | 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] | 516 | break; |
| 517 | default: |
| 518 | lua_pushstring(L, ""); |
| 519 | break; |
| 520 | } |
| 521 | break; |
| 522 | |
| 523 | case SMP_T_SINT: |
| 524 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 525 | case SMP_T_IPV4: |
| 526 | case SMP_T_IPV6: |
| 527 | 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] | 528 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 529 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 530 | 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] | 531 | else |
| 532 | lua_pushstring(L, ""); |
| 533 | break; |
| 534 | default: |
| 535 | lua_pushstring(L, ""); |
| 536 | break; |
| 537 | } |
| 538 | return 1; |
| 539 | } |
| 540 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 541 | /* the following functions are used to convert an Lua type in a |
| 542 | * struct sample. This is useful to provide data from a converter |
| 543 | * to the LUA code. |
| 544 | */ |
| 545 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 546 | { |
| 547 | switch (lua_type(L, ud)) { |
| 548 | |
| 549 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 550 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 551 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 552 | break; |
| 553 | |
| 554 | |
| 555 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 556 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 557 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 558 | break; |
| 559 | |
| 560 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 561 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 562 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 563 | 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] | 564 | /* 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] | 565 | 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] | 566 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 567 | break; |
| 568 | |
| 569 | case LUA_TUSERDATA: |
| 570 | case LUA_TNIL: |
| 571 | case LUA_TTABLE: |
| 572 | case LUA_TFUNCTION: |
| 573 | case LUA_TTHREAD: |
| 574 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 575 | case LUA_TNONE: |
| 576 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 577 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 578 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | break; |
| 580 | } |
| 581 | return 1; |
| 582 | } |
| 583 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 584 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 585 | * 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] | 586 | * 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] | 587 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 588 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 589 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 590 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 591 | __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] | 592 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 593 | { |
| 594 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 595 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 596 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 597 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 598 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 599 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 600 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 601 | |
| 602 | idx = 0; |
| 603 | min_arg = ARGM(mask); |
| 604 | mask >>= ARGM_BITS; |
| 605 | |
| 606 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 607 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 608 | |
| 609 | /* Check oversize. */ |
| 610 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 611 | msg = "Malformed argument mask"; |
| 612 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* Check for mandatory arguments. */ |
| 616 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 617 | if (idx < min_arg) { |
| 618 | |
| 619 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 620 | if (idx > 0) { |
| 621 | msg = "Mandatory argument expected"; |
| 622 | goto error; |
| 623 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 624 | |
| 625 | /* If first argument have a certain type, some default values |
| 626 | * may be used. See the function smp_resolve_args(). |
| 627 | */ |
| 628 | switch (mask & ARGT_MASK) { |
| 629 | |
| 630 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 631 | if (!(p->cap & PR_CAP_FE)) { |
| 632 | msg = "Mandatory argument expected"; |
| 633 | goto error; |
| 634 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 635 | argp[idx].data.prx = p; |
| 636 | argp[idx].type = ARGT_FE; |
| 637 | argp[idx+1].type = ARGT_STOP; |
| 638 | break; |
| 639 | |
| 640 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 641 | if (!(p->cap & PR_CAP_BE)) { |
| 642 | msg = "Mandatory argument expected"; |
| 643 | goto error; |
| 644 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 645 | argp[idx].data.prx = p; |
| 646 | argp[idx].type = ARGT_BE; |
| 647 | argp[idx+1].type = ARGT_STOP; |
| 648 | break; |
| 649 | |
| 650 | case ARGT_TAB: |
| 651 | argp[idx].data.prx = p; |
| 652 | argp[idx].type = ARGT_TAB; |
| 653 | argp[idx+1].type = ARGT_STOP; |
| 654 | break; |
| 655 | |
| 656 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 657 | msg = "Mandatory argument expected"; |
| 658 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 662 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 663 | } |
| 664 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 665 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 666 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 667 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 668 | msg = "Last argument expected"; |
| 669 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 673 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 674 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 677 | /* Convert some argument types. All string in argp[] are for not |
| 678 | * duplicated yet. |
| 679 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 680 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 681 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 682 | if (argp[idx].type != ARGT_SINT) { |
| 683 | msg = "integer expected"; |
| 684 | goto error; |
| 685 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 686 | argp[idx].type = ARGT_SINT; |
| 687 | break; |
| 688 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 689 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 690 | if (argp[idx].type != ARGT_SINT) { |
| 691 | msg = "integer expected"; |
| 692 | goto error; |
| 693 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 694 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 695 | break; |
| 696 | |
| 697 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 698 | if (argp[idx].type != ARGT_SINT) { |
| 699 | msg = "integer expected"; |
| 700 | goto error; |
| 701 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 702 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 703 | break; |
| 704 | |
| 705 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 706 | if (argp[idx].type != ARGT_STR) { |
| 707 | msg = "string expected"; |
| 708 | goto error; |
| 709 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 710 | 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] | 711 | if (!argp[idx].data.prx) { |
| 712 | msg = "frontend doesn't exist"; |
| 713 | goto error; |
| 714 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | argp[idx].type = ARGT_FE; |
| 716 | break; |
| 717 | |
| 718 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 719 | if (argp[idx].type != ARGT_STR) { |
| 720 | msg = "string expected"; |
| 721 | goto error; |
| 722 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 723 | 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] | 724 | if (!argp[idx].data.prx) { |
| 725 | msg = "backend doesn't exist"; |
| 726 | goto error; |
| 727 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 728 | argp[idx].type = ARGT_BE; |
| 729 | break; |
| 730 | |
| 731 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 732 | if (argp[idx].type != ARGT_STR) { |
| 733 | msg = "string expected"; |
| 734 | goto error; |
| 735 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 736 | 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] | 737 | if (!argp[idx].data.t) { |
| 738 | msg = "table doesn't exist"; |
| 739 | goto error; |
| 740 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 741 | argp[idx].type = ARGT_TAB; |
| 742 | break; |
| 743 | |
| 744 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 745 | if (argp[idx].type != ARGT_STR) { |
| 746 | msg = "string expected"; |
| 747 | goto error; |
| 748 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 749 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | if (sname) { |
| 751 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 752 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 753 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 754 | if (!px) { |
| 755 | msg = "backend doesn't exist"; |
| 756 | goto error; |
| 757 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 758 | } |
| 759 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 760 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 761 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 762 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 764 | if (!argp[idx].data.srv) { |
| 765 | msg = "server doesn't exist"; |
| 766 | goto error; |
| 767 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 768 | argp[idx].type = ARGT_SRV; |
| 769 | break; |
| 770 | |
| 771 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 772 | if (argp[idx].type != ARGT_STR) { |
| 773 | msg = "string expected"; |
| 774 | goto error; |
| 775 | } |
| 776 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 777 | msg = "invalid IPv4 address"; |
| 778 | goto error; |
| 779 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 780 | argp[idx].type = ARGT_IPV4; |
| 781 | break; |
| 782 | |
| 783 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 784 | if (argp[idx].type == ARGT_SINT) |
| 785 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 786 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 787 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 788 | msg = "invalid IPv4 mask"; |
| 789 | goto error; |
| 790 | } |
| 791 | } |
| 792 | else { |
| 793 | msg = "integer or string expected"; |
| 794 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 795 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 796 | argp[idx].type = ARGT_MSK4; |
| 797 | break; |
| 798 | |
| 799 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 800 | if (argp[idx].type != ARGT_STR) { |
| 801 | msg = "string expected"; |
| 802 | goto error; |
| 803 | } |
| 804 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 805 | msg = "invalid IPv6 address"; |
| 806 | goto error; |
| 807 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 808 | argp[idx].type = ARGT_IPV6; |
| 809 | break; |
| 810 | |
| 811 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 812 | if (argp[idx].type == ARGT_SINT) |
| 813 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 814 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 815 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 816 | msg = "invalid IPv6 mask"; |
| 817 | goto error; |
| 818 | } |
| 819 | } |
| 820 | else { |
| 821 | msg = "integer or string expected"; |
| 822 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 823 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 824 | argp[idx].type = ARGT_MSK6; |
| 825 | break; |
| 826 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 827 | case ARGT_REG: |
| 828 | if (argp[idx].type != ARGT_STR) { |
| 829 | msg = "string expected"; |
| 830 | goto error; |
| 831 | } |
| 832 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 833 | if (!reg) { |
| 834 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 835 | argp[idx].data.str.area, err); |
| 836 | free(err); |
| 837 | goto error; |
| 838 | } |
| 839 | argp[idx].type = ARGT_REG; |
| 840 | argp[idx].data.reg = reg; |
| 841 | break; |
| 842 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 843 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 844 | if (argp[idx].type != ARGT_STR) { |
| 845 | msg = "string expected"; |
| 846 | goto error; |
| 847 | } |
| 848 | if (p->uri_auth && p->uri_auth->userlist && |
| 849 | !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area)) |
| 850 | ul = p->uri_auth->userlist; |
| 851 | else |
| 852 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 853 | |
| 854 | if (!ul) { |
| 855 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 856 | goto error; |
| 857 | } |
| 858 | argp[idx].type = ARGT_USR; |
| 859 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 860 | break; |
| 861 | |
| 862 | case ARGT_STR: |
| 863 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 864 | msg = "unable to duplicate string arg"; |
| 865 | goto error; |
| 866 | } |
| 867 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 868 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 869 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 870 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 871 | msg = "type not yet supported"; |
| 872 | goto error; |
| 873 | break; |
| 874 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* Check for type of argument. */ |
| 878 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 879 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 880 | arg_type_names[(mask & ARGT_MASK)], |
| 881 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 882 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* Next argument. */ |
| 886 | mask >>= ARGT_BITS; |
| 887 | idx++; |
| 888 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 889 | return 0; |
| 890 | |
| 891 | error: |
| 892 | for (i = 0; i < idx; i++) { |
| 893 | if (argp[i].type == ARGT_STR) |
| 894 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 895 | else if (argp[i].type == ARGT_REG) |
| 896 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 897 | } |
| 898 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 899 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 902 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 903 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 904 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 905 | * |
| 906 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 908 | */ |
| 909 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 910 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 911 | struct hlua **hlua = lua_getextraspace(L); |
| 912 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 913 | } |
| 914 | static inline void hlua_sethlua(struct hlua *hlua) |
| 915 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 916 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 917 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 918 | } |
| 919 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 920 | /* This function is used to send logs. It try to send on screen (stderr) |
| 921 | * and on the default syslog server. |
| 922 | */ |
| 923 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 924 | { |
| 925 | struct tm tm; |
| 926 | char *p; |
| 927 | |
| 928 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 929 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 930 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 931 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 932 | /* Break the message if exceed the buffer size. */ |
| 933 | *(p-4) = ' '; |
| 934 | *(p-3) = '.'; |
| 935 | *(p-2) = '.'; |
| 936 | *(p-1) = '.'; |
| 937 | break; |
| 938 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 939 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 940 | *p = *msg; |
| 941 | else |
| 942 | *p = '.'; |
| 943 | } |
| 944 | *p = '\0'; |
| 945 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 946 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 947 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 948 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 949 | return; |
| 950 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 951 | get_localtime(date.tv_sec, &tm); |
| 952 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 953 | 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] | 954 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 955 | fflush(stderr); |
| 956 | } |
| 957 | } |
| 958 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 959 | /* This function just ensure that the yield will be always |
| 960 | * returned with a timeout and permit to set some flags |
| 961 | */ |
| 962 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 963 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 964 | { |
| 965 | struct hlua *hlua = hlua_gethlua(L); |
| 966 | |
| 967 | /* Set the wake timeout. If timeout is required, we set |
| 968 | * the expiration time. |
| 969 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 970 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 971 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 972 | hlua->flags |= flags; |
| 973 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 974 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 975 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 978 | /* This function initialises the Lua environment stored in the stream. |
| 979 | * 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] | 980 | * 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] | 981 | * |
| 982 | * This function is particular. it initialises a new Lua thread. If the |
| 983 | * initialisation fails (example: out of memory error), the lua function |
| 984 | * throws an error (longjmp). |
| 985 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 986 | * 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] | 987 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 988 | * threads appear, the safe environment set a lock to ensure only one |
| 989 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 990 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 991 | * |
| 992 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 993 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 994 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 995 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 996 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 997 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 998 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 999 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1000 | 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] | 1001 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1002 | if (!already_safe) { |
| 1003 | if (!SET_SAFE_LJMP(gL.T)) { |
| 1004 | lua->Tref = LUA_REFNIL; |
| 1005 | return 0; |
| 1006 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1007 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1008 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1009 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1010 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1011 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1012 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1013 | lua->T = lua_newthread(gL.T); |
| 1014 | if (!lua->T) { |
| 1015 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1016 | if (!already_safe) |
| 1017 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | hlua_sethlua(lua); |
| 1021 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1022 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1023 | if (!already_safe) |
| 1024 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1025 | return 1; |
| 1026 | } |
| 1027 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1028 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1029 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1030 | * is not freed. |
| 1031 | */ |
| 1032 | void hlua_ctx_destroy(struct hlua *lua) |
| 1033 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1034 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1035 | return; |
| 1036 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1037 | if (!lua->T) |
| 1038 | goto end; |
| 1039 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1040 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1041 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1042 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1043 | if (!SET_SAFE_LJMP(lua->T)) |
| 1044 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1045 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1046 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1047 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1048 | if (!SET_SAFE_LJMP(gL.T)) |
| 1049 | return; |
| 1050 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1051 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1052 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1053 | * without error, we run the GC on the thread pointer. Its freed all |
| 1054 | * the unused memory. |
| 1055 | * If the thread is finnish with an error or is currently yielded, |
| 1056 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1057 | * so e run the GC on the main thread. |
| 1058 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1059 | * the garbage collection. |
| 1060 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1061 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1062 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1063 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1064 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 1065 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1066 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1067 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1068 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1069 | |
| 1070 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1071 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /* This function is used to restore the Lua context when a coroutine |
| 1075 | * fails. This function copy the common memory between old coroutine |
| 1076 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1077 | * replaced by the new coroutine. |
| 1078 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1079 | * as string error message and it is copied in the new stack. |
| 1080 | */ |
| 1081 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1082 | { |
| 1083 | lua_State *T; |
| 1084 | int new_ref; |
| 1085 | |
| 1086 | /* Renew the main LUA stack doesn't have sense. */ |
| 1087 | if (lua == &gL) |
| 1088 | return 0; |
| 1089 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1090 | /* New Lua coroutine. */ |
| 1091 | T = lua_newthread(gL.T); |
| 1092 | if (!T) |
| 1093 | return 0; |
| 1094 | |
| 1095 | /* Copy last error message. */ |
| 1096 | if (keep_msg) |
| 1097 | lua_xmove(lua->T, T, 1); |
| 1098 | |
| 1099 | /* Copy data between the coroutines. */ |
| 1100 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1101 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1102 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1103 | |
| 1104 | /* Destroy old data. */ |
| 1105 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1106 | |
| 1107 | /* The thread is garbage collected by Lua. */ |
| 1108 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1109 | |
| 1110 | /* Fill the struct with the new coroutine values. */ |
| 1111 | lua->Mref = new_ref; |
| 1112 | lua->T = T; |
| 1113 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1114 | |
| 1115 | /* Set context. */ |
| 1116 | hlua_sethlua(lua); |
| 1117 | |
| 1118 | return 1; |
| 1119 | } |
| 1120 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1121 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1122 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1123 | struct hlua *hlua = hlua_gethlua(L); |
| 1124 | |
| 1125 | /* Lua cannot yield when its returning from a function, |
| 1126 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1127 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1128 | */ |
| 1129 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1130 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | /* restore the interrupt condition. */ |
| 1135 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1136 | |
| 1137 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1138 | * If the state is not yieldable, trying yield causes an error. |
| 1139 | */ |
| 1140 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1141 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1142 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1143 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1144 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1145 | hlua->run_time += now_ms - hlua->start_time; |
| 1146 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1147 | lua_pushfstring(L, "execution timeout"); |
| 1148 | WILL_LJMP(lua_error(L)); |
| 1149 | } |
| 1150 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1151 | /* Update the start time. */ |
| 1152 | hlua->start_time = now_ms; |
| 1153 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1154 | /* Try to interrupt the process at the end of the current |
| 1155 | * unyieldable function. |
| 1156 | */ |
| 1157 | 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] | 1158 | } |
| 1159 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | /* This function start or resumes the Lua stack execution. If the flag |
| 1161 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1162 | * The function return an error. |
| 1163 | * |
| 1164 | * The function can returns 4 values: |
| 1165 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1166 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1167 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1168 | * - 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] | 1169 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1170 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1171 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1172 | * 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] | 1173 | * LUA code. |
| 1174 | */ |
| 1175 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1176 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1177 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1178 | int nres; |
| 1179 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | int ret; |
| 1181 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1182 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1184 | /* Initialise run time counter. */ |
| 1185 | if (!HLUA_IS_RUNNING(lua)) |
| 1186 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1187 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1188 | /* Lock the whole Lua execution. This lock must be before the |
| 1189 | * label "resume_execution". |
| 1190 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1191 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1192 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1193 | resume_execution: |
| 1194 | |
| 1195 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1196 | * instructions. it is used for preventing infinite loops. |
| 1197 | */ |
| 1198 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1199 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1200 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1201 | HLUA_SET_RUN(lua); |
| 1202 | HLUA_CLR_CTRLYIELD(lua); |
| 1203 | HLUA_CLR_WAKERESWR(lua); |
| 1204 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1205 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1206 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1207 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1208 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1209 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1211 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1212 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1213 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1215 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | switch (ret) { |
| 1217 | |
| 1218 | case LUA_OK: |
| 1219 | ret = HLUA_E_OK; |
| 1220 | break; |
| 1221 | |
| 1222 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1223 | /* Check if the execution timeout is expired. It it is the case, we |
| 1224 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1225 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1226 | tv_update_date(0, 1); |
| 1227 | lua->run_time += now_ms - lua->start_time; |
| 1228 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1229 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1230 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1231 | break; |
| 1232 | } |
| 1233 | /* Process the forced yield. if the general yield is not allowed or |
| 1234 | * if no task were associated this the current Lua execution |
| 1235 | * coroutine, we resume the execution. Else we want to return in the |
| 1236 | * scheduler and we want to be waked up again, to continue the |
| 1237 | * current Lua execution. So we schedule our own task. |
| 1238 | */ |
| 1239 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1240 | if (!yield_allowed || !lua->task) |
| 1241 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1242 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1243 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1244 | if (!yield_allowed) { |
| 1245 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1246 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1247 | break; |
| 1248 | } |
| 1249 | ret = HLUA_E_AGAIN; |
| 1250 | break; |
| 1251 | |
| 1252 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1253 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1254 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1255 | * because the errors ares the only one mean to return immediately |
| 1256 | * from and lua execution. |
| 1257 | */ |
| 1258 | if (lua->flags & HLUA_EXIT) { |
| 1259 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1260 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1261 | break; |
| 1262 | } |
| 1263 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1264 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1265 | if (!lua_checkstack(lua->T, 1)) { |
| 1266 | ret = HLUA_E_ERR; |
| 1267 | break; |
| 1268 | } |
| 1269 | msg = lua_tostring(lua->T, -1); |
| 1270 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1271 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1272 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1273 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1274 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1275 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1276 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1277 | ret = HLUA_E_ERRMSG; |
| 1278 | break; |
| 1279 | |
| 1280 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1281 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1282 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1283 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1284 | break; |
| 1285 | |
| 1286 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1287 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1288 | if (!lua_checkstack(lua->T, 1)) { |
| 1289 | ret = HLUA_E_ERR; |
| 1290 | break; |
| 1291 | } |
| 1292 | msg = lua_tostring(lua->T, -1); |
| 1293 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1294 | lua_pop(lua->T, 1); |
| 1295 | if (msg) |
| 1296 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1297 | else |
| 1298 | lua_pushfstring(lua->T, "message handler error"); |
| 1299 | ret = HLUA_E_ERRMSG; |
| 1300 | break; |
| 1301 | |
| 1302 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1303 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1304 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1305 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1306 | break; |
| 1307 | } |
| 1308 | |
| 1309 | switch (ret) { |
| 1310 | case HLUA_E_AGAIN: |
| 1311 | break; |
| 1312 | |
| 1313 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1314 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1315 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1316 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1317 | break; |
| 1318 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1319 | case HLUA_E_ETMOUT: |
| 1320 | case HLUA_E_NOMEM: |
| 1321 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1322 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1323 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1324 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1325 | hlua_ctx_renew(lua, 0); |
| 1326 | break; |
| 1327 | |
| 1328 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1329 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1330 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1331 | break; |
| 1332 | } |
| 1333 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1334 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1335 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1336 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1337 | return ret; |
| 1338 | } |
| 1339 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1340 | /* This function exit the current code. */ |
| 1341 | __LJMP static int hlua_done(lua_State *L) |
| 1342 | { |
| 1343 | struct hlua *hlua = hlua_gethlua(L); |
| 1344 | |
| 1345 | hlua->flags |= HLUA_EXIT; |
| 1346 | WILL_LJMP(lua_error(L)); |
| 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1351 | /* This function is an LUA binding. It provides a function |
| 1352 | * for deleting ACL from a referenced ACL file. |
| 1353 | */ |
| 1354 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1355 | { |
| 1356 | const char *name; |
| 1357 | const char *key; |
| 1358 | struct pat_ref *ref; |
| 1359 | |
| 1360 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1361 | |
| 1362 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1363 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1364 | |
| 1365 | ref = pat_ref_lookup(name); |
| 1366 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1367 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1368 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1369 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1370 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1371 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | /* This function is an LUA binding. It provides a function |
| 1376 | * for deleting map entry from a referenced map file. |
| 1377 | */ |
| 1378 | static int hlua_del_map(lua_State *L) |
| 1379 | { |
| 1380 | const char *name; |
| 1381 | const char *key; |
| 1382 | struct pat_ref *ref; |
| 1383 | |
| 1384 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1385 | |
| 1386 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1387 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1388 | |
| 1389 | ref = pat_ref_lookup(name); |
| 1390 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1391 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1392 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1393 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1394 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1395 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | /* This function is an LUA binding. It provides a function |
| 1400 | * for adding ACL pattern from a referenced ACL file. |
| 1401 | */ |
| 1402 | static int hlua_add_acl(lua_State *L) |
| 1403 | { |
| 1404 | const char *name; |
| 1405 | const char *key; |
| 1406 | struct pat_ref *ref; |
| 1407 | |
| 1408 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1409 | |
| 1410 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1411 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1412 | |
| 1413 | ref = pat_ref_lookup(name); |
| 1414 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1415 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1416 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1417 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1418 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1419 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1420 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | /* This function is an LUA binding. It provides a function |
| 1425 | * for setting map pattern and sample from a referenced map |
| 1426 | * file. |
| 1427 | */ |
| 1428 | static int hlua_set_map(lua_State *L) |
| 1429 | { |
| 1430 | const char *name; |
| 1431 | const char *key; |
| 1432 | const char *value; |
| 1433 | struct pat_ref *ref; |
| 1434 | |
| 1435 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1436 | |
| 1437 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1438 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1439 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1440 | |
| 1441 | ref = pat_ref_lookup(name); |
| 1442 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1443 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1444 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1445 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1446 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1447 | pat_ref_set(ref, key, value, NULL); |
| 1448 | else |
| 1449 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1450 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1451 | return 0; |
| 1452 | } |
| 1453 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1454 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1455 | * 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] | 1456 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1457 | * the name of the object (_G[<name>] = <metable> ). |
| 1458 | * |
| 1459 | * A metable is a table that modify the standard behavior of a standard |
| 1460 | * access to the associated data. The entries of this new metatable are |
| 1461 | * defined as is: |
| 1462 | * |
| 1463 | * http://lua-users.org/wiki/MetatableEvents |
| 1464 | * |
| 1465 | * __index |
| 1466 | * |
| 1467 | * we access an absent field in a table, the result is nil. This is |
| 1468 | * true, but it is not the whole truth. Actually, such access triggers |
| 1469 | * the interpreter to look for an __index metamethod: If there is no |
| 1470 | * such method, as usually happens, then the access results in nil; |
| 1471 | * otherwise, the metamethod will provide the result. |
| 1472 | * |
| 1473 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1474 | * the key does not appear in the table, but the metatable has an __index |
| 1475 | * property: |
| 1476 | * |
| 1477 | * - if the value is a function, the function is called, passing in the |
| 1478 | * table and the key; the return value of that function is returned as |
| 1479 | * the result. |
| 1480 | * |
| 1481 | * - if the value is another table, the value of the key in that table is |
| 1482 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1483 | * table's metatable has an __index property, then it continues on up) |
| 1484 | * |
| 1485 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1486 | * |
| 1487 | * http://www.lua.org/pil/13.4.1.html |
| 1488 | * |
| 1489 | * __newindex |
| 1490 | * |
| 1491 | * Like __index, but control property assignment. |
| 1492 | * |
| 1493 | * __mode - Control weak references. A string value with one or both |
| 1494 | * of the characters 'k' and 'v' which specifies that the the |
| 1495 | * keys and/or values in the table are weak references. |
| 1496 | * |
| 1497 | * __call - Treat a table like a function. When a table is followed by |
| 1498 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1499 | * a __call key pointing to a function, that function is invoked |
| 1500 | * (passing any specified arguments) and the return value is |
| 1501 | * returned. |
| 1502 | * |
| 1503 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1504 | * called, if the metatable for myTable has a __metatable |
| 1505 | * key, the value of that key is returned instead of the |
| 1506 | * actual metatable. |
| 1507 | * |
| 1508 | * __tostring - Control string representation. When the builtin |
| 1509 | * "tostring( myTable )" function is called, if the metatable |
| 1510 | * for myTable has a __tostring property set to a function, |
| 1511 | * that function is invoked (passing myTable to it) and the |
| 1512 | * return value is used as the string representation. |
| 1513 | * |
| 1514 | * __len - Control table length. When the table length is requested using |
| 1515 | * the length operator ( '#' ), if the metatable for myTable has |
| 1516 | * a __len key pointing to a function, that function is invoked |
| 1517 | * (passing myTable to it) and the return value used as the value |
| 1518 | * of "#myTable". |
| 1519 | * |
| 1520 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1521 | * collected, if the metatable has a __gc field pointing to a |
| 1522 | * function, that function is first invoked, passing the userdata |
| 1523 | * to it. The __gc metamethod is not called for tables. |
| 1524 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1525 | * |
| 1526 | * Special metamethods for redefining standard operators: |
| 1527 | * http://www.lua.org/pil/13.1.html |
| 1528 | * |
| 1529 | * __add "+" |
| 1530 | * __sub "-" |
| 1531 | * __mul "*" |
| 1532 | * __div "/" |
| 1533 | * __unm "!" |
| 1534 | * __pow "^" |
| 1535 | * __concat ".." |
| 1536 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1537 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1538 | * http://www.lua.org/pil/13.2.html |
| 1539 | * |
| 1540 | * __eq "==" |
| 1541 | * __lt "<" |
| 1542 | * __le "<=" |
| 1543 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1544 | |
| 1545 | /* |
| 1546 | * |
| 1547 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1548 | * Class Map |
| 1549 | * |
| 1550 | * |
| 1551 | */ |
| 1552 | |
| 1553 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1554 | * a class session, otherwise it throws an error. |
| 1555 | */ |
| 1556 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1557 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1558 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | /* This function is the map constructor. It don't need |
| 1562 | * the class Map object. It creates and return a new Map |
| 1563 | * object. It must be called only during "body" or "init" |
| 1564 | * context because it process some filesystem accesses. |
| 1565 | */ |
| 1566 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1567 | { |
| 1568 | const char *fn; |
| 1569 | int match = PAT_MATCH_STR; |
| 1570 | struct sample_conv conv; |
| 1571 | const char *file = ""; |
| 1572 | int line = 0; |
| 1573 | lua_Debug ar; |
| 1574 | char *err = NULL; |
| 1575 | struct arg args[2]; |
| 1576 | |
| 1577 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1578 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1579 | |
| 1580 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1581 | |
| 1582 | if (lua_gettop(L) >= 2) { |
| 1583 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1584 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1585 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1586 | } |
| 1587 | |
| 1588 | /* Get Lua filename and line number. */ |
| 1589 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1590 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1591 | if (ar.currentline > 0) { /* is there info? */ |
| 1592 | file = ar.short_src; |
| 1593 | line = ar.currentline; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | /* fill fake sample_conv struct. */ |
| 1598 | conv.kw = ""; /* unused. */ |
| 1599 | conv.process = NULL; /* unused. */ |
| 1600 | conv.arg_mask = 0; /* unused. */ |
| 1601 | conv.val_args = NULL; /* unused. */ |
| 1602 | conv.out_type = SMP_T_STR; |
| 1603 | conv.private = (void *)(long)match; |
| 1604 | switch (match) { |
| 1605 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1606 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1607 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1608 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1609 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1610 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1611 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1612 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1613 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1614 | default: |
| 1615 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1616 | } |
| 1617 | |
| 1618 | /* fill fake args. */ |
| 1619 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1620 | args[0].data.str.area = strdup(fn); |
| 1621 | args[0].data.str.data = strlen(fn); |
| 1622 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1623 | args[1].type = ARGT_STOP; |
| 1624 | |
| 1625 | /* load the map. */ |
| 1626 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1627 | /* error case: we cant use luaL_error because we must |
| 1628 | * free the err variable. |
| 1629 | */ |
| 1630 | luaL_where(L, 1); |
| 1631 | lua_pushfstring(L, "'new': %s.", err); |
| 1632 | lua_concat(L, 2); |
| 1633 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1634 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1635 | WILL_LJMP(lua_error(L)); |
| 1636 | } |
| 1637 | |
| 1638 | /* create the lua object. */ |
| 1639 | lua_newtable(L); |
| 1640 | lua_pushlightuserdata(L, args[0].data.map); |
| 1641 | lua_rawseti(L, -2, 0); |
| 1642 | |
| 1643 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1644 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1645 | lua_setmetatable(L, -2); |
| 1646 | |
| 1647 | |
| 1648 | return 1; |
| 1649 | } |
| 1650 | |
| 1651 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1652 | { |
| 1653 | struct map_descriptor *desc; |
| 1654 | struct pattern *pat; |
| 1655 | struct sample smp; |
| 1656 | |
| 1657 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1658 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1659 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1660 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1661 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1662 | } |
| 1663 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1664 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1665 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1666 | 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] | 1667 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1671 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1672 | if (str) |
| 1673 | lua_pushstring(L, ""); |
| 1674 | else |
| 1675 | lua_pushnil(L); |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
| 1679 | /* 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] | 1680 | 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] | 1681 | return 1; |
| 1682 | } |
| 1683 | |
| 1684 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1685 | { |
| 1686 | return _hlua_map_lookup(L, 0); |
| 1687 | } |
| 1688 | |
| 1689 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1690 | { |
| 1691 | return _hlua_map_lookup(L, 1); |
| 1692 | } |
| 1693 | |
| 1694 | /* |
| 1695 | * |
| 1696 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | * Class Socket |
| 1698 | * |
| 1699 | * |
| 1700 | */ |
| 1701 | |
| 1702 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1703 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1704 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /* 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] | 1708 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1709 | * received. |
| 1710 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1711 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1713 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1715 | if (appctx->ctx.hlua_cosocket.die) { |
| 1716 | si_shutw(si); |
| 1717 | si_shutr(si); |
| 1718 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1719 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1720 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1721 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1722 | } |
| 1723 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1724 | /* If we cant write, wakeup the pending write signals. */ |
| 1725 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1726 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1727 | |
| 1728 | /* If we cant read, wakeup the pending read signals. */ |
| 1729 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1730 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1731 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1732 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1733 | * to be notified whenever the connection completes. |
| 1734 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1735 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1736 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1737 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1738 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1739 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1740 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1741 | |
| 1742 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1743 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1744 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1745 | /* 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] | 1746 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1747 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1748 | |
| 1749 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1750 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1751 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1752 | |
| 1753 | /* Some data were injected in the buffer, notify the stream |
| 1754 | * interface. |
| 1755 | */ |
| 1756 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1757 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1758 | |
| 1759 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1760 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1761 | */ |
| 1762 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1763 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1764 | } |
| 1765 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1766 | /* This function is called when the "struct stream" is destroyed. |
| 1767 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1768 | * Wake all the pending signals. |
| 1769 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1770 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1771 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1772 | struct xref *peer; |
| 1773 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1774 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1775 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1776 | if (peer) |
| 1777 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1780 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1781 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1785 | * uses this object. If the stream does not exists, just quit. |
| 1786 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1787 | * pending signal can rest in the read and write lists. destroy |
| 1788 | * it. |
| 1789 | */ |
| 1790 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1791 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1792 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1794 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1796 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1797 | |
| 1798 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1799 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1800 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1801 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1802 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1803 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1804 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1805 | appctx->ctx.hlua_cosocket.die = 1; |
| 1806 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1807 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1808 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1809 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1814 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1816 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1818 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1820 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1821 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1823 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1824 | |
| 1825 | /* Check if we run on the same thread than the xreator thread. |
| 1826 | * We cannot access to the socket if the thread is different. |
| 1827 | */ |
| 1828 | if (socket->tid != tid) |
| 1829 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1830 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1831 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1832 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1834 | |
| 1835 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1836 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1838 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1839 | appctx->ctx.hlua_cosocket.die = 1; |
| 1840 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1842 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1843 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1844 | return 0; |
| 1845 | } |
| 1846 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1847 | /* The close function calls close_helper. |
| 1848 | */ |
| 1849 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1850 | { |
| 1851 | MAY_LJMP(check_args(L, 1, "close")); |
| 1852 | return hlua_socket_close_helper(L); |
| 1853 | } |
| 1854 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | /* This Lua function assumes that the stack contain three parameters. |
| 1856 | * 1 - USERDATA containing a struct socket |
| 1857 | * 2 - INTEGER with values of the macro defined below |
| 1858 | * If the integer is -1, we must read at most one line. |
| 1859 | * If the integer is -2, we ust read all the data until the |
| 1860 | * end of the stream. |
| 1861 | * If the integer is positive value, we must read a number of |
| 1862 | * bytes corresponding to this value. |
| 1863 | */ |
| 1864 | #define HLSR_READ_LINE (-1) |
| 1865 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1866 | __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] | 1867 | { |
| 1868 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1869 | int wanted = lua_tointeger(L, 2); |
| 1870 | struct hlua *hlua = hlua_gethlua(L); |
| 1871 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1872 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1873 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1874 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1875 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1876 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1877 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1878 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1879 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1880 | struct stream_interface *si; |
| 1881 | struct stream *s; |
| 1882 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1883 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | |
| 1885 | /* Check if this lua stack is schedulable. */ |
| 1886 | if (!hlua || !hlua->task) |
| 1887 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1888 | "'frontend', 'backend' or 'task'")); |
| 1889 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1890 | /* Check if we run on the same thread than the xreator thread. |
| 1891 | * We cannot access to the socket if the thread is different. |
| 1892 | */ |
| 1893 | if (socket->tid != tid) |
| 1894 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1895 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1896 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1897 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1898 | if (!peer) |
| 1899 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1900 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1901 | si = appctx->owner; |
| 1902 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1904 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1907 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | if (nblk < 0) /* Connection close. */ |
| 1909 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1910 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1912 | |
| 1913 | /* remove final \r\n. */ |
| 1914 | if (nblk == 1) { |
| 1915 | if (blk1[len1-1] == '\n') { |
| 1916 | len1--; |
| 1917 | skip_at_end++; |
| 1918 | if (blk1[len1-1] == '\r') { |
| 1919 | len1--; |
| 1920 | skip_at_end++; |
| 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | else { |
| 1925 | if (blk2[len2-1] == '\n') { |
| 1926 | len2--; |
| 1927 | skip_at_end++; |
| 1928 | if (blk2[len2-1] == '\r') { |
| 1929 | len2--; |
| 1930 | skip_at_end++; |
| 1931 | } |
| 1932 | } |
| 1933 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1938 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1939 | if (nblk < 0) /* Connection close. */ |
| 1940 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1941 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1942 | goto connection_empty; |
| 1943 | } |
| 1944 | |
| 1945 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1946 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1947 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1948 | if (nblk < 0) /* Connection close. */ |
| 1949 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1950 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1951 | goto connection_empty; |
| 1952 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1953 | missing_bytes = wanted - socket->b.n; |
| 1954 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1956 | len1 = missing_bytes; |
| 1957 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1958 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | len = len1; |
| 1962 | |
| 1963 | luaL_addlstring(&socket->b, blk1, len1); |
| 1964 | if (nblk == 2) { |
| 1965 | len += len2; |
| 1966 | luaL_addlstring(&socket->b, blk2, len2); |
| 1967 | } |
| 1968 | |
| 1969 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1970 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | |
| 1972 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1973 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* If the pattern reclaim to read all the data |
| 1976 | * in the connection, got out. |
| 1977 | */ |
| 1978 | if (wanted == HLSR_READ_ALL) |
| 1979 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1980 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | goto connection_empty; |
| 1982 | |
| 1983 | /* Return result. */ |
| 1984 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1985 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | return 1; |
| 1987 | |
| 1988 | connection_closed: |
| 1989 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1990 | xref_unlock(&socket->xref, peer); |
| 1991 | |
| 1992 | no_peer: |
| 1993 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1994 | /* If the buffer containds data. */ |
| 1995 | if (socket->b.n > 0) { |
| 1996 | luaL_pushresult(&socket->b); |
| 1997 | return 1; |
| 1998 | } |
| 1999 | lua_pushnil(L); |
| 2000 | lua_pushstring(L, "connection closed."); |
| 2001 | return 2; |
| 2002 | |
| 2003 | connection_empty: |
| 2004 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2005 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2006 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2007 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2008 | } |
| 2009 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2010 | 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] | 2011 | return 0; |
| 2012 | } |
| 2013 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2014 | /* This Lua function gets two parameters. The first one can be string |
| 2015 | * or a number. If the string is "*l", the user requires one line. If |
| 2016 | * 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] | 2017 | * If the value is a number, the user require a number of bytes equal |
| 2018 | * to the value. The default value is "*l" (a line). |
| 2019 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2020 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | * integer takes this values: |
| 2022 | * -1 : read a line |
| 2023 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2024 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2025 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2026 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | * concatenated with the read data. |
| 2028 | */ |
| 2029 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2030 | { |
| 2031 | int wanted = HLSR_READ_LINE; |
| 2032 | const char *pattern; |
| 2033 | int type; |
| 2034 | char *error; |
| 2035 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2036 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | |
| 2038 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2039 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2040 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2041 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2043 | /* Check if we run on the same thread than the xreator thread. |
| 2044 | * We cannot access to the socket if the thread is different. |
| 2045 | */ |
| 2046 | if (socket->tid != tid) |
| 2047 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2048 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | /* check for pattern. */ |
| 2050 | if (lua_gettop(L) >= 2) { |
| 2051 | type = lua_type(L, 2); |
| 2052 | if (type == LUA_TSTRING) { |
| 2053 | pattern = lua_tostring(L, 2); |
| 2054 | if (strcmp(pattern, "*a") == 0) |
| 2055 | wanted = HLSR_READ_ALL; |
| 2056 | else if (strcmp(pattern, "*l") == 0) |
| 2057 | wanted = HLSR_READ_LINE; |
| 2058 | else { |
| 2059 | wanted = strtoll(pattern, &error, 10); |
| 2060 | if (*error != '\0') |
| 2061 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2062 | } |
| 2063 | } |
| 2064 | else if (type == LUA_TNUMBER) { |
| 2065 | wanted = lua_tointeger(L, 2); |
| 2066 | if (wanted < 0) |
| 2067 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | /* Set pattern. */ |
| 2072 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* Check if we would replace the top by itself. */ |
| 2075 | if (lua_gettop(L) != 2) |
| 2076 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2078 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | luaL_buffinit(L, &socket->b); |
| 2080 | |
| 2081 | /* Check prefix. */ |
| 2082 | if (lua_gettop(L) >= 3) { |
| 2083 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2084 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2085 | pattern = lua_tolstring(L, 3, &len); |
| 2086 | luaL_addlstring(&socket->b, pattern, len); |
| 2087 | } |
| 2088 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2089 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2093 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2095 | 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] | 2096 | { |
| 2097 | struct hlua_socket *socket; |
| 2098 | struct hlua *hlua = hlua_gethlua(L); |
| 2099 | struct appctx *appctx; |
| 2100 | size_t buf_len; |
| 2101 | const char *buf; |
| 2102 | int len; |
| 2103 | int send_len; |
| 2104 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2105 | struct xref *peer; |
| 2106 | struct stream_interface *si; |
| 2107 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | |
| 2109 | /* Check if this lua stack is schedulable. */ |
| 2110 | if (!hlua || !hlua->task) |
| 2111 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2112 | "'frontend', 'backend' or 'task'")); |
| 2113 | |
| 2114 | /* Get object */ |
| 2115 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2116 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2117 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2118 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2119 | /* Check if we run on the same thread than the xreator thread. |
| 2120 | * We cannot access to the socket if the thread is different. |
| 2121 | */ |
| 2122 | if (socket->tid != tid) |
| 2123 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2124 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2125 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2126 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2127 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2128 | lua_pushinteger(L, -1); |
| 2129 | return 1; |
| 2130 | } |
| 2131 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2132 | si = appctx->owner; |
| 2133 | s = si_strm(si); |
| 2134 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2135 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2136 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2137 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2138 | lua_pushinteger(L, -1); |
| 2139 | return 1; |
| 2140 | } |
| 2141 | |
| 2142 | /* Update the input buffer data. */ |
| 2143 | buf += sent; |
| 2144 | send_len = buf_len - sent; |
| 2145 | |
| 2146 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2147 | if (sent >= buf_len) { |
| 2148 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2150 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2152 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2153 | * the request buffer if its not required. |
| 2154 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2155 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2156 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2157 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2158 | } |
| 2159 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2160 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2161 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2162 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2164 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | |
| 2166 | /* send data */ |
| 2167 | if (len < send_len) |
| 2168 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2169 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | |
| 2171 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2172 | * the data" (-2) are not expected because the available length |
| 2173 | * is tested. |
| 2174 | * Other unknown error are also not expected. |
| 2175 | */ |
| 2176 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2177 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2178 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2179 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2180 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2182 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2183 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2184 | return 1; |
| 2185 | } |
| 2186 | |
| 2187 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2188 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2189 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2190 | s->req.rex = TICK_ETERNITY; |
| 2191 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | |
| 2193 | /* Update length sent. */ |
| 2194 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2195 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2196 | |
| 2197 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2198 | if (sent + len >= buf_len) { |
| 2199 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2201 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | |
| 2203 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2204 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2205 | xref_unlock(&socket->xref, peer); |
| 2206 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2207 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2208 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2209 | 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] | 2210 | return 0; |
| 2211 | } |
| 2212 | |
| 2213 | /* This function initiate the send of data. It just check the input |
| 2214 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2215 | * 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] | 2216 | * "hlua_socket_write_yield" that can yield. |
| 2217 | * |
| 2218 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2219 | * the associated object. The second is a string buffer. The third is |
| 2220 | * a facultative integer that represents where is the buffer position |
| 2221 | * of the start of the data that can send. The first byte is the |
| 2222 | * position "1". The default value is "1". The fourth argument is a |
| 2223 | * facultative integer that represents where is the buffer position |
| 2224 | * of the end of the data that can send. The default is the last byte. |
| 2225 | */ |
| 2226 | static int hlua_socket_send(struct lua_State *L) |
| 2227 | { |
| 2228 | int i; |
| 2229 | int j; |
| 2230 | const char *buf; |
| 2231 | size_t buf_len; |
| 2232 | |
| 2233 | /* Check number of arguments. */ |
| 2234 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2235 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2236 | |
| 2237 | /* Get the string. */ |
| 2238 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2239 | |
| 2240 | /* Get and check j. */ |
| 2241 | if (lua_gettop(L) == 4) { |
| 2242 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2243 | if (j < 0) |
| 2244 | j = buf_len + j + 1; |
| 2245 | if (j > buf_len) |
| 2246 | j = buf_len + 1; |
| 2247 | lua_pop(L, 1); |
| 2248 | } |
| 2249 | else |
| 2250 | j = buf_len; |
| 2251 | |
| 2252 | /* Get and check i. */ |
| 2253 | if (lua_gettop(L) == 3) { |
| 2254 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2255 | if (i < 0) |
| 2256 | i = buf_len + i + 1; |
| 2257 | if (i > buf_len) |
| 2258 | i = buf_len + 1; |
| 2259 | lua_pop(L, 1); |
| 2260 | } else |
| 2261 | i = 1; |
| 2262 | |
| 2263 | /* Check bth i and j. */ |
| 2264 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2265 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | return 1; |
| 2267 | } |
| 2268 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2269 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | return 1; |
| 2271 | } |
| 2272 | if (i == 0) |
| 2273 | i = 1; |
| 2274 | if (j == 0) |
| 2275 | j = 1; |
| 2276 | |
| 2277 | /* Pop the string. */ |
| 2278 | lua_pop(L, 1); |
| 2279 | |
| 2280 | /* Update the buffer length. */ |
| 2281 | buf += i - 1; |
| 2282 | buf_len = j - i + 1; |
| 2283 | lua_pushlstring(L, buf, buf_len); |
| 2284 | |
| 2285 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2286 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2288 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | } |
| 2290 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2291 | #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] | 2292 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2293 | { |
| 2294 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2295 | int ret; |
| 2296 | int len; |
| 2297 | char *p; |
| 2298 | |
| 2299 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2300 | if (ret <= 0) { |
| 2301 | lua_pushnil(L); |
| 2302 | return 1; |
| 2303 | } |
| 2304 | |
| 2305 | if (ret == AF_UNIX) { |
| 2306 | lua_pushstring(L, buffer+1); |
| 2307 | return 1; |
| 2308 | } |
| 2309 | else if (ret == AF_INET6) { |
| 2310 | buffer[0] = '['; |
| 2311 | len = strlen(buffer); |
| 2312 | buffer[len] = ']'; |
| 2313 | len++; |
| 2314 | buffer[len] = ':'; |
| 2315 | len++; |
| 2316 | p = buffer; |
| 2317 | } |
| 2318 | else if (ret == AF_INET) { |
| 2319 | p = buffer + 1; |
| 2320 | len = strlen(p); |
| 2321 | p[len] = ':'; |
| 2322 | len++; |
| 2323 | } |
| 2324 | else { |
| 2325 | lua_pushnil(L); |
| 2326 | return 1; |
| 2327 | } |
| 2328 | |
| 2329 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2330 | lua_pushnil(L); |
| 2331 | return 1; |
| 2332 | } |
| 2333 | |
| 2334 | lua_pushstring(L, p); |
| 2335 | return 1; |
| 2336 | } |
| 2337 | |
| 2338 | /* Returns information about the peer of the connection. */ |
| 2339 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2340 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2341 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2342 | struct xref *peer; |
| 2343 | struct appctx *appctx; |
| 2344 | struct stream_interface *si; |
| 2345 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2346 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2347 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2348 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2349 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2350 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2352 | /* Check if we run on the same thread than the xreator thread. |
| 2353 | * We cannot access to the socket if the thread is different. |
| 2354 | */ |
| 2355 | if (socket->tid != tid) |
| 2356 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2357 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2358 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2360 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | lua_pushnil(L); |
| 2362 | return 1; |
| 2363 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2364 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2365 | si = appctx->owner; |
| 2366 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2368 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2369 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2370 | lua_pushnil(L); |
| 2371 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2374 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2375 | xref_unlock(&socket->xref, peer); |
| 2376 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | /* Returns information about my connection side. */ |
| 2380 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2381 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2382 | struct hlua_socket *socket; |
| 2383 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2384 | struct appctx *appctx; |
| 2385 | struct xref *peer; |
| 2386 | struct stream_interface *si; |
| 2387 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2388 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2389 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2391 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2392 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2394 | /* Check if we run on the same thread than the xreator thread. |
| 2395 | * We cannot access to the socket if the thread is different. |
| 2396 | */ |
| 2397 | if (socket->tid != tid) |
| 2398 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2399 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2400 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2401 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2402 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | lua_pushnil(L); |
| 2404 | return 1; |
| 2405 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2406 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2407 | si = appctx->owner; |
| 2408 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2410 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2411 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2412 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2413 | lua_pushnil(L); |
| 2414 | return 1; |
| 2415 | } |
| 2416 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2417 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2418 | xref_unlock(&socket->xref, peer); |
| 2419 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2423 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2424 | .obj_type = OBJ_TYPE_APPLET, |
| 2425 | .name = "<LUA_TCP>", |
| 2426 | .fct = hlua_socket_handler, |
| 2427 | .release = hlua_socket_release, |
| 2428 | }; |
| 2429 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2430 | __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] | 2431 | { |
| 2432 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2433 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2435 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2436 | struct stream_interface *si; |
| 2437 | struct stream *s; |
| 2438 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2439 | /* Check if we run on the same thread than the xreator thread. |
| 2440 | * We cannot access to the socket if the thread is different. |
| 2441 | */ |
| 2442 | if (socket->tid != tid) |
| 2443 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2444 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2445 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2447 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2448 | lua_pushnil(L); |
| 2449 | lua_pushstring(L, "Can't connect"); |
| 2450 | return 2; |
| 2451 | } |
| 2452 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2453 | si = appctx->owner; |
| 2454 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2455 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2456 | /* Check if we run on the same thread than the xreator thread. |
| 2457 | * We cannot access to the socket if the thread is different. |
| 2458 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2459 | if (socket->tid != tid) { |
| 2460 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2461 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2462 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2463 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2464 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2465 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2466 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2467 | lua_pushnil(L); |
| 2468 | lua_pushstring(L, "Can't connect"); |
| 2469 | return 2; |
| 2470 | } |
| 2471 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2472 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2473 | |
| 2474 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2475 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2476 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2477 | lua_pushinteger(L, 1); |
| 2478 | return 1; |
| 2479 | } |
| 2480 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2481 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2482 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2483 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2484 | } |
| 2485 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2486 | 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] | 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | /* This function fail or initite the connection. */ |
| 2491 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2492 | { |
| 2493 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2494 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2495 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2496 | struct hlua *hlua; |
| 2497 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2498 | int low, high; |
| 2499 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2500 | struct xref *peer; |
| 2501 | struct stream_interface *si; |
| 2502 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2503 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2504 | if (lua_gettop(L) < 2) |
| 2505 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | |
| 2507 | /* Get args. */ |
| 2508 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2509 | |
| 2510 | /* Check if we run on the same thread than the xreator thread. |
| 2511 | * We cannot access to the socket if the thread is different. |
| 2512 | */ |
| 2513 | if (socket->tid != tid) |
| 2514 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2515 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2517 | if (lua_gettop(L) >= 3) { |
| 2518 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2519 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2520 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2521 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2522 | * that are not enclosed within square brackets. |
| 2523 | */ |
| 2524 | if (port > 0) { |
| 2525 | luaL_buffinit(L, &b); |
| 2526 | luaL_addstring(&b, ip); |
| 2527 | luaL_addchar(&b, ':'); |
| 2528 | luaL_pushresult(&b); |
| 2529 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2530 | } |
| 2531 | } |
| 2532 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2533 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2534 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2535 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2536 | lua_pushnil(L); |
| 2537 | return 1; |
| 2538 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2539 | |
| 2540 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2541 | 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] | 2542 | if (!addr) { |
| 2543 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2544 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2545 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2546 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2547 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2548 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2549 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2550 | if (port == -1) { |
| 2551 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2552 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2553 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2554 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2555 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2556 | if (port == -1) { |
| 2557 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2558 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2559 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2560 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2561 | } |
| 2562 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2563 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2564 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2565 | si = appctx->owner; |
| 2566 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2567 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2568 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2569 | xref_unlock(&socket->xref, peer); |
| 2570 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2571 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2572 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2573 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2574 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2575 | |
| 2576 | /* inform the stream that we want to be notified whenever the |
| 2577 | * connection completes. |
| 2578 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2579 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2580 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2581 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2582 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2583 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2584 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2585 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2586 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2587 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2588 | } |
| 2589 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2590 | |
| 2591 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2592 | /* Return yield waiting for connection. */ |
| 2593 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2594 | 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] | 2595 | |
| 2596 | return 0; |
| 2597 | } |
| 2598 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2599 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2601 | { |
| 2602 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2603 | struct xref *peer; |
| 2604 | struct appctx *appctx; |
| 2605 | struct stream_interface *si; |
| 2606 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2607 | |
| 2608 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2609 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2610 | |
| 2611 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2612 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2613 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2614 | lua_pushnil(L); |
| 2615 | return 1; |
| 2616 | } |
| 2617 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2618 | si = appctx->owner; |
| 2619 | s = si_strm(si); |
| 2620 | |
| 2621 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2622 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2624 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2625 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2626 | |
| 2627 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2628 | { |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
| 2632 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2633 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2634 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2635 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2636 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2637 | struct xref *peer; |
| 2638 | struct appctx *appctx; |
| 2639 | struct stream_interface *si; |
| 2640 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2641 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2643 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2644 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2645 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2646 | /* convert the timeout to millis */ |
| 2647 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2649 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2650 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2651 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2652 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2653 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2654 | 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] | 2655 | |
| 2656 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2657 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2658 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2659 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2660 | /* Check if we run on the same thread than the xreator thread. |
| 2661 | * We cannot access to the socket if the thread is different. |
| 2662 | */ |
| 2663 | if (socket->tid != tid) |
| 2664 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2665 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2666 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2667 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2668 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2669 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2670 | WILL_LJMP(lua_error(L)); |
| 2671 | return 0; |
| 2672 | } |
| 2673 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2674 | si = appctx->owner; |
| 2675 | s = si_strm(si); |
| 2676 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2677 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2678 | s->req.rto = tmout; |
| 2679 | s->req.wto = tmout; |
| 2680 | s->res.rto = tmout; |
| 2681 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2682 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2683 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2684 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2685 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2686 | |
| 2687 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2688 | task_queue(s->task); |
| 2689 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2690 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2691 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2692 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2693 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2697 | { |
| 2698 | struct hlua_socket *socket; |
| 2699 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2700 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2701 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | |
| 2703 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2704 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | hlua_pusherror(L, "socket: full stack"); |
| 2706 | goto out_fail_conf; |
| 2707 | } |
| 2708 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2709 | /* Create the object: obj[0] = userdata. */ |
| 2710 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2712 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2714 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2716 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2717 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2718 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2719 | goto out_fail_conf; |
| 2720 | } |
| 2721 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2722 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2723 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2724 | lua_setmetatable(L, -2); |
| 2725 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2726 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2727 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2728 | if (!appctx) { |
| 2729 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2730 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2731 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2732 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2733 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2734 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2735 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2736 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2737 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2738 | /* Now create a session, task and stream for this applet */ |
| 2739 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2740 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2741 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2742 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2743 | } |
| 2744 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2745 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2746 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2747 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2748 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2749 | } |
| 2750 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2751 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2752 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2753 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2754 | /* Configure "right" stream interface. this "si" is used to connect |
| 2755 | * and retrieve data from the server. The connection is initialized |
| 2756 | * with the "struct server". |
| 2757 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2758 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2759 | |
| 2760 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2761 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2762 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2763 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2764 | return 1; |
| 2765 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2766 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2767 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2768 | out_fail_sess: |
| 2769 | appctx_free(appctx); |
| 2770 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2771 | WILL_LJMP(lua_error(L)); |
| 2772 | return 0; |
| 2773 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2774 | |
| 2775 | /* |
| 2776 | * |
| 2777 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | * Class Channel |
| 2779 | * |
| 2780 | * |
| 2781 | */ |
| 2782 | |
| 2783 | /* Returns the struct hlua_channel join to the class channel in the |
| 2784 | * stack entry "ud" or throws an argument error. |
| 2785 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2786 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2787 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2788 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2789 | } |
| 2790 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2791 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2792 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2793 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2794 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2795 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2797 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2798 | return 0; |
| 2799 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2800 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2801 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2802 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2803 | |
| 2804 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2805 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2806 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2807 | return 1; |
| 2808 | } |
| 2809 | |
| 2810 | /* Duplicate all the data present in the input channel and put it |
| 2811 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2812 | * the stack if the channel is closed and all the data are consumed, |
| 2813 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2814 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2815 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2816 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2817 | { |
| 2818 | char *blk1; |
| 2819 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2820 | size_t len1; |
| 2821 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2822 | int ret; |
| 2823 | luaL_Buffer b; |
| 2824 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2825 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2826 | if (unlikely(ret == 0)) |
| 2827 | return 0; |
| 2828 | |
| 2829 | if (unlikely(ret < 0)) { |
| 2830 | lua_pushnil(L); |
| 2831 | return -1; |
| 2832 | } |
| 2833 | |
| 2834 | luaL_buffinit(L, &b); |
| 2835 | luaL_addlstring(&b, blk1, len1); |
| 2836 | if (unlikely(ret == 2)) |
| 2837 | luaL_addlstring(&b, blk2, len2); |
| 2838 | luaL_pushresult(&b); |
| 2839 | |
| 2840 | if (unlikely(ret == 2)) |
| 2841 | return len1 + len2; |
| 2842 | return len1; |
| 2843 | } |
| 2844 | |
| 2845 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2846 | * a yield. This function keep the data in the buffer. |
| 2847 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2848 | __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] | 2849 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2850 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2851 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2852 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2853 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2854 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2855 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2856 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2857 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2858 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2859 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2860 | 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] | 2861 | return 1; |
| 2862 | } |
| 2863 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2864 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2865 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2866 | { |
| 2867 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2868 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2869 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2870 | } |
| 2871 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2872 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2873 | * a yield. This function consumes the data in the buffer. It returns |
| 2874 | * a string containing the data or a nil pointer if no data are available |
| 2875 | * and the channel is closed. |
| 2876 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2877 | __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] | 2878 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2879 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2880 | int ret; |
| 2881 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2882 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2883 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2884 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2885 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2886 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2887 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2888 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2889 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2891 | 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] | 2892 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2893 | if (unlikely(ret == -1)) |
| 2894 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2895 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2896 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | return 1; |
| 2898 | } |
| 2899 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2900 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2901 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2902 | { |
| 2903 | MAY_LJMP(check_args(L, 1, "get")); |
| 2904 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2905 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2906 | } |
| 2907 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | /* This functions consumes and returns one line. If the channel is closed, |
| 2909 | * 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] | 2910 | * 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] | 2911 | * value. |
| 2912 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2913 | __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] | 2914 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | char *blk1; |
| 2916 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2917 | size_t len1; |
| 2918 | size_t len2; |
| 2919 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2920 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2921 | int ret; |
| 2922 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2923 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2924 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2925 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2926 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2927 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2928 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2929 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2930 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2931 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2933 | 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] | 2934 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2935 | if (ret == -1) { |
| 2936 | lua_pushnil(L); |
| 2937 | return 1; |
| 2938 | } |
| 2939 | |
| 2940 | luaL_buffinit(L, &b); |
| 2941 | luaL_addlstring(&b, blk1, len1); |
| 2942 | len = len1; |
| 2943 | if (unlikely(ret == 2)) { |
| 2944 | luaL_addlstring(&b, blk2, len2); |
| 2945 | len += len2; |
| 2946 | } |
| 2947 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2948 | 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] | 2949 | return 1; |
| 2950 | } |
| 2951 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2952 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2953 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2954 | { |
| 2955 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2956 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2957 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2958 | } |
| 2959 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2960 | /* This function takes a string as input, and append it at the |
| 2961 | * input side of channel. If the data is too big, but a space |
| 2962 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2963 | * yields. If the data is bigger than the buffer, or if the |
| 2964 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2965 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2966 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2967 | __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] | 2968 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2969 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2970 | size_t len; |
| 2971 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2972 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2973 | int ret; |
| 2974 | int max; |
| 2975 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2976 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2977 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2978 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2979 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2980 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2981 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2982 | * the request buffer if its not required. |
| 2983 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2984 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2985 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2986 | 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] | 2987 | } |
| 2988 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2989 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | if (max > len - l) |
| 2991 | max = len - l; |
| 2992 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2993 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | if (ret == -2 || ret == -3) { |
| 2995 | lua_pushinteger(L, -1); |
| 2996 | return 1; |
| 2997 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2998 | if (ret == -1) { |
| 2999 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3000 | 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] | 3001 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | l += ret; |
| 3003 | lua_pop(L, 1); |
| 3004 | lua_pushinteger(L, l); |
| 3005 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3006 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3007 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3008 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3010 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | */ |
| 3012 | return 1; |
| 3013 | } |
| 3014 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3015 | 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] | 3016 | return 1; |
| 3017 | } |
| 3018 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3019 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3020 | * 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] | 3021 | * buffer size is too little for the data. |
| 3022 | */ |
| 3023 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3024 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3025 | size_t len; |
| 3026 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3027 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3028 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3029 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3030 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | lua_pushinteger(L, 0); |
| 3032 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3033 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | } |
| 3035 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3036 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3038 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3039 | * or -1 if the channel is closed or if the buffer size is too |
| 3040 | * little for the data. |
| 3041 | */ |
| 3042 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3043 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3044 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3045 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3046 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3047 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3048 | lua_pushinteger(L, 0); |
| 3049 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3050 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3051 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3052 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3053 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3054 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3055 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3056 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3057 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3058 | } |
| 3059 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3060 | /* Append data in the output side of the buffer. This data is immediately |
| 3061 | * sent. The function returns the amount of data written. If the buffer |
| 3062 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | * if the channel is closed. |
| 3064 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3065 | __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] | 3066 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3067 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | size_t len; |
| 3069 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3070 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3071 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3072 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3074 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3075 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3076 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3077 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3078 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3079 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3080 | lua_pushinteger(L, -1); |
| 3081 | return 1; |
| 3082 | } |
| 3083 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3084 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3085 | * the request buffer if its not required. |
| 3086 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3087 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3088 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3089 | 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] | 3090 | } |
| 3091 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3092 | /* The written data will be immediately sent, so we can check |
| 3093 | * the available space without taking in account the reserve. |
| 3094 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3095 | * data, because the buffer will be flushed. |
| 3096 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3097 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3098 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3099 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3100 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3101 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3102 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3103 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3104 | return 1; |
| 3105 | |
| 3106 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3107 | if (max > len - l) |
| 3108 | max = len - l; |
| 3109 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3110 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3111 | * detects a non contiguous buffer and realign it. |
| 3112 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3113 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3114 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3115 | |
| 3116 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3117 | 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] | 3118 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | /* buffer replace considers that the input part is filled. |
| 3120 | * so, I must forward these new data in the output part. |
| 3121 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3122 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | |
| 3124 | l += max; |
| 3125 | lua_pop(L, 1); |
| 3126 | lua_pushinteger(L, l); |
| 3127 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3128 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3129 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3130 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3131 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3132 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3133 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3134 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3135 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3136 | if (l < len) { |
| 3137 | /* If we are waiting for space in the response buffer, we |
| 3138 | * must set the flag WAKERESWR. This flag required the task |
| 3139 | * wake up if any activity is detected on the response buffer. |
| 3140 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3141 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3142 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3143 | else |
| 3144 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3145 | 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] | 3146 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3147 | |
| 3148 | return 1; |
| 3149 | } |
| 3150 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3151 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3152 | * yield the LUA process, and resume it without checking the |
| 3153 | * input arguments. |
| 3154 | */ |
| 3155 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3156 | { |
| 3157 | MAY_LJMP(check_args(L, 2, "send")); |
| 3158 | lua_pushinteger(L, 0); |
| 3159 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3160 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | /* This function forward and amount of butes. The data pass from |
| 3164 | * the input side of the buffer to the output side, and can be |
| 3165 | * forwarded. This function never fails. |
| 3166 | * |
| 3167 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3168 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3170 | __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] | 3171 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3172 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3173 | int len; |
| 3174 | int l; |
| 3175 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3176 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3177 | |
| 3178 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3179 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3180 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3181 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3182 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3183 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3184 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3185 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3186 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3187 | |
| 3188 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3189 | if (max > ci_data(chn)) |
| 3190 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3191 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3192 | l += max; |
| 3193 | |
| 3194 | lua_pop(L, 1); |
| 3195 | lua_pushinteger(L, l); |
| 3196 | |
| 3197 | /* Check if it miss bytes to forward. */ |
| 3198 | if (l < len) { |
| 3199 | /* The the input channel or the output channel are closed, we |
| 3200 | * must return the amount of data forwarded. |
| 3201 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3202 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3203 | return 1; |
| 3204 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3205 | /* If we are waiting for space data in the response buffer, we |
| 3206 | * must set the flag WAKERESWR. This flag required the task |
| 3207 | * wake up if any activity is detected on the response buffer. |
| 3208 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3209 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3210 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3211 | else |
| 3212 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3213 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3214 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3215 | 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] | 3216 | } |
| 3217 | |
| 3218 | return 1; |
| 3219 | } |
| 3220 | |
| 3221 | /* Just check the input and prepare the stack for the previous |
| 3222 | * function "hlua_channel_forward_yield" |
| 3223 | */ |
| 3224 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3225 | { |
| 3226 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3227 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3228 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3229 | |
| 3230 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3231 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | /* Just returns the number of bytes available in the input |
| 3235 | * side of the buffer. This function never fails. |
| 3236 | */ |
| 3237 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3238 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3239 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3240 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3241 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3242 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3243 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3244 | struct htx *htx = htxbuf(&chn->buf); |
| 3245 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3246 | } |
| 3247 | else |
| 3248 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3249 | return 1; |
| 3250 | } |
| 3251 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3252 | /* Returns true if the channel is full. */ |
| 3253 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3254 | { |
| 3255 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3256 | |
| 3257 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3258 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3259 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3260 | * applet). |
| 3261 | */ |
| 3262 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3263 | return 1; |
| 3264 | } |
| 3265 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3266 | /* Returns true if the channel is the response channel. */ |
| 3267 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3268 | { |
| 3269 | struct channel *chn; |
| 3270 | |
| 3271 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3272 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3273 | |
| 3274 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3275 | return 1; |
| 3276 | } |
| 3277 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3278 | /* Just returns the number of bytes available in the output |
| 3279 | * side of the buffer. This function never fails. |
| 3280 | */ |
| 3281 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3282 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3283 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3284 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3285 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3286 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3287 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3288 | return 1; |
| 3289 | } |
| 3290 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3291 | /* |
| 3292 | * |
| 3293 | * |
| 3294 | * Class Fetches |
| 3295 | * |
| 3296 | * |
| 3297 | */ |
| 3298 | |
| 3299 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3300 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3301 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3302 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3303 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3304 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | /* This function creates and push in the stack a fetch object according |
| 3308 | * with a current TXN. |
| 3309 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3310 | 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] | 3311 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3312 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3313 | |
| 3314 | /* Check stack size. */ |
| 3315 | if (!lua_checkstack(L, 3)) |
| 3316 | return 0; |
| 3317 | |
| 3318 | /* Create the object: obj[0] = userdata. |
| 3319 | * Note that the base of the Fetches object is the |
| 3320 | * transaction object. |
| 3321 | */ |
| 3322 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3323 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3324 | lua_rawseti(L, -2, 0); |
| 3325 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3326 | hsmp->s = txn->s; |
| 3327 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3328 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3329 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3330 | |
| 3331 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3332 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3333 | lua_setmetatable(L, -2); |
| 3334 | |
| 3335 | return 1; |
| 3336 | } |
| 3337 | |
| 3338 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3339 | * It uses closure argument to store the associated sample-fetch. It |
| 3340 | * returns only one argument or throws an error. An error is thrown |
| 3341 | * only if an error is encountered during the argument parsing. If |
| 3342 | * the "sample-fetch" function fails, nil is returned. |
| 3343 | */ |
| 3344 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3345 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3346 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3347 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3348 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3349 | int i; |
| 3350 | struct sample smp; |
| 3351 | |
| 3352 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3353 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3354 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3355 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3356 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3357 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3358 | /* Check execution authorization. */ |
| 3359 | if (f->use & SMP_USE_HTTP_ANY && |
| 3360 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3361 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3362 | "is not available in Lua services", f->kw); |
| 3363 | WILL_LJMP(lua_error(L)); |
| 3364 | } |
| 3365 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3366 | /* Get extra arguments. */ |
| 3367 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3368 | if (i >= ARGM_NBARGS) |
| 3369 | break; |
| 3370 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3371 | } |
| 3372 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3373 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3374 | |
| 3375 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3376 | 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] | 3377 | |
| 3378 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3379 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3380 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3381 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | /* Initialise the sample. */ |
| 3385 | memset(&smp, 0, sizeof(smp)); |
| 3386 | |
| 3387 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3388 | 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] | 3389 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3390 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3391 | lua_pushstring(L, ""); |
| 3392 | else |
| 3393 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3394 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3398 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3399 | hlua_smp2lua_str(L, &smp); |
| 3400 | else |
| 3401 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3402 | |
| 3403 | end: |
| 3404 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3405 | if (args[i].type == ARGT_STR) |
| 3406 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3407 | else if (args[i].type == ARGT_REG) |
| 3408 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3409 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3410 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3411 | |
| 3412 | error: |
| 3413 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3414 | if (args[i].type == ARGT_STR) |
| 3415 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3416 | else if (args[i].type == ARGT_REG) |
| 3417 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3418 | } |
| 3419 | WILL_LJMP(lua_error(L)); |
| 3420 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3421 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3422 | |
| 3423 | /* |
| 3424 | * |
| 3425 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3426 | * Class Converters |
| 3427 | * |
| 3428 | * |
| 3429 | */ |
| 3430 | |
| 3431 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3432 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3433 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3434 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3435 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3436 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | /* This function creates and push in the stack a Converters object |
| 3440 | * according with a current TXN. |
| 3441 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3442 | 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] | 3443 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3444 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3445 | |
| 3446 | /* Check stack size. */ |
| 3447 | if (!lua_checkstack(L, 3)) |
| 3448 | return 0; |
| 3449 | |
| 3450 | /* Create the object: obj[0] = userdata. |
| 3451 | * Note that the base of the Converters object is the |
| 3452 | * same than the TXN object. |
| 3453 | */ |
| 3454 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3455 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3456 | lua_rawseti(L, -2, 0); |
| 3457 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3458 | hsmp->s = txn->s; |
| 3459 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3460 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3461 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3462 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3463 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3464 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3465 | lua_setmetatable(L, -2); |
| 3466 | |
| 3467 | return 1; |
| 3468 | } |
| 3469 | |
| 3470 | /* This function is an LUA binding. It is called with each converter. |
| 3471 | * It uses closure argument to store the associated converter. It |
| 3472 | * returns only one argument or throws an error. An error is thrown |
| 3473 | * only if an error is encountered during the argument parsing. If |
| 3474 | * the converter function function fails, nil is returned. |
| 3475 | */ |
| 3476 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3477 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3478 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3479 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3480 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3481 | int i; |
| 3482 | struct sample smp; |
| 3483 | |
| 3484 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3485 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3486 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3487 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3488 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3489 | |
| 3490 | /* Get extra arguments. */ |
| 3491 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3492 | if (i >= ARGM_NBARGS) |
| 3493 | break; |
| 3494 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3495 | } |
| 3496 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3497 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3498 | |
| 3499 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3500 | 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] | 3501 | |
| 3502 | /* Run the special args checker. */ |
| 3503 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3504 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3505 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3509 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3510 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3511 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3512 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3513 | } |
| 3514 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3515 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3516 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3517 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3518 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3519 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3520 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3521 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3522 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3523 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3524 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3525 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3526 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3530 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3531 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3532 | lua_pushstring(L, ""); |
| 3533 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3534 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3535 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3539 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3540 | hlua_smp2lua_str(L, &smp); |
| 3541 | else |
| 3542 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3543 | end: |
| 3544 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3545 | if (args[i].type == ARGT_STR) |
| 3546 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3547 | else if (args[i].type == ARGT_REG) |
| 3548 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3549 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3550 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3551 | |
| 3552 | error: |
| 3553 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3554 | if (args[i].type == ARGT_STR) |
| 3555 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3556 | else if (args[i].type == ARGT_REG) |
| 3557 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3558 | } |
| 3559 | WILL_LJMP(lua_error(L)); |
| 3560 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3561 | } |
| 3562 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3563 | /* |
| 3564 | * |
| 3565 | * |
| 3566 | * Class AppletTCP |
| 3567 | * |
| 3568 | * |
| 3569 | */ |
| 3570 | |
| 3571 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3572 | * a class stream, otherwise it throws an error. |
| 3573 | */ |
| 3574 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3575 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3576 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | /* This function creates and push in the stack an Applet object |
| 3580 | * according with a current TXN. |
| 3581 | */ |
| 3582 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3583 | { |
| 3584 | struct hlua_appctx *appctx; |
| 3585 | struct stream_interface *si = ctx->owner; |
| 3586 | struct stream *s = si_strm(si); |
| 3587 | struct proxy *p = s->be; |
| 3588 | |
| 3589 | /* Check stack size. */ |
| 3590 | if (!lua_checkstack(L, 3)) |
| 3591 | return 0; |
| 3592 | |
| 3593 | /* Create the object: obj[0] = userdata. |
| 3594 | * Note that the base of the Converters object is the |
| 3595 | * same than the TXN object. |
| 3596 | */ |
| 3597 | lua_newtable(L); |
| 3598 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3599 | lua_rawseti(L, -2, 0); |
| 3600 | appctx->appctx = ctx; |
| 3601 | appctx->htxn.s = s; |
| 3602 | appctx->htxn.p = p; |
| 3603 | |
| 3604 | /* Create the "f" field that contains a list of fetches. */ |
| 3605 | lua_pushstring(L, "f"); |
| 3606 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3607 | return 0; |
| 3608 | lua_settable(L, -3); |
| 3609 | |
| 3610 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3611 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3612 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3613 | return 0; |
| 3614 | lua_settable(L, -3); |
| 3615 | |
| 3616 | /* Create the "c" field that contains a list of converters. */ |
| 3617 | lua_pushstring(L, "c"); |
| 3618 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3619 | return 0; |
| 3620 | lua_settable(L, -3); |
| 3621 | |
| 3622 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3623 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3624 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3625 | return 0; |
| 3626 | lua_settable(L, -3); |
| 3627 | |
| 3628 | /* Pop a class stream metatable and affect it to the table. */ |
| 3629 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3630 | lua_setmetatable(L, -2); |
| 3631 | |
| 3632 | return 1; |
| 3633 | } |
| 3634 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3635 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3636 | { |
| 3637 | struct hlua_appctx *appctx; |
| 3638 | struct stream *s; |
| 3639 | const char *name; |
| 3640 | size_t len; |
| 3641 | struct sample smp; |
| 3642 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3643 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3644 | 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] | 3645 | |
| 3646 | /* It is useles to retrieve the stream, but this function |
| 3647 | * runs only in a stream context. |
| 3648 | */ |
| 3649 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3650 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3651 | s = appctx->htxn.s; |
| 3652 | |
| 3653 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3654 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3655 | hlua_lua2smp(L, 3, &smp); |
| 3656 | |
| 3657 | /* Store the sample in a variable. */ |
| 3658 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3659 | |
| 3660 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3661 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3662 | else |
| 3663 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3664 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3665 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3669 | { |
| 3670 | struct hlua_appctx *appctx; |
| 3671 | struct stream *s; |
| 3672 | const char *name; |
| 3673 | size_t len; |
| 3674 | struct sample smp; |
| 3675 | |
| 3676 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3677 | |
| 3678 | /* It is useles to retrieve the stream, but this function |
| 3679 | * runs only in a stream context. |
| 3680 | */ |
| 3681 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3682 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3683 | s = appctx->htxn.s; |
| 3684 | |
| 3685 | /* Unset the variable. */ |
| 3686 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3687 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3688 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3692 | { |
| 3693 | struct hlua_appctx *appctx; |
| 3694 | struct stream *s; |
| 3695 | const char *name; |
| 3696 | size_t len; |
| 3697 | struct sample smp; |
| 3698 | |
| 3699 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3700 | |
| 3701 | /* It is useles to retrieve the stream, but this function |
| 3702 | * runs only in a stream context. |
| 3703 | */ |
| 3704 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3705 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3706 | s = appctx->htxn.s; |
| 3707 | |
| 3708 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3709 | if (!vars_get_by_name(name, len, &smp)) { |
| 3710 | lua_pushnil(L); |
| 3711 | return 1; |
| 3712 | } |
| 3713 | |
| 3714 | return hlua_smp2lua(L, &smp); |
| 3715 | } |
| 3716 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3717 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3718 | { |
| 3719 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3720 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3721 | struct hlua *hlua; |
| 3722 | |
| 3723 | /* 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] | 3724 | if (!s->hlua) |
| 3725 | return 0; |
| 3726 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3727 | |
| 3728 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3729 | |
| 3730 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3731 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3732 | |
| 3733 | /* Get and store new value. */ |
| 3734 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3735 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3736 | |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
| 3740 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3741 | { |
| 3742 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3743 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3744 | struct hlua *hlua; |
| 3745 | |
| 3746 | /* 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] | 3747 | if (!s->hlua) { |
| 3748 | lua_pushnil(L); |
| 3749 | return 1; |
| 3750 | } |
| 3751 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3752 | |
| 3753 | /* Push configuration index in the stack. */ |
| 3754 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3755 | |
| 3756 | return 1; |
| 3757 | } |
| 3758 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3759 | /* If expected data not yet available, it returns a yield. This function |
| 3760 | * consumes the data in the buffer. It returns a string containing the |
| 3761 | * data. This string can be empty. |
| 3762 | */ |
| 3763 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3764 | { |
| 3765 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3766 | struct stream_interface *si = appctx->appctx->owner; |
| 3767 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3768 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3769 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3770 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3771 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3772 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3773 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3774 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3775 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3776 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3777 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3778 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3779 | 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] | 3780 | } |
| 3781 | |
| 3782 | /* End of data: commit the total strings and return. */ |
| 3783 | if (ret < 0) { |
| 3784 | luaL_pushresult(&appctx->b); |
| 3785 | return 1; |
| 3786 | } |
| 3787 | |
| 3788 | /* Ensure that the block 2 length is usable. */ |
| 3789 | if (ret == 1) |
| 3790 | len2 = 0; |
| 3791 | |
| 3792 | /* dont check the max length read and dont check. */ |
| 3793 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3794 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3795 | |
| 3796 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3797 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3798 | luaL_pushresult(&appctx->b); |
| 3799 | return 1; |
| 3800 | } |
| 3801 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3802 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3803 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3804 | { |
| 3805 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3806 | |
| 3807 | /* Initialise the string catenation. */ |
| 3808 | luaL_buffinit(L, &appctx->b); |
| 3809 | |
| 3810 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3811 | } |
| 3812 | |
| 3813 | /* If expected data not yet available, it returns a yield. This function |
| 3814 | * consumes the data in the buffer. It returns a string containing the |
| 3815 | * data. This string can be empty. |
| 3816 | */ |
| 3817 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3818 | { |
| 3819 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3820 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3821 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3822 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3823 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3824 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3825 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3826 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3827 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3828 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3829 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3830 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3831 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3832 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3833 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3834 | 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] | 3835 | } |
| 3836 | |
| 3837 | /* End of data: commit the total strings and return. */ |
| 3838 | if (ret < 0) { |
| 3839 | luaL_pushresult(&appctx->b); |
| 3840 | return 1; |
| 3841 | } |
| 3842 | |
| 3843 | /* Ensure that the block 2 length is usable. */ |
| 3844 | if (ret == 1) |
| 3845 | len2 = 0; |
| 3846 | |
| 3847 | if (len == -1) { |
| 3848 | |
| 3849 | /* If len == -1, catenate all the data avalaile and |
| 3850 | * yield because we want to get all the data until |
| 3851 | * the end of data stream. |
| 3852 | */ |
| 3853 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3854 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3855 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3856 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3857 | 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] | 3858 | |
| 3859 | } else { |
| 3860 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3861 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3862 | if (len1 > len) |
| 3863 | len1 = len; |
| 3864 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3865 | len -= len1; |
| 3866 | |
| 3867 | /* Copy the second block. */ |
| 3868 | if (len2 > len) |
| 3869 | len2 = len; |
| 3870 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3871 | len -= len2; |
| 3872 | |
| 3873 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3874 | co_skip(si_oc(si), len1 + 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 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3877 | if (len > 0) { |
| 3878 | lua_pushinteger(L, len); |
| 3879 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3880 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3881 | 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] | 3882 | } |
| 3883 | |
| 3884 | /* return the result. */ |
| 3885 | luaL_pushresult(&appctx->b); |
| 3886 | return 1; |
| 3887 | } |
| 3888 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3889 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3890 | hlua_pusherror(L, "Lua: internal error"); |
| 3891 | WILL_LJMP(lua_error(L)); |
| 3892 | return 0; |
| 3893 | } |
| 3894 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3895 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3896 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3897 | { |
| 3898 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3899 | int len = -1; |
| 3900 | |
| 3901 | if (lua_gettop(L) > 2) |
| 3902 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3903 | if (lua_gettop(L) >= 2) { |
| 3904 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3905 | lua_pop(L, 1); |
| 3906 | } |
| 3907 | |
| 3908 | /* Confirm or set the required length */ |
| 3909 | lua_pushinteger(L, len); |
| 3910 | |
| 3911 | /* Initialise the string catenation. */ |
| 3912 | luaL_buffinit(L, &appctx->b); |
| 3913 | |
| 3914 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3915 | } |
| 3916 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3917 | /* Append data in the output side of the buffer. This data is immediately |
| 3918 | * sent. The function returns the amount of data written. If the buffer |
| 3919 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3920 | * if the channel is closed. |
| 3921 | */ |
| 3922 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3923 | { |
| 3924 | size_t len; |
| 3925 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3926 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3927 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3928 | struct stream_interface *si = appctx->appctx->owner; |
| 3929 | struct channel *chn = si_ic(si); |
| 3930 | int max; |
| 3931 | |
| 3932 | /* Get the max amount of data which can write as input in the channel. */ |
| 3933 | max = channel_recv_max(chn); |
| 3934 | if (max > (len - l)) |
| 3935 | max = len - l; |
| 3936 | |
| 3937 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3938 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3939 | |
| 3940 | /* update counters. */ |
| 3941 | l += max; |
| 3942 | lua_pop(L, 1); |
| 3943 | lua_pushinteger(L, l); |
| 3944 | |
| 3945 | /* If some data is not send, declares the situation to the |
| 3946 | * applet, and returns a yield. |
| 3947 | */ |
| 3948 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3949 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3950 | 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] | 3951 | } |
| 3952 | |
| 3953 | return 1; |
| 3954 | } |
| 3955 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3956 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3957 | * yield the LUA process, and resume it without checking the |
| 3958 | * input arguments. |
| 3959 | */ |
| 3960 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3961 | { |
| 3962 | MAY_LJMP(check_args(L, 2, "send")); |
| 3963 | lua_pushinteger(L, 0); |
| 3964 | |
| 3965 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3966 | } |
| 3967 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3968 | /* |
| 3969 | * |
| 3970 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3971 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3972 | * |
| 3973 | * |
| 3974 | */ |
| 3975 | |
| 3976 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3977 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3978 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3979 | __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] | 3980 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3981 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3982 | } |
| 3983 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3984 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3985 | * according with a current TXN. |
| 3986 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3987 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3988 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3989 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3990 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3991 | struct stream_interface *si = ctx->owner; |
| 3992 | struct stream *s = si_strm(si); |
| 3993 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3994 | struct htx *htx; |
| 3995 | struct htx_blk *blk; |
| 3996 | struct htx_sl *sl; |
| 3997 | struct ist path; |
| 3998 | unsigned long long len = 0; |
| 3999 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4000 | |
| 4001 | /* Check stack size. */ |
| 4002 | if (!lua_checkstack(L, 3)) |
| 4003 | return 0; |
| 4004 | |
| 4005 | /* Create the object: obj[0] = userdata. |
| 4006 | * Note that the base of the Converters object is the |
| 4007 | * same than the TXN object. |
| 4008 | */ |
| 4009 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4010 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4011 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4012 | appctx->appctx = ctx; |
| 4013 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4014 | 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] | 4015 | appctx->htxn.s = s; |
| 4016 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4017 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4018 | /* Create the "f" field that contains a list of fetches. */ |
| 4019 | lua_pushstring(L, "f"); |
| 4020 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4021 | return 0; |
| 4022 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4023 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4024 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4025 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4026 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4027 | return 0; |
| 4028 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4029 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4030 | /* Create the "c" field that contains a list of converters. */ |
| 4031 | lua_pushstring(L, "c"); |
| 4032 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4033 | return 0; |
| 4034 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4035 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4036 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4037 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4038 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4039 | return 0; |
| 4040 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4041 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4042 | htx = htxbuf(&s->req.buf); |
| 4043 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4044 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4045 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4046 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4047 | /* Stores the request method. */ |
| 4048 | lua_pushstring(L, "method"); |
| 4049 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4050 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4051 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4052 | /* Stores the http version. */ |
| 4053 | lua_pushstring(L, "version"); |
| 4054 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4055 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4056 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4057 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4058 | * the array on the top of the stack. |
| 4059 | */ |
| 4060 | lua_pushstring(L, "headers"); |
| 4061 | htxn.s = s; |
| 4062 | htxn.p = px; |
| 4063 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4064 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4065 | return 0; |
| 4066 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4067 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4068 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4069 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4070 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4071 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4072 | p = path.ptr; |
| 4073 | end = path.ptr + path.len; |
| 4074 | q = p; |
| 4075 | while (q < end && *q != '?') |
| 4076 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4077 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4078 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4079 | lua_pushstring(L, "path"); |
| 4080 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4081 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4082 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4083 | /* Stores the query string. */ |
| 4084 | lua_pushstring(L, "qs"); |
| 4085 | if (*q == '?') |
| 4086 | q++; |
| 4087 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4088 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4089 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4090 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4091 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4092 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4093 | enum htx_blk_type type = htx_get_blk_type(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 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 4096 | break; |
| 4097 | if (type == HTX_BLK_DATA) |
| 4098 | len += htx_get_blksz(blk); |
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 | if (htx->extra != ULLONG_MAX) |
| 4101 | len += htx->extra; |
| 4102 | |
| 4103 | /* Stores the request path. */ |
| 4104 | lua_pushstring(L, "length"); |
| 4105 | lua_pushinteger(L, len); |
| 4106 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4107 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4108 | /* Create an empty array of HTTP request headers. */ |
| 4109 | lua_pushstring(L, "response"); |
| 4110 | lua_newtable(L); |
| 4111 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4112 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4113 | /* Pop a class stream metatable and affect it to the table. */ |
| 4114 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4115 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4116 | |
| 4117 | return 1; |
| 4118 | } |
| 4119 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4120 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4121 | { |
| 4122 | struct hlua_appctx *appctx; |
| 4123 | struct stream *s; |
| 4124 | const char *name; |
| 4125 | size_t len; |
| 4126 | struct sample smp; |
| 4127 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4128 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4129 | 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] | 4130 | |
| 4131 | /* It is useles to retrieve the stream, but this function |
| 4132 | * runs only in a stream context. |
| 4133 | */ |
| 4134 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4135 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4136 | s = appctx->htxn.s; |
| 4137 | |
| 4138 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4139 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4140 | hlua_lua2smp(L, 3, &smp); |
| 4141 | |
| 4142 | /* Store the sample in a variable. */ |
| 4143 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4144 | |
| 4145 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4146 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4147 | else |
| 4148 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4149 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4150 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4154 | { |
| 4155 | struct hlua_appctx *appctx; |
| 4156 | struct stream *s; |
| 4157 | const char *name; |
| 4158 | size_t len; |
| 4159 | struct sample smp; |
| 4160 | |
| 4161 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4162 | |
| 4163 | /* It is useles to retrieve the stream, but this function |
| 4164 | * runs only in a stream context. |
| 4165 | */ |
| 4166 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4167 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4168 | s = appctx->htxn.s; |
| 4169 | |
| 4170 | /* Unset the variable. */ |
| 4171 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4172 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4173 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4174 | } |
| 4175 | |
| 4176 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4177 | { |
| 4178 | struct hlua_appctx *appctx; |
| 4179 | struct stream *s; |
| 4180 | const char *name; |
| 4181 | size_t len; |
| 4182 | struct sample smp; |
| 4183 | |
| 4184 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4185 | |
| 4186 | /* It is useles to retrieve the stream, but this function |
| 4187 | * runs only in a stream context. |
| 4188 | */ |
| 4189 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4190 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4191 | s = appctx->htxn.s; |
| 4192 | |
| 4193 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4194 | if (!vars_get_by_name(name, len, &smp)) { |
| 4195 | lua_pushnil(L); |
| 4196 | return 1; |
| 4197 | } |
| 4198 | |
| 4199 | return hlua_smp2lua(L, &smp); |
| 4200 | } |
| 4201 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4202 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4203 | { |
| 4204 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4205 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4206 | struct hlua *hlua; |
| 4207 | |
| 4208 | /* 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] | 4209 | if (!s->hlua) |
| 4210 | return 0; |
| 4211 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4212 | |
| 4213 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4214 | |
| 4215 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4216 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4217 | |
| 4218 | /* Get and store new value. */ |
| 4219 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4220 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4221 | |
| 4222 | return 0; |
| 4223 | } |
| 4224 | |
| 4225 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4226 | { |
| 4227 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4228 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4229 | struct hlua *hlua; |
| 4230 | |
| 4231 | /* 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] | 4232 | if (!s->hlua) { |
| 4233 | lua_pushnil(L); |
| 4234 | return 1; |
| 4235 | } |
| 4236 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4237 | |
| 4238 | /* Push configuration index in the stack. */ |
| 4239 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4240 | |
| 4241 | return 1; |
| 4242 | } |
| 4243 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4244 | /* If expected data not yet available, it returns a yield. This function |
| 4245 | * consumes the data in the buffer. It returns a string containing the |
| 4246 | * data. This string can be empty. |
| 4247 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4248 | __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] | 4249 | { |
| 4250 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4251 | struct stream_interface *si = appctx->appctx->owner; |
| 4252 | struct channel *req = si_oc(si); |
| 4253 | struct htx *htx; |
| 4254 | struct htx_blk *blk; |
| 4255 | size_t count; |
| 4256 | int stop = 0; |
| 4257 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4258 | htx = htx_from_buf(&req->buf); |
| 4259 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4260 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4261 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4262 | while (count && !stop && blk) { |
| 4263 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4264 | uint32_t sz = htx_get_blksz(blk); |
| 4265 | struct ist v; |
| 4266 | uint32_t vlen; |
| 4267 | char *nl; |
| 4268 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4269 | if (type == HTX_BLK_EOM) { |
| 4270 | stop = 1; |
| 4271 | break; |
| 4272 | } |
| 4273 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4274 | vlen = sz; |
| 4275 | if (vlen > count) { |
| 4276 | if (type != HTX_BLK_DATA) |
| 4277 | break; |
| 4278 | vlen = count; |
| 4279 | } |
| 4280 | |
| 4281 | switch (type) { |
| 4282 | case HTX_BLK_UNUSED: |
| 4283 | break; |
| 4284 | |
| 4285 | case HTX_BLK_DATA: |
| 4286 | v = htx_get_blk_value(htx, blk); |
| 4287 | v.len = vlen; |
| 4288 | nl = istchr(v, '\n'); |
| 4289 | if (nl != NULL) { |
| 4290 | stop = 1; |
| 4291 | vlen = nl - v.ptr + 1; |
| 4292 | } |
| 4293 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4294 | break; |
| 4295 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4296 | case HTX_BLK_TLR: |
| 4297 | case HTX_BLK_EOM: |
| 4298 | stop = 1; |
| 4299 | break; |
| 4300 | |
| 4301 | default: |
| 4302 | break; |
| 4303 | } |
| 4304 | |
| 4305 | co_set_data(req, co_data(req) - vlen); |
| 4306 | count -= vlen; |
| 4307 | if (sz == vlen) |
| 4308 | blk = htx_remove_blk(htx, blk); |
| 4309 | else { |
| 4310 | htx_cut_data_blk(htx, blk, vlen); |
| 4311 | break; |
| 4312 | } |
| 4313 | } |
| 4314 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4315 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4316 | if (!stop) { |
| 4317 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4318 | 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] | 4319 | } |
| 4320 | |
| 4321 | /* return the result. */ |
| 4322 | luaL_pushresult(&appctx->b); |
| 4323 | return 1; |
| 4324 | } |
| 4325 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4326 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4327 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4328 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4329 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4330 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4331 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4332 | /* Initialise the string catenation. */ |
| 4333 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4334 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4335 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4336 | } |
| 4337 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4338 | /* If expected data not yet available, it returns a yield. This function |
| 4339 | * consumes the data in the buffer. It returns a string containing the |
| 4340 | * data. This string can be empty. |
| 4341 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4342 | __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] | 4343 | { |
| 4344 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4345 | struct stream_interface *si = appctx->appctx->owner; |
| 4346 | struct channel *req = si_oc(si); |
| 4347 | struct htx *htx; |
| 4348 | struct htx_blk *blk; |
| 4349 | size_t count; |
| 4350 | int len; |
| 4351 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4352 | htx = htx_from_buf(&req->buf); |
| 4353 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4354 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4355 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4356 | while (count && len && blk) { |
| 4357 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4358 | uint32_t sz = htx_get_blksz(blk); |
| 4359 | struct ist v; |
| 4360 | uint32_t vlen; |
| 4361 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4362 | if (type == HTX_BLK_EOM) { |
| 4363 | len = 0; |
| 4364 | break; |
| 4365 | } |
| 4366 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4367 | vlen = sz; |
| 4368 | if (len > 0 && vlen > len) |
| 4369 | vlen = len; |
| 4370 | if (vlen > count) { |
| 4371 | if (type != HTX_BLK_DATA) |
| 4372 | break; |
| 4373 | vlen = count; |
| 4374 | } |
| 4375 | |
| 4376 | switch (type) { |
| 4377 | case HTX_BLK_UNUSED: |
| 4378 | break; |
| 4379 | |
| 4380 | case HTX_BLK_DATA: |
| 4381 | v = htx_get_blk_value(htx, blk); |
| 4382 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4383 | break; |
| 4384 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4385 | case HTX_BLK_TLR: |
| 4386 | case HTX_BLK_EOM: |
| 4387 | len = 0; |
| 4388 | break; |
| 4389 | |
| 4390 | default: |
| 4391 | break; |
| 4392 | } |
| 4393 | |
| 4394 | co_set_data(req, co_data(req) - vlen); |
| 4395 | count -= vlen; |
| 4396 | if (len > 0) |
| 4397 | len -= vlen; |
| 4398 | if (sz == vlen) |
| 4399 | blk = htx_remove_blk(htx, blk); |
| 4400 | else { |
| 4401 | htx_cut_data_blk(htx, blk, vlen); |
| 4402 | break; |
| 4403 | } |
| 4404 | } |
| 4405 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4406 | htx_to_buf(htx, &req->buf); |
| 4407 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4408 | /* If we are no other data available, yield waiting for new data. */ |
| 4409 | if (len) { |
| 4410 | if (len > 0) { |
| 4411 | lua_pushinteger(L, len); |
| 4412 | lua_replace(L, 2); |
| 4413 | } |
| 4414 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4415 | 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] | 4416 | } |
| 4417 | |
| 4418 | /* return the result. */ |
| 4419 | luaL_pushresult(&appctx->b); |
| 4420 | return 1; |
| 4421 | } |
| 4422 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4423 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4424 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4425 | { |
| 4426 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4427 | int len = -1; |
| 4428 | |
| 4429 | /* Check arguments. */ |
| 4430 | if (lua_gettop(L) > 2) |
| 4431 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4432 | if (lua_gettop(L) >= 2) { |
| 4433 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4434 | lua_pop(L, 1); |
| 4435 | } |
| 4436 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4437 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4438 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4439 | /* Initialise the string catenation. */ |
| 4440 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4441 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4442 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | /* Append data in the output side of the buffer. This data is immediately |
| 4446 | * sent. The function returns the amount of data written. If the buffer |
| 4447 | * cannot contain the data, the function yields. The function returns -1 |
| 4448 | * if the channel is closed. |
| 4449 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4450 | __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] | 4451 | { |
| 4452 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4453 | struct stream_interface *si = appctx->appctx->owner; |
| 4454 | struct channel *res = si_ic(si); |
| 4455 | struct htx *htx = htx_from_buf(&res->buf); |
| 4456 | const char *data; |
| 4457 | size_t len; |
| 4458 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4459 | int max; |
| 4460 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4461 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4462 | if (!max) |
| 4463 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4464 | |
| 4465 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4466 | |
| 4467 | /* Get the max amount of data which can write as input in the channel. */ |
| 4468 | if (max > (len - l)) |
| 4469 | max = len - l; |
| 4470 | |
| 4471 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4472 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4473 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4474 | |
| 4475 | /* update counters. */ |
| 4476 | l += max; |
| 4477 | lua_pop(L, 1); |
| 4478 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4479 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4480 | /* If some data is not send, declares the situation to the |
| 4481 | * applet, and returns a yield. |
| 4482 | */ |
| 4483 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4484 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4485 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4486 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4487 | 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] | 4488 | } |
| 4489 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4490 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4491 | return 1; |
| 4492 | } |
| 4493 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4494 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4495 | * yield the LUA process, and resume it without checking the |
| 4496 | * input arguments. |
| 4497 | */ |
| 4498 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4499 | { |
| 4500 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4501 | |
| 4502 | /* We want to send some data. Headers must be sent. */ |
| 4503 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4504 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4505 | WILL_LJMP(lua_error(L)); |
| 4506 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4507 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4508 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4509 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4510 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4511 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4515 | { |
| 4516 | const char *name; |
| 4517 | int ret; |
| 4518 | |
| 4519 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4520 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4521 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4522 | |
| 4523 | /* Push in the stack the "response" entry. */ |
| 4524 | ret = lua_getfield(L, 1, "response"); |
| 4525 | if (ret != LUA_TTABLE) { |
| 4526 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4527 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4528 | WILL_LJMP(lua_error(L)); |
| 4529 | } |
| 4530 | |
| 4531 | /* check if the header is already registered if it is not |
| 4532 | * the case, register it. |
| 4533 | */ |
| 4534 | ret = lua_getfield(L, -1, name); |
| 4535 | if (ret == LUA_TNIL) { |
| 4536 | |
| 4537 | /* Entry not found. */ |
| 4538 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4539 | |
| 4540 | /* Insert the new header name in the array in the top of the stack. |
| 4541 | * It left the new array in the top of the stack. |
| 4542 | */ |
| 4543 | lua_newtable(L); |
| 4544 | lua_pushvalue(L, 2); |
| 4545 | lua_pushvalue(L, -2); |
| 4546 | lua_settable(L, -4); |
| 4547 | |
| 4548 | } else if (ret != LUA_TTABLE) { |
| 4549 | |
| 4550 | /* corruption error. */ |
| 4551 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4552 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4553 | WILL_LJMP(lua_error(L)); |
| 4554 | } |
| 4555 | |
| 4556 | /* Now the top od thestack is an array of values. We push |
| 4557 | * the header value as new entry. |
| 4558 | */ |
| 4559 | lua_pushvalue(L, 3); |
| 4560 | ret = lua_rawlen(L, -2); |
| 4561 | lua_rawseti(L, -2, ret + 1); |
| 4562 | lua_pushboolean(L, 1); |
| 4563 | return 1; |
| 4564 | } |
| 4565 | |
| 4566 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4567 | { |
| 4568 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4569 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4570 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4571 | |
| 4572 | if (status < 100 || status > 599) { |
| 4573 | lua_pushboolean(L, 0); |
| 4574 | return 1; |
| 4575 | } |
| 4576 | |
| 4577 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4578 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4579 | lua_pushboolean(L, 1); |
| 4580 | return 1; |
| 4581 | } |
| 4582 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4583 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4584 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4585 | { |
| 4586 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4587 | struct stream_interface *si = appctx->appctx->owner; |
| 4588 | struct channel *res = si_ic(si); |
| 4589 | struct htx *htx; |
| 4590 | struct htx_sl *sl; |
| 4591 | struct h1m h1m; |
| 4592 | const char *status, *reason; |
| 4593 | const char *name, *value; |
| 4594 | size_t nlen, vlen; |
| 4595 | unsigned int flags; |
| 4596 | |
| 4597 | /* Send the message at once. */ |
| 4598 | htx = htx_from_buf(&res->buf); |
| 4599 | h1m_init_res(&h1m); |
| 4600 | |
| 4601 | /* Use the same http version than the request. */ |
| 4602 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4603 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4604 | if (reason == NULL) |
| 4605 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4606 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4607 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4608 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4609 | } |
| 4610 | else { |
| 4611 | flags = HTX_SL_F_IS_RESP; |
| 4612 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4613 | } |
| 4614 | if (!sl) { |
| 4615 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4616 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4617 | WILL_LJMP(lua_error(L)); |
| 4618 | } |
| 4619 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4620 | |
| 4621 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4622 | lua_pushvalue(L, 0); |
| 4623 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4624 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4625 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4626 | WILL_LJMP(lua_error(L)); |
| 4627 | } |
| 4628 | |
| 4629 | /* Browse the list of headers. */ |
| 4630 | lua_pushnil(L); |
| 4631 | while(lua_next(L, -2) != 0) { |
| 4632 | /* We expect a string as -2. */ |
| 4633 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4634 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4635 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4636 | lua_typename(L, lua_type(L, -2))); |
| 4637 | WILL_LJMP(lua_error(L)); |
| 4638 | } |
| 4639 | name = lua_tolstring(L, -2, &nlen); |
| 4640 | |
| 4641 | /* We expect an array as -1. */ |
| 4642 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4643 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4644 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4645 | name, |
| 4646 | lua_typename(L, lua_type(L, -1))); |
| 4647 | WILL_LJMP(lua_error(L)); |
| 4648 | } |
| 4649 | |
| 4650 | /* Browse the table who is on the top of the stack. */ |
| 4651 | lua_pushnil(L); |
| 4652 | while(lua_next(L, -2) != 0) { |
| 4653 | int id; |
| 4654 | |
| 4655 | /* We expect a number as -2. */ |
| 4656 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4657 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4658 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4659 | name, |
| 4660 | lua_typename(L, lua_type(L, -2))); |
| 4661 | WILL_LJMP(lua_error(L)); |
| 4662 | } |
| 4663 | id = lua_tointeger(L, -2); |
| 4664 | |
| 4665 | /* We expect a string as -2. */ |
| 4666 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4667 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4668 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4669 | name, id, |
| 4670 | lua_typename(L, lua_type(L, -1))); |
| 4671 | WILL_LJMP(lua_error(L)); |
| 4672 | } |
| 4673 | value = lua_tolstring(L, -1, &vlen); |
| 4674 | |
| 4675 | /* Simple Protocol checks. */ |
| 4676 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4677 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4678 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4679 | struct ist v = ist2(value, vlen); |
| 4680 | int ret; |
| 4681 | |
| 4682 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4683 | if (ret < 0) { |
| 4684 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4685 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4686 | name); |
| 4687 | WILL_LJMP(lua_error(L)); |
| 4688 | } |
| 4689 | else if (ret == 0) |
| 4690 | goto next; /* Skip it */ |
| 4691 | } |
| 4692 | |
| 4693 | /* Add a new header */ |
| 4694 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4695 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4696 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4697 | name); |
| 4698 | WILL_LJMP(lua_error(L)); |
| 4699 | } |
| 4700 | next: |
| 4701 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4702 | lua_pop(L, 1); |
| 4703 | } |
| 4704 | |
| 4705 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4706 | lua_pop(L, 1); |
| 4707 | } |
| 4708 | |
| 4709 | if (h1m.flags & H1_MF_CHNK) |
| 4710 | h1m.flags &= ~H1_MF_CLEN; |
| 4711 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4712 | h1m.flags |= H1_MF_XFER_LEN; |
| 4713 | |
| 4714 | /* Uset HTX start-line flags */ |
| 4715 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4716 | flags |= HTX_SL_F_XFER_ENC; |
| 4717 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4718 | flags |= HTX_SL_F_XFER_LEN; |
| 4719 | if (h1m.flags & H1_MF_CHNK) |
| 4720 | flags |= HTX_SL_F_CHNK; |
| 4721 | else if (h1m.flags & H1_MF_CLEN) |
| 4722 | flags |= HTX_SL_F_CLEN; |
| 4723 | if (h1m.body_len == 0) |
| 4724 | flags |= HTX_SL_F_BODYLESS; |
| 4725 | } |
| 4726 | sl->flags |= flags; |
| 4727 | |
| 4728 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4729 | * and the status code implies the presence of a message body, we must |
| 4730 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4731 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4732 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4733 | */ |
| 4734 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4735 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4736 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4737 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4738 | /* Add a new header */ |
| 4739 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4740 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4741 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4742 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4743 | WILL_LJMP(lua_error(L)); |
| 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | /* Finalize headers. */ |
| 4748 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4749 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4750 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4751 | WILL_LJMP(lua_error(L)); |
| 4752 | } |
| 4753 | |
| 4754 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4755 | b_reset(&res->buf); |
| 4756 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4757 | WILL_LJMP(lua_error(L)); |
| 4758 | } |
| 4759 | |
| 4760 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4761 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4762 | |
| 4763 | /* Headers sent, set the flag. */ |
| 4764 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4765 | return 0; |
| 4766 | |
| 4767 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4768 | /* We will build the status line and the headers of the HTTP response. |
| 4769 | * We will try send at once if its not possible, we give back the hand |
| 4770 | * waiting for more room. |
| 4771 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4772 | __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] | 4773 | { |
| 4774 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4775 | struct stream_interface *si = appctx->appctx->owner; |
| 4776 | struct channel *res = si_ic(si); |
| 4777 | |
| 4778 | if (co_data(res)) { |
| 4779 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4780 | 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] | 4781 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4782 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4786 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4787 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4788 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4789 | } |
| 4790 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4791 | /* |
| 4792 | * |
| 4793 | * |
| 4794 | * Class HTTP |
| 4795 | * |
| 4796 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4797 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4798 | |
| 4799 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4800 | * a class stream, otherwise it throws an error. |
| 4801 | */ |
| 4802 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4803 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4804 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4805 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4806 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4807 | /* This function creates and push in the stack a HTTP object |
| 4808 | * according with a current TXN. |
| 4809 | */ |
| 4810 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4811 | { |
| 4812 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4813 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4814 | /* Check stack size. */ |
| 4815 | if (!lua_checkstack(L, 3)) |
| 4816 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4817 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4818 | /* Create the object: obj[0] = userdata. |
| 4819 | * Note that the base of the Converters object is the |
| 4820 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4821 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4822 | lua_newtable(L); |
| 4823 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4824 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4825 | |
| 4826 | htxn->s = txn->s; |
| 4827 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4828 | htxn->dir = txn->dir; |
| 4829 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4830 | |
| 4831 | /* Pop a class stream metatable and affect it to the table. */ |
| 4832 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4833 | lua_setmetatable(L, -2); |
| 4834 | |
| 4835 | return 1; |
| 4836 | } |
| 4837 | |
| 4838 | /* This function creates ans returns an array of HTTP headers. |
| 4839 | * This function does not fails. It is used as wrapper with the |
| 4840 | * 2 following functions. |
| 4841 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4842 | __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] | 4843 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4844 | struct htx *htx; |
| 4845 | int32_t pos; |
| 4846 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4847 | /* Create the table. */ |
| 4848 | lua_newtable(L); |
| 4849 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4850 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4851 | htx = htxbuf(&msg->chn->buf); |
| 4852 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4853 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4854 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4855 | struct ist n, v; |
| 4856 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4857 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4858 | if (type == HTX_BLK_HDR) { |
| 4859 | n = htx_get_blk_name(htx,blk); |
| 4860 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4861 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4862 | else if (type == HTX_BLK_EOH) |
| 4863 | break; |
| 4864 | else |
| 4865 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4866 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4867 | /* Check for existing entry: |
| 4868 | * assume that the table is on the top of the stack, and |
| 4869 | * push the key in the stack, the function lua_gettable() |
| 4870 | * perform the lookup. |
| 4871 | */ |
| 4872 | lua_pushlstring(L, n.ptr, n.len); |
| 4873 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4874 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4875 | switch (lua_type(L, -1)) { |
| 4876 | case LUA_TNIL: |
| 4877 | /* Table not found, create it. */ |
| 4878 | lua_pop(L, 1); /* remove the nil value. */ |
| 4879 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4880 | lua_newtable(L); /* create and push empty table. */ |
| 4881 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4882 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4883 | 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] | 4884 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4885 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4886 | case LUA_TTABLE: |
| 4887 | /* Entry found: push the value in the table. */ |
| 4888 | len = lua_rawlen(L, -1); |
| 4889 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4890 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4891 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4892 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4893 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4894 | default: |
| 4895 | /* Other cases are errors. */ |
| 4896 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4897 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4898 | } |
| 4899 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4900 | return 1; |
| 4901 | } |
| 4902 | |
| 4903 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4904 | { |
| 4905 | struct hlua_txn *htxn; |
| 4906 | |
| 4907 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4908 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4909 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4910 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4911 | WILL_LJMP(lua_error(L)); |
| 4912 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4913 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4917 | { |
| 4918 | struct hlua_txn *htxn; |
| 4919 | |
| 4920 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4921 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4922 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4923 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4924 | WILL_LJMP(lua_error(L)); |
| 4925 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4926 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | /* This function replace full header, or just a value in |
| 4930 | * the request or in the response. It is a wrapper fir the |
| 4931 | * 4 following functions. |
| 4932 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4933 | __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] | 4934 | { |
| 4935 | size_t name_len; |
| 4936 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4937 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4938 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4939 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4940 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4941 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4942 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4943 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4944 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4945 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4946 | 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] | 4947 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4948 | return 0; |
| 4949 | } |
| 4950 | |
| 4951 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4952 | { |
| 4953 | struct hlua_txn *htxn; |
| 4954 | |
| 4955 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 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 | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4961 | 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] | 4962 | } |
| 4963 | |
| 4964 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4965 | { |
| 4966 | struct hlua_txn *htxn; |
| 4967 | |
| 4968 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 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 | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4974 | 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] | 4975 | } |
| 4976 | |
| 4977 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4978 | { |
| 4979 | struct hlua_txn *htxn; |
| 4980 | |
| 4981 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4982 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4983 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4984 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4985 | WILL_LJMP(lua_error(L)); |
| 4986 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4987 | 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] | 4988 | } |
| 4989 | |
| 4990 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4991 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4992 | struct hlua_txn *htxn; |
| 4993 | |
| 4994 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4995 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4996 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4997 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4998 | WILL_LJMP(lua_error(L)); |
| 4999 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5000 | 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] | 5001 | } |
| 5002 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5003 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5004 | * It is a wrapper for the 2 following functions. |
| 5005 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5006 | __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] | 5007 | { |
| 5008 | size_t len; |
| 5009 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5010 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5011 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5012 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5013 | ctx.blk = NULL; |
| 5014 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5015 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5016 | return 0; |
| 5017 | } |
| 5018 | |
| 5019 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5020 | { |
| 5021 | struct hlua_txn *htxn; |
| 5022 | |
| 5023 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5024 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5025 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5026 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5027 | WILL_LJMP(lua_error(L)); |
| 5028 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5029 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5030 | } |
| 5031 | |
| 5032 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5033 | { |
| 5034 | struct hlua_txn *htxn; |
| 5035 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5036 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5037 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5038 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5039 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5040 | WILL_LJMP(lua_error(L)); |
| 5041 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5042 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | /* This function adds an header. It is a wrapper used by |
| 5046 | * the 2 following functions. |
| 5047 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5048 | __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] | 5049 | { |
| 5050 | size_t name_len; |
| 5051 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5052 | size_t value_len; |
| 5053 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5054 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5055 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5056 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5057 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5058 | return 0; |
| 5059 | } |
| 5060 | |
| 5061 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5062 | { |
| 5063 | struct hlua_txn *htxn; |
| 5064 | |
| 5065 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5066 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5067 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5068 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5069 | WILL_LJMP(lua_error(L)); |
| 5070 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5071 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5072 | } |
| 5073 | |
| 5074 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5075 | { |
| 5076 | struct hlua_txn *htxn; |
| 5077 | |
| 5078 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5079 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5080 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5081 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5082 | WILL_LJMP(lua_error(L)); |
| 5083 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5084 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5088 | { |
| 5089 | struct hlua_txn *htxn; |
| 5090 | |
| 5091 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5092 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5093 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5094 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5095 | WILL_LJMP(lua_error(L)); |
| 5096 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5097 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5098 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5102 | { |
| 5103 | struct hlua_txn *htxn; |
| 5104 | |
| 5105 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5106 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5107 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5108 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5109 | WILL_LJMP(lua_error(L)); |
| 5110 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5111 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5112 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5113 | } |
| 5114 | |
| 5115 | /* This function set the method. */ |
| 5116 | static int hlua_http_req_set_meth(lua_State *L) |
| 5117 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5118 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5119 | size_t name_len; |
| 5120 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5121 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5122 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5123 | WILL_LJMP(lua_error(L)); |
| 5124 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5125 | 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] | 5126 | return 1; |
| 5127 | } |
| 5128 | |
| 5129 | /* This function set the method. */ |
| 5130 | static int hlua_http_req_set_path(lua_State *L) |
| 5131 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5132 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5133 | size_t name_len; |
| 5134 | 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] | 5135 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5136 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5137 | WILL_LJMP(lua_error(L)); |
| 5138 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5139 | 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] | 5140 | return 1; |
| 5141 | } |
| 5142 | |
| 5143 | /* This function set the query-string. */ |
| 5144 | static int hlua_http_req_set_query(lua_State *L) |
| 5145 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5146 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5147 | size_t name_len; |
| 5148 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5149 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5150 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5151 | WILL_LJMP(lua_error(L)); |
| 5152 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5153 | /* Check length. */ |
| 5154 | if (name_len > trash.size - 1) { |
| 5155 | lua_pushboolean(L, 0); |
| 5156 | return 1; |
| 5157 | } |
| 5158 | |
| 5159 | /* Add the mark question as prefix. */ |
| 5160 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5161 | trash.area[trash.data++] = '?'; |
| 5162 | memcpy(trash.area + trash.data, name, name_len); |
| 5163 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5164 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5165 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5166 | 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] | 5167 | return 1; |
| 5168 | } |
| 5169 | |
| 5170 | /* This function set the uri. */ |
| 5171 | static int hlua_http_req_set_uri(lua_State *L) |
| 5172 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5173 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5174 | size_t name_len; |
| 5175 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5176 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5177 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5178 | WILL_LJMP(lua_error(L)); |
| 5179 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5180 | 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] | 5181 | return 1; |
| 5182 | } |
| 5183 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5184 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5185 | static int hlua_http_res_set_status(lua_State *L) |
| 5186 | { |
| 5187 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5188 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5189 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5190 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5191 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5192 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5193 | WILL_LJMP(lua_error(L)); |
| 5194 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5195 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5196 | return 0; |
| 5197 | } |
| 5198 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5199 | /* |
| 5200 | * |
| 5201 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5202 | * Class TXN |
| 5203 | * |
| 5204 | * |
| 5205 | */ |
| 5206 | |
| 5207 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5208 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5209 | */ |
| 5210 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5211 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5212 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5213 | } |
| 5214 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5215 | __LJMP static int hlua_set_var(lua_State *L) |
| 5216 | { |
| 5217 | struct hlua_txn *htxn; |
| 5218 | const char *name; |
| 5219 | size_t len; |
| 5220 | struct sample smp; |
| 5221 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5222 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5223 | 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] | 5224 | |
| 5225 | /* It is useles to retrieve the stream, but this function |
| 5226 | * runs only in a stream context. |
| 5227 | */ |
| 5228 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5229 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5230 | |
| 5231 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5232 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5233 | hlua_lua2smp(L, 3, &smp); |
| 5234 | |
| 5235 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5236 | 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] | 5237 | |
| 5238 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5239 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5240 | else |
| 5241 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5242 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5243 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5244 | } |
| 5245 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5246 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5247 | { |
| 5248 | struct hlua_txn *htxn; |
| 5249 | const char *name; |
| 5250 | size_t len; |
| 5251 | struct sample smp; |
| 5252 | |
| 5253 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5254 | |
| 5255 | /* It is useles to retrieve the stream, but this function |
| 5256 | * runs only in a stream context. |
| 5257 | */ |
| 5258 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5259 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5260 | |
| 5261 | /* Unset the variable. */ |
| 5262 | 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] | 5263 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5264 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5265 | } |
| 5266 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5267 | __LJMP static int hlua_get_var(lua_State *L) |
| 5268 | { |
| 5269 | struct hlua_txn *htxn; |
| 5270 | const char *name; |
| 5271 | size_t len; |
| 5272 | struct sample smp; |
| 5273 | |
| 5274 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5275 | |
| 5276 | /* It is useles to retrieve the stream, but this function |
| 5277 | * runs only in a stream context. |
| 5278 | */ |
| 5279 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5280 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5281 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5282 | 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] | 5283 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5284 | lua_pushnil(L); |
| 5285 | return 1; |
| 5286 | } |
| 5287 | |
| 5288 | return hlua_smp2lua(L, &smp); |
| 5289 | } |
| 5290 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5291 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5292 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5293 | struct hlua *hlua; |
| 5294 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5295 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5296 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5297 | /* It is useles to retrieve the stream, but this function |
| 5298 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5299 | */ |
| 5300 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5301 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5302 | |
| 5303 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5304 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5305 | |
| 5306 | /* Get and store new value. */ |
| 5307 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5308 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5309 | |
| 5310 | return 0; |
| 5311 | } |
| 5312 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5313 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5314 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5315 | struct hlua *hlua; |
| 5316 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5317 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5318 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5319 | /* It is useles to retrieve the stream, but this function |
| 5320 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5321 | */ |
| 5322 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5323 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5324 | |
| 5325 | /* Push configuration index in the stack. */ |
| 5326 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5327 | |
| 5328 | return 1; |
| 5329 | } |
| 5330 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5331 | /* Create stack entry containing a class TXN. This function |
| 5332 | * return 0 if the stack does not contains free slots, |
| 5333 | * otherwise it returns 1. |
| 5334 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5335 | 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] | 5336 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5337 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5338 | |
| 5339 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5340 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5341 | return 0; |
| 5342 | |
| 5343 | /* NOTE: The allocation never fails. The failure |
| 5344 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5345 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5346 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5347 | /* Create the object: obj[0] = userdata. */ |
| 5348 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5349 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5350 | lua_rawseti(L, -2, 0); |
| 5351 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5352 | htxn->s = s; |
| 5353 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5354 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5355 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5356 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5357 | /* Create the "f" field that contains a list of fetches. */ |
| 5358 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5359 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5360 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5361 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5362 | |
| 5363 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5364 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5365 | 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] | 5366 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5367 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5368 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5369 | /* Create the "c" field that contains a list of converters. */ |
| 5370 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5371 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5372 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5373 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5374 | |
| 5375 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5376 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5377 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5378 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5379 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5380 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5381 | /* Create the "req" field that contains the request channel object. */ |
| 5382 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5383 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5384 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5385 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5386 | |
| 5387 | /* Create the "res" field that contains the response channel object. */ |
| 5388 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5389 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5390 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5391 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5392 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5393 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5394 | lua_pushstring(L, "http"); |
| 5395 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5396 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5397 | return 0; |
| 5398 | } |
| 5399 | else |
| 5400 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5401 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5402 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5403 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5404 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5405 | lua_setmetatable(L, -2); |
| 5406 | |
| 5407 | return 1; |
| 5408 | } |
| 5409 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5410 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5411 | { |
| 5412 | const char *msg; |
| 5413 | struct hlua_txn *htxn; |
| 5414 | |
| 5415 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5416 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5417 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5418 | |
| 5419 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5420 | return 0; |
| 5421 | } |
| 5422 | |
| 5423 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5424 | { |
| 5425 | int level; |
| 5426 | const char *msg; |
| 5427 | struct hlua_txn *htxn; |
| 5428 | |
| 5429 | MAY_LJMP(check_args(L, 3, "log")); |
| 5430 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5431 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5432 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5433 | |
| 5434 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5435 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5436 | |
| 5437 | hlua_sendlog(htxn->s->be, level, msg); |
| 5438 | return 0; |
| 5439 | } |
| 5440 | |
| 5441 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5442 | { |
| 5443 | const char *msg; |
| 5444 | struct hlua_txn *htxn; |
| 5445 | |
| 5446 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5447 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5448 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5449 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5450 | return 0; |
| 5451 | } |
| 5452 | |
| 5453 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5454 | { |
| 5455 | const char *msg; |
| 5456 | struct hlua_txn *htxn; |
| 5457 | |
| 5458 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5459 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5460 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5461 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5462 | return 0; |
| 5463 | } |
| 5464 | |
| 5465 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5466 | { |
| 5467 | const char *msg; |
| 5468 | struct hlua_txn *htxn; |
| 5469 | |
| 5470 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5471 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5472 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5473 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5474 | return 0; |
| 5475 | } |
| 5476 | |
| 5477 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5478 | { |
| 5479 | const char *msg; |
| 5480 | struct hlua_txn *htxn; |
| 5481 | |
| 5482 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5483 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5484 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5485 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5486 | return 0; |
| 5487 | } |
| 5488 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5489 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5490 | { |
| 5491 | struct hlua_txn *htxn; |
| 5492 | int ll; |
| 5493 | |
| 5494 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5495 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5496 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5497 | |
| 5498 | if (ll < 0 || ll > 7) |
| 5499 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5500 | |
| 5501 | htxn->s->logs.level = ll; |
| 5502 | return 0; |
| 5503 | } |
| 5504 | |
| 5505 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5506 | { |
| 5507 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5508 | int tos; |
| 5509 | |
| 5510 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5511 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5512 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5513 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5514 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
| 5517 | |
| 5518 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5519 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5520 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5521 | int mark; |
| 5522 | |
| 5523 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5524 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5525 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5526 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5527 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5528 | return 0; |
| 5529 | } |
| 5530 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5531 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5532 | { |
| 5533 | struct hlua_txn *htxn; |
| 5534 | |
| 5535 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5536 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5537 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5538 | return 0; |
| 5539 | } |
| 5540 | |
| 5541 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5542 | { |
| 5543 | struct hlua_txn *htxn; |
| 5544 | |
| 5545 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5546 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5547 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5548 | return 0; |
| 5549 | } |
| 5550 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5551 | /* 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] | 5552 | * 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] | 5553 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5554 | * error. The Reply must be on top of the stack. |
| 5555 | */ |
| 5556 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5557 | { |
| 5558 | struct htx *htx; |
| 5559 | struct htx_sl *sl; |
| 5560 | struct h1m h1m; |
| 5561 | const char *status, *reason, *body; |
| 5562 | size_t status_len, reason_len, body_len; |
| 5563 | int ret, code, flags; |
| 5564 | |
| 5565 | code = 200; |
| 5566 | status = "200"; |
| 5567 | status_len = 3; |
| 5568 | ret = lua_getfield(L, -1, "status"); |
| 5569 | if (ret == LUA_TNUMBER) { |
| 5570 | code = lua_tointeger(L, -1); |
| 5571 | status = lua_tolstring(L, -1, &status_len); |
| 5572 | } |
| 5573 | lua_pop(L, 1); |
| 5574 | |
| 5575 | reason = http_get_reason(code); |
| 5576 | reason_len = strlen(reason); |
| 5577 | ret = lua_getfield(L, -1, "reason"); |
| 5578 | if (ret == LUA_TSTRING) |
| 5579 | reason = lua_tolstring(L, -1, &reason_len); |
| 5580 | lua_pop(L, 1); |
| 5581 | |
| 5582 | body = NULL; |
| 5583 | body_len = 0; |
| 5584 | ret = lua_getfield(L, -1, "body"); |
| 5585 | if (ret == LUA_TSTRING) |
| 5586 | body = lua_tolstring(L, -1, &body_len); |
| 5587 | lua_pop(L, 1); |
| 5588 | |
| 5589 | /* Prepare the response before inserting the headers */ |
| 5590 | h1m_init_res(&h1m); |
| 5591 | htx = htx_from_buf(&s->res.buf); |
| 5592 | channel_htx_truncate(&s->res, htx); |
| 5593 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5594 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5595 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5596 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5597 | } |
| 5598 | else { |
| 5599 | flags = HTX_SL_F_IS_RESP; |
| 5600 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5601 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5602 | } |
| 5603 | if (!sl) |
| 5604 | goto fail; |
| 5605 | sl->info.res.status = code; |
| 5606 | |
| 5607 | /* Push in the stack the "headers" entry. */ |
| 5608 | ret = lua_getfield(L, -1, "headers"); |
| 5609 | if (ret != LUA_TTABLE) |
| 5610 | goto skip_headers; |
| 5611 | |
| 5612 | lua_pushnil(L); |
| 5613 | while (lua_next(L, -2) != 0) { |
| 5614 | struct ist name, value; |
| 5615 | const char *n, *v; |
| 5616 | size_t nlen, vlen; |
| 5617 | |
| 5618 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5619 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5620 | goto next_hdr; |
| 5621 | } |
| 5622 | |
| 5623 | n = lua_tolstring(L, -2, &nlen); |
| 5624 | name = ist2(n, nlen); |
| 5625 | if (isteqi(name, ist("content-length"))) { |
| 5626 | /* Always skip content-length header. It will be added |
| 5627 | * later with the correct len |
| 5628 | */ |
| 5629 | goto next_hdr; |
| 5630 | } |
| 5631 | |
| 5632 | /* Loop on header's values */ |
| 5633 | lua_pushnil(L); |
| 5634 | while (lua_next(L, -2)) { |
| 5635 | if (!lua_isstring(L, -1)) { |
| 5636 | /* Skip the value if it is not a string */ |
| 5637 | goto next_value; |
| 5638 | } |
| 5639 | |
| 5640 | v = lua_tolstring(L, -1, &vlen); |
| 5641 | value = ist2(v, vlen); |
| 5642 | |
| 5643 | if (isteqi(name, ist("transfer-encoding"))) |
| 5644 | h1_parse_xfer_enc_header(&h1m, value); |
| 5645 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5646 | goto fail; |
| 5647 | |
| 5648 | next_value: |
| 5649 | lua_pop(L, 1); |
| 5650 | } |
| 5651 | |
| 5652 | next_hdr: |
| 5653 | lua_pop(L, 1); |
| 5654 | } |
| 5655 | skip_headers: |
| 5656 | lua_pop(L, 1); |
| 5657 | |
| 5658 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5659 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5660 | const char *clen = ultoa(body_len); |
| 5661 | |
| 5662 | h1m.flags |= H1_MF_CLEN; |
| 5663 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5664 | goto fail; |
| 5665 | } |
| 5666 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5667 | h1m.flags |= H1_MF_XFER_LEN; |
| 5668 | |
| 5669 | /* Update HTX start-line flags */ |
| 5670 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5671 | flags |= HTX_SL_F_XFER_ENC; |
| 5672 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5673 | flags |= HTX_SL_F_XFER_LEN; |
| 5674 | if (h1m.flags & H1_MF_CHNK) |
| 5675 | flags |= HTX_SL_F_CHNK; |
| 5676 | else if (h1m.flags & H1_MF_CLEN) |
| 5677 | flags |= HTX_SL_F_CLEN; |
| 5678 | if (h1m.body_len == 0) |
| 5679 | flags |= HTX_SL_F_BODYLESS; |
| 5680 | } |
| 5681 | sl->flags |= flags; |
| 5682 | |
| 5683 | |
| 5684 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5685 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5686 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5687 | goto fail; |
| 5688 | |
| 5689 | /* Now, forward the response and terminate the transaction */ |
| 5690 | s->txn->status = code; |
| 5691 | htx_to_buf(htx, &s->res.buf); |
| 5692 | if (!http_forward_proxy_resp(s, 1)) |
| 5693 | goto fail; |
| 5694 | |
| 5695 | return 1; |
| 5696 | |
| 5697 | fail: |
| 5698 | channel_htx_truncate(&s->res, htx); |
| 5699 | return 0; |
| 5700 | } |
| 5701 | |
| 5702 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5703 | * processing is just aborted. Nothing is returned to the client and all |
| 5704 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5705 | * is forwarded to the client before terminating the transaction. On success, |
| 5706 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5707 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5708 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5709 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5710 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5711 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5712 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5713 | struct stream *s; |
| 5714 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5715 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5716 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5717 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5718 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5719 | * just end the execution of the current lua code. */ |
| 5720 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5721 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5722 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5723 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5724 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5725 | struct channel *req = &s->req; |
| 5726 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5727 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5728 | channel_auto_read(req); |
| 5729 | channel_abort(req); |
| 5730 | channel_auto_close(req); |
| 5731 | channel_erase(req); |
| 5732 | |
| 5733 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5734 | channel_auto_read(res); |
| 5735 | channel_auto_close(res); |
| 5736 | channel_shutr_now(res); |
| 5737 | |
| 5738 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5739 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5740 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5741 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5742 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5743 | /* No reply or invalid reply */ |
| 5744 | s->txn->status = 0; |
| 5745 | http_reply_and_close(s, 0, NULL); |
| 5746 | } |
| 5747 | else { |
| 5748 | /* Remove extra args to have the reply on top of the stack */ |
| 5749 | if (lua_gettop(L) > 2) |
| 5750 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5751 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5752 | if (!hlua_txn_forward_reply(L, s)) { |
| 5753 | if (!(s->flags & SF_ERR_MASK)) |
| 5754 | s->flags |= SF_ERR_PRXCOND; |
| 5755 | lua_pushinteger(L, ACT_RET_ERR); |
| 5756 | WILL_LJMP(hlua_done(L)); |
| 5757 | return 0; /* Never reached */ |
| 5758 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5759 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5760 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5761 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5762 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5763 | /* let's log the request time */ |
| 5764 | s->logs.tv_request = now; |
| 5765 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5766 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5767 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5768 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5769 | done: |
| 5770 | if (!(s->flags & SF_ERR_MASK)) |
| 5771 | s->flags |= SF_ERR_LOCAL; |
| 5772 | if (!(s->flags & SF_FINST_MASK)) |
| 5773 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5774 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5775 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5776 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5777 | return 0; |
| 5778 | } |
| 5779 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5780 | /* |
| 5781 | * |
| 5782 | * |
| 5783 | * Class REPLY |
| 5784 | * |
| 5785 | * |
| 5786 | */ |
| 5787 | |
| 5788 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5789 | * free slots, the function fails and returns 0; |
| 5790 | */ |
| 5791 | static int hlua_txn_reply_new(lua_State *L) |
| 5792 | { |
| 5793 | struct hlua_txn *htxn; |
| 5794 | const char *reason, *body = NULL; |
| 5795 | int ret, status; |
| 5796 | |
| 5797 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5798 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5799 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5800 | WILL_LJMP(lua_error(L)); |
| 5801 | } |
| 5802 | |
| 5803 | /* Default value */ |
| 5804 | status = 200; |
| 5805 | reason = http_get_reason(status); |
| 5806 | |
| 5807 | if (lua_istable(L, 2)) { |
| 5808 | /* load status and reason from the table argument at index 2 */ |
| 5809 | ret = lua_getfield(L, 2, "status"); |
| 5810 | if (ret == LUA_TNIL) |
| 5811 | goto reason; |
| 5812 | else if (ret != LUA_TNUMBER) { |
| 5813 | /* invalid status: ignore the reason */ |
| 5814 | goto body; |
| 5815 | } |
| 5816 | status = lua_tointeger(L, -1); |
| 5817 | |
| 5818 | reason: |
| 5819 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5820 | ret = lua_getfield(L, 2, "reason"); |
| 5821 | if (ret == LUA_TSTRING) |
| 5822 | reason = lua_tostring(L, -1); |
| 5823 | |
| 5824 | body: |
| 5825 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5826 | ret = lua_getfield(L, 2, "body"); |
| 5827 | if (ret == LUA_TSTRING) |
| 5828 | body = lua_tostring(L, -1); |
| 5829 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5830 | } |
| 5831 | |
| 5832 | /* Create the Reply table */ |
| 5833 | lua_newtable(L); |
| 5834 | |
| 5835 | /* Add status element */ |
| 5836 | lua_pushstring(L, "status"); |
| 5837 | lua_pushinteger(L, status); |
| 5838 | lua_settable(L, -3); |
| 5839 | |
| 5840 | /* Add reason element */ |
| 5841 | reason = http_get_reason(status); |
| 5842 | lua_pushstring(L, "reason"); |
| 5843 | lua_pushstring(L, reason); |
| 5844 | lua_settable(L, -3); |
| 5845 | |
| 5846 | /* Add body element, nil if undefined */ |
| 5847 | lua_pushstring(L, "body"); |
| 5848 | if (body) |
| 5849 | lua_pushstring(L, body); |
| 5850 | else |
| 5851 | lua_pushnil(L); |
| 5852 | lua_settable(L, -3); |
| 5853 | |
| 5854 | /* Add headers element */ |
| 5855 | lua_pushstring(L, "headers"); |
| 5856 | lua_newtable(L); |
| 5857 | |
| 5858 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5859 | if (lua_istable(L, 2)) { |
| 5860 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5861 | ret = lua_getfield(L, 2, "headers"); |
| 5862 | if (ret == LUA_TTABLE) { |
| 5863 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5864 | lua_pushnil(L); |
| 5865 | while (lua_next(L, -2) != 0) { |
| 5866 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5867 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5868 | /* invalid value type, skip it */ |
| 5869 | lua_pop(L, 1); |
| 5870 | continue; |
| 5871 | } |
| 5872 | |
| 5873 | |
| 5874 | /* Duplicate the key and swap it with the value. */ |
| 5875 | lua_pushvalue(L, -2); |
| 5876 | lua_insert(L, -2); |
| 5877 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5878 | |
| 5879 | lua_newtable(L); |
| 5880 | lua_insert(L, -2); |
| 5881 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5882 | |
| 5883 | if (lua_isstring(L, -1)) { |
| 5884 | /* push the value in the inner table */ |
| 5885 | lua_rawseti(L, -2, 1); |
| 5886 | } |
| 5887 | else { /* table */ |
| 5888 | lua_pushnil(L); |
| 5889 | while (lua_next(L, -2) != 0) { |
| 5890 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5891 | if (!lua_isstring(L, -1)) { |
| 5892 | /* invalid value type, skip it*/ |
| 5893 | lua_pop(L, 1); |
| 5894 | continue; |
| 5895 | } |
| 5896 | /* push the value in the inner table */ |
| 5897 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5898 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5899 | } |
| 5900 | lua_pop(L, 1); |
| 5901 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5902 | } |
| 5903 | |
| 5904 | /* push (k,v) on the stack in the headers table: |
| 5905 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5906 | */ |
| 5907 | lua_settable(L, -5); |
| 5908 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5909 | } |
| 5910 | } |
| 5911 | lua_pop(L, 1); |
| 5912 | } |
| 5913 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5914 | lua_settable(L, -3); |
| 5915 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5916 | |
| 5917 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5918 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5919 | lua_setmetatable(L, -2); |
| 5920 | return 1; |
| 5921 | } |
| 5922 | |
| 5923 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5924 | * provided, the default one corresponding to the status code is used. |
| 5925 | */ |
| 5926 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5927 | { |
| 5928 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5929 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5930 | |
| 5931 | /* First argument (self) must be a table */ |
| 5932 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5933 | |
| 5934 | if (status < 100 || status > 599) { |
| 5935 | lua_pushboolean(L, 0); |
| 5936 | return 1; |
| 5937 | } |
| 5938 | if (!reason) |
| 5939 | reason = http_get_reason(status); |
| 5940 | |
| 5941 | lua_pushinteger(L, status); |
| 5942 | lua_setfield(L, 1, "status"); |
| 5943 | |
| 5944 | lua_pushstring(L, reason); |
| 5945 | lua_setfield(L, 1, "reason"); |
| 5946 | |
| 5947 | lua_pushboolean(L, 1); |
| 5948 | return 1; |
| 5949 | } |
| 5950 | |
| 5951 | /* Add a header into the reply object. Each header name is associated to an |
| 5952 | * array of values in the "headers" table. If the header name is not found, a |
| 5953 | * new entry is created. |
| 5954 | */ |
| 5955 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5956 | { |
| 5957 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5958 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5959 | int ret; |
| 5960 | |
| 5961 | /* First argument (self) must be a table */ |
| 5962 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5963 | |
| 5964 | /* Push in the stack the "headers" entry. */ |
| 5965 | ret = lua_getfield(L, 1, "headers"); |
| 5966 | if (ret != LUA_TTABLE) { |
| 5967 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5968 | WILL_LJMP(lua_error(L)); |
| 5969 | } |
| 5970 | |
| 5971 | /* check if the header is already registered. If not, register it. */ |
| 5972 | ret = lua_getfield(L, -1, name); |
| 5973 | if (ret == LUA_TNIL) { |
| 5974 | /* Entry not found. */ |
| 5975 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5976 | |
| 5977 | /* Insert the new header name in the array in the top of the stack. |
| 5978 | * It left the new array in the top of the stack. |
| 5979 | */ |
| 5980 | lua_newtable(L); |
| 5981 | lua_pushstring(L, name); |
| 5982 | lua_pushvalue(L, -2); |
| 5983 | lua_settable(L, -4); |
| 5984 | } |
| 5985 | else if (ret != LUA_TTABLE) { |
| 5986 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5987 | WILL_LJMP(lua_error(L)); |
| 5988 | } |
| 5989 | |
| 5990 | /* Now the top od thestack is an array of values. We push |
| 5991 | * the header value as new entry. |
| 5992 | */ |
| 5993 | lua_pushstring(L, value); |
| 5994 | ret = lua_rawlen(L, -2); |
| 5995 | lua_rawseti(L, -2, ret + 1); |
| 5996 | |
| 5997 | lua_pushboolean(L, 1); |
| 5998 | return 1; |
| 5999 | } |
| 6000 | |
| 6001 | /* Remove all occurrences of a given header name. */ |
| 6002 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6003 | { |
| 6004 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6005 | int ret; |
| 6006 | |
| 6007 | /* First argument (self) must be a table */ |
| 6008 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6009 | |
| 6010 | /* Push in the stack the "headers" entry. */ |
| 6011 | ret = lua_getfield(L, 1, "headers"); |
| 6012 | if (ret != LUA_TTABLE) { |
| 6013 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6014 | WILL_LJMP(lua_error(L)); |
| 6015 | } |
| 6016 | |
| 6017 | lua_pushstring(L, name); |
| 6018 | lua_pushnil(L); |
| 6019 | lua_settable(L, -3); |
| 6020 | |
| 6021 | lua_pushboolean(L, 1); |
| 6022 | return 1; |
| 6023 | } |
| 6024 | |
| 6025 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6026 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6027 | { |
| 6028 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6029 | |
| 6030 | /* First argument (self) must be a table */ |
| 6031 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6032 | |
| 6033 | lua_pushstring(L, payload); |
| 6034 | lua_setfield(L, 1, "body"); |
| 6035 | |
| 6036 | lua_pushboolean(L, 1); |
| 6037 | return 1; |
| 6038 | } |
| 6039 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6040 | __LJMP static int hlua_log(lua_State *L) |
| 6041 | { |
| 6042 | int level; |
| 6043 | const char *msg; |
| 6044 | |
| 6045 | MAY_LJMP(check_args(L, 2, "log")); |
| 6046 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6047 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6048 | |
| 6049 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6050 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6051 | |
| 6052 | hlua_sendlog(NULL, level, msg); |
| 6053 | return 0; |
| 6054 | } |
| 6055 | |
| 6056 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6057 | { |
| 6058 | const char *msg; |
| 6059 | |
| 6060 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6061 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6062 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6063 | return 0; |
| 6064 | } |
| 6065 | |
| 6066 | __LJMP static int hlua_log_info(lua_State *L) |
| 6067 | { |
| 6068 | const char *msg; |
| 6069 | |
| 6070 | MAY_LJMP(check_args(L, 1, "info")); |
| 6071 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6072 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6073 | return 0; |
| 6074 | } |
| 6075 | |
| 6076 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6077 | { |
| 6078 | const char *msg; |
| 6079 | |
| 6080 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6081 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6082 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6083 | return 0; |
| 6084 | } |
| 6085 | |
| 6086 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6087 | { |
| 6088 | const char *msg; |
| 6089 | |
| 6090 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6091 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6092 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6093 | return 0; |
| 6094 | } |
| 6095 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6096 | __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] | 6097 | { |
| 6098 | int wakeup_ms = lua_tointeger(L, -1); |
| 6099 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6100 | 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] | 6101 | return 0; |
| 6102 | } |
| 6103 | |
| 6104 | __LJMP static int hlua_sleep(lua_State *L) |
| 6105 | { |
| 6106 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6107 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6108 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6109 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6110 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6111 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6112 | wakeup_ms = tick_add(now_ms, delay); |
| 6113 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6114 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6115 | 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] | 6116 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6117 | } |
| 6118 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6119 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6120 | { |
| 6121 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6122 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6123 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6124 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6125 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6126 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6127 | wakeup_ms = tick_add(now_ms, delay); |
| 6128 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6129 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6130 | 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] | 6131 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6132 | } |
| 6133 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6134 | /* This functionis an LUA binding. it permits to give back |
| 6135 | * the hand at the HAProxy scheduler. It is used when the |
| 6136 | * LUA processing consumes a lot of time. |
| 6137 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6138 | __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] | 6139 | { |
| 6140 | return 0; |
| 6141 | } |
| 6142 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6143 | __LJMP static int hlua_yield(lua_State *L) |
| 6144 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6145 | 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] | 6146 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6147 | } |
| 6148 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6149 | /* This function change the nice of the currently executed |
| 6150 | * task. It is used set low or high priority at the current |
| 6151 | * task. |
| 6152 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6153 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6154 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6155 | struct hlua *hlua; |
| 6156 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6157 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6158 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6159 | hlua = hlua_gethlua(L); |
| 6160 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6161 | |
| 6162 | /* If he task is not set, I'm in a start mode. */ |
| 6163 | if (!hlua || !hlua->task) |
| 6164 | return 0; |
| 6165 | |
| 6166 | if (nice < -1024) |
| 6167 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6168 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6169 | nice = 1024; |
| 6170 | |
| 6171 | hlua->task->nice = nice; |
| 6172 | return 0; |
| 6173 | } |
| 6174 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6175 | /* 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] | 6176 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6177 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6178 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6179 | * |
| 6180 | * Task wrapper are longjmp safe because the only one Lua code |
| 6181 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6182 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6183 | 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] | 6184 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6185 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6186 | enum hlua_exec status; |
| 6187 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6188 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6189 | task_set_affinity(task, tid_bit); |
| 6190 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6191 | /* If it is the first call to the task, we must initialize the |
| 6192 | * execution timeouts. |
| 6193 | */ |
| 6194 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6195 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6196 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6197 | /* Execute the Lua code. */ |
| 6198 | status = hlua_ctx_resume(hlua, 1); |
| 6199 | |
| 6200 | switch (status) { |
| 6201 | /* finished or yield */ |
| 6202 | case HLUA_E_OK: |
| 6203 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6204 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6205 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6206 | break; |
| 6207 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6208 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6209 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6210 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6211 | break; |
| 6212 | |
| 6213 | /* finished with error. */ |
| 6214 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6215 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6216 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6217 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6218 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6219 | break; |
| 6220 | |
| 6221 | case HLUA_E_ERR: |
| 6222 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6223 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6224 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6225 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6226 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6227 | break; |
| 6228 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6229 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6230 | } |
| 6231 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6232 | /* This function is an LUA binding that register LUA function to be |
| 6233 | * executed after the HAProxy configuration parsing and before the |
| 6234 | * HAProxy scheduler starts. This function expect only one LUA |
| 6235 | * argument that is a function. This function returns nothing, but |
| 6236 | * throws if an error is encountered. |
| 6237 | */ |
| 6238 | __LJMP static int hlua_register_init(lua_State *L) |
| 6239 | { |
| 6240 | struct hlua_init_function *init; |
| 6241 | int ref; |
| 6242 | |
| 6243 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6244 | |
| 6245 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6246 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6247 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6248 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6249 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6250 | |
| 6251 | init->function_ref = ref; |
| 6252 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6253 | return 0; |
| 6254 | } |
| 6255 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6256 | /* This functio is an LUA binding. It permits to register a task |
| 6257 | * executed in parallel of the main HAroxy activity. The task is |
| 6258 | * created and it is set in the HAProxy scheduler. It can be called |
| 6259 | * from the "init" section, "post init" or during the runtime. |
| 6260 | * |
| 6261 | * Lua prototype: |
| 6262 | * |
| 6263 | * <none> core.register_task(<function>) |
| 6264 | */ |
| 6265 | static int hlua_register_task(lua_State *L) |
| 6266 | { |
| 6267 | struct hlua *hlua; |
| 6268 | struct task *task; |
| 6269 | int ref; |
| 6270 | |
| 6271 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6272 | |
| 6273 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6274 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6275 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6276 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6277 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6278 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6279 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6280 | if (!task) |
| 6281 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6282 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6283 | task->context = hlua; |
| 6284 | task->process = hlua_process_task; |
| 6285 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6286 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6287 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6288 | |
| 6289 | /* Restore the function in the stack. */ |
| 6290 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6291 | hlua->nargs = 0; |
| 6292 | |
| 6293 | /* Schedule task. */ |
| 6294 | task_schedule(task, now_ms); |
| 6295 | |
| 6296 | return 0; |
| 6297 | } |
| 6298 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6299 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6300 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6301 | * resume converters. |
| 6302 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6303 | 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] | 6304 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6305 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6306 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6307 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6308 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6309 | if (!stream) |
| 6310 | return 0; |
| 6311 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6312 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6313 | * Lua context can be not initialized. This behavior |
| 6314 | * permits to save performances because a systematic |
| 6315 | * Lua initialization cause 5% performances loss. |
| 6316 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6317 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6318 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6319 | if (!stream->hlua) { |
| 6320 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6321 | return 0; |
| 6322 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6323 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6324 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6325 | return 0; |
| 6326 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6327 | } |
| 6328 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6329 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6330 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6331 | |
| 6332 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6333 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6334 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6335 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6336 | else |
| 6337 | error = "critical error"; |
| 6338 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6339 | return 0; |
| 6340 | } |
| 6341 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6342 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6343 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6344 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6345 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6346 | return 0; |
| 6347 | } |
| 6348 | |
| 6349 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6350 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6351 | |
| 6352 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6353 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6354 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6355 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6356 | return 0; |
| 6357 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6358 | hlua_smp2lua(stream->hlua->T, smp); |
| 6359 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6360 | |
| 6361 | /* push keywords in the stack. */ |
| 6362 | if (arg_p) { |
| 6363 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6364 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6365 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6366 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6367 | return 0; |
| 6368 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6369 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6370 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6371 | } |
| 6372 | } |
| 6373 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6374 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6375 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6376 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6377 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6378 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6379 | } |
| 6380 | |
| 6381 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6382 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6383 | /* finished. */ |
| 6384 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6385 | /* If the stack is empty, the function fails. */ |
| 6386 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6387 | return 0; |
| 6388 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6389 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6390 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6391 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6392 | return 1; |
| 6393 | |
| 6394 | /* yield. */ |
| 6395 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6396 | 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] | 6397 | return 0; |
| 6398 | |
| 6399 | /* finished with error. */ |
| 6400 | case HLUA_E_ERRMSG: |
| 6401 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6402 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6403 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6404 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6405 | return 0; |
| 6406 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6407 | case HLUA_E_ETMOUT: |
| 6408 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6409 | return 0; |
| 6410 | |
| 6411 | case HLUA_E_NOMEM: |
| 6412 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6413 | return 0; |
| 6414 | |
| 6415 | case HLUA_E_YIELD: |
| 6416 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6417 | return 0; |
| 6418 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6419 | case HLUA_E_ERR: |
| 6420 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6421 | 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] | 6422 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6423 | |
| 6424 | default: |
| 6425 | return 0; |
| 6426 | } |
| 6427 | } |
| 6428 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6429 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6430 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6431 | * resume sample-fetches. This function will be called by the sample |
| 6432 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6433 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6434 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6435 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6436 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6437 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6438 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6439 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6440 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6441 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6442 | if (!stream) |
| 6443 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6444 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6445 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6446 | * Lua context can be not initialized. This behavior |
| 6447 | * permits to save performances because a systematic |
| 6448 | * Lua initialization cause 5% performances loss. |
| 6449 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6450 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6451 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6452 | if (!stream->hlua) { |
| 6453 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6454 | return 0; |
| 6455 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6456 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6457 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6458 | return 0; |
| 6459 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6460 | } |
| 6461 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6462 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6463 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6464 | |
| 6465 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6466 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6467 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6468 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6469 | else |
| 6470 | error = "critical error"; |
| 6471 | 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] | 6472 | return 0; |
| 6473 | } |
| 6474 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6475 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6476 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6477 | 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] | 6478 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6479 | return 0; |
| 6480 | } |
| 6481 | |
| 6482 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6483 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6484 | |
| 6485 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6486 | 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] | 6487 | 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] | 6488 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6489 | return 0; |
| 6490 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6491 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6492 | |
| 6493 | /* push keywords in the stack. */ |
| 6494 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6495 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6496 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6497 | 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] | 6498 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6499 | return 0; |
| 6500 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6501 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6502 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6503 | } |
| 6504 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6505 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6506 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6507 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6508 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6509 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6510 | } |
| 6511 | |
| 6512 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6513 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6514 | /* finished. */ |
| 6515 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6516 | /* If the stack is empty, the function fails. */ |
| 6517 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6518 | return 0; |
| 6519 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6520 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6521 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6522 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6523 | |
| 6524 | /* Set the end of execution flag. */ |
| 6525 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6526 | return 1; |
| 6527 | |
| 6528 | /* yield. */ |
| 6529 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6530 | 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] | 6531 | return 0; |
| 6532 | |
| 6533 | /* finished with error. */ |
| 6534 | case HLUA_E_ERRMSG: |
| 6535 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6536 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6537 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6538 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6539 | return 0; |
| 6540 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6541 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6542 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6543 | return 0; |
| 6544 | |
| 6545 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6546 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6547 | return 0; |
| 6548 | |
| 6549 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6550 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6551 | return 0; |
| 6552 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6553 | case HLUA_E_ERR: |
| 6554 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6555 | 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] | 6556 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6557 | |
| 6558 | default: |
| 6559 | return 0; |
| 6560 | } |
| 6561 | } |
| 6562 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6563 | /* This function is an LUA binding used for registering |
| 6564 | * "sample-conv" functions. It expects a converter name used |
| 6565 | * in the haproxy configuration file, and an LUA function. |
| 6566 | */ |
| 6567 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6568 | { |
| 6569 | struct sample_conv_kw_list *sck; |
| 6570 | const char *name; |
| 6571 | int ref; |
| 6572 | int len; |
| 6573 | struct hlua_function *fcn; |
| 6574 | |
| 6575 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6576 | |
| 6577 | /* First argument : converter name. */ |
| 6578 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6579 | |
| 6580 | /* Second argument : lua function. */ |
| 6581 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6582 | |
| 6583 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6584 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6585 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6586 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6587 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6588 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6589 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6590 | |
| 6591 | /* Fill fcn. */ |
| 6592 | fcn->name = strdup(name); |
| 6593 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6594 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6595 | fcn->function_ref = ref; |
| 6596 | |
| 6597 | /* List head */ |
| 6598 | sck->list.n = sck->list.p = NULL; |
| 6599 | |
| 6600 | /* converter keyword. */ |
| 6601 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6602 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6603 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6604 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6605 | |
| 6606 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6607 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6608 | 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] | 6609 | sck->kw[0].val_args = NULL; |
| 6610 | sck->kw[0].in_type = SMP_T_STR; |
| 6611 | sck->kw[0].out_type = SMP_T_STR; |
| 6612 | sck->kw[0].private = fcn; |
| 6613 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6614 | /* Register this new converter */ |
| 6615 | sample_register_convs(sck); |
| 6616 | |
| 6617 | return 0; |
| 6618 | } |
| 6619 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6620 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6621 | * "sample-fetch" functions. It expects a converter name used |
| 6622 | * in the haproxy configuration file, and an LUA function. |
| 6623 | */ |
| 6624 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6625 | { |
| 6626 | const char *name; |
| 6627 | int ref; |
| 6628 | int len; |
| 6629 | struct sample_fetch_kw_list *sfk; |
| 6630 | struct hlua_function *fcn; |
| 6631 | |
| 6632 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6633 | |
| 6634 | /* First argument : sample-fetch name. */ |
| 6635 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6636 | |
| 6637 | /* Second argument : lua function. */ |
| 6638 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6639 | |
| 6640 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6641 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6642 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6643 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6644 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6645 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6646 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6647 | |
| 6648 | /* Fill fcn. */ |
| 6649 | fcn->name = strdup(name); |
| 6650 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6651 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6652 | fcn->function_ref = ref; |
| 6653 | |
| 6654 | /* List head */ |
| 6655 | sfk->list.n = sfk->list.p = NULL; |
| 6656 | |
| 6657 | /* sample-fetch keyword. */ |
| 6658 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6659 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6660 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6661 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6662 | |
| 6663 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6664 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6665 | 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] | 6666 | sfk->kw[0].val_args = NULL; |
| 6667 | sfk->kw[0].out_type = SMP_T_STR; |
| 6668 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6669 | sfk->kw[0].val = 0; |
| 6670 | sfk->kw[0].private = fcn; |
| 6671 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6672 | /* Register this new fetch. */ |
| 6673 | sample_register_fetches(sfk); |
| 6674 | |
| 6675 | return 0; |
| 6676 | } |
| 6677 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6678 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6679 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6680 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6681 | { |
| 6682 | struct hlua *hlua = hlua_gethlua(L); |
| 6683 | unsigned int delay; |
| 6684 | unsigned int wakeup_ms; |
| 6685 | |
| 6686 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6687 | |
| 6688 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6689 | wakeup_ms = tick_add(now_ms, delay); |
| 6690 | hlua->wake_time = wakeup_ms; |
| 6691 | return 0; |
| 6692 | } |
| 6693 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6694 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6695 | * wrapper during the initialisation period. This function may return any |
| 6696 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6697 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6698 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6699 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6700 | 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] | 6701 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6702 | { |
| 6703 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6704 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6705 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6706 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6707 | |
| 6708 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6709 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6710 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6711 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6712 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6713 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6714 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6715 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6716 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6717 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6718 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6719 | * Lua context can be not initialized. This behavior |
| 6720 | * permits to save performances because a systematic |
| 6721 | * Lua initialization cause 5% performances loss. |
| 6722 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6723 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6724 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6725 | if (!s->hlua) { |
| 6726 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6727 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6728 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6729 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6730 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6731 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6732 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6733 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6734 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6735 | } |
| 6736 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6737 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6738 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6739 | |
| 6740 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6741 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6742 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6743 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6744 | else |
| 6745 | error = "critical error"; |
| 6746 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6747 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6748 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6749 | } |
| 6750 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6751 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6752 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6753 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6754 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6755 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6756 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6757 | } |
| 6758 | |
| 6759 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6760 | 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] | 6761 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6762 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6763 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6764 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6765 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6766 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6767 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6768 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6769 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6770 | |
| 6771 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6772 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6773 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6774 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6775 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6776 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6777 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6778 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6779 | lua_pushstring(s->hlua->T, *arg); |
| 6780 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6781 | } |
| 6782 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6783 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6784 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6785 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6786 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6787 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6788 | } |
| 6789 | |
| 6790 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6791 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6792 | /* finished. */ |
| 6793 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6794 | /* Catch the return value */ |
| 6795 | if (lua_gettop(s->hlua->T) > 0) |
| 6796 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6797 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6798 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6799 | if (act_ret == ACT_RET_YIELD) { |
| 6800 | if (flags & ACT_OPT_FINAL) |
| 6801 | goto err_yield; |
| 6802 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6803 | if (dir == SMP_OPT_DIR_REQ) |
| 6804 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6805 | s->hlua->wake_time); |
| 6806 | else |
| 6807 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6808 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6809 | } |
| 6810 | goto end; |
| 6811 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6812 | /* yield. */ |
| 6813 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6814 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6815 | if (dir == SMP_OPT_DIR_REQ) |
| 6816 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6817 | s->hlua->wake_time); |
| 6818 | else |
| 6819 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6820 | s->hlua->wake_time); |
| 6821 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6822 | /* Some actions can be wake up when a "write" event |
| 6823 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6824 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6825 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6826 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6827 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6828 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6829 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6830 | act_ret = ACT_RET_YIELD; |
| 6831 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6832 | |
| 6833 | /* finished with error. */ |
| 6834 | case HLUA_E_ERRMSG: |
| 6835 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6836 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6837 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6838 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6839 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6840 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6841 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6842 | 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] | 6843 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6844 | |
| 6845 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6846 | 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] | 6847 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6848 | |
| 6849 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6850 | err_yield: |
| 6851 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6852 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6853 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6854 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6855 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6856 | case HLUA_E_ERR: |
| 6857 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6858 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6859 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6860 | |
| 6861 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6862 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6863 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6864 | |
| 6865 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6866 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6867 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6868 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6869 | } |
| 6870 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6871 | 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] | 6872 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6873 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6874 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6875 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6876 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6877 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6878 | } |
| 6879 | |
| 6880 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6881 | { |
| 6882 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6883 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6884 | struct task *task; |
| 6885 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6886 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6887 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6888 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6889 | if (!hlua) { |
| 6890 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6891 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6892 | return 0; |
| 6893 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6894 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6895 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6896 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6897 | |
| 6898 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6899 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6900 | if (!task) { |
| 6901 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6902 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6903 | return 0; |
| 6904 | } |
| 6905 | task->nice = 0; |
| 6906 | task->context = ctx; |
| 6907 | task->process = hlua_applet_wakeup; |
| 6908 | ctx->ctx.hlua_apptcp.task = task; |
| 6909 | |
| 6910 | /* In the execution wrappers linked with a stream, the |
| 6911 | * Lua context can be not initialized. This behavior |
| 6912 | * permits to save performances because a systematic |
| 6913 | * Lua initialization cause 5% performances loss. |
| 6914 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6915 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6916 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6917 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6918 | return 0; |
| 6919 | } |
| 6920 | |
| 6921 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6922 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6923 | |
| 6924 | /* The following Lua calls can fail. */ |
| 6925 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6926 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6927 | error = lua_tostring(hlua->T, -1); |
| 6928 | else |
| 6929 | error = "critical error"; |
| 6930 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6931 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6932 | return 0; |
| 6933 | } |
| 6934 | |
| 6935 | /* Check stack available size. */ |
| 6936 | if (!lua_checkstack(hlua->T, 1)) { |
| 6937 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6938 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6939 | RESET_SAFE_LJMP(hlua->T); |
| 6940 | return 0; |
| 6941 | } |
| 6942 | |
| 6943 | /* Restore the function in the stack. */ |
| 6944 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6945 | |
| 6946 | /* Create and and push object stream in the stack. */ |
| 6947 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6948 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6949 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6950 | RESET_SAFE_LJMP(hlua->T); |
| 6951 | return 0; |
| 6952 | } |
| 6953 | hlua->nargs = 1; |
| 6954 | |
| 6955 | /* push keywords in the stack. */ |
| 6956 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6957 | if (!lua_checkstack(hlua->T, 1)) { |
| 6958 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6959 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6960 | RESET_SAFE_LJMP(hlua->T); |
| 6961 | return 0; |
| 6962 | } |
| 6963 | lua_pushstring(hlua->T, *arg); |
| 6964 | hlua->nargs++; |
| 6965 | } |
| 6966 | |
| 6967 | RESET_SAFE_LJMP(hlua->T); |
| 6968 | |
| 6969 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6970 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6971 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6972 | |
| 6973 | return 1; |
| 6974 | } |
| 6975 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6976 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6977 | { |
| 6978 | struct stream_interface *si = ctx->owner; |
| 6979 | struct stream *strm = si_strm(si); |
| 6980 | struct channel *res = si_ic(si); |
| 6981 | struct act_rule *rule = ctx->rule; |
| 6982 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6983 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6984 | |
| 6985 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6986 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6987 | /* eat the whole request */ |
| 6988 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6989 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6990 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6991 | |
| 6992 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6993 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6994 | return; |
| 6995 | |
| 6996 | /* Execute the function. */ |
| 6997 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6998 | /* finished. */ |
| 6999 | case HLUA_E_OK: |
| 7000 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7001 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7002 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7003 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7004 | res->flags |= CF_READ_NULL; |
| 7005 | si_shutr(si); |
| 7006 | return; |
| 7007 | |
| 7008 | /* yield. */ |
| 7009 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7010 | if (hlua->wake_time != TICK_ETERNITY) |
| 7011 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7012 | return; |
| 7013 | |
| 7014 | /* finished with error. */ |
| 7015 | case HLUA_E_ERRMSG: |
| 7016 | /* Display log. */ |
| 7017 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7018 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7019 | lua_pop(hlua->T, 1); |
| 7020 | goto error; |
| 7021 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7022 | case HLUA_E_ETMOUT: |
| 7023 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7024 | rule->arg.hlua_rule->fcn.name); |
| 7025 | goto error; |
| 7026 | |
| 7027 | case HLUA_E_NOMEM: |
| 7028 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7029 | rule->arg.hlua_rule->fcn.name); |
| 7030 | goto error; |
| 7031 | |
| 7032 | case HLUA_E_YIELD: /* unexpected */ |
| 7033 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7034 | rule->arg.hlua_rule->fcn.name); |
| 7035 | goto error; |
| 7036 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7037 | case HLUA_E_ERR: |
| 7038 | /* Display log. */ |
| 7039 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7040 | rule->arg.hlua_rule->fcn.name); |
| 7041 | goto error; |
| 7042 | |
| 7043 | default: |
| 7044 | goto error; |
| 7045 | } |
| 7046 | |
| 7047 | error: |
| 7048 | |
| 7049 | /* For all other cases, just close the stream. */ |
| 7050 | si_shutw(si); |
| 7051 | si_shutr(si); |
| 7052 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7053 | } |
| 7054 | |
| 7055 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7056 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7057 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7058 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7059 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7060 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7061 | } |
| 7062 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7063 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7064 | * an errors occurs and -1 if more data are required for initializing |
| 7065 | * the applet. |
| 7066 | */ |
| 7067 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7068 | { |
| 7069 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7070 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7071 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7072 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7073 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7074 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7075 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7076 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7077 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7078 | if (!hlua) { |
| 7079 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7080 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7081 | return 0; |
| 7082 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7083 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7084 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7085 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7086 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7087 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7088 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7089 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7090 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7091 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7092 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7093 | if (!task) { |
| 7094 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7095 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7096 | return 0; |
| 7097 | } |
| 7098 | task->nice = 0; |
| 7099 | task->context = ctx; |
| 7100 | task->process = hlua_applet_wakeup; |
| 7101 | ctx->ctx.hlua_apphttp.task = task; |
| 7102 | |
| 7103 | /* In the execution wrappers linked with a stream, the |
| 7104 | * Lua context can be not initialized. This behavior |
| 7105 | * permits to save performances because a systematic |
| 7106 | * Lua initialization cause 5% performances loss. |
| 7107 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7108 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7109 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7110 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7111 | return 0; |
| 7112 | } |
| 7113 | |
| 7114 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7115 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7116 | |
| 7117 | /* The following Lua calls can fail. */ |
| 7118 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7119 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7120 | error = lua_tostring(hlua->T, -1); |
| 7121 | else |
| 7122 | error = "critical error"; |
| 7123 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7124 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7125 | return 0; |
| 7126 | } |
| 7127 | |
| 7128 | /* Check stack available size. */ |
| 7129 | if (!lua_checkstack(hlua->T, 1)) { |
| 7130 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7131 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7132 | RESET_SAFE_LJMP(hlua->T); |
| 7133 | return 0; |
| 7134 | } |
| 7135 | |
| 7136 | /* Restore the function in the stack. */ |
| 7137 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7138 | |
| 7139 | /* Create and and push object stream in the stack. */ |
| 7140 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7141 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7142 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7143 | RESET_SAFE_LJMP(hlua->T); |
| 7144 | return 0; |
| 7145 | } |
| 7146 | hlua->nargs = 1; |
| 7147 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7148 | /* push keywords in the stack. */ |
| 7149 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7150 | if (!lua_checkstack(hlua->T, 1)) { |
| 7151 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7152 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7153 | RESET_SAFE_LJMP(hlua->T); |
| 7154 | return 0; |
| 7155 | } |
| 7156 | lua_pushstring(hlua->T, *arg); |
| 7157 | hlua->nargs++; |
| 7158 | } |
| 7159 | |
| 7160 | RESET_SAFE_LJMP(hlua->T); |
| 7161 | |
| 7162 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7163 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7164 | |
| 7165 | return 1; |
| 7166 | } |
| 7167 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7168 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7169 | { |
| 7170 | struct stream_interface *si = ctx->owner; |
| 7171 | struct stream *strm = si_strm(si); |
| 7172 | struct channel *req = si_oc(si); |
| 7173 | struct channel *res = si_ic(si); |
| 7174 | struct act_rule *rule = ctx->rule; |
| 7175 | struct proxy *px = strm->be; |
| 7176 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7177 | struct htx *req_htx, *res_htx; |
| 7178 | |
| 7179 | res_htx = htx_from_buf(&res->buf); |
| 7180 | |
| 7181 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7182 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7183 | goto out; |
| 7184 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7185 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7186 | if (!b_size(&res->buf)) { |
| 7187 | si_rx_room_blk(si); |
| 7188 | goto out; |
| 7189 | } |
| 7190 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7191 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7192 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7193 | |
| 7194 | /* Set the currently running flag. */ |
| 7195 | if (!HLUA_IS_RUNNING(hlua) && |
| 7196 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7197 | struct htx_blk *blk; |
| 7198 | size_t count = co_data(req); |
| 7199 | |
| 7200 | if (!count) { |
| 7201 | si_cant_get(si); |
| 7202 | goto out; |
| 7203 | } |
| 7204 | |
| 7205 | /* We need to flush the request header. This left the body for |
| 7206 | * the Lua. |
| 7207 | */ |
| 7208 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7209 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7210 | while (count && blk) { |
| 7211 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7212 | uint32_t sz = htx_get_blksz(blk); |
| 7213 | |
| 7214 | if (sz > count) { |
| 7215 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7216 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7217 | goto out; |
| 7218 | } |
| 7219 | |
| 7220 | count -= sz; |
| 7221 | co_set_data(req, co_data(req) - sz); |
| 7222 | blk = htx_remove_blk(req_htx, blk); |
| 7223 | |
| 7224 | if (type == HTX_BLK_EOH) |
| 7225 | break; |
| 7226 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7227 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7228 | } |
| 7229 | |
| 7230 | /* Executes The applet if it is not done. */ |
| 7231 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7232 | |
| 7233 | /* Execute the function. */ |
| 7234 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7235 | /* finished. */ |
| 7236 | case HLUA_E_OK: |
| 7237 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7238 | break; |
| 7239 | |
| 7240 | /* yield. */ |
| 7241 | case HLUA_E_AGAIN: |
| 7242 | if (hlua->wake_time != TICK_ETERNITY) |
| 7243 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7244 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7245 | |
| 7246 | /* finished with error. */ |
| 7247 | case HLUA_E_ERRMSG: |
| 7248 | /* Display log. */ |
| 7249 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7250 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7251 | lua_pop(hlua->T, 1); |
| 7252 | goto error; |
| 7253 | |
| 7254 | case HLUA_E_ETMOUT: |
| 7255 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7256 | rule->arg.hlua_rule->fcn.name); |
| 7257 | goto error; |
| 7258 | |
| 7259 | case HLUA_E_NOMEM: |
| 7260 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7261 | rule->arg.hlua_rule->fcn.name); |
| 7262 | goto error; |
| 7263 | |
| 7264 | case HLUA_E_YIELD: /* unexpected */ |
| 7265 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7266 | rule->arg.hlua_rule->fcn.name); |
| 7267 | goto error; |
| 7268 | |
| 7269 | case HLUA_E_ERR: |
| 7270 | /* Display log. */ |
| 7271 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7272 | rule->arg.hlua_rule->fcn.name); |
| 7273 | goto error; |
| 7274 | |
| 7275 | default: |
| 7276 | goto error; |
| 7277 | } |
| 7278 | } |
| 7279 | |
| 7280 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7281 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7282 | goto done; |
| 7283 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7284 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7285 | goto error; |
| 7286 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7287 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7288 | 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] | 7289 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7290 | si_rx_room_blk(si); |
| 7291 | goto out; |
| 7292 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7293 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7294 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7295 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7296 | } |
| 7297 | |
| 7298 | done: |
| 7299 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7300 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7301 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7302 | si_shutr(si); |
| 7303 | } |
| 7304 | |
| 7305 | /* eat the whole request */ |
| 7306 | if (co_data(req)) { |
| 7307 | req_htx = htx_from_buf(&req->buf); |
| 7308 | co_htx_skip(req, req_htx, co_data(req)); |
| 7309 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7310 | } |
| 7311 | } |
| 7312 | |
| 7313 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7314 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7315 | return; |
| 7316 | |
| 7317 | error: |
| 7318 | |
| 7319 | /* If we are in HTTP mode, and we are not send any |
| 7320 | * data, return a 500 server error in best effort: |
| 7321 | * if there is no room available in the buffer, |
| 7322 | * just close the connection. |
| 7323 | */ |
| 7324 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7325 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7326 | |
| 7327 | channel_erase(res); |
| 7328 | res->buf.data = b_data(err); |
| 7329 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7330 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7331 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7332 | } |
| 7333 | if (!(strm->flags & SF_ERR_MASK)) |
| 7334 | strm->flags |= SF_ERR_RESOURCE; |
| 7335 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7336 | goto done; |
| 7337 | } |
| 7338 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7339 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7340 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7341 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7342 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7343 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7344 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7345 | } |
| 7346 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7347 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7348 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7349 | * |
| 7350 | * This function can fail with an abort() due to an Lua critical error. |
| 7351 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7352 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7353 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7354 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7355 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7356 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7357 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7358 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7359 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7360 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7361 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7362 | if (!rule->arg.hlua_rule) { |
| 7363 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7364 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7365 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7366 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7367 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7368 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7369 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7370 | if (!rule->arg.hlua_rule->args) { |
| 7371 | memprintf(err, "out of memory error"); |
| 7372 | return ACT_RET_PRS_ERR; |
| 7373 | } |
| 7374 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7375 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7376 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7377 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7378 | /* Expect some arguments */ |
| 7379 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7380 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7381 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7382 | return ACT_RET_PRS_ERR; |
| 7383 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7384 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7385 | if (!rule->arg.hlua_rule->args[i]) { |
| 7386 | memprintf(err, "out of memory error"); |
| 7387 | return ACT_RET_PRS_ERR; |
| 7388 | } |
| 7389 | (*cur_arg)++; |
| 7390 | } |
| 7391 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7392 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7393 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7394 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7395 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7396 | } |
| 7397 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7398 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7399 | struct act_rule *rule, char **err) |
| 7400 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7401 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7402 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7403 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7404 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7405 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7406 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7407 | * the call of this analyzer. |
| 7408 | */ |
| 7409 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7410 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7411 | return ACT_RET_PRS_ERR; |
| 7412 | } |
| 7413 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7414 | /* Memory for the rule. */ |
| 7415 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7416 | if (!rule->arg.hlua_rule) { |
| 7417 | memprintf(err, "out of memory error"); |
| 7418 | return ACT_RET_PRS_ERR; |
| 7419 | } |
| 7420 | |
| 7421 | /* Reference the Lua function and store the reference. */ |
| 7422 | rule->arg.hlua_rule->fcn = *fcn; |
| 7423 | |
| 7424 | /* TODO: later accept arguments. */ |
| 7425 | rule->arg.hlua_rule->args = NULL; |
| 7426 | |
| 7427 | /* Add applet pointer in the rule. */ |
| 7428 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7429 | rule->applet.name = fcn->name; |
| 7430 | rule->applet.init = hlua_applet_http_init; |
| 7431 | rule->applet.fct = hlua_applet_http_fct; |
| 7432 | rule->applet.release = hlua_applet_http_release; |
| 7433 | rule->applet.timeout = hlua_timeout_applet; |
| 7434 | |
| 7435 | return ACT_RET_PRS_OK; |
| 7436 | } |
| 7437 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7438 | /* This function is an LUA binding used for registering |
| 7439 | * "sample-conv" functions. It expects a converter name used |
| 7440 | * in the haproxy configuration file, and an LUA function. |
| 7441 | */ |
| 7442 | __LJMP static int hlua_register_action(lua_State *L) |
| 7443 | { |
| 7444 | struct action_kw_list *akl; |
| 7445 | const char *name; |
| 7446 | int ref; |
| 7447 | int len; |
| 7448 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7449 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7450 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7451 | /* Initialise the number of expected arguments at 0. */ |
| 7452 | nargs = 0; |
| 7453 | |
| 7454 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7455 | 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] | 7456 | |
| 7457 | /* First argument : converter name. */ |
| 7458 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7459 | |
| 7460 | /* Second argument : environment. */ |
| 7461 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7462 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7463 | |
| 7464 | /* Third argument : lua function. */ |
| 7465 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7466 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7467 | /* 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] | 7468 | if (lua_gettop(L) >= 4) |
| 7469 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7470 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7471 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7472 | lua_pushnil(L); |
| 7473 | while (lua_next(L, 2) != 0) { |
| 7474 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7475 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7476 | |
| 7477 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7478 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7479 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7480 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7481 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7482 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7483 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7484 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7485 | |
| 7486 | /* Fill fcn. */ |
| 7487 | fcn->name = strdup(name); |
| 7488 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7489 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7490 | fcn->function_ref = ref; |
| 7491 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7492 | /* Set the expected number od arguments. */ |
| 7493 | fcn->nargs = nargs; |
| 7494 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7495 | /* List head */ |
| 7496 | akl->list.n = akl->list.p = NULL; |
| 7497 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7498 | /* action keyword. */ |
| 7499 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7500 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7501 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7502 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7503 | |
| 7504 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7505 | |
| 7506 | akl->kw[0].match_pfx = 0; |
| 7507 | akl->kw[0].private = fcn; |
| 7508 | akl->kw[0].parse = action_register_lua; |
| 7509 | |
| 7510 | /* select the action registering point. */ |
| 7511 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7512 | tcp_req_cont_keywords_register(akl); |
| 7513 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7514 | tcp_res_cont_keywords_register(akl); |
| 7515 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7516 | http_req_keywords_register(akl); |
| 7517 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7518 | http_res_keywords_register(akl); |
| 7519 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7520 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7521 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7522 | "are expected.", lua_tostring(L, -1))); |
| 7523 | |
| 7524 | /* pop the environment string. */ |
| 7525 | lua_pop(L, 1); |
| 7526 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7527 | return ACT_RET_PRS_OK; |
| 7528 | } |
| 7529 | |
| 7530 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7531 | struct act_rule *rule, char **err) |
| 7532 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7533 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7534 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7535 | if (px->mode == PR_MODE_HTTP) { |
| 7536 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7537 | return ACT_RET_PRS_ERR; |
| 7538 | } |
| 7539 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7540 | /* Memory for the rule. */ |
| 7541 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7542 | if (!rule->arg.hlua_rule) { |
| 7543 | memprintf(err, "out of memory error"); |
| 7544 | return ACT_RET_PRS_ERR; |
| 7545 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7546 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7547 | /* Reference the Lua function and store the reference. */ |
| 7548 | rule->arg.hlua_rule->fcn = *fcn; |
| 7549 | |
| 7550 | /* TODO: later accept arguments. */ |
| 7551 | rule->arg.hlua_rule->args = NULL; |
| 7552 | |
| 7553 | /* Add applet pointer in the rule. */ |
| 7554 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7555 | rule->applet.name = fcn->name; |
| 7556 | rule->applet.init = hlua_applet_tcp_init; |
| 7557 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7558 | rule->applet.release = hlua_applet_tcp_release; |
| 7559 | rule->applet.timeout = hlua_timeout_applet; |
| 7560 | |
| 7561 | return 0; |
| 7562 | } |
| 7563 | |
| 7564 | /* This function is an LUA binding used for registering |
| 7565 | * "sample-conv" functions. It expects a converter name used |
| 7566 | * in the haproxy configuration file, and an LUA function. |
| 7567 | */ |
| 7568 | __LJMP static int hlua_register_service(lua_State *L) |
| 7569 | { |
| 7570 | struct action_kw_list *akl; |
| 7571 | const char *name; |
| 7572 | const char *env; |
| 7573 | int ref; |
| 7574 | int len; |
| 7575 | struct hlua_function *fcn; |
| 7576 | |
| 7577 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7578 | |
| 7579 | /* First argument : converter name. */ |
| 7580 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7581 | |
| 7582 | /* Second argument : environment. */ |
| 7583 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7584 | |
| 7585 | /* Third argument : lua function. */ |
| 7586 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7587 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7588 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7589 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7590 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7591 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7592 | fcn = calloc(1, sizeof(*fcn)); |
| 7593 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7594 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7595 | |
| 7596 | /* Fill fcn. */ |
| 7597 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7598 | fcn->name = calloc(1, len); |
| 7599 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7600 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7601 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7602 | fcn->function_ref = ref; |
| 7603 | |
| 7604 | /* List head */ |
| 7605 | akl->list.n = akl->list.p = NULL; |
| 7606 | |
| 7607 | /* converter keyword. */ |
| 7608 | len = strlen("lua.") + strlen(name) + 1; |
| 7609 | akl->kw[0].kw = calloc(1, len); |
| 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 | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7612 | |
| 7613 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7614 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7615 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7616 | if (strcmp(env, "tcp") == 0) |
| 7617 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7618 | else if (strcmp(env, "http") == 0) |
| 7619 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7620 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7621 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7622 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7623 | |
| 7624 | akl->kw[0].match_pfx = 0; |
| 7625 | akl->kw[0].private = fcn; |
| 7626 | |
| 7627 | /* End of array. */ |
| 7628 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7629 | |
| 7630 | /* Register this new converter */ |
| 7631 | service_keywords_register(akl); |
| 7632 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7633 | return 0; |
| 7634 | } |
| 7635 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7636 | /* This function initialises Lua cli handler. It copies the |
| 7637 | * arguments in the Lua stack and create channel IO objects. |
| 7638 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7639 | 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] | 7640 | { |
| 7641 | struct hlua *hlua; |
| 7642 | struct hlua_function *fcn; |
| 7643 | int i; |
| 7644 | const char *error; |
| 7645 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7646 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7647 | appctx->ctx.hlua_cli.fcn = private; |
| 7648 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7649 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7650 | if (!hlua) { |
| 7651 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7652 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7653 | } |
| 7654 | HLUA_INIT(hlua); |
| 7655 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7656 | |
| 7657 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7658 | * 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] | 7659 | * applet_http. It is absolutely compatible. |
| 7660 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7661 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7662 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7663 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7664 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7665 | } |
| 7666 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7667 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7668 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7669 | |
| 7670 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7671 | 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] | 7672 | 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] | 7673 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7674 | } |
| 7675 | |
| 7676 | /* The following Lua calls can fail. */ |
| 7677 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7678 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7679 | error = lua_tostring(hlua->T, -1); |
| 7680 | else |
| 7681 | error = "critical error"; |
| 7682 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7683 | goto error; |
| 7684 | } |
| 7685 | |
| 7686 | /* Check stack available size. */ |
| 7687 | if (!lua_checkstack(hlua->T, 2)) { |
| 7688 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7689 | goto error; |
| 7690 | } |
| 7691 | |
| 7692 | /* Restore the function in the stack. */ |
| 7693 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7694 | |
| 7695 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7696 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7697 | */ |
| 7698 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7699 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7700 | goto error; |
| 7701 | } |
| 7702 | hlua->nargs = 1; |
| 7703 | |
| 7704 | /* push keywords in the stack. */ |
| 7705 | for (i = 0; *args[i]; i++) { |
| 7706 | /* Check stack available size. */ |
| 7707 | if (!lua_checkstack(hlua->T, 1)) { |
| 7708 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7709 | goto error; |
| 7710 | } |
| 7711 | lua_pushstring(hlua->T, args[i]); |
| 7712 | hlua->nargs++; |
| 7713 | } |
| 7714 | |
| 7715 | /* We must initialize the execution timeouts. */ |
| 7716 | hlua->max_time = hlua_timeout_session; |
| 7717 | |
| 7718 | /* At this point the execution is safe. */ |
| 7719 | RESET_SAFE_LJMP(hlua->T); |
| 7720 | |
| 7721 | /* It's ok */ |
| 7722 | return 0; |
| 7723 | |
| 7724 | /* It's not ok. */ |
| 7725 | error: |
| 7726 | RESET_SAFE_LJMP(hlua->T); |
| 7727 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7728 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7729 | return 1; |
| 7730 | } |
| 7731 | |
| 7732 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7733 | { |
| 7734 | struct hlua *hlua; |
| 7735 | struct stream_interface *si; |
| 7736 | struct hlua_function *fcn; |
| 7737 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7738 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7739 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7740 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7741 | |
| 7742 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7743 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7744 | return 1; |
| 7745 | |
| 7746 | /* Execute the function. */ |
| 7747 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7748 | |
| 7749 | /* finished. */ |
| 7750 | case HLUA_E_OK: |
| 7751 | return 1; |
| 7752 | |
| 7753 | /* yield. */ |
| 7754 | case HLUA_E_AGAIN: |
| 7755 | /* We want write. */ |
| 7756 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7757 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7758 | /* Set the timeout. */ |
| 7759 | if (hlua->wake_time != TICK_ETERNITY) |
| 7760 | task_schedule(hlua->task, hlua->wake_time); |
| 7761 | return 0; |
| 7762 | |
| 7763 | /* finished with error. */ |
| 7764 | case HLUA_E_ERRMSG: |
| 7765 | /* Display log. */ |
| 7766 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7767 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7768 | lua_pop(hlua->T, 1); |
| 7769 | return 1; |
| 7770 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7771 | case HLUA_E_ETMOUT: |
| 7772 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7773 | fcn->name); |
| 7774 | return 1; |
| 7775 | |
| 7776 | case HLUA_E_NOMEM: |
| 7777 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7778 | fcn->name); |
| 7779 | return 1; |
| 7780 | |
| 7781 | case HLUA_E_YIELD: /* unexpected */ |
| 7782 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7783 | fcn->name); |
| 7784 | return 1; |
| 7785 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7786 | case HLUA_E_ERR: |
| 7787 | /* Display log. */ |
| 7788 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7789 | fcn->name); |
| 7790 | return 1; |
| 7791 | |
| 7792 | default: |
| 7793 | return 1; |
| 7794 | } |
| 7795 | |
| 7796 | return 1; |
| 7797 | } |
| 7798 | |
| 7799 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7800 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7801 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7802 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7803 | } |
| 7804 | |
| 7805 | /* This function is an LUA binding used for registering |
| 7806 | * new keywords in the cli. It expects a list of keywords |
| 7807 | * which are the "path". It is limited to 5 keywords. A |
| 7808 | * description of the command, a function to be executed |
| 7809 | * for the parsing and a function for io handlers. |
| 7810 | */ |
| 7811 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7812 | { |
| 7813 | struct cli_kw_list *cli_kws; |
| 7814 | const char *message; |
| 7815 | int ref_io; |
| 7816 | int len; |
| 7817 | struct hlua_function *fcn; |
| 7818 | int index; |
| 7819 | int i; |
| 7820 | |
| 7821 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7822 | |
| 7823 | /* First argument : an array of maximum 5 keywords. */ |
| 7824 | if (!lua_istable(L, 1)) |
| 7825 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7826 | |
| 7827 | /* Second argument : string with contextual message. */ |
| 7828 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7829 | |
| 7830 | /* Third and fourth argument : lua function. */ |
| 7831 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7832 | |
| 7833 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7834 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7835 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7836 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7837 | fcn = calloc(1, sizeof(*fcn)); |
| 7838 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7839 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7840 | |
| 7841 | /* Fill path. */ |
| 7842 | index = 0; |
| 7843 | lua_pushnil(L); |
| 7844 | while(lua_next(L, 1) != 0) { |
| 7845 | if (index >= 5) |
| 7846 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7847 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7848 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7849 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7850 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7851 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7852 | index++; |
| 7853 | lua_pop(L, 1); |
| 7854 | } |
| 7855 | |
| 7856 | /* Copy help message. */ |
| 7857 | cli_kws->kw[0].usage = strdup(message); |
| 7858 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7859 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7860 | |
| 7861 | /* Fill fcn io handler. */ |
| 7862 | len = strlen("<lua.cli>") + 1; |
| 7863 | for (i = 0; i < index; i++) |
| 7864 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7865 | fcn->name = calloc(1, len); |
| 7866 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7867 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7868 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7869 | for (i = 0; i < index; i++) { |
| 7870 | strncat((char *)fcn->name, ".", len); |
| 7871 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7872 | } |
| 7873 | strncat((char *)fcn->name, ">", len); |
| 7874 | fcn->function_ref = ref_io; |
| 7875 | |
| 7876 | /* Fill last entries. */ |
| 7877 | cli_kws->kw[0].private = fcn; |
| 7878 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7879 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7880 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7881 | |
| 7882 | /* Register this new converter */ |
| 7883 | cli_register_kw(cli_kws); |
| 7884 | |
| 7885 | return 0; |
| 7886 | } |
| 7887 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7888 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7889 | struct proxy *defpx, const char *file, int line, |
| 7890 | char **err, unsigned int *timeout) |
| 7891 | { |
| 7892 | const char *error; |
| 7893 | |
| 7894 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7895 | if (error == PARSE_TIME_OVER) { |
| 7896 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7897 | args[1], args[0]); |
| 7898 | return -1; |
| 7899 | } |
| 7900 | else if (error == PARSE_TIME_UNDER) { |
| 7901 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7902 | args[1], args[0]); |
| 7903 | return -1; |
| 7904 | } |
| 7905 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7906 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7907 | return -1; |
| 7908 | } |
| 7909 | return 0; |
| 7910 | } |
| 7911 | |
| 7912 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7913 | struct proxy *defpx, const char *file, int line, |
| 7914 | char **err) |
| 7915 | { |
| 7916 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7917 | file, line, err, &hlua_timeout_session); |
| 7918 | } |
| 7919 | |
| 7920 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7921 | struct proxy *defpx, const char *file, int line, |
| 7922 | char **err) |
| 7923 | { |
| 7924 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7925 | file, line, err, &hlua_timeout_task); |
| 7926 | } |
| 7927 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7928 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7929 | struct proxy *defpx, const char *file, int line, |
| 7930 | char **err) |
| 7931 | { |
| 7932 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7933 | file, line, err, &hlua_timeout_applet); |
| 7934 | } |
| 7935 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7936 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7937 | struct proxy *defpx, const char *file, int line, |
| 7938 | char **err) |
| 7939 | { |
| 7940 | char *error; |
| 7941 | |
| 7942 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7943 | if (*error != '\0') { |
| 7944 | memprintf(err, "%s: invalid number", args[0]); |
| 7945 | return -1; |
| 7946 | } |
| 7947 | return 0; |
| 7948 | } |
| 7949 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7950 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7951 | struct proxy *defpx, const char *file, int line, |
| 7952 | char **err) |
| 7953 | { |
| 7954 | char *error; |
| 7955 | |
| 7956 | if (*(args[1]) == 0) { |
| 7957 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7958 | return -1; |
| 7959 | } |
| 7960 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7961 | if (*error != '\0') { |
| 7962 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7963 | return -1; |
| 7964 | } |
| 7965 | return 0; |
| 7966 | } |
| 7967 | |
| 7968 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7969 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7970 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7971 | * the main lua entry point. |
| 7972 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7973 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7974 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7975 | * |
| 7976 | * In some error case, LUA set an error message in top of the stack. This function |
| 7977 | * 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] | 7978 | * |
| 7979 | * This function can fail with an abort() due to an Lua critical error. |
| 7980 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7981 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7982 | */ |
| 7983 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7984 | struct proxy *defpx, const char *file, int line, |
| 7985 | char **err) |
| 7986 | { |
| 7987 | int error; |
| 7988 | |
| 7989 | /* Just load and compile the file. */ |
| 7990 | error = luaL_loadfile(gL.T, args[1]); |
| 7991 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7992 | 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] | 7993 | lua_pop(gL.T, 1); |
| 7994 | return -1; |
| 7995 | } |
| 7996 | |
| 7997 | /* If no syntax error where detected, execute the code. */ |
| 7998 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7999 | switch (error) { |
| 8000 | case LUA_OK: |
| 8001 | break; |
| 8002 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8003 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8004 | lua_pop(gL.T, 1); |
| 8005 | return -1; |
| 8006 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8007 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8008 | return -1; |
| 8009 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8010 | 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] | 8011 | lua_pop(gL.T, 1); |
| 8012 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8013 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8014 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8015 | 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] | 8016 | lua_pop(gL.T, 1); |
| 8017 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8018 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8019 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8020 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8021 | lua_pop(gL.T, 1); |
| 8022 | return -1; |
| 8023 | } |
| 8024 | |
| 8025 | return 0; |
| 8026 | } |
| 8027 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8028 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8029 | * in the given <ctx>. |
| 8030 | */ |
| 8031 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 8032 | { |
| 8033 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 8034 | lua_pushstring(ctx.T, path); /* push given path */ |
| 8035 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 8036 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 8037 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 8038 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 8039 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 8040 | |
| 8041 | return 0; |
| 8042 | } |
| 8043 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8044 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 8045 | struct proxy *defpx, const char *file, int line, |
| 8046 | char **err) |
| 8047 | { |
| 8048 | char *path; |
| 8049 | char *type = "path"; |
| 8050 | if (too_many_args(2, args, err, NULL)) { |
| 8051 | return -1; |
| 8052 | } |
| 8053 | |
| 8054 | if (!(*args[1])) { |
| 8055 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 8056 | return -1; |
| 8057 | } |
| 8058 | path = args[1]; |
| 8059 | |
| 8060 | if (*args[2]) { |
| 8061 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8062 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 8063 | return -1; |
| 8064 | } |
| 8065 | type = args[2]; |
| 8066 | } |
| 8067 | |
| 8068 | return hlua_prepend_path(gL, type, path); |
| 8069 | } |
| 8070 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8071 | /* configuration keywords declaration */ |
| 8072 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8073 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8074 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8075 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8076 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8077 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8078 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8079 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8080 | { 0, NULL, NULL }, |
| 8081 | }}; |
| 8082 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8083 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8084 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8085 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8086 | /* This function can fail with an abort() due to an Lua critical error. |
| 8087 | * We are in the initialisation process of HAProxy, this abort() is |
| 8088 | * tolerated. |
| 8089 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8090 | int hlua_post_init() |
| 8091 | { |
| 8092 | struct hlua_init_function *init; |
| 8093 | const char *msg; |
| 8094 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8095 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8096 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8097 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8098 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8099 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8100 | error = lua_tostring(gL.T, -1); |
| 8101 | else |
| 8102 | error = "critical error"; |
| 8103 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8104 | exit(1); |
| 8105 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8106 | |
| 8107 | #if USE_OPENSSL |
| 8108 | /* Initialize SSL server. */ |
| 8109 | if (socket_ssl.xprt->prepare_srv) { |
| 8110 | int saved_used_backed = global.ssl_used_backend; |
| 8111 | // don't affect maxconn automatic computation |
| 8112 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8113 | global.ssl_used_backend = saved_used_backed; |
| 8114 | } |
| 8115 | #endif |
| 8116 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8117 | hlua_fcn_post_init(gL.T); |
| 8118 | RESET_SAFE_LJMP(gL.T); |
| 8119 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8120 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8121 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8122 | ret = hlua_ctx_resume(&gL, 0); |
| 8123 | switch (ret) { |
| 8124 | case HLUA_E_OK: |
| 8125 | lua_pop(gL.T, -1); |
| 8126 | return 1; |
| 8127 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8128 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8129 | return 0; |
| 8130 | case HLUA_E_ERRMSG: |
| 8131 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8132 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8133 | return 0; |
| 8134 | case HLUA_E_ERR: |
| 8135 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8136 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8137 | return 0; |
| 8138 | } |
| 8139 | } |
| 8140 | return 1; |
| 8141 | } |
| 8142 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8143 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8144 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8145 | * is the previously allocated size or the kind of object in case of a new |
| 8146 | * allocation. <nsize> is the requested new size. |
| 8147 | */ |
| 8148 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8149 | { |
| 8150 | struct hlua_mem_allocator *zone = ud; |
| 8151 | |
| 8152 | if (nsize == 0) { |
| 8153 | /* it's a free */ |
| 8154 | if (ptr) |
| 8155 | zone->allocated -= osize; |
| 8156 | free(ptr); |
| 8157 | return NULL; |
| 8158 | } |
| 8159 | |
| 8160 | if (!ptr) { |
| 8161 | /* it's a new allocation */ |
| 8162 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8163 | return NULL; |
| 8164 | |
| 8165 | ptr = malloc(nsize); |
| 8166 | if (ptr) |
| 8167 | zone->allocated += nsize; |
| 8168 | return ptr; |
| 8169 | } |
| 8170 | |
| 8171 | /* it's a realloc */ |
| 8172 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8173 | return NULL; |
| 8174 | |
| 8175 | ptr = realloc(ptr, nsize); |
| 8176 | if (ptr) |
| 8177 | zone->allocated += nsize - osize; |
| 8178 | return ptr; |
| 8179 | } |
| 8180 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8181 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8182 | * We are in the initialisation process of HAProxy, this abort() is |
| 8183 | * tolerated. |
| 8184 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8185 | void hlua_init(void) |
| 8186 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8187 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8188 | int idx; |
| 8189 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8190 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8191 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8192 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8193 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8194 | struct srv_kw *kw; |
| 8195 | int tmp_error; |
| 8196 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8197 | char *args[] = { /* SSL client configuration. */ |
| 8198 | "ssl", |
| 8199 | "verify", |
| 8200 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8201 | NULL |
| 8202 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8203 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8204 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8205 | /* Init main lua stack. */ |
| 8206 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8207 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8208 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8209 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8210 | hlua_sethlua(&gL); |
| 8211 | gL.Tref = LUA_REFNIL; |
| 8212 | gL.task = NULL; |
| 8213 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8214 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8215 | * the Lua function can fail with an abort. We are in the initialisation |
| 8216 | * process of HAProxy, this abort() is tolerated. |
| 8217 | */ |
| 8218 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8219 | /* Initialise lua. */ |
| 8220 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8221 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8222 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8223 | #ifdef HLUA_PREPEND_PATH |
| 8224 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8225 | #endif |
| 8226 | #ifdef HLUA_PREPEND_CPATH |
| 8227 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8228 | #endif |
| 8229 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8230 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8231 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8232 | /* Set safe environment for the initialisation. */ |
| 8233 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8234 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8235 | error_msg = lua_tostring(gL.T, -1); |
| 8236 | else |
| 8237 | error_msg = "critical error"; |
| 8238 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8239 | exit(1); |
| 8240 | } |
| 8241 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8242 | /* |
| 8243 | * |
| 8244 | * Create "core" object. |
| 8245 | * |
| 8246 | */ |
| 8247 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8248 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8249 | lua_newtable(gL.T); |
| 8250 | |
| 8251 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8252 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8253 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8254 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8255 | /* Register special functions. */ |
| 8256 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8257 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8258 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8259 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8260 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8261 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8262 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8263 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8264 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8265 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8266 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8267 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8268 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8269 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8270 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8271 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8272 | hlua_class_function(gL.T, "log", hlua_log); |
| 8273 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8274 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8275 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8276 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8277 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8278 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8279 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8280 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8281 | |
| 8282 | /* |
| 8283 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8284 | * Create "act" object. |
| 8285 | * |
| 8286 | */ |
| 8287 | |
| 8288 | /* This table entry is the object "act" base. */ |
| 8289 | lua_newtable(gL.T); |
| 8290 | |
| 8291 | /* push action return constants */ |
| 8292 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8293 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8294 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8295 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8296 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8297 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8298 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8299 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8300 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8301 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8302 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8303 | lua_setglobal(gL.T, "act"); |
| 8304 | |
| 8305 | /* |
| 8306 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8307 | * Register class Map |
| 8308 | * |
| 8309 | */ |
| 8310 | |
| 8311 | /* This table entry is the object "Map" base. */ |
| 8312 | lua_newtable(gL.T); |
| 8313 | |
| 8314 | /* register pattern types. */ |
| 8315 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8316 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8317 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8318 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8319 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8320 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8321 | |
| 8322 | /* register constructor. */ |
| 8323 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8324 | |
| 8325 | /* Create and fill the metatable. */ |
| 8326 | lua_newtable(gL.T); |
| 8327 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8328 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8329 | lua_pushstring(gL.T, "__index"); |
| 8330 | lua_newtable(gL.T); |
| 8331 | |
| 8332 | /* Register . */ |
| 8333 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8334 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8335 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8336 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8337 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8338 | /* Register previous table in the registry with reference and named entry. |
| 8339 | * The function hlua_register_metatable() pops the stack, so we |
| 8340 | * previously create a copy of the table. |
| 8341 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8342 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8343 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8344 | |
| 8345 | /* Assign the metatable to the mai Map object. */ |
| 8346 | lua_setmetatable(gL.T, -2); |
| 8347 | |
| 8348 | /* Set a name to the table. */ |
| 8349 | lua_setglobal(gL.T, "Map"); |
| 8350 | |
| 8351 | /* |
| 8352 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8353 | * Register class Channel |
| 8354 | * |
| 8355 | */ |
| 8356 | |
| 8357 | /* Create and fill the metatable. */ |
| 8358 | lua_newtable(gL.T); |
| 8359 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8360 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8361 | lua_pushstring(gL.T, "__index"); |
| 8362 | lua_newtable(gL.T); |
| 8363 | |
| 8364 | /* Register . */ |
| 8365 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8366 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8367 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8368 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8369 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8370 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8371 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8372 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8373 | hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 8374 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8375 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8376 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8377 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8378 | |
| 8379 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8380 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8381 | |
| 8382 | /* |
| 8383 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8384 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8385 | * |
| 8386 | */ |
| 8387 | |
| 8388 | /* Create and fill the metatable. */ |
| 8389 | lua_newtable(gL.T); |
| 8390 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8391 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8392 | lua_pushstring(gL.T, "__index"); |
| 8393 | lua_newtable(gL.T); |
| 8394 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8395 | /* Browse existing fetches and create the associated |
| 8396 | * object method. |
| 8397 | */ |
| 8398 | sf = NULL; |
| 8399 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8400 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8401 | * by an underscore. |
| 8402 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8403 | strncpy(trash.area, sf->kw, trash.size); |
| 8404 | trash.area[trash.size - 1] = '\0'; |
| 8405 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8406 | if (*p == '.' || *p == '-' || *p == '+') |
| 8407 | *p = '_'; |
| 8408 | |
| 8409 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8410 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8411 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8412 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8413 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8414 | } |
| 8415 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8416 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8417 | |
| 8418 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8419 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8420 | |
| 8421 | /* |
| 8422 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8423 | * Register class Converters |
| 8424 | * |
| 8425 | */ |
| 8426 | |
| 8427 | /* Create and fill the metatable. */ |
| 8428 | lua_newtable(gL.T); |
| 8429 | |
| 8430 | /* Create and fill the __index entry. */ |
| 8431 | lua_pushstring(gL.T, "__index"); |
| 8432 | lua_newtable(gL.T); |
| 8433 | |
| 8434 | /* Browse existing converters and create the associated |
| 8435 | * object method. |
| 8436 | */ |
| 8437 | sc = NULL; |
| 8438 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8439 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8440 | * by an underscore. |
| 8441 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8442 | strncpy(trash.area, sc->kw, trash.size); |
| 8443 | trash.area[trash.size - 1] = '\0'; |
| 8444 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8445 | if (*p == '.' || *p == '-' || *p == '+') |
| 8446 | *p = '_'; |
| 8447 | |
| 8448 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8449 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8450 | lua_pushlightuserdata(gL.T, sc); |
| 8451 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8452 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8453 | } |
| 8454 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8455 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8456 | |
| 8457 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8458 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8459 | |
| 8460 | /* |
| 8461 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8462 | * Register class HTTP |
| 8463 | * |
| 8464 | */ |
| 8465 | |
| 8466 | /* Create and fill the metatable. */ |
| 8467 | lua_newtable(gL.T); |
| 8468 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8469 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8470 | lua_pushstring(gL.T, "__index"); |
| 8471 | lua_newtable(gL.T); |
| 8472 | |
| 8473 | /* Register Lua functions. */ |
| 8474 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8475 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8476 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8477 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8478 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8479 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8480 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8481 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8482 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8483 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8484 | |
| 8485 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8486 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8487 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8488 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8489 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8490 | hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 8491 | hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8492 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8493 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8494 | |
| 8495 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8496 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8497 | |
| 8498 | /* |
| 8499 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8500 | * Register class AppletTCP |
| 8501 | * |
| 8502 | */ |
| 8503 | |
| 8504 | /* Create and fill the metatable. */ |
| 8505 | lua_newtable(gL.T); |
| 8506 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8507 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8508 | lua_pushstring(gL.T, "__index"); |
| 8509 | lua_newtable(gL.T); |
| 8510 | |
| 8511 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8512 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8513 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8514 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8515 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8516 | hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 8517 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8518 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8519 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8520 | |
| 8521 | lua_settable(gL.T, -3); |
| 8522 | |
| 8523 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8524 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8525 | |
| 8526 | /* |
| 8527 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8528 | * Register class AppletHTTP |
| 8529 | * |
| 8530 | */ |
| 8531 | |
| 8532 | /* Create and fill the metatable. */ |
| 8533 | lua_newtable(gL.T); |
| 8534 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8535 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8536 | lua_pushstring(gL.T, "__index"); |
| 8537 | lua_newtable(gL.T); |
| 8538 | |
| 8539 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8540 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8541 | hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 8542 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8543 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8544 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8545 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8546 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8547 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8548 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8549 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8550 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8551 | |
| 8552 | lua_settable(gL.T, -3); |
| 8553 | |
| 8554 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8555 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8556 | |
| 8557 | /* |
| 8558 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8559 | * Register class TXN |
| 8560 | * |
| 8561 | */ |
| 8562 | |
| 8563 | /* Create and fill the metatable. */ |
| 8564 | lua_newtable(gL.T); |
| 8565 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8566 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8567 | lua_pushstring(gL.T, "__index"); |
| 8568 | lua_newtable(gL.T); |
| 8569 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8570 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8571 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8572 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8573 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8574 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8575 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8576 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8577 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8578 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8579 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8580 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8581 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8582 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8583 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8584 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8585 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8586 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8587 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8588 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8589 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8590 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8591 | |
| 8592 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8593 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8594 | |
| 8595 | /* |
| 8596 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8597 | * Register class reply |
| 8598 | * |
| 8599 | */ |
| 8600 | lua_newtable(gL.T); |
| 8601 | lua_pushstring(gL.T, "__index"); |
| 8602 | lua_newtable(gL.T); |
| 8603 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8604 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8605 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8606 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8607 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8608 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8609 | |
| 8610 | |
| 8611 | /* |
| 8612 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8613 | * Register class Socket |
| 8614 | * |
| 8615 | */ |
| 8616 | |
| 8617 | /* Create and fill the metatable. */ |
| 8618 | lua_newtable(gL.T); |
| 8619 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8620 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8621 | lua_pushstring(gL.T, "__index"); |
| 8622 | lua_newtable(gL.T); |
| 8623 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8624 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8625 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8626 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8627 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8628 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8629 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8630 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8631 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8632 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8633 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8634 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8635 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8636 | lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8637 | |
| 8638 | /* Register the garbage collector entry. */ |
| 8639 | lua_pushstring(gL.T, "__gc"); |
| 8640 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8641 | lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8642 | |
| 8643 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8644 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8645 | |
| 8646 | /* Proxy and server configuration initialisation. */ |
| 8647 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8648 | init_new_proxy(&socket_proxy); |
| 8649 | socket_proxy.parent = NULL; |
| 8650 | socket_proxy.last_change = now.tv_sec; |
| 8651 | socket_proxy.id = "LUA-SOCKET"; |
| 8652 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8653 | socket_proxy.maxconn = 0; |
| 8654 | socket_proxy.accept = NULL; |
| 8655 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8656 | socket_proxy.srv = NULL; |
| 8657 | socket_proxy.conn_retries = 0; |
| 8658 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8659 | |
| 8660 | /* Init TCP server: unchanged parameters */ |
| 8661 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8662 | socket_tcp.next = NULL; |
| 8663 | socket_tcp.proxy = &socket_proxy; |
| 8664 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8665 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8666 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8667 | socket_tcp.idle_conns = NULL; |
| 8668 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8669 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8670 | socket_tcp.last_change = 0; |
| 8671 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8672 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8673 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8674 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8675 | |
| 8676 | /* XXX: Copy default parameter from default server, |
| 8677 | * but the default server is not initialized. |
| 8678 | */ |
| 8679 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8680 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8681 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8682 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8683 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8684 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8685 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8686 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8687 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8688 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8689 | |
| 8690 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8691 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8692 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8693 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8694 | socket_tcp.check.server = &socket_tcp; |
| 8695 | |
| 8696 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8697 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8698 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8699 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8700 | socket_tcp.agent.server = &socket_tcp; |
| 8701 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8702 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8703 | |
| 8704 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8705 | /* Init TCP server: unchanged parameters */ |
| 8706 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8707 | socket_ssl.next = NULL; |
| 8708 | socket_ssl.proxy = &socket_proxy; |
| 8709 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8710 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8711 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8712 | socket_ssl.idle_conns = NULL; |
| 8713 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8714 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8715 | socket_ssl.last_change = 0; |
| 8716 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8717 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8718 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8719 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8720 | |
| 8721 | /* XXX: Copy default parameter from default server, |
| 8722 | * but the default server is not initialized. |
| 8723 | */ |
| 8724 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8725 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8726 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8727 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8728 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8729 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8730 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8731 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8732 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8733 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8734 | |
| 8735 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8736 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8737 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8738 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8739 | socket_ssl.check.server = &socket_ssl; |
| 8740 | |
| 8741 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8742 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8743 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8744 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8745 | socket_ssl.agent.server = &socket_ssl; |
| 8746 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8747 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8748 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8749 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8750 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8751 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8752 | /* |
| 8753 | * |
| 8754 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8755 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8756 | * features like client certificates and ssl_verify. |
| 8757 | * |
| 8758 | */ |
| 8759 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8760 | if (tmp_error != 0) { |
| 8761 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8762 | abort(); /* This must be never arrives because the command line |
| 8763 | not editable by the user. */ |
| 8764 | } |
| 8765 | idx += kw->skip; |
| 8766 | } |
| 8767 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8768 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8769 | |
| 8770 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8771 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8772 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8773 | static void hlua_deinit() |
| 8774 | { |
| 8775 | lua_close(gL.T); |
| 8776 | } |
| 8777 | |
| 8778 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8779 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8780 | static void hlua_register_build_options(void) |
| 8781 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8782 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8783 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8784 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8785 | hap_register_build_opts(ptr, 1); |
| 8786 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8787 | |
| 8788 | INITCALL0(STG_REGISTER, hlua_register_build_options); |