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 | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1670 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1671 | if (str) |
| 1672 | lua_pushstring(L, ""); |
| 1673 | else |
| 1674 | lua_pushnil(L); |
| 1675 | return 1; |
| 1676 | } |
| 1677 | |
| 1678 | /* 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] | 1679 | 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] | 1680 | return 1; |
| 1681 | } |
| 1682 | |
| 1683 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1684 | { |
| 1685 | return _hlua_map_lookup(L, 0); |
| 1686 | } |
| 1687 | |
| 1688 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1689 | { |
| 1690 | return _hlua_map_lookup(L, 1); |
| 1691 | } |
| 1692 | |
| 1693 | /* |
| 1694 | * |
| 1695 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1696 | * Class Socket |
| 1697 | * |
| 1698 | * |
| 1699 | */ |
| 1700 | |
| 1701 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1702 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1703 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* 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] | 1707 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1708 | * received. |
| 1709 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1710 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1711 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1712 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1714 | if (appctx->ctx.hlua_cosocket.die) { |
| 1715 | si_shutw(si); |
| 1716 | si_shutr(si); |
| 1717 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1718 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1719 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1720 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1721 | } |
| 1722 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1723 | /* If we cant write, wakeup the pending write signals. */ |
| 1724 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1725 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1726 | |
| 1727 | /* If we cant read, wakeup the pending read signals. */ |
| 1728 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1729 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1730 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1731 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1732 | * to be notified whenever the connection completes. |
| 1733 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1734 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1735 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1736 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1737 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1738 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1739 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1740 | |
| 1741 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1742 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1743 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1744 | /* 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] | 1745 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1746 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1747 | |
| 1748 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1749 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1750 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1751 | |
| 1752 | /* Some data were injected in the buffer, notify the stream |
| 1753 | * interface. |
| 1754 | */ |
| 1755 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1756 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1757 | |
| 1758 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1759 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1760 | */ |
| 1761 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1762 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1763 | } |
| 1764 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1765 | /* This function is called when the "struct stream" is destroyed. |
| 1766 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1767 | * Wake all the pending signals. |
| 1768 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1769 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1770 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1771 | struct xref *peer; |
| 1772 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1773 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1774 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1775 | if (peer) |
| 1776 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1777 | |
| 1778 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1779 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1780 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1784 | * uses this object. If the stream does not exists, just quit. |
| 1785 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | * pending signal can rest in the read and write lists. destroy |
| 1787 | * it. |
| 1788 | */ |
| 1789 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1790 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1791 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1792 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1793 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1795 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1796 | |
| 1797 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1798 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1799 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1800 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1801 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1802 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1803 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1804 | appctx->ctx.hlua_cosocket.die = 1; |
| 1805 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1806 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1807 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1808 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1809 | return 0; |
| 1810 | } |
| 1811 | |
| 1812 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1813 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1814 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1815 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1816 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1817 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1818 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1819 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1820 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1821 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1822 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1823 | |
| 1824 | /* Check if we run on the same thread than the xreator thread. |
| 1825 | * We cannot access to the socket if the thread is different. |
| 1826 | */ |
| 1827 | if (socket->tid != tid) |
| 1828 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1829 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1830 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1831 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1833 | |
| 1834 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1835 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1837 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1838 | appctx->ctx.hlua_cosocket.die = 1; |
| 1839 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1841 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1842 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | return 0; |
| 1844 | } |
| 1845 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1846 | /* The close function calls close_helper. |
| 1847 | */ |
| 1848 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1849 | { |
| 1850 | MAY_LJMP(check_args(L, 1, "close")); |
| 1851 | return hlua_socket_close_helper(L); |
| 1852 | } |
| 1853 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1854 | /* This Lua function assumes that the stack contain three parameters. |
| 1855 | * 1 - USERDATA containing a struct socket |
| 1856 | * 2 - INTEGER with values of the macro defined below |
| 1857 | * If the integer is -1, we must read at most one line. |
| 1858 | * If the integer is -2, we ust read all the data until the |
| 1859 | * end of the stream. |
| 1860 | * If the integer is positive value, we must read a number of |
| 1861 | * bytes corresponding to this value. |
| 1862 | */ |
| 1863 | #define HLSR_READ_LINE (-1) |
| 1864 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1865 | __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] | 1866 | { |
| 1867 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1868 | int wanted = lua_tointeger(L, 2); |
| 1869 | struct hlua *hlua = hlua_gethlua(L); |
| 1870 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1871 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1873 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1874 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1875 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1876 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1877 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1878 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1879 | struct stream_interface *si; |
| 1880 | struct stream *s; |
| 1881 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1882 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1883 | |
| 1884 | /* Check if this lua stack is schedulable. */ |
| 1885 | if (!hlua || !hlua->task) |
| 1886 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1887 | "'frontend', 'backend' or 'task'")); |
| 1888 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1889 | /* Check if we run on the same thread than the xreator thread. |
| 1890 | * We cannot access to the socket if the thread is different. |
| 1891 | */ |
| 1892 | if (socket->tid != tid) |
| 1893 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1894 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1895 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1896 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1897 | if (!peer) |
| 1898 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1899 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1900 | si = appctx->owner; |
| 1901 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1903 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1906 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | if (nblk < 0) /* Connection close. */ |
| 1908 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1909 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1911 | |
| 1912 | /* remove final \r\n. */ |
| 1913 | if (nblk == 1) { |
| 1914 | if (blk1[len1-1] == '\n') { |
| 1915 | len1--; |
| 1916 | skip_at_end++; |
| 1917 | if (blk1[len1-1] == '\r') { |
| 1918 | len1--; |
| 1919 | skip_at_end++; |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | else { |
| 1924 | if (blk2[len2-1] == '\n') { |
| 1925 | len2--; |
| 1926 | skip_at_end++; |
| 1927 | if (blk2[len2-1] == '\r') { |
| 1928 | len2--; |
| 1929 | skip_at_end++; |
| 1930 | } |
| 1931 | } |
| 1932 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1937 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1938 | if (nblk < 0) /* Connection close. */ |
| 1939 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1940 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | goto connection_empty; |
| 1942 | } |
| 1943 | |
| 1944 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1945 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1946 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | if (nblk < 0) /* Connection close. */ |
| 1948 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1949 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1950 | goto connection_empty; |
| 1951 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1952 | missing_bytes = wanted - socket->b.n; |
| 1953 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1954 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1955 | len1 = missing_bytes; |
| 1956 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1957 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | len = len1; |
| 1961 | |
| 1962 | luaL_addlstring(&socket->b, blk1, len1); |
| 1963 | if (nblk == 2) { |
| 1964 | len += len2; |
| 1965 | luaL_addlstring(&socket->b, blk2, len2); |
| 1966 | } |
| 1967 | |
| 1968 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1969 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | |
| 1971 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1972 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | |
| 1974 | /* If the pattern reclaim to read all the data |
| 1975 | * in the connection, got out. |
| 1976 | */ |
| 1977 | if (wanted == HLSR_READ_ALL) |
| 1978 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1979 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | goto connection_empty; |
| 1981 | |
| 1982 | /* Return result. */ |
| 1983 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1984 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | return 1; |
| 1986 | |
| 1987 | connection_closed: |
| 1988 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1989 | xref_unlock(&socket->xref, peer); |
| 1990 | |
| 1991 | no_peer: |
| 1992 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | /* If the buffer containds data. */ |
| 1994 | if (socket->b.n > 0) { |
| 1995 | luaL_pushresult(&socket->b); |
| 1996 | return 1; |
| 1997 | } |
| 1998 | lua_pushnil(L); |
| 1999 | lua_pushstring(L, "connection closed."); |
| 2000 | return 2; |
| 2001 | |
| 2002 | connection_empty: |
| 2003 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2004 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2005 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2007 | } |
| 2008 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2009 | 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] | 2010 | return 0; |
| 2011 | } |
| 2012 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2013 | /* This Lua function gets two parameters. The first one can be string |
| 2014 | * or a number. If the string is "*l", the user requires one line. If |
| 2015 | * 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] | 2016 | * If the value is a number, the user require a number of bytes equal |
| 2017 | * to the value. The default value is "*l" (a line). |
| 2018 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2019 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2020 | * integer takes this values: |
| 2021 | * -1 : read a line |
| 2022 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2023 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2025 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | * concatenated with the read data. |
| 2027 | */ |
| 2028 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2029 | { |
| 2030 | int wanted = HLSR_READ_LINE; |
| 2031 | const char *pattern; |
| 2032 | int type; |
| 2033 | char *error; |
| 2034 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2035 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2036 | |
| 2037 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2038 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2039 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2040 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2041 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2042 | /* Check if we run on the same thread than the xreator thread. |
| 2043 | * We cannot access to the socket if the thread is different. |
| 2044 | */ |
| 2045 | if (socket->tid != tid) |
| 2046 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2047 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | /* check for pattern. */ |
| 2049 | if (lua_gettop(L) >= 2) { |
| 2050 | type = lua_type(L, 2); |
| 2051 | if (type == LUA_TSTRING) { |
| 2052 | pattern = lua_tostring(L, 2); |
| 2053 | if (strcmp(pattern, "*a") == 0) |
| 2054 | wanted = HLSR_READ_ALL; |
| 2055 | else if (strcmp(pattern, "*l") == 0) |
| 2056 | wanted = HLSR_READ_LINE; |
| 2057 | else { |
| 2058 | wanted = strtoll(pattern, &error, 10); |
| 2059 | if (*error != '\0') |
| 2060 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2061 | } |
| 2062 | } |
| 2063 | else if (type == LUA_TNUMBER) { |
| 2064 | wanted = lua_tointeger(L, 2); |
| 2065 | if (wanted < 0) |
| 2066 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | /* Set pattern. */ |
| 2071 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2072 | |
| 2073 | /* Check if we would replace the top by itself. */ |
| 2074 | if (lua_gettop(L) != 2) |
| 2075 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2077 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | luaL_buffinit(L, &socket->b); |
| 2079 | |
| 2080 | /* Check prefix. */ |
| 2081 | if (lua_gettop(L) >= 3) { |
| 2082 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2083 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2084 | pattern = lua_tolstring(L, 3, &len); |
| 2085 | luaL_addlstring(&socket->b, pattern, len); |
| 2086 | } |
| 2087 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2088 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2092 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2093 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2094 | 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] | 2095 | { |
| 2096 | struct hlua_socket *socket; |
| 2097 | struct hlua *hlua = hlua_gethlua(L); |
| 2098 | struct appctx *appctx; |
| 2099 | size_t buf_len; |
| 2100 | const char *buf; |
| 2101 | int len; |
| 2102 | int send_len; |
| 2103 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2104 | struct xref *peer; |
| 2105 | struct stream_interface *si; |
| 2106 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2107 | |
| 2108 | /* Check if this lua stack is schedulable. */ |
| 2109 | if (!hlua || !hlua->task) |
| 2110 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2111 | "'frontend', 'backend' or 'task'")); |
| 2112 | |
| 2113 | /* Get object */ |
| 2114 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2115 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2116 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2117 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2118 | /* Check if we run on the same thread than the xreator thread. |
| 2119 | * We cannot access to the socket if the thread is different. |
| 2120 | */ |
| 2121 | if (socket->tid != tid) |
| 2122 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2123 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2124 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2125 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2126 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2127 | lua_pushinteger(L, -1); |
| 2128 | return 1; |
| 2129 | } |
| 2130 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2131 | si = appctx->owner; |
| 2132 | s = si_strm(si); |
| 2133 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2135 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2136 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | lua_pushinteger(L, -1); |
| 2138 | return 1; |
| 2139 | } |
| 2140 | |
| 2141 | /* Update the input buffer data. */ |
| 2142 | buf += sent; |
| 2143 | send_len = buf_len - sent; |
| 2144 | |
| 2145 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2146 | if (sent >= buf_len) { |
| 2147 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2149 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2151 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2152 | * the request buffer if its not required. |
| 2153 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2154 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2155 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2156 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2157 | } |
| 2158 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2159 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2160 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2161 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2163 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2164 | |
| 2165 | /* send data */ |
| 2166 | if (len < send_len) |
| 2167 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2168 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | |
| 2170 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2171 | * the data" (-2) are not expected because the available length |
| 2172 | * is tested. |
| 2173 | * Other unknown error are also not expected. |
| 2174 | */ |
| 2175 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2176 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2177 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2178 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2179 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2181 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2182 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | return 1; |
| 2184 | } |
| 2185 | |
| 2186 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2187 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2188 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2189 | s->req.rex = TICK_ETERNITY; |
| 2190 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2191 | |
| 2192 | /* Update length sent. */ |
| 2193 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2194 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | |
| 2196 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2197 | if (sent + len >= buf_len) { |
| 2198 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2199 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2200 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2201 | |
| 2202 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2203 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2204 | xref_unlock(&socket->xref, peer); |
| 2205 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2206 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2207 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2208 | 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] | 2209 | return 0; |
| 2210 | } |
| 2211 | |
| 2212 | /* This function initiate the send of data. It just check the input |
| 2213 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2214 | * 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] | 2215 | * "hlua_socket_write_yield" that can yield. |
| 2216 | * |
| 2217 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2218 | * the associated object. The second is a string buffer. The third is |
| 2219 | * a facultative integer that represents where is the buffer position |
| 2220 | * of the start of the data that can send. The first byte is the |
| 2221 | * position "1". The default value is "1". The fourth argument is a |
| 2222 | * facultative integer that represents where is the buffer position |
| 2223 | * of the end of the data that can send. The default is the last byte. |
| 2224 | */ |
| 2225 | static int hlua_socket_send(struct lua_State *L) |
| 2226 | { |
| 2227 | int i; |
| 2228 | int j; |
| 2229 | const char *buf; |
| 2230 | size_t buf_len; |
| 2231 | |
| 2232 | /* Check number of arguments. */ |
| 2233 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2234 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2235 | |
| 2236 | /* Get the string. */ |
| 2237 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2238 | |
| 2239 | /* Get and check j. */ |
| 2240 | if (lua_gettop(L) == 4) { |
| 2241 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2242 | if (j < 0) |
| 2243 | j = buf_len + j + 1; |
| 2244 | if (j > buf_len) |
| 2245 | j = buf_len + 1; |
| 2246 | lua_pop(L, 1); |
| 2247 | } |
| 2248 | else |
| 2249 | j = buf_len; |
| 2250 | |
| 2251 | /* Get and check i. */ |
| 2252 | if (lua_gettop(L) == 3) { |
| 2253 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2254 | if (i < 0) |
| 2255 | i = buf_len + i + 1; |
| 2256 | if (i > buf_len) |
| 2257 | i = buf_len + 1; |
| 2258 | lua_pop(L, 1); |
| 2259 | } else |
| 2260 | i = 1; |
| 2261 | |
| 2262 | /* Check bth i and j. */ |
| 2263 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2264 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | return 1; |
| 2266 | } |
| 2267 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2268 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2269 | return 1; |
| 2270 | } |
| 2271 | if (i == 0) |
| 2272 | i = 1; |
| 2273 | if (j == 0) |
| 2274 | j = 1; |
| 2275 | |
| 2276 | /* Pop the string. */ |
| 2277 | lua_pop(L, 1); |
| 2278 | |
| 2279 | /* Update the buffer length. */ |
| 2280 | buf += i - 1; |
| 2281 | buf_len = j - i + 1; |
| 2282 | lua_pushlstring(L, buf, buf_len); |
| 2283 | |
| 2284 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2285 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2287 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | } |
| 2289 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2290 | #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] | 2291 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2292 | { |
| 2293 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2294 | int ret; |
| 2295 | int len; |
| 2296 | char *p; |
| 2297 | |
| 2298 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2299 | if (ret <= 0) { |
| 2300 | lua_pushnil(L); |
| 2301 | return 1; |
| 2302 | } |
| 2303 | |
| 2304 | if (ret == AF_UNIX) { |
| 2305 | lua_pushstring(L, buffer+1); |
| 2306 | return 1; |
| 2307 | } |
| 2308 | else if (ret == AF_INET6) { |
| 2309 | buffer[0] = '['; |
| 2310 | len = strlen(buffer); |
| 2311 | buffer[len] = ']'; |
| 2312 | len++; |
| 2313 | buffer[len] = ':'; |
| 2314 | len++; |
| 2315 | p = buffer; |
| 2316 | } |
| 2317 | else if (ret == AF_INET) { |
| 2318 | p = buffer + 1; |
| 2319 | len = strlen(p); |
| 2320 | p[len] = ':'; |
| 2321 | len++; |
| 2322 | } |
| 2323 | else { |
| 2324 | lua_pushnil(L); |
| 2325 | return 1; |
| 2326 | } |
| 2327 | |
| 2328 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2329 | lua_pushnil(L); |
| 2330 | return 1; |
| 2331 | } |
| 2332 | |
| 2333 | lua_pushstring(L, p); |
| 2334 | return 1; |
| 2335 | } |
| 2336 | |
| 2337 | /* Returns information about the peer of the connection. */ |
| 2338 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2339 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2340 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2341 | struct xref *peer; |
| 2342 | struct appctx *appctx; |
| 2343 | struct stream_interface *si; |
| 2344 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2345 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2346 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2348 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2349 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2351 | /* Check if we run on the same thread than the xreator thread. |
| 2352 | * We cannot access to the socket if the thread is different. |
| 2353 | */ |
| 2354 | if (socket->tid != tid) |
| 2355 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2356 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2357 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2358 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2359 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | lua_pushnil(L); |
| 2361 | return 1; |
| 2362 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2363 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2364 | si = appctx->owner; |
| 2365 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2367 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2368 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2369 | lua_pushnil(L); |
| 2370 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | } |
| 2372 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2373 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2374 | xref_unlock(&socket->xref, peer); |
| 2375 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | /* Returns information about my connection side. */ |
| 2379 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2380 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2381 | struct hlua_socket *socket; |
| 2382 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2383 | struct appctx *appctx; |
| 2384 | struct xref *peer; |
| 2385 | struct stream_interface *si; |
| 2386 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2387 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2388 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2390 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2391 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2392 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2393 | /* Check if we run on the same thread than the xreator thread. |
| 2394 | * We cannot access to the socket if the thread is different. |
| 2395 | */ |
| 2396 | if (socket->tid != tid) |
| 2397 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2398 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2399 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2400 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2401 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2402 | lua_pushnil(L); |
| 2403 | return 1; |
| 2404 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2405 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2406 | si = appctx->owner; |
| 2407 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2408 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2409 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2410 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2411 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | lua_pushnil(L); |
| 2413 | return 1; |
| 2414 | } |
| 2415 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2416 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2417 | xref_unlock(&socket->xref, peer); |
| 2418 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2422 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2423 | .obj_type = OBJ_TYPE_APPLET, |
| 2424 | .name = "<LUA_TCP>", |
| 2425 | .fct = hlua_socket_handler, |
| 2426 | .release = hlua_socket_release, |
| 2427 | }; |
| 2428 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2429 | __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] | 2430 | { |
| 2431 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2432 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2433 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2435 | struct stream_interface *si; |
| 2436 | struct stream *s; |
| 2437 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2438 | /* Check if we run on the same thread than the xreator thread. |
| 2439 | * We cannot access to the socket if the thread is different. |
| 2440 | */ |
| 2441 | if (socket->tid != tid) |
| 2442 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2443 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2444 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2445 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2446 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2447 | lua_pushnil(L); |
| 2448 | lua_pushstring(L, "Can't connect"); |
| 2449 | return 2; |
| 2450 | } |
| 2451 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2452 | si = appctx->owner; |
| 2453 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2454 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2455 | /* Check if we run on the same thread than the xreator thread. |
| 2456 | * We cannot access to the socket if the thread is different. |
| 2457 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2458 | if (socket->tid != tid) { |
| 2459 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2460 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2461 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2462 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2463 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2464 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2465 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2466 | lua_pushnil(L); |
| 2467 | lua_pushstring(L, "Can't connect"); |
| 2468 | return 2; |
| 2469 | } |
| 2470 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2471 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | |
| 2473 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2474 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2475 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | lua_pushinteger(L, 1); |
| 2477 | return 1; |
| 2478 | } |
| 2479 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2480 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2481 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | } |
| 2484 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2485 | 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] | 2486 | return 0; |
| 2487 | } |
| 2488 | |
| 2489 | /* This function fail or initite the connection. */ |
| 2490 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2491 | { |
| 2492 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2493 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2494 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2495 | struct hlua *hlua; |
| 2496 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2497 | int low, high; |
| 2498 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2499 | struct xref *peer; |
| 2500 | struct stream_interface *si; |
| 2501 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2503 | if (lua_gettop(L) < 2) |
| 2504 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2505 | |
| 2506 | /* Get args. */ |
| 2507 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2508 | |
| 2509 | /* Check if we run on the same thread than the xreator thread. |
| 2510 | * We cannot access to the socket if the thread is different. |
| 2511 | */ |
| 2512 | if (socket->tid != tid) |
| 2513 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2514 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2515 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2516 | if (lua_gettop(L) >= 3) { |
| 2517 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2518 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2520 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2521 | * that are not enclosed within square brackets. |
| 2522 | */ |
| 2523 | if (port > 0) { |
| 2524 | luaL_buffinit(L, &b); |
| 2525 | luaL_addstring(&b, ip); |
| 2526 | luaL_addchar(&b, ':'); |
| 2527 | luaL_pushresult(&b); |
| 2528 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2529 | } |
| 2530 | } |
| 2531 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2532 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2533 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2534 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2535 | lua_pushnil(L); |
| 2536 | return 1; |
| 2537 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | |
| 2539 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2540 | 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] | 2541 | if (!addr) { |
| 2542 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2543 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2544 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2545 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2546 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2547 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2548 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | if (port == -1) { |
| 2550 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2551 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2552 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2553 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2554 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2555 | if (port == -1) { |
| 2556 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2557 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2558 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2559 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2560 | } |
| 2561 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2562 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2563 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2564 | si = appctx->owner; |
| 2565 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2566 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2567 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2568 | xref_unlock(&socket->xref, peer); |
| 2569 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2570 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2571 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2572 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2573 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2574 | |
| 2575 | /* inform the stream that we want to be notified whenever the |
| 2576 | * connection completes. |
| 2577 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2578 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2579 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2580 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2581 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2582 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2583 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2584 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2585 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2586 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2587 | } |
| 2588 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2589 | |
| 2590 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2591 | /* Return yield waiting for connection. */ |
| 2592 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2593 | 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] | 2594 | |
| 2595 | return 0; |
| 2596 | } |
| 2597 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2598 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2599 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2600 | { |
| 2601 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2602 | struct xref *peer; |
| 2603 | struct appctx *appctx; |
| 2604 | struct stream_interface *si; |
| 2605 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
| 2607 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2608 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2609 | |
| 2610 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2611 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2612 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2613 | lua_pushnil(L); |
| 2614 | return 1; |
| 2615 | } |
| 2616 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2617 | si = appctx->owner; |
| 2618 | s = si_strm(si); |
| 2619 | |
| 2620 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2621 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2623 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2624 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2625 | |
| 2626 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2627 | { |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2632 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2633 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2634 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2635 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2636 | struct xref *peer; |
| 2637 | struct appctx *appctx; |
| 2638 | struct stream_interface *si; |
| 2639 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2640 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2641 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2642 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2643 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2644 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2645 | /* convert the timeout to millis */ |
| 2646 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2648 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2649 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2650 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2651 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2652 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2653 | 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] | 2654 | |
| 2655 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2656 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2657 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2658 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2659 | /* Check if we run on the same thread than the xreator thread. |
| 2660 | * We cannot access to the socket if the thread is different. |
| 2661 | */ |
| 2662 | if (socket->tid != tid) |
| 2663 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2664 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2665 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2666 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2667 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2668 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2669 | WILL_LJMP(lua_error(L)); |
| 2670 | return 0; |
| 2671 | } |
| 2672 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2673 | si = appctx->owner; |
| 2674 | s = si_strm(si); |
| 2675 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2676 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2677 | s->req.rto = tmout; |
| 2678 | s->req.wto = tmout; |
| 2679 | s->res.rto = tmout; |
| 2680 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2681 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2682 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2683 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2684 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2685 | |
| 2686 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2687 | task_queue(s->task); |
| 2688 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2689 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2690 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2691 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2692 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2696 | { |
| 2697 | struct hlua_socket *socket; |
| 2698 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2699 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2700 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2701 | |
| 2702 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2703 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2704 | hlua_pusherror(L, "socket: full stack"); |
| 2705 | goto out_fail_conf; |
| 2706 | } |
| 2707 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2708 | /* Create the object: obj[0] = userdata. */ |
| 2709 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2711 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2712 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2713 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2714 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2715 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2716 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2717 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2718 | goto out_fail_conf; |
| 2719 | } |
| 2720 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2721 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2722 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2723 | lua_setmetatable(L, -2); |
| 2724 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2725 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2726 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2727 | if (!appctx) { |
| 2728 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2729 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2730 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2731 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2732 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2733 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2734 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2735 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2736 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2737 | /* Now create a session, task and stream for this applet */ |
| 2738 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2739 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2740 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2741 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2744 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2745 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2746 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2747 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2750 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2751 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2752 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2753 | /* Configure "right" stream interface. this "si" is used to connect |
| 2754 | * and retrieve data from the server. The connection is initialized |
| 2755 | * with the "struct server". |
| 2756 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2757 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2758 | |
| 2759 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2760 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2761 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2762 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2763 | return 1; |
| 2764 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2765 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2766 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2767 | out_fail_sess: |
| 2768 | appctx_free(appctx); |
| 2769 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2770 | WILL_LJMP(lua_error(L)); |
| 2771 | return 0; |
| 2772 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2773 | |
| 2774 | /* |
| 2775 | * |
| 2776 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2777 | * Class Channel |
| 2778 | * |
| 2779 | * |
| 2780 | */ |
| 2781 | |
| 2782 | /* Returns the struct hlua_channel join to the class channel in the |
| 2783 | * stack entry "ud" or throws an argument error. |
| 2784 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2785 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2786 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2787 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2788 | } |
| 2789 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2790 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2791 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2793 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2794 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2795 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2796 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | return 0; |
| 2798 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2799 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2800 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2801 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2802 | |
| 2803 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2804 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2805 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2806 | return 1; |
| 2807 | } |
| 2808 | |
| 2809 | /* Duplicate all the data present in the input channel and put it |
| 2810 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2811 | * the stack if the channel is closed and all the data are consumed, |
| 2812 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2813 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2814 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2815 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2816 | { |
| 2817 | char *blk1; |
| 2818 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2819 | size_t len1; |
| 2820 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2821 | int ret; |
| 2822 | luaL_Buffer b; |
| 2823 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2824 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2825 | if (unlikely(ret == 0)) |
| 2826 | return 0; |
| 2827 | |
| 2828 | if (unlikely(ret < 0)) { |
| 2829 | lua_pushnil(L); |
| 2830 | return -1; |
| 2831 | } |
| 2832 | |
| 2833 | luaL_buffinit(L, &b); |
| 2834 | luaL_addlstring(&b, blk1, len1); |
| 2835 | if (unlikely(ret == 2)) |
| 2836 | luaL_addlstring(&b, blk2, len2); |
| 2837 | luaL_pushresult(&b); |
| 2838 | |
| 2839 | if (unlikely(ret == 2)) |
| 2840 | return len1 + len2; |
| 2841 | return len1; |
| 2842 | } |
| 2843 | |
| 2844 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2845 | * a yield. This function keep the data in the buffer. |
| 2846 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2847 | __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] | 2848 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2849 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2850 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2851 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2852 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2853 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2854 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2855 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2856 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2857 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2858 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2859 | 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] | 2860 | return 1; |
| 2861 | } |
| 2862 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2863 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2864 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2865 | { |
| 2866 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2867 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2868 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2869 | } |
| 2870 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2871 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2872 | * a yield. This function consumes the data in the buffer. It returns |
| 2873 | * a string containing the data or a nil pointer if no data are available |
| 2874 | * and the channel is closed. |
| 2875 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2876 | __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] | 2877 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2878 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2879 | int ret; |
| 2880 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2881 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2882 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2883 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2884 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2885 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2886 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2887 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2888 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2889 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2890 | 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] | 2891 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | if (unlikely(ret == -1)) |
| 2893 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2895 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2896 | return 1; |
| 2897 | } |
| 2898 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2899 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2900 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2901 | { |
| 2902 | MAY_LJMP(check_args(L, 1, "get")); |
| 2903 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2904 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2905 | } |
| 2906 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2907 | /* This functions consumes and returns one line. If the channel is closed, |
| 2908 | * 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] | 2909 | * 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] | 2910 | * value. |
| 2911 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2912 | __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] | 2913 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | char *blk1; |
| 2915 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2916 | size_t len1; |
| 2917 | size_t len2; |
| 2918 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2919 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | int ret; |
| 2921 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2922 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2923 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2924 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2925 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2926 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2927 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2928 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2929 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2930 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2932 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2933 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | if (ret == -1) { |
| 2935 | lua_pushnil(L); |
| 2936 | return 1; |
| 2937 | } |
| 2938 | |
| 2939 | luaL_buffinit(L, &b); |
| 2940 | luaL_addlstring(&b, blk1, len1); |
| 2941 | len = len1; |
| 2942 | if (unlikely(ret == 2)) { |
| 2943 | luaL_addlstring(&b, blk2, len2); |
| 2944 | len += len2; |
| 2945 | } |
| 2946 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2947 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2948 | return 1; |
| 2949 | } |
| 2950 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2951 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2952 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2953 | { |
| 2954 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2955 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2956 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2957 | } |
| 2958 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2959 | /* This function takes a string as input, and append it at the |
| 2960 | * input side of channel. If the data is too big, but a space |
| 2961 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2962 | * yields. If the data is bigger than the buffer, or if the |
| 2963 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2964 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2965 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2966 | __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2967 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2968 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2969 | size_t len; |
| 2970 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2971 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2972 | int ret; |
| 2973 | int max; |
| 2974 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2975 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2976 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2977 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2978 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2979 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2980 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2981 | * the request buffer if its not required. |
| 2982 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2983 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2984 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2985 | 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] | 2986 | } |
| 2987 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2988 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2989 | if (max > len - l) |
| 2990 | max = len - l; |
| 2991 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2992 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | if (ret == -2 || ret == -3) { |
| 2994 | lua_pushinteger(L, -1); |
| 2995 | return 1; |
| 2996 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2997 | if (ret == -1) { |
| 2998 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2999 | 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] | 3000 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3001 | l += ret; |
| 3002 | lua_pop(L, 1); |
| 3003 | lua_pushinteger(L, l); |
| 3004 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3005 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3006 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3007 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3008 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3009 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | */ |
| 3011 | return 1; |
| 3012 | } |
| 3013 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3014 | 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] | 3015 | return 1; |
| 3016 | } |
| 3017 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3018 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3019 | * 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] | 3020 | * buffer size is too little for the data. |
| 3021 | */ |
| 3022 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3023 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3024 | size_t len; |
| 3025 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3027 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3028 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3029 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | lua_pushinteger(L, 0); |
| 3031 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3032 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | } |
| 3034 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3035 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3036 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3037 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3038 | * or -1 if the channel is closed or if the buffer size is too |
| 3039 | * little for the data. |
| 3040 | */ |
| 3041 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3042 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3043 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3044 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3045 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3046 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3047 | lua_pushinteger(L, 0); |
| 3048 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3049 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3050 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3051 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3052 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3053 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3054 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3055 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3056 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | } |
| 3058 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3059 | /* Append data in the output side of the buffer. This data is immediately |
| 3060 | * sent. The function returns the amount of data written. If the buffer |
| 3061 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | * if the channel is closed. |
| 3063 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3064 | __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] | 3065 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3066 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | size_t len; |
| 3068 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3069 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3070 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3071 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3073 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3074 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3075 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3076 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3077 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3078 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3079 | lua_pushinteger(L, -1); |
| 3080 | return 1; |
| 3081 | } |
| 3082 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3083 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3084 | * the request buffer if its not required. |
| 3085 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3086 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3087 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3088 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3089 | } |
| 3090 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3091 | /* The written data will be immediately sent, so we can check |
| 3092 | * the available space without taking in account the reserve. |
| 3093 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3094 | * data, because the buffer will be flushed. |
| 3095 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3096 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3097 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3098 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3099 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3100 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3101 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3102 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3103 | return 1; |
| 3104 | |
| 3105 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3106 | if (max > len - l) |
| 3107 | max = len - l; |
| 3108 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3109 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3110 | * detects a non contiguous buffer and realign it. |
| 3111 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3112 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3113 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3114 | |
| 3115 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3116 | max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3117 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3118 | /* buffer replace considers that the input part is filled. |
| 3119 | * so, I must forward these new data in the output part. |
| 3120 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3121 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3122 | |
| 3123 | l += max; |
| 3124 | lua_pop(L, 1); |
| 3125 | lua_pushinteger(L, l); |
| 3126 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3127 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3128 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3129 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3130 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3131 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3132 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3133 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3134 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3135 | if (l < len) { |
| 3136 | /* If we are waiting for space in the response buffer, we |
| 3137 | * must set the flag WAKERESWR. This flag required the task |
| 3138 | * wake up if any activity is detected on the response buffer. |
| 3139 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3140 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3141 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3142 | else |
| 3143 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3144 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3145 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3146 | |
| 3147 | return 1; |
| 3148 | } |
| 3149 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3150 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3151 | * yield the LUA process, and resume it without checking the |
| 3152 | * input arguments. |
| 3153 | */ |
| 3154 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3155 | { |
| 3156 | MAY_LJMP(check_args(L, 2, "send")); |
| 3157 | lua_pushinteger(L, 0); |
| 3158 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3159 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | /* This function forward and amount of butes. The data pass from |
| 3163 | * the input side of the buffer to the output side, and can be |
| 3164 | * forwarded. This function never fails. |
| 3165 | * |
| 3166 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3167 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3168 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3169 | __LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3171 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3172 | int len; |
| 3173 | int l; |
| 3174 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3175 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3176 | |
| 3177 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3178 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3179 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3180 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3181 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3182 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3183 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3184 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3185 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3186 | |
| 3187 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3188 | if (max > ci_data(chn)) |
| 3189 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3190 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3191 | l += max; |
| 3192 | |
| 3193 | lua_pop(L, 1); |
| 3194 | lua_pushinteger(L, l); |
| 3195 | |
| 3196 | /* Check if it miss bytes to forward. */ |
| 3197 | if (l < len) { |
| 3198 | /* The the input channel or the output channel are closed, we |
| 3199 | * must return the amount of data forwarded. |
| 3200 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3201 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3202 | return 1; |
| 3203 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3204 | /* If we are waiting for space data in the response buffer, we |
| 3205 | * must set the flag WAKERESWR. This flag required the task |
| 3206 | * wake up if any activity is detected on the response buffer. |
| 3207 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3208 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3209 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3210 | else |
| 3211 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3212 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3213 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3214 | 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] | 3215 | } |
| 3216 | |
| 3217 | return 1; |
| 3218 | } |
| 3219 | |
| 3220 | /* Just check the input and prepare the stack for the previous |
| 3221 | * function "hlua_channel_forward_yield" |
| 3222 | */ |
| 3223 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3224 | { |
| 3225 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3226 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3227 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3228 | |
| 3229 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3230 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | /* Just returns the number of bytes available in the input |
| 3234 | * side of the buffer. This function never fails. |
| 3235 | */ |
| 3236 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3237 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3238 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3239 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3240 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3241 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3242 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3243 | struct htx *htx = htxbuf(&chn->buf); |
| 3244 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3245 | } |
| 3246 | else |
| 3247 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3248 | return 1; |
| 3249 | } |
| 3250 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3251 | /* Returns true if the channel is full. */ |
| 3252 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3253 | { |
| 3254 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3255 | |
| 3256 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3257 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3258 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3259 | * applet). |
| 3260 | */ |
| 3261 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3262 | return 1; |
| 3263 | } |
| 3264 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3265 | /* Returns true if the channel is the response channel. */ |
| 3266 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3267 | { |
| 3268 | struct channel *chn; |
| 3269 | |
| 3270 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3271 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3272 | |
| 3273 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3274 | return 1; |
| 3275 | } |
| 3276 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3277 | /* Just returns the number of bytes available in the output |
| 3278 | * side of the buffer. This function never fails. |
| 3279 | */ |
| 3280 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3281 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3282 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3283 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3284 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3285 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3286 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3287 | return 1; |
| 3288 | } |
| 3289 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3290 | /* |
| 3291 | * |
| 3292 | * |
| 3293 | * Class Fetches |
| 3294 | * |
| 3295 | * |
| 3296 | */ |
| 3297 | |
| 3298 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3299 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3300 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3301 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3302 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3303 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | /* This function creates and push in the stack a fetch object according |
| 3307 | * with a current TXN. |
| 3308 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3309 | 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] | 3310 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3311 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3312 | |
| 3313 | /* Check stack size. */ |
| 3314 | if (!lua_checkstack(L, 3)) |
| 3315 | return 0; |
| 3316 | |
| 3317 | /* Create the object: obj[0] = userdata. |
| 3318 | * Note that the base of the Fetches object is the |
| 3319 | * transaction object. |
| 3320 | */ |
| 3321 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3322 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3323 | lua_rawseti(L, -2, 0); |
| 3324 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3325 | hsmp->s = txn->s; |
| 3326 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3327 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3328 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3329 | |
| 3330 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3331 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3332 | lua_setmetatable(L, -2); |
| 3333 | |
| 3334 | return 1; |
| 3335 | } |
| 3336 | |
| 3337 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3338 | * It uses closure argument to store the associated sample-fetch. It |
| 3339 | * returns only one argument or throws an error. An error is thrown |
| 3340 | * only if an error is encountered during the argument parsing. If |
| 3341 | * the "sample-fetch" function fails, nil is returned. |
| 3342 | */ |
| 3343 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3344 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3345 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3346 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3347 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3348 | int i; |
| 3349 | struct sample smp; |
| 3350 | |
| 3351 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3352 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3353 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3354 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3355 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3356 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3357 | /* Check execution authorization. */ |
| 3358 | if (f->use & SMP_USE_HTTP_ANY && |
| 3359 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3360 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3361 | "is not available in Lua services", f->kw); |
| 3362 | WILL_LJMP(lua_error(L)); |
| 3363 | } |
| 3364 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3365 | /* Get extra arguments. */ |
| 3366 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3367 | if (i >= ARGM_NBARGS) |
| 3368 | break; |
| 3369 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3370 | } |
| 3371 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3372 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3373 | |
| 3374 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3375 | 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] | 3376 | |
| 3377 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3378 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3379 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3380 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | /* Initialise the sample. */ |
| 3384 | memset(&smp, 0, sizeof(smp)); |
| 3385 | |
| 3386 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3387 | 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] | 3388 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3389 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3390 | lua_pushstring(L, ""); |
| 3391 | else |
| 3392 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3393 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3397 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3398 | hlua_smp2lua_str(L, &smp); |
| 3399 | else |
| 3400 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3401 | |
| 3402 | end: |
| 3403 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3404 | if (args[i].type == ARGT_STR) |
| 3405 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3406 | else if (args[i].type == ARGT_REG) |
| 3407 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3408 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3409 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3410 | |
| 3411 | error: |
| 3412 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3413 | if (args[i].type == ARGT_STR) |
| 3414 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3415 | else if (args[i].type == ARGT_REG) |
| 3416 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3417 | } |
| 3418 | WILL_LJMP(lua_error(L)); |
| 3419 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3420 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3421 | |
| 3422 | /* |
| 3423 | * |
| 3424 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3425 | * Class Converters |
| 3426 | * |
| 3427 | * |
| 3428 | */ |
| 3429 | |
| 3430 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3431 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3432 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3433 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3434 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3435 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* This function creates and push in the stack a Converters object |
| 3439 | * according with a current TXN. |
| 3440 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3441 | 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] | 3442 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3443 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3444 | |
| 3445 | /* Check stack size. */ |
| 3446 | if (!lua_checkstack(L, 3)) |
| 3447 | return 0; |
| 3448 | |
| 3449 | /* Create the object: obj[0] = userdata. |
| 3450 | * Note that the base of the Converters object is the |
| 3451 | * same than the TXN object. |
| 3452 | */ |
| 3453 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3454 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3455 | lua_rawseti(L, -2, 0); |
| 3456 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3457 | hsmp->s = txn->s; |
| 3458 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3459 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3460 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3461 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3462 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3463 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3464 | lua_setmetatable(L, -2); |
| 3465 | |
| 3466 | return 1; |
| 3467 | } |
| 3468 | |
| 3469 | /* This function is an LUA binding. It is called with each converter. |
| 3470 | * It uses closure argument to store the associated converter. It |
| 3471 | * returns only one argument or throws an error. An error is thrown |
| 3472 | * only if an error is encountered during the argument parsing. If |
| 3473 | * the converter function function fails, nil is returned. |
| 3474 | */ |
| 3475 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3476 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3477 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3478 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3479 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3480 | int i; |
| 3481 | struct sample smp; |
| 3482 | |
| 3483 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3484 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3485 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3486 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3487 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3488 | |
| 3489 | /* Get extra arguments. */ |
| 3490 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3491 | if (i >= ARGM_NBARGS) |
| 3492 | break; |
| 3493 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3494 | } |
| 3495 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3496 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3497 | |
| 3498 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3499 | 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] | 3500 | |
| 3501 | /* Run the special args checker. */ |
| 3502 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3503 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3504 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | /* Initialise the sample. */ |
| 3508 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3509 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3510 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3511 | } |
| 3512 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3513 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3514 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3515 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3516 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3517 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3518 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3519 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3520 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3521 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3522 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3523 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3524 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3528 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3529 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3530 | lua_pushstring(L, ""); |
| 3531 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3532 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3533 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3537 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3538 | hlua_smp2lua_str(L, &smp); |
| 3539 | else |
| 3540 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3541 | end: |
| 3542 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3543 | if (args[i].type == ARGT_STR) |
| 3544 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3545 | else if (args[i].type == ARGT_REG) |
| 3546 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3547 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3548 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3549 | |
| 3550 | error: |
| 3551 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3552 | if (args[i].type == ARGT_STR) |
| 3553 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3554 | else if (args[i].type == ARGT_REG) |
| 3555 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3556 | } |
| 3557 | WILL_LJMP(lua_error(L)); |
| 3558 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3559 | } |
| 3560 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3561 | /* |
| 3562 | * |
| 3563 | * |
| 3564 | * Class AppletTCP |
| 3565 | * |
| 3566 | * |
| 3567 | */ |
| 3568 | |
| 3569 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3570 | * a class stream, otherwise it throws an error. |
| 3571 | */ |
| 3572 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3573 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3574 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /* This function creates and push in the stack an Applet object |
| 3578 | * according with a current TXN. |
| 3579 | */ |
| 3580 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3581 | { |
| 3582 | struct hlua_appctx *appctx; |
| 3583 | struct stream_interface *si = ctx->owner; |
| 3584 | struct stream *s = si_strm(si); |
| 3585 | struct proxy *p = s->be; |
| 3586 | |
| 3587 | /* Check stack size. */ |
| 3588 | if (!lua_checkstack(L, 3)) |
| 3589 | return 0; |
| 3590 | |
| 3591 | /* Create the object: obj[0] = userdata. |
| 3592 | * Note that the base of the Converters object is the |
| 3593 | * same than the TXN object. |
| 3594 | */ |
| 3595 | lua_newtable(L); |
| 3596 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3597 | lua_rawseti(L, -2, 0); |
| 3598 | appctx->appctx = ctx; |
| 3599 | appctx->htxn.s = s; |
| 3600 | appctx->htxn.p = p; |
| 3601 | |
| 3602 | /* Create the "f" field that contains a list of fetches. */ |
| 3603 | lua_pushstring(L, "f"); |
| 3604 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3605 | return 0; |
| 3606 | lua_settable(L, -3); |
| 3607 | |
| 3608 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3609 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3610 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3611 | return 0; |
| 3612 | lua_settable(L, -3); |
| 3613 | |
| 3614 | /* Create the "c" field that contains a list of converters. */ |
| 3615 | lua_pushstring(L, "c"); |
| 3616 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3617 | return 0; |
| 3618 | lua_settable(L, -3); |
| 3619 | |
| 3620 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3621 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3622 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3623 | return 0; |
| 3624 | lua_settable(L, -3); |
| 3625 | |
| 3626 | /* Pop a class stream metatable and affect it to the table. */ |
| 3627 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3628 | lua_setmetatable(L, -2); |
| 3629 | |
| 3630 | return 1; |
| 3631 | } |
| 3632 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3633 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3634 | { |
| 3635 | struct hlua_appctx *appctx; |
| 3636 | struct stream *s; |
| 3637 | const char *name; |
| 3638 | size_t len; |
| 3639 | struct sample smp; |
| 3640 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3641 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3642 | 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] | 3643 | |
| 3644 | /* It is useles to retrieve the stream, but this function |
| 3645 | * runs only in a stream context. |
| 3646 | */ |
| 3647 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3648 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3649 | s = appctx->htxn.s; |
| 3650 | |
| 3651 | /* Converts the third argument in a sample. */ |
| 3652 | hlua_lua2smp(L, 3, &smp); |
| 3653 | |
| 3654 | /* Store the sample in a variable. */ |
| 3655 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3656 | |
| 3657 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3658 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3659 | else |
| 3660 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3661 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3662 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3666 | { |
| 3667 | struct hlua_appctx *appctx; |
| 3668 | struct stream *s; |
| 3669 | const char *name; |
| 3670 | size_t len; |
| 3671 | struct sample smp; |
| 3672 | |
| 3673 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3674 | |
| 3675 | /* It is useles to retrieve the stream, but this function |
| 3676 | * runs only in a stream context. |
| 3677 | */ |
| 3678 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3679 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3680 | s = appctx->htxn.s; |
| 3681 | |
| 3682 | /* Unset the variable. */ |
| 3683 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3684 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3685 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3689 | { |
| 3690 | struct hlua_appctx *appctx; |
| 3691 | struct stream *s; |
| 3692 | const char *name; |
| 3693 | size_t len; |
| 3694 | struct sample smp; |
| 3695 | |
| 3696 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3697 | |
| 3698 | /* It is useles to retrieve the stream, but this function |
| 3699 | * runs only in a stream context. |
| 3700 | */ |
| 3701 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3702 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3703 | s = appctx->htxn.s; |
| 3704 | |
| 3705 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3706 | if (!vars_get_by_name(name, len, &smp)) { |
| 3707 | lua_pushnil(L); |
| 3708 | return 1; |
| 3709 | } |
| 3710 | |
| 3711 | return hlua_smp2lua(L, &smp); |
| 3712 | } |
| 3713 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3714 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3715 | { |
| 3716 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3717 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3718 | struct hlua *hlua; |
| 3719 | |
| 3720 | /* 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] | 3721 | if (!s->hlua) |
| 3722 | return 0; |
| 3723 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3724 | |
| 3725 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3726 | |
| 3727 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3728 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3729 | |
| 3730 | /* Get and store new value. */ |
| 3731 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3732 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3733 | |
| 3734 | return 0; |
| 3735 | } |
| 3736 | |
| 3737 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3738 | { |
| 3739 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3740 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3741 | struct hlua *hlua; |
| 3742 | |
| 3743 | /* 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] | 3744 | if (!s->hlua) { |
| 3745 | lua_pushnil(L); |
| 3746 | return 1; |
| 3747 | } |
| 3748 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3749 | |
| 3750 | /* Push configuration index in the stack. */ |
| 3751 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3752 | |
| 3753 | return 1; |
| 3754 | } |
| 3755 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3756 | /* If expected data not yet available, it returns a yield. This function |
| 3757 | * consumes the data in the buffer. It returns a string containing the |
| 3758 | * data. This string can be empty. |
| 3759 | */ |
| 3760 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3761 | { |
| 3762 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3763 | struct stream_interface *si = appctx->appctx->owner; |
| 3764 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3765 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3766 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3767 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3768 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3769 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3770 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3771 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &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 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3774 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3775 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3776 | 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] | 3777 | } |
| 3778 | |
| 3779 | /* End of data: commit the total strings and return. */ |
| 3780 | if (ret < 0) { |
| 3781 | luaL_pushresult(&appctx->b); |
| 3782 | return 1; |
| 3783 | } |
| 3784 | |
| 3785 | /* Ensure that the block 2 length is usable. */ |
| 3786 | if (ret == 1) |
| 3787 | len2 = 0; |
| 3788 | |
| 3789 | /* dont check the max length read and dont check. */ |
| 3790 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3791 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3792 | |
| 3793 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3794 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3795 | luaL_pushresult(&appctx->b); |
| 3796 | return 1; |
| 3797 | } |
| 3798 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3799 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3800 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3801 | { |
| 3802 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3803 | |
| 3804 | /* Initialise the string catenation. */ |
| 3805 | luaL_buffinit(L, &appctx->b); |
| 3806 | |
| 3807 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3808 | } |
| 3809 | |
| 3810 | /* If expected data not yet available, it returns a yield. This function |
| 3811 | * consumes the data in the buffer. It returns a string containing the |
| 3812 | * data. This string can be empty. |
| 3813 | */ |
| 3814 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3815 | { |
| 3816 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3817 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3818 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3819 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3820 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3821 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3822 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3823 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3824 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3825 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3826 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &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 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3829 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3830 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3831 | 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] | 3832 | } |
| 3833 | |
| 3834 | /* End of data: commit the total strings and return. */ |
| 3835 | if (ret < 0) { |
| 3836 | luaL_pushresult(&appctx->b); |
| 3837 | return 1; |
| 3838 | } |
| 3839 | |
| 3840 | /* Ensure that the block 2 length is usable. */ |
| 3841 | if (ret == 1) |
| 3842 | len2 = 0; |
| 3843 | |
| 3844 | if (len == -1) { |
| 3845 | |
| 3846 | /* If len == -1, catenate all the data avalaile and |
| 3847 | * yield because we want to get all the data until |
| 3848 | * the end of data stream. |
| 3849 | */ |
| 3850 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3851 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3852 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3853 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3854 | 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] | 3855 | |
| 3856 | } else { |
| 3857 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3858 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3859 | if (len1 > len) |
| 3860 | len1 = len; |
| 3861 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3862 | len -= len1; |
| 3863 | |
| 3864 | /* Copy the second block. */ |
| 3865 | if (len2 > len) |
| 3866 | len2 = len; |
| 3867 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3868 | len -= len2; |
| 3869 | |
| 3870 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3871 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3872 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3873 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3874 | if (len > 0) { |
| 3875 | lua_pushinteger(L, len); |
| 3876 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3877 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3878 | 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] | 3879 | } |
| 3880 | |
| 3881 | /* return the result. */ |
| 3882 | luaL_pushresult(&appctx->b); |
| 3883 | return 1; |
| 3884 | } |
| 3885 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3886 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3887 | hlua_pusherror(L, "Lua: internal error"); |
| 3888 | WILL_LJMP(lua_error(L)); |
| 3889 | return 0; |
| 3890 | } |
| 3891 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3892 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3893 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3894 | { |
| 3895 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3896 | int len = -1; |
| 3897 | |
| 3898 | if (lua_gettop(L) > 2) |
| 3899 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3900 | if (lua_gettop(L) >= 2) { |
| 3901 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3902 | lua_pop(L, 1); |
| 3903 | } |
| 3904 | |
| 3905 | /* Confirm or set the required length */ |
| 3906 | lua_pushinteger(L, len); |
| 3907 | |
| 3908 | /* Initialise the string catenation. */ |
| 3909 | luaL_buffinit(L, &appctx->b); |
| 3910 | |
| 3911 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3912 | } |
| 3913 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3914 | /* Append data in the output side of the buffer. This data is immediately |
| 3915 | * sent. The function returns the amount of data written. If the buffer |
| 3916 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3917 | * if the channel is closed. |
| 3918 | */ |
| 3919 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3920 | { |
| 3921 | size_t len; |
| 3922 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3923 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3924 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3925 | struct stream_interface *si = appctx->appctx->owner; |
| 3926 | struct channel *chn = si_ic(si); |
| 3927 | int max; |
| 3928 | |
| 3929 | /* Get the max amount of data which can write as input in the channel. */ |
| 3930 | max = channel_recv_max(chn); |
| 3931 | if (max > (len - l)) |
| 3932 | max = len - l; |
| 3933 | |
| 3934 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3935 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3936 | |
| 3937 | /* update counters. */ |
| 3938 | l += max; |
| 3939 | lua_pop(L, 1); |
| 3940 | lua_pushinteger(L, l); |
| 3941 | |
| 3942 | /* If some data is not send, declares the situation to the |
| 3943 | * applet, and returns a yield. |
| 3944 | */ |
| 3945 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3946 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3947 | 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] | 3948 | } |
| 3949 | |
| 3950 | return 1; |
| 3951 | } |
| 3952 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3953 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3954 | * yield the LUA process, and resume it without checking the |
| 3955 | * input arguments. |
| 3956 | */ |
| 3957 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3958 | { |
| 3959 | MAY_LJMP(check_args(L, 2, "send")); |
| 3960 | lua_pushinteger(L, 0); |
| 3961 | |
| 3962 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3963 | } |
| 3964 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3965 | /* |
| 3966 | * |
| 3967 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3968 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3969 | * |
| 3970 | * |
| 3971 | */ |
| 3972 | |
| 3973 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3974 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3975 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3976 | __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] | 3977 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3978 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3979 | } |
| 3980 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3981 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3982 | * according with a current TXN. |
| 3983 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3984 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3985 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3986 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3987 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3988 | struct stream_interface *si = ctx->owner; |
| 3989 | struct stream *s = si_strm(si); |
| 3990 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3991 | struct htx *htx; |
| 3992 | struct htx_blk *blk; |
| 3993 | struct htx_sl *sl; |
| 3994 | struct ist path; |
| 3995 | unsigned long long len = 0; |
| 3996 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3997 | |
| 3998 | /* Check stack size. */ |
| 3999 | if (!lua_checkstack(L, 3)) |
| 4000 | return 0; |
| 4001 | |
| 4002 | /* Create the object: obj[0] = userdata. |
| 4003 | * Note that the base of the Converters object is the |
| 4004 | * same than the TXN object. |
| 4005 | */ |
| 4006 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4007 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4008 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4009 | appctx->appctx = ctx; |
| 4010 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4011 | 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] | 4012 | appctx->htxn.s = s; |
| 4013 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4014 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4015 | /* Create the "f" field that contains a list of fetches. */ |
| 4016 | lua_pushstring(L, "f"); |
| 4017 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4018 | return 0; |
| 4019 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4020 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4021 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4022 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4023 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4024 | return 0; |
| 4025 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4026 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4027 | /* Create the "c" field that contains a list of converters. */ |
| 4028 | lua_pushstring(L, "c"); |
| 4029 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4030 | return 0; |
| 4031 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4032 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4033 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4034 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4035 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4036 | return 0; |
| 4037 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4038 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4039 | htx = htxbuf(&s->req.buf); |
| 4040 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4041 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4042 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4043 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4044 | /* Stores the request method. */ |
| 4045 | lua_pushstring(L, "method"); |
| 4046 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4047 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4048 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4049 | /* Stores the http version. */ |
| 4050 | lua_pushstring(L, "version"); |
| 4051 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4052 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4053 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4054 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4055 | * the array on the top of the stack. |
| 4056 | */ |
| 4057 | lua_pushstring(L, "headers"); |
| 4058 | htxn.s = s; |
| 4059 | htxn.p = px; |
| 4060 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4061 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4062 | return 0; |
| 4063 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4064 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4065 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4066 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4067 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4068 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4069 | p = path.ptr; |
| 4070 | end = path.ptr + path.len; |
| 4071 | q = p; |
| 4072 | while (q < end && *q != '?') |
| 4073 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4074 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4075 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4076 | lua_pushstring(L, "path"); |
| 4077 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4078 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4079 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4080 | /* Stores the query string. */ |
| 4081 | lua_pushstring(L, "qs"); |
| 4082 | if (*q == '?') |
| 4083 | q++; |
| 4084 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4085 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4086 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4087 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4088 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4089 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4090 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4091 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4092 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 4093 | break; |
| 4094 | if (type == HTX_BLK_DATA) |
| 4095 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4096 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4097 | if (htx->extra != ULLONG_MAX) |
| 4098 | len += htx->extra; |
| 4099 | |
| 4100 | /* Stores the request path. */ |
| 4101 | lua_pushstring(L, "length"); |
| 4102 | lua_pushinteger(L, len); |
| 4103 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4104 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4105 | /* Create an empty array of HTTP request headers. */ |
| 4106 | lua_pushstring(L, "response"); |
| 4107 | lua_newtable(L); |
| 4108 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4109 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4110 | /* Pop a class stream metatable and affect it to the table. */ |
| 4111 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4112 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4113 | |
| 4114 | return 1; |
| 4115 | } |
| 4116 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4117 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4118 | { |
| 4119 | struct hlua_appctx *appctx; |
| 4120 | struct stream *s; |
| 4121 | const char *name; |
| 4122 | size_t len; |
| 4123 | struct sample smp; |
| 4124 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4125 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4126 | 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] | 4127 | |
| 4128 | /* It is useles to retrieve the stream, but this function |
| 4129 | * runs only in a stream context. |
| 4130 | */ |
| 4131 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4132 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4133 | s = appctx->htxn.s; |
| 4134 | |
| 4135 | /* Converts the third argument in a sample. */ |
| 4136 | hlua_lua2smp(L, 3, &smp); |
| 4137 | |
| 4138 | /* Store the sample in a variable. */ |
| 4139 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4140 | |
| 4141 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4142 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4143 | else |
| 4144 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4145 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4146 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4150 | { |
| 4151 | struct hlua_appctx *appctx; |
| 4152 | struct stream *s; |
| 4153 | const char *name; |
| 4154 | size_t len; |
| 4155 | struct sample smp; |
| 4156 | |
| 4157 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4158 | |
| 4159 | /* It is useles to retrieve the stream, but this function |
| 4160 | * runs only in a stream context. |
| 4161 | */ |
| 4162 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4163 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4164 | s = appctx->htxn.s; |
| 4165 | |
| 4166 | /* Unset the variable. */ |
| 4167 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4168 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4169 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4173 | { |
| 4174 | struct hlua_appctx *appctx; |
| 4175 | struct stream *s; |
| 4176 | const char *name; |
| 4177 | size_t len; |
| 4178 | struct sample smp; |
| 4179 | |
| 4180 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4181 | |
| 4182 | /* It is useles to retrieve the stream, but this function |
| 4183 | * runs only in a stream context. |
| 4184 | */ |
| 4185 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4186 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4187 | s = appctx->htxn.s; |
| 4188 | |
| 4189 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4190 | if (!vars_get_by_name(name, len, &smp)) { |
| 4191 | lua_pushnil(L); |
| 4192 | return 1; |
| 4193 | } |
| 4194 | |
| 4195 | return hlua_smp2lua(L, &smp); |
| 4196 | } |
| 4197 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4198 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4199 | { |
| 4200 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4201 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4202 | struct hlua *hlua; |
| 4203 | |
| 4204 | /* 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] | 4205 | if (!s->hlua) |
| 4206 | return 0; |
| 4207 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4208 | |
| 4209 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4210 | |
| 4211 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4212 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4213 | |
| 4214 | /* Get and store new value. */ |
| 4215 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4216 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4217 | |
| 4218 | return 0; |
| 4219 | } |
| 4220 | |
| 4221 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4222 | { |
| 4223 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4224 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4225 | struct hlua *hlua; |
| 4226 | |
| 4227 | /* 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] | 4228 | if (!s->hlua) { |
| 4229 | lua_pushnil(L); |
| 4230 | return 1; |
| 4231 | } |
| 4232 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4233 | |
| 4234 | /* Push configuration index in the stack. */ |
| 4235 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4236 | |
| 4237 | return 1; |
| 4238 | } |
| 4239 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4240 | /* If expected data not yet available, it returns a yield. This function |
| 4241 | * consumes the data in the buffer. It returns a string containing the |
| 4242 | * data. This string can be empty. |
| 4243 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4244 | __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] | 4245 | { |
| 4246 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4247 | struct stream_interface *si = appctx->appctx->owner; |
| 4248 | struct channel *req = si_oc(si); |
| 4249 | struct htx *htx; |
| 4250 | struct htx_blk *blk; |
| 4251 | size_t count; |
| 4252 | int stop = 0; |
| 4253 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4254 | htx = htx_from_buf(&req->buf); |
| 4255 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4256 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4257 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4258 | while (count && !stop && blk) { |
| 4259 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4260 | uint32_t sz = htx_get_blksz(blk); |
| 4261 | struct ist v; |
| 4262 | uint32_t vlen; |
| 4263 | char *nl; |
| 4264 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4265 | if (type == HTX_BLK_EOM) { |
| 4266 | stop = 1; |
| 4267 | break; |
| 4268 | } |
| 4269 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4270 | vlen = sz; |
| 4271 | if (vlen > count) { |
| 4272 | if (type != HTX_BLK_DATA) |
| 4273 | break; |
| 4274 | vlen = count; |
| 4275 | } |
| 4276 | |
| 4277 | switch (type) { |
| 4278 | case HTX_BLK_UNUSED: |
| 4279 | break; |
| 4280 | |
| 4281 | case HTX_BLK_DATA: |
| 4282 | v = htx_get_blk_value(htx, blk); |
| 4283 | v.len = vlen; |
| 4284 | nl = istchr(v, '\n'); |
| 4285 | if (nl != NULL) { |
| 4286 | stop = 1; |
| 4287 | vlen = nl - v.ptr + 1; |
| 4288 | } |
| 4289 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4290 | break; |
| 4291 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4292 | case HTX_BLK_TLR: |
| 4293 | case HTX_BLK_EOM: |
| 4294 | stop = 1; |
| 4295 | break; |
| 4296 | |
| 4297 | default: |
| 4298 | break; |
| 4299 | } |
| 4300 | |
| 4301 | co_set_data(req, co_data(req) - vlen); |
| 4302 | count -= vlen; |
| 4303 | if (sz == vlen) |
| 4304 | blk = htx_remove_blk(htx, blk); |
| 4305 | else { |
| 4306 | htx_cut_data_blk(htx, blk, vlen); |
| 4307 | break; |
| 4308 | } |
| 4309 | } |
| 4310 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4311 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4312 | if (!stop) { |
| 4313 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4314 | 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] | 4315 | } |
| 4316 | |
| 4317 | /* return the result. */ |
| 4318 | luaL_pushresult(&appctx->b); |
| 4319 | return 1; |
| 4320 | } |
| 4321 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4322 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4323 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4324 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4325 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4326 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4327 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4328 | /* Initialise the string catenation. */ |
| 4329 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4330 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4331 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4332 | } |
| 4333 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4334 | /* If expected data not yet available, it returns a yield. This function |
| 4335 | * consumes the data in the buffer. It returns a string containing the |
| 4336 | * data. This string can be empty. |
| 4337 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4338 | __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] | 4339 | { |
| 4340 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4341 | struct stream_interface *si = appctx->appctx->owner; |
| 4342 | struct channel *req = si_oc(si); |
| 4343 | struct htx *htx; |
| 4344 | struct htx_blk *blk; |
| 4345 | size_t count; |
| 4346 | int len; |
| 4347 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4348 | htx = htx_from_buf(&req->buf); |
| 4349 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4350 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4351 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4352 | while (count && len && blk) { |
| 4353 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4354 | uint32_t sz = htx_get_blksz(blk); |
| 4355 | struct ist v; |
| 4356 | uint32_t vlen; |
| 4357 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4358 | if (type == HTX_BLK_EOM) { |
| 4359 | len = 0; |
| 4360 | break; |
| 4361 | } |
| 4362 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4363 | vlen = sz; |
| 4364 | if (len > 0 && vlen > len) |
| 4365 | vlen = len; |
| 4366 | if (vlen > count) { |
| 4367 | if (type != HTX_BLK_DATA) |
| 4368 | break; |
| 4369 | vlen = count; |
| 4370 | } |
| 4371 | |
| 4372 | switch (type) { |
| 4373 | case HTX_BLK_UNUSED: |
| 4374 | break; |
| 4375 | |
| 4376 | case HTX_BLK_DATA: |
| 4377 | v = htx_get_blk_value(htx, blk); |
| 4378 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4379 | break; |
| 4380 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4381 | case HTX_BLK_TLR: |
| 4382 | case HTX_BLK_EOM: |
| 4383 | len = 0; |
| 4384 | break; |
| 4385 | |
| 4386 | default: |
| 4387 | break; |
| 4388 | } |
| 4389 | |
| 4390 | co_set_data(req, co_data(req) - vlen); |
| 4391 | count -= vlen; |
| 4392 | if (len > 0) |
| 4393 | len -= vlen; |
| 4394 | if (sz == vlen) |
| 4395 | blk = htx_remove_blk(htx, blk); |
| 4396 | else { |
| 4397 | htx_cut_data_blk(htx, blk, vlen); |
| 4398 | break; |
| 4399 | } |
| 4400 | } |
| 4401 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4402 | htx_to_buf(htx, &req->buf); |
| 4403 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4404 | /* If we are no other data available, yield waiting for new data. */ |
| 4405 | if (len) { |
| 4406 | if (len > 0) { |
| 4407 | lua_pushinteger(L, len); |
| 4408 | lua_replace(L, 2); |
| 4409 | } |
| 4410 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4411 | 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] | 4412 | } |
| 4413 | |
| 4414 | /* return the result. */ |
| 4415 | luaL_pushresult(&appctx->b); |
| 4416 | return 1; |
| 4417 | } |
| 4418 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4419 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4420 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4421 | { |
| 4422 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4423 | int len = -1; |
| 4424 | |
| 4425 | /* Check arguments. */ |
| 4426 | if (lua_gettop(L) > 2) |
| 4427 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4428 | if (lua_gettop(L) >= 2) { |
| 4429 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4430 | lua_pop(L, 1); |
| 4431 | } |
| 4432 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4433 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4434 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4435 | /* Initialise the string catenation. */ |
| 4436 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4437 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4438 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | /* Append data in the output side of the buffer. This data is immediately |
| 4442 | * sent. The function returns the amount of data written. If the buffer |
| 4443 | * cannot contain the data, the function yields. The function returns -1 |
| 4444 | * if the channel is closed. |
| 4445 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4446 | __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] | 4447 | { |
| 4448 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4449 | struct stream_interface *si = appctx->appctx->owner; |
| 4450 | struct channel *res = si_ic(si); |
| 4451 | struct htx *htx = htx_from_buf(&res->buf); |
| 4452 | const char *data; |
| 4453 | size_t len; |
| 4454 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4455 | int max; |
| 4456 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4457 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4458 | if (!max) |
| 4459 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4460 | |
| 4461 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4462 | |
| 4463 | /* Get the max amount of data which can write as input in the channel. */ |
| 4464 | if (max > (len - l)) |
| 4465 | max = len - l; |
| 4466 | |
| 4467 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4468 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4469 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4470 | |
| 4471 | /* update counters. */ |
| 4472 | l += max; |
| 4473 | lua_pop(L, 1); |
| 4474 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4475 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4476 | /* If some data is not send, declares the situation to the |
| 4477 | * applet, and returns a yield. |
| 4478 | */ |
| 4479 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4480 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4481 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4482 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4483 | 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] | 4484 | } |
| 4485 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4486 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4487 | return 1; |
| 4488 | } |
| 4489 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4490 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4491 | * yield the LUA process, and resume it without checking the |
| 4492 | * input arguments. |
| 4493 | */ |
| 4494 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4495 | { |
| 4496 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4497 | |
| 4498 | /* We want to send some data. Headers must be sent. */ |
| 4499 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4500 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4501 | WILL_LJMP(lua_error(L)); |
| 4502 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4503 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4504 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4505 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4506 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4507 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4511 | { |
| 4512 | const char *name; |
| 4513 | int ret; |
| 4514 | |
| 4515 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4516 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4517 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4518 | |
| 4519 | /* Push in the stack the "response" entry. */ |
| 4520 | ret = lua_getfield(L, 1, "response"); |
| 4521 | if (ret != LUA_TTABLE) { |
| 4522 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4523 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4524 | WILL_LJMP(lua_error(L)); |
| 4525 | } |
| 4526 | |
| 4527 | /* check if the header is already registered if it is not |
| 4528 | * the case, register it. |
| 4529 | */ |
| 4530 | ret = lua_getfield(L, -1, name); |
| 4531 | if (ret == LUA_TNIL) { |
| 4532 | |
| 4533 | /* Entry not found. */ |
| 4534 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4535 | |
| 4536 | /* Insert the new header name in the array in the top of the stack. |
| 4537 | * It left the new array in the top of the stack. |
| 4538 | */ |
| 4539 | lua_newtable(L); |
| 4540 | lua_pushvalue(L, 2); |
| 4541 | lua_pushvalue(L, -2); |
| 4542 | lua_settable(L, -4); |
| 4543 | |
| 4544 | } else if (ret != LUA_TTABLE) { |
| 4545 | |
| 4546 | /* corruption error. */ |
| 4547 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4548 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4549 | WILL_LJMP(lua_error(L)); |
| 4550 | } |
| 4551 | |
| 4552 | /* Now the top od thestack is an array of values. We push |
| 4553 | * the header value as new entry. |
| 4554 | */ |
| 4555 | lua_pushvalue(L, 3); |
| 4556 | ret = lua_rawlen(L, -2); |
| 4557 | lua_rawseti(L, -2, ret + 1); |
| 4558 | lua_pushboolean(L, 1); |
| 4559 | return 1; |
| 4560 | } |
| 4561 | |
| 4562 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4563 | { |
| 4564 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4565 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4566 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4567 | |
| 4568 | if (status < 100 || status > 599) { |
| 4569 | lua_pushboolean(L, 0); |
| 4570 | return 1; |
| 4571 | } |
| 4572 | |
| 4573 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4574 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4575 | lua_pushboolean(L, 1); |
| 4576 | return 1; |
| 4577 | } |
| 4578 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4579 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4580 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4581 | { |
| 4582 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4583 | struct stream_interface *si = appctx->appctx->owner; |
| 4584 | struct channel *res = si_ic(si); |
| 4585 | struct htx *htx; |
| 4586 | struct htx_sl *sl; |
| 4587 | struct h1m h1m; |
| 4588 | const char *status, *reason; |
| 4589 | const char *name, *value; |
| 4590 | size_t nlen, vlen; |
| 4591 | unsigned int flags; |
| 4592 | |
| 4593 | /* Send the message at once. */ |
| 4594 | htx = htx_from_buf(&res->buf); |
| 4595 | h1m_init_res(&h1m); |
| 4596 | |
| 4597 | /* Use the same http version than the request. */ |
| 4598 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4599 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4600 | if (reason == NULL) |
| 4601 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4602 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4603 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4604 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4605 | } |
| 4606 | else { |
| 4607 | flags = HTX_SL_F_IS_RESP; |
| 4608 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4609 | } |
| 4610 | if (!sl) { |
| 4611 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4612 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4613 | WILL_LJMP(lua_error(L)); |
| 4614 | } |
| 4615 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4616 | |
| 4617 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4618 | lua_pushvalue(L, 0); |
| 4619 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4620 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4621 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4622 | WILL_LJMP(lua_error(L)); |
| 4623 | } |
| 4624 | |
| 4625 | /* Browse the list of headers. */ |
| 4626 | lua_pushnil(L); |
| 4627 | while(lua_next(L, -2) != 0) { |
| 4628 | /* We expect a string as -2. */ |
| 4629 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4630 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4631 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4632 | lua_typename(L, lua_type(L, -2))); |
| 4633 | WILL_LJMP(lua_error(L)); |
| 4634 | } |
| 4635 | name = lua_tolstring(L, -2, &nlen); |
| 4636 | |
| 4637 | /* We expect an array as -1. */ |
| 4638 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4639 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4640 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4641 | name, |
| 4642 | lua_typename(L, lua_type(L, -1))); |
| 4643 | WILL_LJMP(lua_error(L)); |
| 4644 | } |
| 4645 | |
| 4646 | /* Browse the table who is on the top of the stack. */ |
| 4647 | lua_pushnil(L); |
| 4648 | while(lua_next(L, -2) != 0) { |
| 4649 | int id; |
| 4650 | |
| 4651 | /* We expect a number as -2. */ |
| 4652 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4653 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4654 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4655 | name, |
| 4656 | lua_typename(L, lua_type(L, -2))); |
| 4657 | WILL_LJMP(lua_error(L)); |
| 4658 | } |
| 4659 | id = lua_tointeger(L, -2); |
| 4660 | |
| 4661 | /* We expect a string as -2. */ |
| 4662 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4663 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4664 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4665 | name, id, |
| 4666 | lua_typename(L, lua_type(L, -1))); |
| 4667 | WILL_LJMP(lua_error(L)); |
| 4668 | } |
| 4669 | value = lua_tolstring(L, -1, &vlen); |
| 4670 | |
| 4671 | /* Simple Protocol checks. */ |
| 4672 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4673 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4674 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4675 | struct ist v = ist2(value, vlen); |
| 4676 | int ret; |
| 4677 | |
| 4678 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4679 | if (ret < 0) { |
| 4680 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4681 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4682 | name); |
| 4683 | WILL_LJMP(lua_error(L)); |
| 4684 | } |
| 4685 | else if (ret == 0) |
| 4686 | goto next; /* Skip it */ |
| 4687 | } |
| 4688 | |
| 4689 | /* Add a new header */ |
| 4690 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4691 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4692 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4693 | name); |
| 4694 | WILL_LJMP(lua_error(L)); |
| 4695 | } |
| 4696 | next: |
| 4697 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4698 | lua_pop(L, 1); |
| 4699 | } |
| 4700 | |
| 4701 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4702 | lua_pop(L, 1); |
| 4703 | } |
| 4704 | |
| 4705 | if (h1m.flags & H1_MF_CHNK) |
| 4706 | h1m.flags &= ~H1_MF_CLEN; |
| 4707 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4708 | h1m.flags |= H1_MF_XFER_LEN; |
| 4709 | |
| 4710 | /* Uset HTX start-line flags */ |
| 4711 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4712 | flags |= HTX_SL_F_XFER_ENC; |
| 4713 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4714 | flags |= HTX_SL_F_XFER_LEN; |
| 4715 | if (h1m.flags & H1_MF_CHNK) |
| 4716 | flags |= HTX_SL_F_CHNK; |
| 4717 | else if (h1m.flags & H1_MF_CLEN) |
| 4718 | flags |= HTX_SL_F_CLEN; |
| 4719 | if (h1m.body_len == 0) |
| 4720 | flags |= HTX_SL_F_BODYLESS; |
| 4721 | } |
| 4722 | sl->flags |= flags; |
| 4723 | |
| 4724 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4725 | * and the status code implies the presence of a message body, we must |
| 4726 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4727 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4728 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4729 | */ |
| 4730 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4731 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4732 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4733 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4734 | /* Add a new header */ |
| 4735 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4736 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4737 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4738 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4739 | WILL_LJMP(lua_error(L)); |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | /* Finalize headers. */ |
| 4744 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4745 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4746 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4747 | WILL_LJMP(lua_error(L)); |
| 4748 | } |
| 4749 | |
| 4750 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4751 | b_reset(&res->buf); |
| 4752 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4753 | WILL_LJMP(lua_error(L)); |
| 4754 | } |
| 4755 | |
| 4756 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4757 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4758 | |
| 4759 | /* Headers sent, set the flag. */ |
| 4760 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4761 | return 0; |
| 4762 | |
| 4763 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4764 | /* We will build the status line and the headers of the HTTP response. |
| 4765 | * We will try send at once if its not possible, we give back the hand |
| 4766 | * waiting for more room. |
| 4767 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4768 | __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] | 4769 | { |
| 4770 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4771 | struct stream_interface *si = appctx->appctx->owner; |
| 4772 | struct channel *res = si_ic(si); |
| 4773 | |
| 4774 | if (co_data(res)) { |
| 4775 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4776 | 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] | 4777 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4778 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4782 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4783 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4784 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4785 | } |
| 4786 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4787 | /* |
| 4788 | * |
| 4789 | * |
| 4790 | * Class HTTP |
| 4791 | * |
| 4792 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4793 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4794 | |
| 4795 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4796 | * a class stream, otherwise it throws an error. |
| 4797 | */ |
| 4798 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4799 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4800 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4801 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4802 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4803 | /* This function creates and push in the stack a HTTP object |
| 4804 | * according with a current TXN. |
| 4805 | */ |
| 4806 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4807 | { |
| 4808 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4809 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4810 | /* Check stack size. */ |
| 4811 | if (!lua_checkstack(L, 3)) |
| 4812 | return 0; |
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 | /* Create the object: obj[0] = userdata. |
| 4815 | * Note that the base of the Converters object is the |
| 4816 | * same than the TXN object. |
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 | lua_newtable(L); |
| 4819 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4820 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4821 | |
| 4822 | htxn->s = txn->s; |
| 4823 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4824 | htxn->dir = txn->dir; |
| 4825 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4826 | |
| 4827 | /* Pop a class stream metatable and affect it to the table. */ |
| 4828 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4829 | lua_setmetatable(L, -2); |
| 4830 | |
| 4831 | return 1; |
| 4832 | } |
| 4833 | |
| 4834 | /* This function creates ans returns an array of HTTP headers. |
| 4835 | * This function does not fails. It is used as wrapper with the |
| 4836 | * 2 following functions. |
| 4837 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4838 | __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] | 4839 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4840 | struct htx *htx; |
| 4841 | int32_t pos; |
| 4842 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4843 | /* Create the table. */ |
| 4844 | lua_newtable(L); |
| 4845 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4846 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4847 | htx = htxbuf(&msg->chn->buf); |
| 4848 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4849 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4850 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4851 | struct ist n, v; |
| 4852 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4853 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4854 | if (type == HTX_BLK_HDR) { |
| 4855 | n = htx_get_blk_name(htx,blk); |
| 4856 | v = htx_get_blk_value(htx, blk); |
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 | else if (type == HTX_BLK_EOH) |
| 4859 | break; |
| 4860 | else |
| 4861 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4862 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4863 | /* Check for existing entry: |
| 4864 | * assume that the table is on the top of the stack, and |
| 4865 | * push the key in the stack, the function lua_gettable() |
| 4866 | * perform the lookup. |
| 4867 | */ |
| 4868 | lua_pushlstring(L, n.ptr, n.len); |
| 4869 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4870 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4871 | switch (lua_type(L, -1)) { |
| 4872 | case LUA_TNIL: |
| 4873 | /* Table not found, create it. */ |
| 4874 | lua_pop(L, 1); /* remove the nil value. */ |
| 4875 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4876 | lua_newtable(L); /* create and push empty table. */ |
| 4877 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4878 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4879 | 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] | 4880 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4881 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4882 | case LUA_TTABLE: |
| 4883 | /* Entry found: push the value in the table. */ |
| 4884 | len = lua_rawlen(L, -1); |
| 4885 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4886 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4887 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4888 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4889 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4890 | default: |
| 4891 | /* Other cases are errors. */ |
| 4892 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4893 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4894 | } |
| 4895 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4896 | return 1; |
| 4897 | } |
| 4898 | |
| 4899 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4900 | { |
| 4901 | struct hlua_txn *htxn; |
| 4902 | |
| 4903 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4904 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4905 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4906 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4907 | WILL_LJMP(lua_error(L)); |
| 4908 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4909 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4913 | { |
| 4914 | struct hlua_txn *htxn; |
| 4915 | |
| 4916 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4917 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4918 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4919 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4920 | WILL_LJMP(lua_error(L)); |
| 4921 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4922 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | /* This function replace full header, or just a value in |
| 4926 | * the request or in the response. It is a wrapper fir the |
| 4927 | * 4 following functions. |
| 4928 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4929 | __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] | 4930 | { |
| 4931 | size_t name_len; |
| 4932 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4933 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4934 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4935 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4936 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4937 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4938 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4939 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4940 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4941 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4942 | 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] | 4943 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4944 | return 0; |
| 4945 | } |
| 4946 | |
| 4947 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4948 | { |
| 4949 | struct hlua_txn *htxn; |
| 4950 | |
| 4951 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4952 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4953 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4954 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4955 | WILL_LJMP(lua_error(L)); |
| 4956 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4957 | 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] | 4958 | } |
| 4959 | |
| 4960 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4961 | { |
| 4962 | struct hlua_txn *htxn; |
| 4963 | |
| 4964 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4965 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4966 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4967 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4968 | WILL_LJMP(lua_error(L)); |
| 4969 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4970 | 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] | 4971 | } |
| 4972 | |
| 4973 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4974 | { |
| 4975 | struct hlua_txn *htxn; |
| 4976 | |
| 4977 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4978 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4979 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4980 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4981 | WILL_LJMP(lua_error(L)); |
| 4982 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4983 | 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] | 4984 | } |
| 4985 | |
| 4986 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4987 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4988 | struct hlua_txn *htxn; |
| 4989 | |
| 4990 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4991 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4992 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4993 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4994 | WILL_LJMP(lua_error(L)); |
| 4995 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4996 | 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] | 4997 | } |
| 4998 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4999 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5000 | * It is a wrapper for the 2 following functions. |
| 5001 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5002 | __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] | 5003 | { |
| 5004 | size_t len; |
| 5005 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5006 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5007 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5008 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5009 | ctx.blk = NULL; |
| 5010 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5011 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5012 | return 0; |
| 5013 | } |
| 5014 | |
| 5015 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5016 | { |
| 5017 | struct hlua_txn *htxn; |
| 5018 | |
| 5019 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5020 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5021 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5022 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5023 | WILL_LJMP(lua_error(L)); |
| 5024 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5025 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5029 | { |
| 5030 | struct hlua_txn *htxn; |
| 5031 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5032 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5033 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5034 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5035 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5036 | WILL_LJMP(lua_error(L)); |
| 5037 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5038 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5039 | } |
| 5040 | |
| 5041 | /* This function adds an header. It is a wrapper used by |
| 5042 | * the 2 following functions. |
| 5043 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5044 | __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] | 5045 | { |
| 5046 | size_t name_len; |
| 5047 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5048 | size_t value_len; |
| 5049 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5050 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5051 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5052 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5053 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5054 | return 0; |
| 5055 | } |
| 5056 | |
| 5057 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5058 | { |
| 5059 | struct hlua_txn *htxn; |
| 5060 | |
| 5061 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5062 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5063 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5064 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5065 | WILL_LJMP(lua_error(L)); |
| 5066 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5067 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5071 | { |
| 5072 | struct hlua_txn *htxn; |
| 5073 | |
| 5074 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5075 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5076 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5077 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5078 | WILL_LJMP(lua_error(L)); |
| 5079 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5080 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5084 | { |
| 5085 | struct hlua_txn *htxn; |
| 5086 | |
| 5087 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5088 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5089 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5090 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5091 | WILL_LJMP(lua_error(L)); |
| 5092 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5093 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5094 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5098 | { |
| 5099 | struct hlua_txn *htxn; |
| 5100 | |
| 5101 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5102 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5103 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5104 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5105 | WILL_LJMP(lua_error(L)); |
| 5106 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5107 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5108 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | /* This function set the method. */ |
| 5112 | static int hlua_http_req_set_meth(lua_State *L) |
| 5113 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5114 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5115 | size_t name_len; |
| 5116 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5117 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5118 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5119 | WILL_LJMP(lua_error(L)); |
| 5120 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5121 | 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] | 5122 | return 1; |
| 5123 | } |
| 5124 | |
| 5125 | /* This function set the method. */ |
| 5126 | static int hlua_http_req_set_path(lua_State *L) |
| 5127 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5128 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5129 | size_t name_len; |
| 5130 | 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] | 5131 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5132 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5133 | WILL_LJMP(lua_error(L)); |
| 5134 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5135 | 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] | 5136 | return 1; |
| 5137 | } |
| 5138 | |
| 5139 | /* This function set the query-string. */ |
| 5140 | static int hlua_http_req_set_query(lua_State *L) |
| 5141 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5142 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5143 | size_t name_len; |
| 5144 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5145 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5146 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5147 | WILL_LJMP(lua_error(L)); |
| 5148 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5149 | /* Check length. */ |
| 5150 | if (name_len > trash.size - 1) { |
| 5151 | lua_pushboolean(L, 0); |
| 5152 | return 1; |
| 5153 | } |
| 5154 | |
| 5155 | /* Add the mark question as prefix. */ |
| 5156 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5157 | trash.area[trash.data++] = '?'; |
| 5158 | memcpy(trash.area + trash.data, name, name_len); |
| 5159 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5160 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5161 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5162 | 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] | 5163 | return 1; |
| 5164 | } |
| 5165 | |
| 5166 | /* This function set the uri. */ |
| 5167 | static int hlua_http_req_set_uri(lua_State *L) |
| 5168 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5169 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5170 | size_t name_len; |
| 5171 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5172 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5173 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5174 | WILL_LJMP(lua_error(L)); |
| 5175 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5176 | 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] | 5177 | return 1; |
| 5178 | } |
| 5179 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5180 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5181 | static int hlua_http_res_set_status(lua_State *L) |
| 5182 | { |
| 5183 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5184 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5185 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5186 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5187 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5188 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5189 | WILL_LJMP(lua_error(L)); |
| 5190 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5191 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5192 | return 0; |
| 5193 | } |
| 5194 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5195 | /* |
| 5196 | * |
| 5197 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5198 | * Class TXN |
| 5199 | * |
| 5200 | * |
| 5201 | */ |
| 5202 | |
| 5203 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5204 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5205 | */ |
| 5206 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5207 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5208 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5209 | } |
| 5210 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5211 | __LJMP static int hlua_set_var(lua_State *L) |
| 5212 | { |
| 5213 | struct hlua_txn *htxn; |
| 5214 | const char *name; |
| 5215 | size_t len; |
| 5216 | struct sample smp; |
| 5217 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5218 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5219 | 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] | 5220 | |
| 5221 | /* It is useles to retrieve the stream, but this function |
| 5222 | * runs only in a stream context. |
| 5223 | */ |
| 5224 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5225 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5226 | |
| 5227 | /* Converts the third argument in a sample. */ |
| 5228 | hlua_lua2smp(L, 3, &smp); |
| 5229 | |
| 5230 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5231 | 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] | 5232 | |
| 5233 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5234 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5235 | else |
| 5236 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5237 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5238 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5239 | } |
| 5240 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5241 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5242 | { |
| 5243 | struct hlua_txn *htxn; |
| 5244 | const char *name; |
| 5245 | size_t len; |
| 5246 | struct sample smp; |
| 5247 | |
| 5248 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5249 | |
| 5250 | /* It is useles to retrieve the stream, but this function |
| 5251 | * runs only in a stream context. |
| 5252 | */ |
| 5253 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5254 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5255 | |
| 5256 | /* Unset the variable. */ |
| 5257 | 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] | 5258 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5259 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5260 | } |
| 5261 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5262 | __LJMP static int hlua_get_var(lua_State *L) |
| 5263 | { |
| 5264 | struct hlua_txn *htxn; |
| 5265 | const char *name; |
| 5266 | size_t len; |
| 5267 | struct sample smp; |
| 5268 | |
| 5269 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5270 | |
| 5271 | /* It is useles to retrieve the stream, but this function |
| 5272 | * runs only in a stream context. |
| 5273 | */ |
| 5274 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5275 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5276 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5277 | 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] | 5278 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5279 | lua_pushnil(L); |
| 5280 | return 1; |
| 5281 | } |
| 5282 | |
| 5283 | return hlua_smp2lua(L, &smp); |
| 5284 | } |
| 5285 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5286 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5287 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5288 | struct hlua *hlua; |
| 5289 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5290 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5291 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5292 | /* It is useles to retrieve the stream, but this function |
| 5293 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5294 | */ |
| 5295 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5296 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5297 | |
| 5298 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5299 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5300 | |
| 5301 | /* Get and store new value. */ |
| 5302 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5303 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5304 | |
| 5305 | return 0; |
| 5306 | } |
| 5307 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5308 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5309 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5310 | struct hlua *hlua; |
| 5311 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5312 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5313 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5314 | /* It is useles to retrieve the stream, but this function |
| 5315 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5316 | */ |
| 5317 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5318 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5319 | |
| 5320 | /* Push configuration index in the stack. */ |
| 5321 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5322 | |
| 5323 | return 1; |
| 5324 | } |
| 5325 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5326 | /* Create stack entry containing a class TXN. This function |
| 5327 | * return 0 if the stack does not contains free slots, |
| 5328 | * otherwise it returns 1. |
| 5329 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5330 | 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] | 5331 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5332 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5333 | |
| 5334 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5335 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5336 | return 0; |
| 5337 | |
| 5338 | /* NOTE: The allocation never fails. The failure |
| 5339 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5340 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5341 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5342 | /* Create the object: obj[0] = userdata. */ |
| 5343 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5344 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5345 | lua_rawseti(L, -2, 0); |
| 5346 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5347 | htxn->s = s; |
| 5348 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5349 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5350 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5351 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5352 | /* Create the "f" field that contains a list of fetches. */ |
| 5353 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5354 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5355 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5356 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5357 | |
| 5358 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5359 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5360 | 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] | 5361 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5362 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5363 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5364 | /* Create the "c" field that contains a list of converters. */ |
| 5365 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5366 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5367 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5368 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5369 | |
| 5370 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5371 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5372 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5373 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5374 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5375 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5376 | /* Create the "req" field that contains the request channel object. */ |
| 5377 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5378 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5379 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5380 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5381 | |
| 5382 | /* Create the "res" field that contains the response channel object. */ |
| 5383 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5384 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5385 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5386 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5387 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5388 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5389 | lua_pushstring(L, "http"); |
| 5390 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5391 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5392 | return 0; |
| 5393 | } |
| 5394 | else |
| 5395 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5396 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5397 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5398 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5399 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5400 | lua_setmetatable(L, -2); |
| 5401 | |
| 5402 | return 1; |
| 5403 | } |
| 5404 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5405 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5406 | { |
| 5407 | const char *msg; |
| 5408 | struct hlua_txn *htxn; |
| 5409 | |
| 5410 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5411 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5412 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5413 | |
| 5414 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5415 | return 0; |
| 5416 | } |
| 5417 | |
| 5418 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5419 | { |
| 5420 | int level; |
| 5421 | const char *msg; |
| 5422 | struct hlua_txn *htxn; |
| 5423 | |
| 5424 | MAY_LJMP(check_args(L, 3, "log")); |
| 5425 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5426 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5427 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5428 | |
| 5429 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5430 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5431 | |
| 5432 | hlua_sendlog(htxn->s->be, level, msg); |
| 5433 | return 0; |
| 5434 | } |
| 5435 | |
| 5436 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5437 | { |
| 5438 | const char *msg; |
| 5439 | struct hlua_txn *htxn; |
| 5440 | |
| 5441 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5442 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5443 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5444 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5445 | return 0; |
| 5446 | } |
| 5447 | |
| 5448 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5449 | { |
| 5450 | const char *msg; |
| 5451 | struct hlua_txn *htxn; |
| 5452 | |
| 5453 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5454 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5455 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5456 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5457 | return 0; |
| 5458 | } |
| 5459 | |
| 5460 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5461 | { |
| 5462 | const char *msg; |
| 5463 | struct hlua_txn *htxn; |
| 5464 | |
| 5465 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5466 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5467 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5468 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5469 | return 0; |
| 5470 | } |
| 5471 | |
| 5472 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5473 | { |
| 5474 | const char *msg; |
| 5475 | struct hlua_txn *htxn; |
| 5476 | |
| 5477 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5478 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5479 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5480 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5481 | return 0; |
| 5482 | } |
| 5483 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5484 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5485 | { |
| 5486 | struct hlua_txn *htxn; |
| 5487 | int ll; |
| 5488 | |
| 5489 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5490 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5491 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5492 | |
| 5493 | if (ll < 0 || ll > 7) |
| 5494 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5495 | |
| 5496 | htxn->s->logs.level = ll; |
| 5497 | return 0; |
| 5498 | } |
| 5499 | |
| 5500 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5501 | { |
| 5502 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5503 | int tos; |
| 5504 | |
| 5505 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5506 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5507 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5508 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5509 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5510 | return 0; |
| 5511 | } |
| 5512 | |
| 5513 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5514 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5515 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5516 | int mark; |
| 5517 | |
| 5518 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5519 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5520 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5521 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5522 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5523 | return 0; |
| 5524 | } |
| 5525 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5526 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5527 | { |
| 5528 | struct hlua_txn *htxn; |
| 5529 | |
| 5530 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5531 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5532 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5533 | return 0; |
| 5534 | } |
| 5535 | |
| 5536 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5537 | { |
| 5538 | struct hlua_txn *htxn; |
| 5539 | |
| 5540 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5541 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5542 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5543 | return 0; |
| 5544 | } |
| 5545 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5546 | /* 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] | 5547 | * 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] | 5548 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5549 | * error. The Reply must be on top of the stack. |
| 5550 | */ |
| 5551 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5552 | { |
| 5553 | struct htx *htx; |
| 5554 | struct htx_sl *sl; |
| 5555 | struct h1m h1m; |
| 5556 | const char *status, *reason, *body; |
| 5557 | size_t status_len, reason_len, body_len; |
| 5558 | int ret, code, flags; |
| 5559 | |
| 5560 | code = 200; |
| 5561 | status = "200"; |
| 5562 | status_len = 3; |
| 5563 | ret = lua_getfield(L, -1, "status"); |
| 5564 | if (ret == LUA_TNUMBER) { |
| 5565 | code = lua_tointeger(L, -1); |
| 5566 | status = lua_tolstring(L, -1, &status_len); |
| 5567 | } |
| 5568 | lua_pop(L, 1); |
| 5569 | |
| 5570 | reason = http_get_reason(code); |
| 5571 | reason_len = strlen(reason); |
| 5572 | ret = lua_getfield(L, -1, "reason"); |
| 5573 | if (ret == LUA_TSTRING) |
| 5574 | reason = lua_tolstring(L, -1, &reason_len); |
| 5575 | lua_pop(L, 1); |
| 5576 | |
| 5577 | body = NULL; |
| 5578 | body_len = 0; |
| 5579 | ret = lua_getfield(L, -1, "body"); |
| 5580 | if (ret == LUA_TSTRING) |
| 5581 | body = lua_tolstring(L, -1, &body_len); |
| 5582 | lua_pop(L, 1); |
| 5583 | |
| 5584 | /* Prepare the response before inserting the headers */ |
| 5585 | h1m_init_res(&h1m); |
| 5586 | htx = htx_from_buf(&s->res.buf); |
| 5587 | channel_htx_truncate(&s->res, htx); |
| 5588 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5589 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5590 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5591 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5592 | } |
| 5593 | else { |
| 5594 | flags = HTX_SL_F_IS_RESP; |
| 5595 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5596 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5597 | } |
| 5598 | if (!sl) |
| 5599 | goto fail; |
| 5600 | sl->info.res.status = code; |
| 5601 | |
| 5602 | /* Push in the stack the "headers" entry. */ |
| 5603 | ret = lua_getfield(L, -1, "headers"); |
| 5604 | if (ret != LUA_TTABLE) |
| 5605 | goto skip_headers; |
| 5606 | |
| 5607 | lua_pushnil(L); |
| 5608 | while (lua_next(L, -2) != 0) { |
| 5609 | struct ist name, value; |
| 5610 | const char *n, *v; |
| 5611 | size_t nlen, vlen; |
| 5612 | |
| 5613 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5614 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5615 | goto next_hdr; |
| 5616 | } |
| 5617 | |
| 5618 | n = lua_tolstring(L, -2, &nlen); |
| 5619 | name = ist2(n, nlen); |
| 5620 | if (isteqi(name, ist("content-length"))) { |
| 5621 | /* Always skip content-length header. It will be added |
| 5622 | * later with the correct len |
| 5623 | */ |
| 5624 | goto next_hdr; |
| 5625 | } |
| 5626 | |
| 5627 | /* Loop on header's values */ |
| 5628 | lua_pushnil(L); |
| 5629 | while (lua_next(L, -2)) { |
| 5630 | if (!lua_isstring(L, -1)) { |
| 5631 | /* Skip the value if it is not a string */ |
| 5632 | goto next_value; |
| 5633 | } |
| 5634 | |
| 5635 | v = lua_tolstring(L, -1, &vlen); |
| 5636 | value = ist2(v, vlen); |
| 5637 | |
| 5638 | if (isteqi(name, ist("transfer-encoding"))) |
| 5639 | h1_parse_xfer_enc_header(&h1m, value); |
| 5640 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5641 | goto fail; |
| 5642 | |
| 5643 | next_value: |
| 5644 | lua_pop(L, 1); |
| 5645 | } |
| 5646 | |
| 5647 | next_hdr: |
| 5648 | lua_pop(L, 1); |
| 5649 | } |
| 5650 | skip_headers: |
| 5651 | lua_pop(L, 1); |
| 5652 | |
| 5653 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5654 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5655 | const char *clen = ultoa(body_len); |
| 5656 | |
| 5657 | h1m.flags |= H1_MF_CLEN; |
| 5658 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5659 | goto fail; |
| 5660 | } |
| 5661 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5662 | h1m.flags |= H1_MF_XFER_LEN; |
| 5663 | |
| 5664 | /* Update HTX start-line flags */ |
| 5665 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5666 | flags |= HTX_SL_F_XFER_ENC; |
| 5667 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5668 | flags |= HTX_SL_F_XFER_LEN; |
| 5669 | if (h1m.flags & H1_MF_CHNK) |
| 5670 | flags |= HTX_SL_F_CHNK; |
| 5671 | else if (h1m.flags & H1_MF_CLEN) |
| 5672 | flags |= HTX_SL_F_CLEN; |
| 5673 | if (h1m.body_len == 0) |
| 5674 | flags |= HTX_SL_F_BODYLESS; |
| 5675 | } |
| 5676 | sl->flags |= flags; |
| 5677 | |
| 5678 | |
| 5679 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5680 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5681 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5682 | goto fail; |
| 5683 | |
| 5684 | /* Now, forward the response and terminate the transaction */ |
| 5685 | s->txn->status = code; |
| 5686 | htx_to_buf(htx, &s->res.buf); |
| 5687 | if (!http_forward_proxy_resp(s, 1)) |
| 5688 | goto fail; |
| 5689 | |
| 5690 | return 1; |
| 5691 | |
| 5692 | fail: |
| 5693 | channel_htx_truncate(&s->res, htx); |
| 5694 | return 0; |
| 5695 | } |
| 5696 | |
| 5697 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5698 | * processing is just aborted. Nothing is returned to the client and all |
| 5699 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5700 | * is forwarded to the client before terminating the transaction. On success, |
| 5701 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5702 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5703 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5704 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5705 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5706 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5707 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5708 | struct stream *s; |
| 5709 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5710 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5711 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5712 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5713 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5714 | * just end the execution of the current lua code. */ |
| 5715 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5716 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5717 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5718 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5719 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5720 | struct channel *req = &s->req; |
| 5721 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5722 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5723 | channel_auto_read(req); |
| 5724 | channel_abort(req); |
| 5725 | channel_auto_close(req); |
| 5726 | channel_erase(req); |
| 5727 | |
| 5728 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5729 | channel_auto_read(res); |
| 5730 | channel_auto_close(res); |
| 5731 | channel_shutr_now(res); |
| 5732 | |
| 5733 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5734 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5735 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5736 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5737 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5738 | /* No reply or invalid reply */ |
| 5739 | s->txn->status = 0; |
| 5740 | http_reply_and_close(s, 0, NULL); |
| 5741 | } |
| 5742 | else { |
| 5743 | /* Remove extra args to have the reply on top of the stack */ |
| 5744 | if (lua_gettop(L) > 2) |
| 5745 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5746 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5747 | if (!hlua_txn_forward_reply(L, s)) { |
| 5748 | if (!(s->flags & SF_ERR_MASK)) |
| 5749 | s->flags |= SF_ERR_PRXCOND; |
| 5750 | lua_pushinteger(L, ACT_RET_ERR); |
| 5751 | WILL_LJMP(hlua_done(L)); |
| 5752 | return 0; /* Never reached */ |
| 5753 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5754 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5755 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5756 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5757 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5758 | /* let's log the request time */ |
| 5759 | s->logs.tv_request = now; |
| 5760 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5761 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5762 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5763 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5764 | done: |
| 5765 | if (!(s->flags & SF_ERR_MASK)) |
| 5766 | s->flags |= SF_ERR_LOCAL; |
| 5767 | if (!(s->flags & SF_FINST_MASK)) |
| 5768 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5769 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5770 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5771 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5772 | return 0; |
| 5773 | } |
| 5774 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5775 | /* |
| 5776 | * |
| 5777 | * |
| 5778 | * Class REPLY |
| 5779 | * |
| 5780 | * |
| 5781 | */ |
| 5782 | |
| 5783 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5784 | * free slots, the function fails and returns 0; |
| 5785 | */ |
| 5786 | static int hlua_txn_reply_new(lua_State *L) |
| 5787 | { |
| 5788 | struct hlua_txn *htxn; |
| 5789 | const char *reason, *body = NULL; |
| 5790 | int ret, status; |
| 5791 | |
| 5792 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5793 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5794 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5795 | WILL_LJMP(lua_error(L)); |
| 5796 | } |
| 5797 | |
| 5798 | /* Default value */ |
| 5799 | status = 200; |
| 5800 | reason = http_get_reason(status); |
| 5801 | |
| 5802 | if (lua_istable(L, 2)) { |
| 5803 | /* load status and reason from the table argument at index 2 */ |
| 5804 | ret = lua_getfield(L, 2, "status"); |
| 5805 | if (ret == LUA_TNIL) |
| 5806 | goto reason; |
| 5807 | else if (ret != LUA_TNUMBER) { |
| 5808 | /* invalid status: ignore the reason */ |
| 5809 | goto body; |
| 5810 | } |
| 5811 | status = lua_tointeger(L, -1); |
| 5812 | |
| 5813 | reason: |
| 5814 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5815 | ret = lua_getfield(L, 2, "reason"); |
| 5816 | if (ret == LUA_TSTRING) |
| 5817 | reason = lua_tostring(L, -1); |
| 5818 | |
| 5819 | body: |
| 5820 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5821 | ret = lua_getfield(L, 2, "body"); |
| 5822 | if (ret == LUA_TSTRING) |
| 5823 | body = lua_tostring(L, -1); |
| 5824 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5825 | } |
| 5826 | |
| 5827 | /* Create the Reply table */ |
| 5828 | lua_newtable(L); |
| 5829 | |
| 5830 | /* Add status element */ |
| 5831 | lua_pushstring(L, "status"); |
| 5832 | lua_pushinteger(L, status); |
| 5833 | lua_settable(L, -3); |
| 5834 | |
| 5835 | /* Add reason element */ |
| 5836 | reason = http_get_reason(status); |
| 5837 | lua_pushstring(L, "reason"); |
| 5838 | lua_pushstring(L, reason); |
| 5839 | lua_settable(L, -3); |
| 5840 | |
| 5841 | /* Add body element, nil if undefined */ |
| 5842 | lua_pushstring(L, "body"); |
| 5843 | if (body) |
| 5844 | lua_pushstring(L, body); |
| 5845 | else |
| 5846 | lua_pushnil(L); |
| 5847 | lua_settable(L, -3); |
| 5848 | |
| 5849 | /* Add headers element */ |
| 5850 | lua_pushstring(L, "headers"); |
| 5851 | lua_newtable(L); |
| 5852 | |
| 5853 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5854 | if (lua_istable(L, 2)) { |
| 5855 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5856 | ret = lua_getfield(L, 2, "headers"); |
| 5857 | if (ret == LUA_TTABLE) { |
| 5858 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5859 | lua_pushnil(L); |
| 5860 | while (lua_next(L, -2) != 0) { |
| 5861 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5862 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5863 | /* invalid value type, skip it */ |
| 5864 | lua_pop(L, 1); |
| 5865 | continue; |
| 5866 | } |
| 5867 | |
| 5868 | |
| 5869 | /* Duplicate the key and swap it with the value. */ |
| 5870 | lua_pushvalue(L, -2); |
| 5871 | lua_insert(L, -2); |
| 5872 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5873 | |
| 5874 | lua_newtable(L); |
| 5875 | lua_insert(L, -2); |
| 5876 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5877 | |
| 5878 | if (lua_isstring(L, -1)) { |
| 5879 | /* push the value in the inner table */ |
| 5880 | lua_rawseti(L, -2, 1); |
| 5881 | } |
| 5882 | else { /* table */ |
| 5883 | lua_pushnil(L); |
| 5884 | while (lua_next(L, -2) != 0) { |
| 5885 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5886 | if (!lua_isstring(L, -1)) { |
| 5887 | /* invalid value type, skip it*/ |
| 5888 | lua_pop(L, 1); |
| 5889 | continue; |
| 5890 | } |
| 5891 | /* push the value in the inner table */ |
| 5892 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5893 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5894 | } |
| 5895 | lua_pop(L, 1); |
| 5896 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5897 | } |
| 5898 | |
| 5899 | /* push (k,v) on the stack in the headers table: |
| 5900 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5901 | */ |
| 5902 | lua_settable(L, -5); |
| 5903 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5904 | } |
| 5905 | } |
| 5906 | lua_pop(L, 1); |
| 5907 | } |
| 5908 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5909 | lua_settable(L, -3); |
| 5910 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5911 | |
| 5912 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5913 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5914 | lua_setmetatable(L, -2); |
| 5915 | return 1; |
| 5916 | } |
| 5917 | |
| 5918 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5919 | * provided, the default one corresponding to the status code is used. |
| 5920 | */ |
| 5921 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5922 | { |
| 5923 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5924 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5925 | |
| 5926 | /* First argument (self) must be a table */ |
| 5927 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5928 | |
| 5929 | if (status < 100 || status > 599) { |
| 5930 | lua_pushboolean(L, 0); |
| 5931 | return 1; |
| 5932 | } |
| 5933 | if (!reason) |
| 5934 | reason = http_get_reason(status); |
| 5935 | |
| 5936 | lua_pushinteger(L, status); |
| 5937 | lua_setfield(L, 1, "status"); |
| 5938 | |
| 5939 | lua_pushstring(L, reason); |
| 5940 | lua_setfield(L, 1, "reason"); |
| 5941 | |
| 5942 | lua_pushboolean(L, 1); |
| 5943 | return 1; |
| 5944 | } |
| 5945 | |
| 5946 | /* Add a header into the reply object. Each header name is associated to an |
| 5947 | * array of values in the "headers" table. If the header name is not found, a |
| 5948 | * new entry is created. |
| 5949 | */ |
| 5950 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5951 | { |
| 5952 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5953 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5954 | int ret; |
| 5955 | |
| 5956 | /* First argument (self) must be a table */ |
| 5957 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5958 | |
| 5959 | /* Push in the stack the "headers" entry. */ |
| 5960 | ret = lua_getfield(L, 1, "headers"); |
| 5961 | if (ret != LUA_TTABLE) { |
| 5962 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5963 | WILL_LJMP(lua_error(L)); |
| 5964 | } |
| 5965 | |
| 5966 | /* check if the header is already registered. If not, register it. */ |
| 5967 | ret = lua_getfield(L, -1, name); |
| 5968 | if (ret == LUA_TNIL) { |
| 5969 | /* Entry not found. */ |
| 5970 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5971 | |
| 5972 | /* Insert the new header name in the array in the top of the stack. |
| 5973 | * It left the new array in the top of the stack. |
| 5974 | */ |
| 5975 | lua_newtable(L); |
| 5976 | lua_pushstring(L, name); |
| 5977 | lua_pushvalue(L, -2); |
| 5978 | lua_settable(L, -4); |
| 5979 | } |
| 5980 | else if (ret != LUA_TTABLE) { |
| 5981 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5982 | WILL_LJMP(lua_error(L)); |
| 5983 | } |
| 5984 | |
| 5985 | /* Now the top od thestack is an array of values. We push |
| 5986 | * the header value as new entry. |
| 5987 | */ |
| 5988 | lua_pushstring(L, value); |
| 5989 | ret = lua_rawlen(L, -2); |
| 5990 | lua_rawseti(L, -2, ret + 1); |
| 5991 | |
| 5992 | lua_pushboolean(L, 1); |
| 5993 | return 1; |
| 5994 | } |
| 5995 | |
| 5996 | /* Remove all occurrences of a given header name. */ |
| 5997 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5998 | { |
| 5999 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6000 | int ret; |
| 6001 | |
| 6002 | /* First argument (self) must be a table */ |
| 6003 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6004 | |
| 6005 | /* Push in the stack the "headers" entry. */ |
| 6006 | ret = lua_getfield(L, 1, "headers"); |
| 6007 | if (ret != LUA_TTABLE) { |
| 6008 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6009 | WILL_LJMP(lua_error(L)); |
| 6010 | } |
| 6011 | |
| 6012 | lua_pushstring(L, name); |
| 6013 | lua_pushnil(L); |
| 6014 | lua_settable(L, -3); |
| 6015 | |
| 6016 | lua_pushboolean(L, 1); |
| 6017 | return 1; |
| 6018 | } |
| 6019 | |
| 6020 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6021 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6022 | { |
| 6023 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6024 | |
| 6025 | /* First argument (self) must be a table */ |
| 6026 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6027 | |
| 6028 | lua_pushstring(L, payload); |
| 6029 | lua_setfield(L, 1, "body"); |
| 6030 | |
| 6031 | lua_pushboolean(L, 1); |
| 6032 | return 1; |
| 6033 | } |
| 6034 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6035 | __LJMP static int hlua_log(lua_State *L) |
| 6036 | { |
| 6037 | int level; |
| 6038 | const char *msg; |
| 6039 | |
| 6040 | MAY_LJMP(check_args(L, 2, "log")); |
| 6041 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6042 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6043 | |
| 6044 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6045 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6046 | |
| 6047 | hlua_sendlog(NULL, level, msg); |
| 6048 | return 0; |
| 6049 | } |
| 6050 | |
| 6051 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6052 | { |
| 6053 | const char *msg; |
| 6054 | |
| 6055 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6056 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6057 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6058 | return 0; |
| 6059 | } |
| 6060 | |
| 6061 | __LJMP static int hlua_log_info(lua_State *L) |
| 6062 | { |
| 6063 | const char *msg; |
| 6064 | |
| 6065 | MAY_LJMP(check_args(L, 1, "info")); |
| 6066 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6067 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6068 | return 0; |
| 6069 | } |
| 6070 | |
| 6071 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6072 | { |
| 6073 | const char *msg; |
| 6074 | |
| 6075 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6076 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6077 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6078 | return 0; |
| 6079 | } |
| 6080 | |
| 6081 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6082 | { |
| 6083 | const char *msg; |
| 6084 | |
| 6085 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6086 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6087 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6088 | return 0; |
| 6089 | } |
| 6090 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6091 | __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] | 6092 | { |
| 6093 | int wakeup_ms = lua_tointeger(L, -1); |
| 6094 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6095 | 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] | 6096 | return 0; |
| 6097 | } |
| 6098 | |
| 6099 | __LJMP static int hlua_sleep(lua_State *L) |
| 6100 | { |
| 6101 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6102 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6103 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6104 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6105 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6106 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6107 | wakeup_ms = tick_add(now_ms, delay); |
| 6108 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6109 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6110 | 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] | 6111 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6112 | } |
| 6113 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6114 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6115 | { |
| 6116 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6117 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6118 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6119 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6120 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6121 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6122 | wakeup_ms = tick_add(now_ms, delay); |
| 6123 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6124 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6125 | 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] | 6126 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6127 | } |
| 6128 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6129 | /* This functionis an LUA binding. it permits to give back |
| 6130 | * the hand at the HAProxy scheduler. It is used when the |
| 6131 | * LUA processing consumes a lot of time. |
| 6132 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6133 | __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] | 6134 | { |
| 6135 | return 0; |
| 6136 | } |
| 6137 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6138 | __LJMP static int hlua_yield(lua_State *L) |
| 6139 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6140 | 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] | 6141 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6142 | } |
| 6143 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6144 | /* This function change the nice of the currently executed |
| 6145 | * task. It is used set low or high priority at the current |
| 6146 | * task. |
| 6147 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6148 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6149 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6150 | struct hlua *hlua; |
| 6151 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6152 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6153 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6154 | hlua = hlua_gethlua(L); |
| 6155 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6156 | |
| 6157 | /* If he task is not set, I'm in a start mode. */ |
| 6158 | if (!hlua || !hlua->task) |
| 6159 | return 0; |
| 6160 | |
| 6161 | if (nice < -1024) |
| 6162 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6163 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6164 | nice = 1024; |
| 6165 | |
| 6166 | hlua->task->nice = nice; |
| 6167 | return 0; |
| 6168 | } |
| 6169 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6170 | /* 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] | 6171 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6172 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6173 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6174 | * |
| 6175 | * Task wrapper are longjmp safe because the only one Lua code |
| 6176 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6177 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6178 | 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] | 6179 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6180 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6181 | enum hlua_exec status; |
| 6182 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6183 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6184 | task_set_affinity(task, tid_bit); |
| 6185 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6186 | /* If it is the first call to the task, we must initialize the |
| 6187 | * execution timeouts. |
| 6188 | */ |
| 6189 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6190 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6191 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6192 | /* Execute the Lua code. */ |
| 6193 | status = hlua_ctx_resume(hlua, 1); |
| 6194 | |
| 6195 | switch (status) { |
| 6196 | /* finished or yield */ |
| 6197 | case HLUA_E_OK: |
| 6198 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6199 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6200 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6201 | break; |
| 6202 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6203 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6204 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6205 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6206 | break; |
| 6207 | |
| 6208 | /* finished with error. */ |
| 6209 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6210 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6211 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6212 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6213 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6214 | break; |
| 6215 | |
| 6216 | case HLUA_E_ERR: |
| 6217 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6218 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6219 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6220 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6221 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6222 | break; |
| 6223 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6224 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6225 | } |
| 6226 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6227 | /* This function is an LUA binding that register LUA function to be |
| 6228 | * executed after the HAProxy configuration parsing and before the |
| 6229 | * HAProxy scheduler starts. This function expect only one LUA |
| 6230 | * argument that is a function. This function returns nothing, but |
| 6231 | * throws if an error is encountered. |
| 6232 | */ |
| 6233 | __LJMP static int hlua_register_init(lua_State *L) |
| 6234 | { |
| 6235 | struct hlua_init_function *init; |
| 6236 | int ref; |
| 6237 | |
| 6238 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6239 | |
| 6240 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6241 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6242 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6243 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6244 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6245 | |
| 6246 | init->function_ref = ref; |
| 6247 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6248 | return 0; |
| 6249 | } |
| 6250 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6251 | /* This functio is an LUA binding. It permits to register a task |
| 6252 | * executed in parallel of the main HAroxy activity. The task is |
| 6253 | * created and it is set in the HAProxy scheduler. It can be called |
| 6254 | * from the "init" section, "post init" or during the runtime. |
| 6255 | * |
| 6256 | * Lua prototype: |
| 6257 | * |
| 6258 | * <none> core.register_task(<function>) |
| 6259 | */ |
| 6260 | static int hlua_register_task(lua_State *L) |
| 6261 | { |
| 6262 | struct hlua *hlua; |
| 6263 | struct task *task; |
| 6264 | int ref; |
| 6265 | |
| 6266 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6267 | |
| 6268 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6269 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6270 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6271 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6272 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6273 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6274 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6275 | if (!task) |
| 6276 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6277 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6278 | task->context = hlua; |
| 6279 | task->process = hlua_process_task; |
| 6280 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6281 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6282 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6283 | |
| 6284 | /* Restore the function in the stack. */ |
| 6285 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6286 | hlua->nargs = 0; |
| 6287 | |
| 6288 | /* Schedule task. */ |
| 6289 | task_schedule(task, now_ms); |
| 6290 | |
| 6291 | return 0; |
| 6292 | } |
| 6293 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6294 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6295 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6296 | * resume converters. |
| 6297 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6298 | 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] | 6299 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6300 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6301 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6302 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6303 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6304 | if (!stream) |
| 6305 | return 0; |
| 6306 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6307 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6308 | * Lua context can be not initialized. This behavior |
| 6309 | * permits to save performances because a systematic |
| 6310 | * Lua initialization cause 5% performances loss. |
| 6311 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6312 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6313 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6314 | if (!stream->hlua) { |
| 6315 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6316 | return 0; |
| 6317 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6318 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6319 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6320 | return 0; |
| 6321 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6322 | } |
| 6323 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6324 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6325 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6326 | |
| 6327 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6328 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6329 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6330 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6331 | else |
| 6332 | error = "critical error"; |
| 6333 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6334 | return 0; |
| 6335 | } |
| 6336 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6337 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6338 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6339 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6340 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6341 | return 0; |
| 6342 | } |
| 6343 | |
| 6344 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6345 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6346 | |
| 6347 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6348 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6349 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6350 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6351 | return 0; |
| 6352 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6353 | hlua_smp2lua(stream->hlua->T, smp); |
| 6354 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6355 | |
| 6356 | /* push keywords in the stack. */ |
| 6357 | if (arg_p) { |
| 6358 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6359 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6360 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6361 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6362 | return 0; |
| 6363 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6364 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6365 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6366 | } |
| 6367 | } |
| 6368 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6369 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6370 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6371 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6372 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6373 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6374 | } |
| 6375 | |
| 6376 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6377 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6378 | /* finished. */ |
| 6379 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6380 | /* If the stack is empty, the function fails. */ |
| 6381 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6382 | return 0; |
| 6383 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6384 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6385 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6386 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6387 | return 1; |
| 6388 | |
| 6389 | /* yield. */ |
| 6390 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6391 | 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] | 6392 | return 0; |
| 6393 | |
| 6394 | /* finished with error. */ |
| 6395 | case HLUA_E_ERRMSG: |
| 6396 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6397 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6398 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6399 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6400 | return 0; |
| 6401 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6402 | case HLUA_E_ETMOUT: |
| 6403 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6404 | return 0; |
| 6405 | |
| 6406 | case HLUA_E_NOMEM: |
| 6407 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6408 | return 0; |
| 6409 | |
| 6410 | case HLUA_E_YIELD: |
| 6411 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6412 | return 0; |
| 6413 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6414 | case HLUA_E_ERR: |
| 6415 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6416 | 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] | 6417 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6418 | |
| 6419 | default: |
| 6420 | return 0; |
| 6421 | } |
| 6422 | } |
| 6423 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6424 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6425 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6426 | * resume sample-fetches. This function will be called by the sample |
| 6427 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6428 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6429 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6430 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6431 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6432 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6433 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6434 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6435 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6436 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6437 | if (!stream) |
| 6438 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6439 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6440 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6441 | * Lua context can be not initialized. This behavior |
| 6442 | * permits to save performances because a systematic |
| 6443 | * Lua initialization cause 5% performances loss. |
| 6444 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6445 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6446 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6447 | if (!stream->hlua) { |
| 6448 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6449 | return 0; |
| 6450 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6451 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6452 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6453 | return 0; |
| 6454 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6455 | } |
| 6456 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6457 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6458 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6459 | |
| 6460 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6461 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6462 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6463 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6464 | else |
| 6465 | error = "critical error"; |
| 6466 | 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] | 6467 | return 0; |
| 6468 | } |
| 6469 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6470 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6471 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6472 | 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] | 6473 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6474 | return 0; |
| 6475 | } |
| 6476 | |
| 6477 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6478 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6479 | |
| 6480 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6481 | 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] | 6482 | 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] | 6483 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6484 | return 0; |
| 6485 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6486 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6487 | |
| 6488 | /* push keywords in the stack. */ |
| 6489 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6490 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6491 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6492 | 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] | 6493 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6494 | return 0; |
| 6495 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6496 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6497 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6498 | } |
| 6499 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6500 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6501 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6502 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6503 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6504 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6505 | } |
| 6506 | |
| 6507 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6508 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6509 | /* finished. */ |
| 6510 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6511 | /* If the stack is empty, the function fails. */ |
| 6512 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6513 | return 0; |
| 6514 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6515 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6516 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6517 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6518 | |
| 6519 | /* Set the end of execution flag. */ |
| 6520 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6521 | return 1; |
| 6522 | |
| 6523 | /* yield. */ |
| 6524 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6525 | 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] | 6526 | return 0; |
| 6527 | |
| 6528 | /* finished with error. */ |
| 6529 | case HLUA_E_ERRMSG: |
| 6530 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6531 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6532 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6533 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6534 | return 0; |
| 6535 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6536 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6537 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6538 | return 0; |
| 6539 | |
| 6540 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6541 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6542 | return 0; |
| 6543 | |
| 6544 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6545 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6546 | return 0; |
| 6547 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6548 | case HLUA_E_ERR: |
| 6549 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6550 | 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] | 6551 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6552 | |
| 6553 | default: |
| 6554 | return 0; |
| 6555 | } |
| 6556 | } |
| 6557 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6558 | /* This function is an LUA binding used for registering |
| 6559 | * "sample-conv" functions. It expects a converter name used |
| 6560 | * in the haproxy configuration file, and an LUA function. |
| 6561 | */ |
| 6562 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6563 | { |
| 6564 | struct sample_conv_kw_list *sck; |
| 6565 | const char *name; |
| 6566 | int ref; |
| 6567 | int len; |
| 6568 | struct hlua_function *fcn; |
| 6569 | |
| 6570 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6571 | |
| 6572 | /* First argument : converter name. */ |
| 6573 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6574 | |
| 6575 | /* Second argument : lua function. */ |
| 6576 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6577 | |
| 6578 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6579 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6580 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6581 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6582 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6583 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6584 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6585 | |
| 6586 | /* Fill fcn. */ |
| 6587 | fcn->name = strdup(name); |
| 6588 | if (!fcn->name) |
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 | fcn->function_ref = ref; |
| 6591 | |
| 6592 | /* List head */ |
| 6593 | sck->list.n = sck->list.p = NULL; |
| 6594 | |
| 6595 | /* converter keyword. */ |
| 6596 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6597 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6598 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6599 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6600 | |
| 6601 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6602 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6603 | 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] | 6604 | sck->kw[0].val_args = NULL; |
| 6605 | sck->kw[0].in_type = SMP_T_STR; |
| 6606 | sck->kw[0].out_type = SMP_T_STR; |
| 6607 | sck->kw[0].private = fcn; |
| 6608 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6609 | /* Register this new converter */ |
| 6610 | sample_register_convs(sck); |
| 6611 | |
| 6612 | return 0; |
| 6613 | } |
| 6614 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6615 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6616 | * "sample-fetch" functions. It expects a converter name used |
| 6617 | * in the haproxy configuration file, and an LUA function. |
| 6618 | */ |
| 6619 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6620 | { |
| 6621 | const char *name; |
| 6622 | int ref; |
| 6623 | int len; |
| 6624 | struct sample_fetch_kw_list *sfk; |
| 6625 | struct hlua_function *fcn; |
| 6626 | |
| 6627 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6628 | |
| 6629 | /* First argument : sample-fetch name. */ |
| 6630 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6631 | |
| 6632 | /* Second argument : lua function. */ |
| 6633 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6634 | |
| 6635 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6636 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6637 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6638 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6639 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6640 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6641 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6642 | |
| 6643 | /* Fill fcn. */ |
| 6644 | fcn->name = strdup(name); |
| 6645 | if (!fcn->name) |
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 | fcn->function_ref = ref; |
| 6648 | |
| 6649 | /* List head */ |
| 6650 | sfk->list.n = sfk->list.p = NULL; |
| 6651 | |
| 6652 | /* sample-fetch keyword. */ |
| 6653 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6654 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6655 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6656 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6657 | |
| 6658 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6659 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6660 | 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] | 6661 | sfk->kw[0].val_args = NULL; |
| 6662 | sfk->kw[0].out_type = SMP_T_STR; |
| 6663 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6664 | sfk->kw[0].val = 0; |
| 6665 | sfk->kw[0].private = fcn; |
| 6666 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6667 | /* Register this new fetch. */ |
| 6668 | sample_register_fetches(sfk); |
| 6669 | |
| 6670 | return 0; |
| 6671 | } |
| 6672 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6673 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6674 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6675 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6676 | { |
| 6677 | struct hlua *hlua = hlua_gethlua(L); |
| 6678 | unsigned int delay; |
| 6679 | unsigned int wakeup_ms; |
| 6680 | |
| 6681 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6682 | |
| 6683 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6684 | wakeup_ms = tick_add(now_ms, delay); |
| 6685 | hlua->wake_time = wakeup_ms; |
| 6686 | return 0; |
| 6687 | } |
| 6688 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6689 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6690 | * wrapper during the initialisation period. This function may return any |
| 6691 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6692 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6693 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6694 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6695 | 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] | 6696 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6697 | { |
| 6698 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6699 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6700 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6701 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6702 | |
| 6703 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6704 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6705 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6706 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6707 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6708 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6709 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6710 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6711 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6712 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6713 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6714 | * Lua context can be not initialized. This behavior |
| 6715 | * permits to save performances because a systematic |
| 6716 | * Lua initialization cause 5% performances loss. |
| 6717 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6718 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6719 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6720 | if (!s->hlua) { |
| 6721 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6722 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6723 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6724 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6725 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 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 | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6730 | } |
| 6731 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6732 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6733 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6734 | |
| 6735 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6736 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6737 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6738 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6739 | else |
| 6740 | error = "critical error"; |
| 6741 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6742 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6743 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6744 | } |
| 6745 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6746 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6747 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6748 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6749 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6750 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6751 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6755 | 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] | 6756 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6757 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6758 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6759 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6760 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6761 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6762 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6763 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6764 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6765 | |
| 6766 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6767 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6768 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6769 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6770 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6771 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6772 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6773 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6774 | lua_pushstring(s->hlua->T, *arg); |
| 6775 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6776 | } |
| 6777 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6778 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6779 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6780 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6781 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6782 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6783 | } |
| 6784 | |
| 6785 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6786 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6787 | /* finished. */ |
| 6788 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6789 | /* Catch the return value */ |
| 6790 | if (lua_gettop(s->hlua->T) > 0) |
| 6791 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6792 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6793 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6794 | if (act_ret == ACT_RET_YIELD) { |
| 6795 | if (flags & ACT_OPT_FINAL) |
| 6796 | goto err_yield; |
| 6797 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6798 | if (dir == SMP_OPT_DIR_REQ) |
| 6799 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6800 | s->hlua->wake_time); |
| 6801 | else |
| 6802 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6803 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6804 | } |
| 6805 | goto end; |
| 6806 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6807 | /* yield. */ |
| 6808 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6809 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6810 | if (dir == SMP_OPT_DIR_REQ) |
| 6811 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6812 | s->hlua->wake_time); |
| 6813 | else |
| 6814 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6815 | s->hlua->wake_time); |
| 6816 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6817 | /* Some actions can be wake up when a "write" event |
| 6818 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6819 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6820 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6821 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6822 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6823 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6824 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6825 | act_ret = ACT_RET_YIELD; |
| 6826 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6827 | |
| 6828 | /* finished with error. */ |
| 6829 | case HLUA_E_ERRMSG: |
| 6830 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6831 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6832 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6833 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6834 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6835 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6836 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6837 | 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] | 6838 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6839 | |
| 6840 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6841 | 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] | 6842 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6843 | |
| 6844 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6845 | err_yield: |
| 6846 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6847 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6848 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6849 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6850 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6851 | case HLUA_E_ERR: |
| 6852 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6853 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6854 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6855 | |
| 6856 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6857 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6858 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6859 | |
| 6860 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6861 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6862 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6863 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6864 | } |
| 6865 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6866 | 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] | 6867 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6868 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6869 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6870 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6871 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6872 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6873 | } |
| 6874 | |
| 6875 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6876 | { |
| 6877 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6878 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6879 | struct task *task; |
| 6880 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6881 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6882 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6883 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6884 | if (!hlua) { |
| 6885 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6886 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6887 | return 0; |
| 6888 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6889 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6890 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6891 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6892 | |
| 6893 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6894 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6895 | if (!task) { |
| 6896 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6897 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6898 | return 0; |
| 6899 | } |
| 6900 | task->nice = 0; |
| 6901 | task->context = ctx; |
| 6902 | task->process = hlua_applet_wakeup; |
| 6903 | ctx->ctx.hlua_apptcp.task = task; |
| 6904 | |
| 6905 | /* In the execution wrappers linked with a stream, the |
| 6906 | * Lua context can be not initialized. This behavior |
| 6907 | * permits to save performances because a systematic |
| 6908 | * Lua initialization cause 5% performances loss. |
| 6909 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6910 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6911 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6912 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6913 | return 0; |
| 6914 | } |
| 6915 | |
| 6916 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6917 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6918 | |
| 6919 | /* The following Lua calls can fail. */ |
| 6920 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6921 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6922 | error = lua_tostring(hlua->T, -1); |
| 6923 | else |
| 6924 | error = "critical error"; |
| 6925 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6926 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6927 | return 0; |
| 6928 | } |
| 6929 | |
| 6930 | /* Check stack available size. */ |
| 6931 | if (!lua_checkstack(hlua->T, 1)) { |
| 6932 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6933 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6934 | RESET_SAFE_LJMP(hlua->T); |
| 6935 | return 0; |
| 6936 | } |
| 6937 | |
| 6938 | /* Restore the function in the stack. */ |
| 6939 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6940 | |
| 6941 | /* Create and and push object stream in the stack. */ |
| 6942 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6943 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6944 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6945 | RESET_SAFE_LJMP(hlua->T); |
| 6946 | return 0; |
| 6947 | } |
| 6948 | hlua->nargs = 1; |
| 6949 | |
| 6950 | /* push keywords in the stack. */ |
| 6951 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6952 | if (!lua_checkstack(hlua->T, 1)) { |
| 6953 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6954 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6955 | RESET_SAFE_LJMP(hlua->T); |
| 6956 | return 0; |
| 6957 | } |
| 6958 | lua_pushstring(hlua->T, *arg); |
| 6959 | hlua->nargs++; |
| 6960 | } |
| 6961 | |
| 6962 | RESET_SAFE_LJMP(hlua->T); |
| 6963 | |
| 6964 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6965 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6966 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6967 | |
| 6968 | return 1; |
| 6969 | } |
| 6970 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6971 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6972 | { |
| 6973 | struct stream_interface *si = ctx->owner; |
| 6974 | struct stream *strm = si_strm(si); |
| 6975 | struct channel *res = si_ic(si); |
| 6976 | struct act_rule *rule = ctx->rule; |
| 6977 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6978 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6979 | |
| 6980 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6981 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6982 | /* eat the whole request */ |
| 6983 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6984 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6985 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6986 | |
| 6987 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6988 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6989 | return; |
| 6990 | |
| 6991 | /* Execute the function. */ |
| 6992 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6993 | /* finished. */ |
| 6994 | case HLUA_E_OK: |
| 6995 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6996 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6997 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6998 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6999 | res->flags |= CF_READ_NULL; |
| 7000 | si_shutr(si); |
| 7001 | return; |
| 7002 | |
| 7003 | /* yield. */ |
| 7004 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7005 | if (hlua->wake_time != TICK_ETERNITY) |
| 7006 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7007 | return; |
| 7008 | |
| 7009 | /* finished with error. */ |
| 7010 | case HLUA_E_ERRMSG: |
| 7011 | /* Display log. */ |
| 7012 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7013 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7014 | lua_pop(hlua->T, 1); |
| 7015 | goto error; |
| 7016 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7017 | case HLUA_E_ETMOUT: |
| 7018 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7019 | rule->arg.hlua_rule->fcn.name); |
| 7020 | goto error; |
| 7021 | |
| 7022 | case HLUA_E_NOMEM: |
| 7023 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7024 | rule->arg.hlua_rule->fcn.name); |
| 7025 | goto error; |
| 7026 | |
| 7027 | case HLUA_E_YIELD: /* unexpected */ |
| 7028 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7029 | rule->arg.hlua_rule->fcn.name); |
| 7030 | goto error; |
| 7031 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7032 | case HLUA_E_ERR: |
| 7033 | /* Display log. */ |
| 7034 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7035 | rule->arg.hlua_rule->fcn.name); |
| 7036 | goto error; |
| 7037 | |
| 7038 | default: |
| 7039 | goto error; |
| 7040 | } |
| 7041 | |
| 7042 | error: |
| 7043 | |
| 7044 | /* For all other cases, just close the stream. */ |
| 7045 | si_shutw(si); |
| 7046 | si_shutr(si); |
| 7047 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7048 | } |
| 7049 | |
| 7050 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7051 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7052 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7053 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7054 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7055 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7056 | } |
| 7057 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7058 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7059 | * an errors occurs and -1 if more data are required for initializing |
| 7060 | * the applet. |
| 7061 | */ |
| 7062 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7063 | { |
| 7064 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7065 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7066 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7067 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7068 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7069 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7070 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7071 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7072 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7073 | if (!hlua) { |
| 7074 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7075 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7076 | return 0; |
| 7077 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7078 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7079 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7080 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7081 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7082 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7083 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7084 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7085 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7086 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7087 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7088 | if (!task) { |
| 7089 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7090 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7091 | return 0; |
| 7092 | } |
| 7093 | task->nice = 0; |
| 7094 | task->context = ctx; |
| 7095 | task->process = hlua_applet_wakeup; |
| 7096 | ctx->ctx.hlua_apphttp.task = task; |
| 7097 | |
| 7098 | /* In the execution wrappers linked with a stream, the |
| 7099 | * Lua context can be not initialized. This behavior |
| 7100 | * permits to save performances because a systematic |
| 7101 | * Lua initialization cause 5% performances loss. |
| 7102 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7103 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7104 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7105 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7106 | return 0; |
| 7107 | } |
| 7108 | |
| 7109 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7110 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7111 | |
| 7112 | /* The following Lua calls can fail. */ |
| 7113 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7114 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7115 | error = lua_tostring(hlua->T, -1); |
| 7116 | else |
| 7117 | error = "critical error"; |
| 7118 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7119 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7120 | return 0; |
| 7121 | } |
| 7122 | |
| 7123 | /* Check stack available size. */ |
| 7124 | if (!lua_checkstack(hlua->T, 1)) { |
| 7125 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7126 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7127 | RESET_SAFE_LJMP(hlua->T); |
| 7128 | return 0; |
| 7129 | } |
| 7130 | |
| 7131 | /* Restore the function in the stack. */ |
| 7132 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7133 | |
| 7134 | /* Create and and push object stream in the stack. */ |
| 7135 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7136 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7137 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7138 | RESET_SAFE_LJMP(hlua->T); |
| 7139 | return 0; |
| 7140 | } |
| 7141 | hlua->nargs = 1; |
| 7142 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7143 | /* push keywords in the stack. */ |
| 7144 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7145 | if (!lua_checkstack(hlua->T, 1)) { |
| 7146 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7147 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7148 | RESET_SAFE_LJMP(hlua->T); |
| 7149 | return 0; |
| 7150 | } |
| 7151 | lua_pushstring(hlua->T, *arg); |
| 7152 | hlua->nargs++; |
| 7153 | } |
| 7154 | |
| 7155 | RESET_SAFE_LJMP(hlua->T); |
| 7156 | |
| 7157 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7158 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7159 | |
| 7160 | return 1; |
| 7161 | } |
| 7162 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7163 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7164 | { |
| 7165 | struct stream_interface *si = ctx->owner; |
| 7166 | struct stream *strm = si_strm(si); |
| 7167 | struct channel *req = si_oc(si); |
| 7168 | struct channel *res = si_ic(si); |
| 7169 | struct act_rule *rule = ctx->rule; |
| 7170 | struct proxy *px = strm->be; |
| 7171 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7172 | struct htx *req_htx, *res_htx; |
| 7173 | |
| 7174 | res_htx = htx_from_buf(&res->buf); |
| 7175 | |
| 7176 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7177 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7178 | goto out; |
| 7179 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7180 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7181 | if (!b_size(&res->buf)) { |
| 7182 | si_rx_room_blk(si); |
| 7183 | goto out; |
| 7184 | } |
| 7185 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7186 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7187 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7188 | |
| 7189 | /* Set the currently running flag. */ |
| 7190 | if (!HLUA_IS_RUNNING(hlua) && |
| 7191 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7192 | struct htx_blk *blk; |
| 7193 | size_t count = co_data(req); |
| 7194 | |
| 7195 | if (!count) { |
| 7196 | si_cant_get(si); |
| 7197 | goto out; |
| 7198 | } |
| 7199 | |
| 7200 | /* We need to flush the request header. This left the body for |
| 7201 | * the Lua. |
| 7202 | */ |
| 7203 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7204 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7205 | while (count && blk) { |
| 7206 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7207 | uint32_t sz = htx_get_blksz(blk); |
| 7208 | |
| 7209 | if (sz > count) { |
| 7210 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7211 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7212 | goto out; |
| 7213 | } |
| 7214 | |
| 7215 | count -= sz; |
| 7216 | co_set_data(req, co_data(req) - sz); |
| 7217 | blk = htx_remove_blk(req_htx, blk); |
| 7218 | |
| 7219 | if (type == HTX_BLK_EOH) |
| 7220 | break; |
| 7221 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7222 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7223 | } |
| 7224 | |
| 7225 | /* Executes The applet if it is not done. */ |
| 7226 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7227 | |
| 7228 | /* Execute the function. */ |
| 7229 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7230 | /* finished. */ |
| 7231 | case HLUA_E_OK: |
| 7232 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7233 | break; |
| 7234 | |
| 7235 | /* yield. */ |
| 7236 | case HLUA_E_AGAIN: |
| 7237 | if (hlua->wake_time != TICK_ETERNITY) |
| 7238 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7239 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7240 | |
| 7241 | /* finished with error. */ |
| 7242 | case HLUA_E_ERRMSG: |
| 7243 | /* Display log. */ |
| 7244 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7245 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7246 | lua_pop(hlua->T, 1); |
| 7247 | goto error; |
| 7248 | |
| 7249 | case HLUA_E_ETMOUT: |
| 7250 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7251 | rule->arg.hlua_rule->fcn.name); |
| 7252 | goto error; |
| 7253 | |
| 7254 | case HLUA_E_NOMEM: |
| 7255 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7256 | rule->arg.hlua_rule->fcn.name); |
| 7257 | goto error; |
| 7258 | |
| 7259 | case HLUA_E_YIELD: /* unexpected */ |
| 7260 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7261 | rule->arg.hlua_rule->fcn.name); |
| 7262 | goto error; |
| 7263 | |
| 7264 | case HLUA_E_ERR: |
| 7265 | /* Display log. */ |
| 7266 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7267 | rule->arg.hlua_rule->fcn.name); |
| 7268 | goto error; |
| 7269 | |
| 7270 | default: |
| 7271 | goto error; |
| 7272 | } |
| 7273 | } |
| 7274 | |
| 7275 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7276 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7277 | goto done; |
| 7278 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7279 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7280 | goto error; |
| 7281 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7282 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7283 | 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] | 7284 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7285 | si_rx_room_blk(si); |
| 7286 | goto out; |
| 7287 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7288 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7289 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7290 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7291 | } |
| 7292 | |
| 7293 | done: |
| 7294 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7295 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7296 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7297 | si_shutr(si); |
| 7298 | } |
| 7299 | |
| 7300 | /* eat the whole request */ |
| 7301 | if (co_data(req)) { |
| 7302 | req_htx = htx_from_buf(&req->buf); |
| 7303 | co_htx_skip(req, req_htx, co_data(req)); |
| 7304 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7305 | } |
| 7306 | } |
| 7307 | |
| 7308 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7309 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7310 | return; |
| 7311 | |
| 7312 | error: |
| 7313 | |
| 7314 | /* If we are in HTTP mode, and we are not send any |
| 7315 | * data, return a 500 server error in best effort: |
| 7316 | * if there is no room available in the buffer, |
| 7317 | * just close the connection. |
| 7318 | */ |
| 7319 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7320 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7321 | |
| 7322 | channel_erase(res); |
| 7323 | res->buf.data = b_data(err); |
| 7324 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7325 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7326 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7327 | } |
| 7328 | if (!(strm->flags & SF_ERR_MASK)) |
| 7329 | strm->flags |= SF_ERR_RESOURCE; |
| 7330 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7331 | goto done; |
| 7332 | } |
| 7333 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7334 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7335 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7336 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7337 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7338 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7339 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7340 | } |
| 7341 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7342 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7343 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7344 | * |
| 7345 | * This function can fail with an abort() due to an Lua critical error. |
| 7346 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7347 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7348 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7349 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7350 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7351 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7352 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7353 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7354 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7355 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7356 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7357 | if (!rule->arg.hlua_rule) { |
| 7358 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7359 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7360 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7361 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7362 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7363 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7364 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7365 | if (!rule->arg.hlua_rule->args) { |
| 7366 | memprintf(err, "out of memory error"); |
| 7367 | return ACT_RET_PRS_ERR; |
| 7368 | } |
| 7369 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7370 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7371 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7372 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7373 | /* Expect some arguments */ |
| 7374 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7375 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7376 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7377 | return ACT_RET_PRS_ERR; |
| 7378 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7379 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7380 | if (!rule->arg.hlua_rule->args[i]) { |
| 7381 | memprintf(err, "out of memory error"); |
| 7382 | return ACT_RET_PRS_ERR; |
| 7383 | } |
| 7384 | (*cur_arg)++; |
| 7385 | } |
| 7386 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7387 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7388 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7389 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7390 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7391 | } |
| 7392 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7393 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7394 | struct act_rule *rule, char **err) |
| 7395 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7396 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7397 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7398 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7399 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7400 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7401 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7402 | * the call of this analyzer. |
| 7403 | */ |
| 7404 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7405 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7406 | return ACT_RET_PRS_ERR; |
| 7407 | } |
| 7408 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7409 | /* Memory for the rule. */ |
| 7410 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7411 | if (!rule->arg.hlua_rule) { |
| 7412 | memprintf(err, "out of memory error"); |
| 7413 | return ACT_RET_PRS_ERR; |
| 7414 | } |
| 7415 | |
| 7416 | /* Reference the Lua function and store the reference. */ |
| 7417 | rule->arg.hlua_rule->fcn = *fcn; |
| 7418 | |
| 7419 | /* TODO: later accept arguments. */ |
| 7420 | rule->arg.hlua_rule->args = NULL; |
| 7421 | |
| 7422 | /* Add applet pointer in the rule. */ |
| 7423 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7424 | rule->applet.name = fcn->name; |
| 7425 | rule->applet.init = hlua_applet_http_init; |
| 7426 | rule->applet.fct = hlua_applet_http_fct; |
| 7427 | rule->applet.release = hlua_applet_http_release; |
| 7428 | rule->applet.timeout = hlua_timeout_applet; |
| 7429 | |
| 7430 | return ACT_RET_PRS_OK; |
| 7431 | } |
| 7432 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7433 | /* This function is an LUA binding used for registering |
| 7434 | * "sample-conv" functions. It expects a converter name used |
| 7435 | * in the haproxy configuration file, and an LUA function. |
| 7436 | */ |
| 7437 | __LJMP static int hlua_register_action(lua_State *L) |
| 7438 | { |
| 7439 | struct action_kw_list *akl; |
| 7440 | const char *name; |
| 7441 | int ref; |
| 7442 | int len; |
| 7443 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7444 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7445 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7446 | /* Initialise the number of expected arguments at 0. */ |
| 7447 | nargs = 0; |
| 7448 | |
| 7449 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7450 | 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] | 7451 | |
| 7452 | /* First argument : converter name. */ |
| 7453 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7454 | |
| 7455 | /* Second argument : environment. */ |
| 7456 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7457 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7458 | |
| 7459 | /* Third argument : lua function. */ |
| 7460 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7461 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7462 | /* 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] | 7463 | if (lua_gettop(L) >= 4) |
| 7464 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7465 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7466 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7467 | lua_pushnil(L); |
| 7468 | while (lua_next(L, 2) != 0) { |
| 7469 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7470 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7471 | |
| 7472 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7473 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7474 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7475 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7476 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7477 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7478 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7479 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7480 | |
| 7481 | /* Fill fcn. */ |
| 7482 | fcn->name = strdup(name); |
| 7483 | if (!fcn->name) |
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 | fcn->function_ref = ref; |
| 7486 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7487 | /* Set the expected number od arguments. */ |
| 7488 | fcn->nargs = nargs; |
| 7489 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7490 | /* List head */ |
| 7491 | akl->list.n = akl->list.p = NULL; |
| 7492 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7493 | /* action keyword. */ |
| 7494 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7495 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7496 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7497 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7498 | |
| 7499 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7500 | |
| 7501 | akl->kw[0].match_pfx = 0; |
| 7502 | akl->kw[0].private = fcn; |
| 7503 | akl->kw[0].parse = action_register_lua; |
| 7504 | |
| 7505 | /* select the action registering point. */ |
| 7506 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7507 | tcp_req_cont_keywords_register(akl); |
| 7508 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7509 | tcp_res_cont_keywords_register(akl); |
| 7510 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7511 | http_req_keywords_register(akl); |
| 7512 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7513 | http_res_keywords_register(akl); |
| 7514 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7515 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7516 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7517 | "are expected.", lua_tostring(L, -1))); |
| 7518 | |
| 7519 | /* pop the environment string. */ |
| 7520 | lua_pop(L, 1); |
| 7521 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7522 | return ACT_RET_PRS_OK; |
| 7523 | } |
| 7524 | |
| 7525 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7526 | struct act_rule *rule, char **err) |
| 7527 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7528 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7529 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7530 | if (px->mode == PR_MODE_HTTP) { |
| 7531 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7532 | return ACT_RET_PRS_ERR; |
| 7533 | } |
| 7534 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7535 | /* Memory for the rule. */ |
| 7536 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7537 | if (!rule->arg.hlua_rule) { |
| 7538 | memprintf(err, "out of memory error"); |
| 7539 | return ACT_RET_PRS_ERR; |
| 7540 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7541 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7542 | /* Reference the Lua function and store the reference. */ |
| 7543 | rule->arg.hlua_rule->fcn = *fcn; |
| 7544 | |
| 7545 | /* TODO: later accept arguments. */ |
| 7546 | rule->arg.hlua_rule->args = NULL; |
| 7547 | |
| 7548 | /* Add applet pointer in the rule. */ |
| 7549 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7550 | rule->applet.name = fcn->name; |
| 7551 | rule->applet.init = hlua_applet_tcp_init; |
| 7552 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7553 | rule->applet.release = hlua_applet_tcp_release; |
| 7554 | rule->applet.timeout = hlua_timeout_applet; |
| 7555 | |
| 7556 | return 0; |
| 7557 | } |
| 7558 | |
| 7559 | /* This function is an LUA binding used for registering |
| 7560 | * "sample-conv" functions. It expects a converter name used |
| 7561 | * in the haproxy configuration file, and an LUA function. |
| 7562 | */ |
| 7563 | __LJMP static int hlua_register_service(lua_State *L) |
| 7564 | { |
| 7565 | struct action_kw_list *akl; |
| 7566 | const char *name; |
| 7567 | const char *env; |
| 7568 | int ref; |
| 7569 | int len; |
| 7570 | struct hlua_function *fcn; |
| 7571 | |
| 7572 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7573 | |
| 7574 | /* First argument : converter name. */ |
| 7575 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7576 | |
| 7577 | /* Second argument : environment. */ |
| 7578 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7579 | |
| 7580 | /* Third argument : lua function. */ |
| 7581 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7582 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7583 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7584 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7585 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7586 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7587 | fcn = calloc(1, sizeof(*fcn)); |
| 7588 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7589 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7590 | |
| 7591 | /* Fill fcn. */ |
| 7592 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7593 | fcn->name = calloc(1, len); |
| 7594 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7595 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7596 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7597 | fcn->function_ref = ref; |
| 7598 | |
| 7599 | /* List head */ |
| 7600 | akl->list.n = akl->list.p = NULL; |
| 7601 | |
| 7602 | /* converter keyword. */ |
| 7603 | len = strlen("lua.") + strlen(name) + 1; |
| 7604 | akl->kw[0].kw = calloc(1, len); |
| 7605 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7606 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7607 | |
| 7608 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7609 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7610 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7611 | if (strcmp(env, "tcp") == 0) |
| 7612 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7613 | else if (strcmp(env, "http") == 0) |
| 7614 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7615 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7616 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7617 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7618 | |
| 7619 | akl->kw[0].match_pfx = 0; |
| 7620 | akl->kw[0].private = fcn; |
| 7621 | |
| 7622 | /* End of array. */ |
| 7623 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7624 | |
| 7625 | /* Register this new converter */ |
| 7626 | service_keywords_register(akl); |
| 7627 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7628 | return 0; |
| 7629 | } |
| 7630 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7631 | /* This function initialises Lua cli handler. It copies the |
| 7632 | * arguments in the Lua stack and create channel IO objects. |
| 7633 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7634 | 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] | 7635 | { |
| 7636 | struct hlua *hlua; |
| 7637 | struct hlua_function *fcn; |
| 7638 | int i; |
| 7639 | const char *error; |
| 7640 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7641 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7642 | appctx->ctx.hlua_cli.fcn = private; |
| 7643 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7644 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7645 | if (!hlua) { |
| 7646 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7647 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7648 | } |
| 7649 | HLUA_INIT(hlua); |
| 7650 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7651 | |
| 7652 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7653 | * 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] | 7654 | * applet_http. It is absolutely compatible. |
| 7655 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7656 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7657 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7658 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7659 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7660 | } |
| 7661 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7662 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7663 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7664 | |
| 7665 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7666 | 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] | 7667 | 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] | 7668 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7669 | } |
| 7670 | |
| 7671 | /* The following Lua calls can fail. */ |
| 7672 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7673 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7674 | error = lua_tostring(hlua->T, -1); |
| 7675 | else |
| 7676 | error = "critical error"; |
| 7677 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7678 | goto error; |
| 7679 | } |
| 7680 | |
| 7681 | /* Check stack available size. */ |
| 7682 | if (!lua_checkstack(hlua->T, 2)) { |
| 7683 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7684 | goto error; |
| 7685 | } |
| 7686 | |
| 7687 | /* Restore the function in the stack. */ |
| 7688 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7689 | |
| 7690 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7691 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7692 | */ |
| 7693 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7694 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7695 | goto error; |
| 7696 | } |
| 7697 | hlua->nargs = 1; |
| 7698 | |
| 7699 | /* push keywords in the stack. */ |
| 7700 | for (i = 0; *args[i]; i++) { |
| 7701 | /* Check stack available size. */ |
| 7702 | if (!lua_checkstack(hlua->T, 1)) { |
| 7703 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7704 | goto error; |
| 7705 | } |
| 7706 | lua_pushstring(hlua->T, args[i]); |
| 7707 | hlua->nargs++; |
| 7708 | } |
| 7709 | |
| 7710 | /* We must initialize the execution timeouts. */ |
| 7711 | hlua->max_time = hlua_timeout_session; |
| 7712 | |
| 7713 | /* At this point the execution is safe. */ |
| 7714 | RESET_SAFE_LJMP(hlua->T); |
| 7715 | |
| 7716 | /* It's ok */ |
| 7717 | return 0; |
| 7718 | |
| 7719 | /* It's not ok. */ |
| 7720 | error: |
| 7721 | RESET_SAFE_LJMP(hlua->T); |
| 7722 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7723 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7724 | return 1; |
| 7725 | } |
| 7726 | |
| 7727 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7728 | { |
| 7729 | struct hlua *hlua; |
| 7730 | struct stream_interface *si; |
| 7731 | struct hlua_function *fcn; |
| 7732 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7733 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7734 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7735 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7736 | |
| 7737 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7738 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7739 | return 1; |
| 7740 | |
| 7741 | /* Execute the function. */ |
| 7742 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7743 | |
| 7744 | /* finished. */ |
| 7745 | case HLUA_E_OK: |
| 7746 | return 1; |
| 7747 | |
| 7748 | /* yield. */ |
| 7749 | case HLUA_E_AGAIN: |
| 7750 | /* We want write. */ |
| 7751 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7752 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7753 | /* Set the timeout. */ |
| 7754 | if (hlua->wake_time != TICK_ETERNITY) |
| 7755 | task_schedule(hlua->task, hlua->wake_time); |
| 7756 | return 0; |
| 7757 | |
| 7758 | /* finished with error. */ |
| 7759 | case HLUA_E_ERRMSG: |
| 7760 | /* Display log. */ |
| 7761 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7762 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7763 | lua_pop(hlua->T, 1); |
| 7764 | return 1; |
| 7765 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7766 | case HLUA_E_ETMOUT: |
| 7767 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7768 | fcn->name); |
| 7769 | return 1; |
| 7770 | |
| 7771 | case HLUA_E_NOMEM: |
| 7772 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7773 | fcn->name); |
| 7774 | return 1; |
| 7775 | |
| 7776 | case HLUA_E_YIELD: /* unexpected */ |
| 7777 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7778 | fcn->name); |
| 7779 | return 1; |
| 7780 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7781 | case HLUA_E_ERR: |
| 7782 | /* Display log. */ |
| 7783 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7784 | fcn->name); |
| 7785 | return 1; |
| 7786 | |
| 7787 | default: |
| 7788 | return 1; |
| 7789 | } |
| 7790 | |
| 7791 | return 1; |
| 7792 | } |
| 7793 | |
| 7794 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7795 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7796 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7797 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7798 | } |
| 7799 | |
| 7800 | /* This function is an LUA binding used for registering |
| 7801 | * new keywords in the cli. It expects a list of keywords |
| 7802 | * which are the "path". It is limited to 5 keywords. A |
| 7803 | * description of the command, a function to be executed |
| 7804 | * for the parsing and a function for io handlers. |
| 7805 | */ |
| 7806 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7807 | { |
| 7808 | struct cli_kw_list *cli_kws; |
| 7809 | const char *message; |
| 7810 | int ref_io; |
| 7811 | int len; |
| 7812 | struct hlua_function *fcn; |
| 7813 | int index; |
| 7814 | int i; |
| 7815 | |
| 7816 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7817 | |
| 7818 | /* First argument : an array of maximum 5 keywords. */ |
| 7819 | if (!lua_istable(L, 1)) |
| 7820 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7821 | |
| 7822 | /* Second argument : string with contextual message. */ |
| 7823 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7824 | |
| 7825 | /* Third and fourth argument : lua function. */ |
| 7826 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7827 | |
| 7828 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7829 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7830 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7831 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7832 | fcn = calloc(1, sizeof(*fcn)); |
| 7833 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7834 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7835 | |
| 7836 | /* Fill path. */ |
| 7837 | index = 0; |
| 7838 | lua_pushnil(L); |
| 7839 | while(lua_next(L, 1) != 0) { |
| 7840 | if (index >= 5) |
| 7841 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7842 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7843 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7844 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7845 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7846 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7847 | index++; |
| 7848 | lua_pop(L, 1); |
| 7849 | } |
| 7850 | |
| 7851 | /* Copy help message. */ |
| 7852 | cli_kws->kw[0].usage = strdup(message); |
| 7853 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7854 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7855 | |
| 7856 | /* Fill fcn io handler. */ |
| 7857 | len = strlen("<lua.cli>") + 1; |
| 7858 | for (i = 0; i < index; i++) |
| 7859 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7860 | fcn->name = calloc(1, len); |
| 7861 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7862 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7863 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7864 | for (i = 0; i < index; i++) { |
| 7865 | strncat((char *)fcn->name, ".", len); |
| 7866 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7867 | } |
| 7868 | strncat((char *)fcn->name, ">", len); |
| 7869 | fcn->function_ref = ref_io; |
| 7870 | |
| 7871 | /* Fill last entries. */ |
| 7872 | cli_kws->kw[0].private = fcn; |
| 7873 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7874 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7875 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7876 | |
| 7877 | /* Register this new converter */ |
| 7878 | cli_register_kw(cli_kws); |
| 7879 | |
| 7880 | return 0; |
| 7881 | } |
| 7882 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7883 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7884 | struct proxy *defpx, const char *file, int line, |
| 7885 | char **err, unsigned int *timeout) |
| 7886 | { |
| 7887 | const char *error; |
| 7888 | |
| 7889 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7890 | if (error == PARSE_TIME_OVER) { |
| 7891 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7892 | args[1], args[0]); |
| 7893 | return -1; |
| 7894 | } |
| 7895 | else if (error == PARSE_TIME_UNDER) { |
| 7896 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7897 | args[1], args[0]); |
| 7898 | return -1; |
| 7899 | } |
| 7900 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7901 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7902 | return -1; |
| 7903 | } |
| 7904 | return 0; |
| 7905 | } |
| 7906 | |
| 7907 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7908 | struct proxy *defpx, const char *file, int line, |
| 7909 | char **err) |
| 7910 | { |
| 7911 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7912 | file, line, err, &hlua_timeout_session); |
| 7913 | } |
| 7914 | |
| 7915 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7916 | struct proxy *defpx, const char *file, int line, |
| 7917 | char **err) |
| 7918 | { |
| 7919 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7920 | file, line, err, &hlua_timeout_task); |
| 7921 | } |
| 7922 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7923 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7924 | struct proxy *defpx, const char *file, int line, |
| 7925 | char **err) |
| 7926 | { |
| 7927 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7928 | file, line, err, &hlua_timeout_applet); |
| 7929 | } |
| 7930 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7931 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7932 | struct proxy *defpx, const char *file, int line, |
| 7933 | char **err) |
| 7934 | { |
| 7935 | char *error; |
| 7936 | |
| 7937 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7938 | if (*error != '\0') { |
| 7939 | memprintf(err, "%s: invalid number", args[0]); |
| 7940 | return -1; |
| 7941 | } |
| 7942 | return 0; |
| 7943 | } |
| 7944 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7945 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7946 | struct proxy *defpx, const char *file, int line, |
| 7947 | char **err) |
| 7948 | { |
| 7949 | char *error; |
| 7950 | |
| 7951 | if (*(args[1]) == 0) { |
| 7952 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7953 | return -1; |
| 7954 | } |
| 7955 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7956 | if (*error != '\0') { |
| 7957 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7958 | return -1; |
| 7959 | } |
| 7960 | return 0; |
| 7961 | } |
| 7962 | |
| 7963 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7964 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7965 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7966 | * the main lua entry point. |
| 7967 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7968 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7969 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7970 | * |
| 7971 | * In some error case, LUA set an error message in top of the stack. This function |
| 7972 | * 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] | 7973 | * |
| 7974 | * This function can fail with an abort() due to an Lua critical error. |
| 7975 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7976 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7977 | */ |
| 7978 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7979 | struct proxy *defpx, const char *file, int line, |
| 7980 | char **err) |
| 7981 | { |
| 7982 | int error; |
| 7983 | |
| 7984 | /* Just load and compile the file. */ |
| 7985 | error = luaL_loadfile(gL.T, args[1]); |
| 7986 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7987 | 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] | 7988 | lua_pop(gL.T, 1); |
| 7989 | return -1; |
| 7990 | } |
| 7991 | |
| 7992 | /* If no syntax error where detected, execute the code. */ |
| 7993 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7994 | switch (error) { |
| 7995 | case LUA_OK: |
| 7996 | break; |
| 7997 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7998 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7999 | lua_pop(gL.T, 1); |
| 8000 | return -1; |
| 8001 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8002 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8003 | return -1; |
| 8004 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8005 | 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] | 8006 | lua_pop(gL.T, 1); |
| 8007 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8008 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8009 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8010 | 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] | 8011 | lua_pop(gL.T, 1); |
| 8012 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8013 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8014 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8015 | memprintf(err, "Lua unknown 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; |
| 8018 | } |
| 8019 | |
| 8020 | return 0; |
| 8021 | } |
| 8022 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8023 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8024 | * in the given <ctx>. |
| 8025 | */ |
| 8026 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 8027 | { |
| 8028 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 8029 | lua_pushstring(ctx.T, path); /* push given path */ |
| 8030 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 8031 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 8032 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 8033 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 8034 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 8035 | |
| 8036 | return 0; |
| 8037 | } |
| 8038 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8039 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 8040 | struct proxy *defpx, const char *file, int line, |
| 8041 | char **err) |
| 8042 | { |
| 8043 | char *path; |
| 8044 | char *type = "path"; |
| 8045 | if (too_many_args(2, args, err, NULL)) { |
| 8046 | return -1; |
| 8047 | } |
| 8048 | |
| 8049 | if (!(*args[1])) { |
| 8050 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 8051 | return -1; |
| 8052 | } |
| 8053 | path = args[1]; |
| 8054 | |
| 8055 | if (*args[2]) { |
| 8056 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8057 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 8058 | return -1; |
| 8059 | } |
| 8060 | type = args[2]; |
| 8061 | } |
| 8062 | |
| 8063 | return hlua_prepend_path(gL, type, path); |
| 8064 | } |
| 8065 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8066 | /* configuration keywords declaration */ |
| 8067 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8068 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8069 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8070 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8071 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8072 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8073 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8074 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8075 | { 0, NULL, NULL }, |
| 8076 | }}; |
| 8077 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8078 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8079 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8080 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8081 | /* This function can fail with an abort() due to an Lua critical error. |
| 8082 | * We are in the initialisation process of HAProxy, this abort() is |
| 8083 | * tolerated. |
| 8084 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8085 | int hlua_post_init() |
| 8086 | { |
| 8087 | struct hlua_init_function *init; |
| 8088 | const char *msg; |
| 8089 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8090 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8091 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8092 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8093 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8094 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8095 | error = lua_tostring(gL.T, -1); |
| 8096 | else |
| 8097 | error = "critical error"; |
| 8098 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8099 | exit(1); |
| 8100 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8101 | |
| 8102 | #if USE_OPENSSL |
| 8103 | /* Initialize SSL server. */ |
| 8104 | if (socket_ssl.xprt->prepare_srv) { |
| 8105 | int saved_used_backed = global.ssl_used_backend; |
| 8106 | // don't affect maxconn automatic computation |
| 8107 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8108 | global.ssl_used_backend = saved_used_backed; |
| 8109 | } |
| 8110 | #endif |
| 8111 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8112 | hlua_fcn_post_init(gL.T); |
| 8113 | RESET_SAFE_LJMP(gL.T); |
| 8114 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8115 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8116 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8117 | ret = hlua_ctx_resume(&gL, 0); |
| 8118 | switch (ret) { |
| 8119 | case HLUA_E_OK: |
| 8120 | lua_pop(gL.T, -1); |
| 8121 | return 1; |
| 8122 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8123 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8124 | return 0; |
| 8125 | case HLUA_E_ERRMSG: |
| 8126 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8127 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8128 | return 0; |
| 8129 | case HLUA_E_ERR: |
| 8130 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8131 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8132 | return 0; |
| 8133 | } |
| 8134 | } |
| 8135 | return 1; |
| 8136 | } |
| 8137 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8138 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8139 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8140 | * is the previously allocated size or the kind of object in case of a new |
| 8141 | * allocation. <nsize> is the requested new size. |
| 8142 | */ |
| 8143 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8144 | { |
| 8145 | struct hlua_mem_allocator *zone = ud; |
| 8146 | |
| 8147 | if (nsize == 0) { |
| 8148 | /* it's a free */ |
| 8149 | if (ptr) |
| 8150 | zone->allocated -= osize; |
| 8151 | free(ptr); |
| 8152 | return NULL; |
| 8153 | } |
| 8154 | |
| 8155 | if (!ptr) { |
| 8156 | /* it's a new allocation */ |
| 8157 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8158 | return NULL; |
| 8159 | |
| 8160 | ptr = malloc(nsize); |
| 8161 | if (ptr) |
| 8162 | zone->allocated += nsize; |
| 8163 | return ptr; |
| 8164 | } |
| 8165 | |
| 8166 | /* it's a realloc */ |
| 8167 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8168 | return NULL; |
| 8169 | |
| 8170 | ptr = realloc(ptr, nsize); |
| 8171 | if (ptr) |
| 8172 | zone->allocated += nsize - osize; |
| 8173 | return ptr; |
| 8174 | } |
| 8175 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8176 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8177 | * We are in the initialisation process of HAProxy, this abort() is |
| 8178 | * tolerated. |
| 8179 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8180 | void hlua_init(void) |
| 8181 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8182 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8183 | int idx; |
| 8184 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8185 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8186 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8187 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8188 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8189 | struct srv_kw *kw; |
| 8190 | int tmp_error; |
| 8191 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8192 | char *args[] = { /* SSL client configuration. */ |
| 8193 | "ssl", |
| 8194 | "verify", |
| 8195 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8196 | NULL |
| 8197 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8198 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8199 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8200 | /* Init main lua stack. */ |
| 8201 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8202 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8203 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8204 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8205 | hlua_sethlua(&gL); |
| 8206 | gL.Tref = LUA_REFNIL; |
| 8207 | gL.task = NULL; |
| 8208 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8209 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8210 | * the Lua function can fail with an abort. We are in the initialisation |
| 8211 | * process of HAProxy, this abort() is tolerated. |
| 8212 | */ |
| 8213 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8214 | /* Initialise lua. */ |
| 8215 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8216 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8217 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8218 | #ifdef HLUA_PREPEND_PATH |
| 8219 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8220 | #endif |
| 8221 | #ifdef HLUA_PREPEND_CPATH |
| 8222 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8223 | #endif |
| 8224 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8225 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8226 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8227 | /* Set safe environment for the initialisation. */ |
| 8228 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8229 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8230 | error_msg = lua_tostring(gL.T, -1); |
| 8231 | else |
| 8232 | error_msg = "critical error"; |
| 8233 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8234 | exit(1); |
| 8235 | } |
| 8236 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8237 | /* |
| 8238 | * |
| 8239 | * Create "core" object. |
| 8240 | * |
| 8241 | */ |
| 8242 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8243 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8244 | lua_newtable(gL.T); |
| 8245 | |
| 8246 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8247 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8248 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8249 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8250 | /* Register special functions. */ |
| 8251 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8252 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8253 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8254 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8255 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8256 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8257 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8258 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8259 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8260 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8261 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8262 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8263 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8264 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8265 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8266 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8267 | hlua_class_function(gL.T, "log", hlua_log); |
| 8268 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8269 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8270 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8271 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8272 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8273 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8274 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8275 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8276 | |
| 8277 | /* |
| 8278 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8279 | * Create "act" object. |
| 8280 | * |
| 8281 | */ |
| 8282 | |
| 8283 | /* This table entry is the object "act" base. */ |
| 8284 | lua_newtable(gL.T); |
| 8285 | |
| 8286 | /* push action return constants */ |
| 8287 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8288 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8289 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8290 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8291 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8292 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8293 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8294 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8295 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8296 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8297 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8298 | lua_setglobal(gL.T, "act"); |
| 8299 | |
| 8300 | /* |
| 8301 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8302 | * Register class Map |
| 8303 | * |
| 8304 | */ |
| 8305 | |
| 8306 | /* This table entry is the object "Map" base. */ |
| 8307 | lua_newtable(gL.T); |
| 8308 | |
| 8309 | /* register pattern types. */ |
| 8310 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8311 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8312 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8313 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8314 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8315 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8316 | |
| 8317 | /* register constructor. */ |
| 8318 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8319 | |
| 8320 | /* Create and fill the metatable. */ |
| 8321 | lua_newtable(gL.T); |
| 8322 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8323 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8324 | lua_pushstring(gL.T, "__index"); |
| 8325 | lua_newtable(gL.T); |
| 8326 | |
| 8327 | /* Register . */ |
| 8328 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8329 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8330 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8331 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8332 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8333 | /* Register previous table in the registry with reference and named entry. |
| 8334 | * The function hlua_register_metatable() pops the stack, so we |
| 8335 | * previously create a copy of the table. |
| 8336 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8337 | 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] | 8338 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8339 | |
| 8340 | /* Assign the metatable to the mai Map object. */ |
| 8341 | lua_setmetatable(gL.T, -2); |
| 8342 | |
| 8343 | /* Set a name to the table. */ |
| 8344 | lua_setglobal(gL.T, "Map"); |
| 8345 | |
| 8346 | /* |
| 8347 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8348 | * Register class Channel |
| 8349 | * |
| 8350 | */ |
| 8351 | |
| 8352 | /* Create and fill the metatable. */ |
| 8353 | lua_newtable(gL.T); |
| 8354 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8355 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8356 | lua_pushstring(gL.T, "__index"); |
| 8357 | lua_newtable(gL.T); |
| 8358 | |
| 8359 | /* Register . */ |
| 8360 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8361 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8362 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8363 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8364 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8365 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8366 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8367 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8368 | 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] | 8369 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8370 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8371 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8372 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8373 | |
| 8374 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8375 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8376 | |
| 8377 | /* |
| 8378 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8379 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8380 | * |
| 8381 | */ |
| 8382 | |
| 8383 | /* Create and fill the metatable. */ |
| 8384 | lua_newtable(gL.T); |
| 8385 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8386 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8387 | lua_pushstring(gL.T, "__index"); |
| 8388 | lua_newtable(gL.T); |
| 8389 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8390 | /* Browse existing fetches and create the associated |
| 8391 | * object method. |
| 8392 | */ |
| 8393 | sf = NULL; |
| 8394 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8395 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8396 | * by an underscore. |
| 8397 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8398 | strncpy(trash.area, sf->kw, trash.size); |
| 8399 | trash.area[trash.size - 1] = '\0'; |
| 8400 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8401 | if (*p == '.' || *p == '-' || *p == '+') |
| 8402 | *p = '_'; |
| 8403 | |
| 8404 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8405 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8406 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8407 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8408 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8409 | } |
| 8410 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8411 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8412 | |
| 8413 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8414 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8415 | |
| 8416 | /* |
| 8417 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8418 | * Register class Converters |
| 8419 | * |
| 8420 | */ |
| 8421 | |
| 8422 | /* Create and fill the metatable. */ |
| 8423 | lua_newtable(gL.T); |
| 8424 | |
| 8425 | /* Create and fill the __index entry. */ |
| 8426 | lua_pushstring(gL.T, "__index"); |
| 8427 | lua_newtable(gL.T); |
| 8428 | |
| 8429 | /* Browse existing converters and create the associated |
| 8430 | * object method. |
| 8431 | */ |
| 8432 | sc = NULL; |
| 8433 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8434 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8435 | * by an underscore. |
| 8436 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8437 | strncpy(trash.area, sc->kw, trash.size); |
| 8438 | trash.area[trash.size - 1] = '\0'; |
| 8439 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8440 | if (*p == '.' || *p == '-' || *p == '+') |
| 8441 | *p = '_'; |
| 8442 | |
| 8443 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8444 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8445 | lua_pushlightuserdata(gL.T, sc); |
| 8446 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8447 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8448 | } |
| 8449 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8450 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8451 | |
| 8452 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8453 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8454 | |
| 8455 | /* |
| 8456 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8457 | * Register class HTTP |
| 8458 | * |
| 8459 | */ |
| 8460 | |
| 8461 | /* Create and fill the metatable. */ |
| 8462 | lua_newtable(gL.T); |
| 8463 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8464 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8465 | lua_pushstring(gL.T, "__index"); |
| 8466 | lua_newtable(gL.T); |
| 8467 | |
| 8468 | /* Register Lua functions. */ |
| 8469 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8470 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8471 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8472 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8473 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8474 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8475 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8476 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8477 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8478 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8479 | |
| 8480 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8481 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8482 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8483 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8484 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8485 | 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] | 8486 | 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] | 8487 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8488 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8489 | |
| 8490 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8491 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8492 | |
| 8493 | /* |
| 8494 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8495 | * Register class AppletTCP |
| 8496 | * |
| 8497 | */ |
| 8498 | |
| 8499 | /* Create and fill the metatable. */ |
| 8500 | lua_newtable(gL.T); |
| 8501 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8502 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8503 | lua_pushstring(gL.T, "__index"); |
| 8504 | lua_newtable(gL.T); |
| 8505 | |
| 8506 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8507 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8508 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8509 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8510 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8511 | 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] | 8512 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8513 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8514 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8515 | |
| 8516 | lua_settable(gL.T, -3); |
| 8517 | |
| 8518 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8519 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8520 | |
| 8521 | /* |
| 8522 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8523 | * Register class AppletHTTP |
| 8524 | * |
| 8525 | */ |
| 8526 | |
| 8527 | /* Create and fill the metatable. */ |
| 8528 | lua_newtable(gL.T); |
| 8529 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8530 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8531 | lua_pushstring(gL.T, "__index"); |
| 8532 | lua_newtable(gL.T); |
| 8533 | |
| 8534 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8535 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8536 | 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] | 8537 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8538 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8539 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8540 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8541 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8542 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8543 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8544 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8545 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8546 | |
| 8547 | lua_settable(gL.T, -3); |
| 8548 | |
| 8549 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8550 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8551 | |
| 8552 | /* |
| 8553 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8554 | * Register class TXN |
| 8555 | * |
| 8556 | */ |
| 8557 | |
| 8558 | /* Create and fill the metatable. */ |
| 8559 | lua_newtable(gL.T); |
| 8560 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8561 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8562 | lua_pushstring(gL.T, "__index"); |
| 8563 | lua_newtable(gL.T); |
| 8564 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8565 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8566 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8567 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8568 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8569 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8570 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8571 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8572 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8573 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8574 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8575 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8576 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8577 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8578 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8579 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8580 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8581 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8582 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8583 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8584 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8585 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8586 | |
| 8587 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8588 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8589 | |
| 8590 | /* |
| 8591 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8592 | * Register class reply |
| 8593 | * |
| 8594 | */ |
| 8595 | lua_newtable(gL.T); |
| 8596 | lua_pushstring(gL.T, "__index"); |
| 8597 | lua_newtable(gL.T); |
| 8598 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8599 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8600 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8601 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8602 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8603 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8604 | |
| 8605 | |
| 8606 | /* |
| 8607 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8608 | * Register class Socket |
| 8609 | * |
| 8610 | */ |
| 8611 | |
| 8612 | /* Create and fill the metatable. */ |
| 8613 | lua_newtable(gL.T); |
| 8614 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8615 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8616 | lua_pushstring(gL.T, "__index"); |
| 8617 | lua_newtable(gL.T); |
| 8618 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8619 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8620 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8621 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8622 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8623 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8624 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8625 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8626 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8627 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8628 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8629 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8630 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8631 | 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] | 8632 | |
| 8633 | /* Register the garbage collector entry. */ |
| 8634 | lua_pushstring(gL.T, "__gc"); |
| 8635 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
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 previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8639 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8640 | |
| 8641 | /* Proxy and server configuration initialisation. */ |
| 8642 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8643 | init_new_proxy(&socket_proxy); |
| 8644 | socket_proxy.parent = NULL; |
| 8645 | socket_proxy.last_change = now.tv_sec; |
| 8646 | socket_proxy.id = "LUA-SOCKET"; |
| 8647 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8648 | socket_proxy.maxconn = 0; |
| 8649 | socket_proxy.accept = NULL; |
| 8650 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8651 | socket_proxy.srv = NULL; |
| 8652 | socket_proxy.conn_retries = 0; |
| 8653 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8654 | |
| 8655 | /* Init TCP server: unchanged parameters */ |
| 8656 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8657 | socket_tcp.next = NULL; |
| 8658 | socket_tcp.proxy = &socket_proxy; |
| 8659 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8660 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8661 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8662 | socket_tcp.idle_conns = NULL; |
| 8663 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8664 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8665 | socket_tcp.last_change = 0; |
| 8666 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8667 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8668 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8669 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8670 | |
| 8671 | /* XXX: Copy default parameter from default server, |
| 8672 | * but the default server is not initialized. |
| 8673 | */ |
| 8674 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8675 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8676 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8677 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8678 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8679 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8680 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8681 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8682 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8683 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8684 | |
| 8685 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8686 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8687 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8688 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8689 | socket_tcp.check.server = &socket_tcp; |
| 8690 | |
| 8691 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8692 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8693 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8694 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8695 | socket_tcp.agent.server = &socket_tcp; |
| 8696 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8697 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8698 | |
| 8699 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8700 | /* Init TCP server: unchanged parameters */ |
| 8701 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8702 | socket_ssl.next = NULL; |
| 8703 | socket_ssl.proxy = &socket_proxy; |
| 8704 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8705 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8706 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8707 | socket_ssl.idle_conns = NULL; |
| 8708 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8709 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8710 | socket_ssl.last_change = 0; |
| 8711 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8712 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8713 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8714 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8715 | |
| 8716 | /* XXX: Copy default parameter from default server, |
| 8717 | * but the default server is not initialized. |
| 8718 | */ |
| 8719 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8720 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8721 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8722 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8723 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8724 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8725 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8726 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8727 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8728 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8729 | |
| 8730 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8731 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8732 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8733 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8734 | socket_ssl.check.server = &socket_ssl; |
| 8735 | |
| 8736 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8737 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8738 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8739 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8740 | socket_ssl.agent.server = &socket_ssl; |
| 8741 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8742 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8743 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8744 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8745 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8746 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8747 | /* |
| 8748 | * |
| 8749 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8750 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8751 | * features like client certificates and ssl_verify. |
| 8752 | * |
| 8753 | */ |
| 8754 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8755 | if (tmp_error != 0) { |
| 8756 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8757 | abort(); /* This must be never arrives because the command line |
| 8758 | not editable by the user. */ |
| 8759 | } |
| 8760 | idx += kw->skip; |
| 8761 | } |
| 8762 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8763 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8764 | |
| 8765 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8766 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8767 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8768 | static void hlua_deinit() |
| 8769 | { |
| 8770 | lua_close(gL.T); |
| 8771 | } |
| 8772 | |
| 8773 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8774 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8775 | static void hlua_register_build_options(void) |
| 8776 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8777 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8778 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8779 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8780 | hap_register_build_opts(ptr, 1); |
| 8781 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8782 | |
| 8783 | INITCALL0(STG_REGISTER, hlua_register_build_options); |