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))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 948 | get_localtime(date.tv_sec, &tm); |
| 949 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 950 | 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] | 951 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 952 | fflush(stderr); |
| 953 | } |
| 954 | } |
| 955 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 956 | /* This function just ensure that the yield will be always |
| 957 | * returned with a timeout and permit to set some flags |
| 958 | */ |
| 959 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 960 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 961 | { |
| 962 | struct hlua *hlua = hlua_gethlua(L); |
| 963 | |
| 964 | /* Set the wake timeout. If timeout is required, we set |
| 965 | * the expiration time. |
| 966 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 967 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 968 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 969 | hlua->flags |= flags; |
| 970 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 971 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 972 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 973 | } |
| 974 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 975 | /* This function initialises the Lua environment stored in the stream. |
| 976 | * 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] | 977 | * 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] | 978 | * |
| 979 | * This function is particular. it initialises a new Lua thread. If the |
| 980 | * initialisation fails (example: out of memory error), the lua function |
| 981 | * throws an error (longjmp). |
| 982 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 983 | * 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] | 984 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 985 | * threads appear, the safe environment set a lock to ensure only one |
| 986 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 987 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 988 | * |
| 989 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 990 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 991 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 992 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 993 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 994 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 995 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 996 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 997 | 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] | 998 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 999 | if (!already_safe) { |
| 1000 | if (!SET_SAFE_LJMP(gL.T)) { |
| 1001 | lua->Tref = LUA_REFNIL; |
| 1002 | return 0; |
| 1003 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1004 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1005 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1006 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1007 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1008 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1009 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1010 | lua->T = lua_newthread(gL.T); |
| 1011 | if (!lua->T) { |
| 1012 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1013 | if (!already_safe) |
| 1014 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1015 | return 0; |
| 1016 | } |
| 1017 | hlua_sethlua(lua); |
| 1018 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1019 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1020 | if (!already_safe) |
| 1021 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1022 | return 1; |
| 1023 | } |
| 1024 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1025 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1026 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1027 | * is not freed. |
| 1028 | */ |
| 1029 | void hlua_ctx_destroy(struct hlua *lua) |
| 1030 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1031 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1032 | return; |
| 1033 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1034 | if (!lua->T) |
| 1035 | goto end; |
| 1036 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1037 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1038 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1039 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1040 | if (!SET_SAFE_LJMP(lua->T)) |
| 1041 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1042 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1043 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1044 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1045 | if (!SET_SAFE_LJMP(gL.T)) |
| 1046 | return; |
| 1047 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1048 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1049 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1050 | * without error, we run the GC on the thread pointer. Its freed all |
| 1051 | * the unused memory. |
| 1052 | * If the thread is finnish with an error or is currently yielded, |
| 1053 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1054 | * so e run the GC on the main thread. |
| 1055 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1056 | * the garbage collection. |
| 1057 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1058 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1059 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1060 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1061 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 1062 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1063 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1064 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1065 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1066 | |
| 1067 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1068 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /* This function is used to restore the Lua context when a coroutine |
| 1072 | * fails. This function copy the common memory between old coroutine |
| 1073 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1074 | * replaced by the new coroutine. |
| 1075 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1076 | * as string error message and it is copied in the new stack. |
| 1077 | */ |
| 1078 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1079 | { |
| 1080 | lua_State *T; |
| 1081 | int new_ref; |
| 1082 | |
| 1083 | /* Renew the main LUA stack doesn't have sense. */ |
| 1084 | if (lua == &gL) |
| 1085 | return 0; |
| 1086 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1087 | /* New Lua coroutine. */ |
| 1088 | T = lua_newthread(gL.T); |
| 1089 | if (!T) |
| 1090 | return 0; |
| 1091 | |
| 1092 | /* Copy last error message. */ |
| 1093 | if (keep_msg) |
| 1094 | lua_xmove(lua->T, T, 1); |
| 1095 | |
| 1096 | /* Copy data between the coroutines. */ |
| 1097 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1098 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1099 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1100 | |
| 1101 | /* Destroy old data. */ |
| 1102 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1103 | |
| 1104 | /* The thread is garbage collected by Lua. */ |
| 1105 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1106 | |
| 1107 | /* Fill the struct with the new coroutine values. */ |
| 1108 | lua->Mref = new_ref; |
| 1109 | lua->T = T; |
| 1110 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1111 | |
| 1112 | /* Set context. */ |
| 1113 | hlua_sethlua(lua); |
| 1114 | |
| 1115 | return 1; |
| 1116 | } |
| 1117 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1118 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1119 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1120 | struct hlua *hlua = hlua_gethlua(L); |
| 1121 | |
| 1122 | /* Lua cannot yield when its returning from a function, |
| 1123 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1124 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1125 | */ |
| 1126 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1127 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | /* restore the interrupt condition. */ |
| 1132 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1133 | |
| 1134 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1135 | * If the state is not yieldable, trying yield causes an error. |
| 1136 | */ |
| 1137 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1138 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1139 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1140 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1141 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1142 | hlua->run_time += now_ms - hlua->start_time; |
| 1143 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1144 | lua_pushfstring(L, "execution timeout"); |
| 1145 | WILL_LJMP(lua_error(L)); |
| 1146 | } |
| 1147 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1148 | /* Update the start time. */ |
| 1149 | hlua->start_time = now_ms; |
| 1150 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1151 | /* Try to interrupt the process at the end of the current |
| 1152 | * unyieldable function. |
| 1153 | */ |
| 1154 | 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] | 1155 | } |
| 1156 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | /* This function start or resumes the Lua stack execution. If the flag |
| 1158 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1159 | * The function return an error. |
| 1160 | * |
| 1161 | * The function can returns 4 values: |
| 1162 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1163 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1164 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1165 | * - 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] | 1166 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1167 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1168 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1169 | * 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] | 1170 | * LUA code. |
| 1171 | */ |
| 1172 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1173 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1174 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1175 | int nres; |
| 1176 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1177 | int ret; |
| 1178 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1179 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1181 | /* Initialise run time counter. */ |
| 1182 | if (!HLUA_IS_RUNNING(lua)) |
| 1183 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1184 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1185 | /* Lock the whole Lua execution. This lock must be before the |
| 1186 | * label "resume_execution". |
| 1187 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1188 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1189 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1190 | resume_execution: |
| 1191 | |
| 1192 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1193 | * instructions. it is used for preventing infinite loops. |
| 1194 | */ |
| 1195 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1196 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1197 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1198 | HLUA_SET_RUN(lua); |
| 1199 | HLUA_CLR_CTRLYIELD(lua); |
| 1200 | HLUA_CLR_WAKERESWR(lua); |
| 1201 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1202 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1203 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1204 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1205 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1206 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1207 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1208 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1209 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1210 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1211 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1212 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1213 | switch (ret) { |
| 1214 | |
| 1215 | case LUA_OK: |
| 1216 | ret = HLUA_E_OK; |
| 1217 | break; |
| 1218 | |
| 1219 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1220 | /* Check if the execution timeout is expired. It it is the case, we |
| 1221 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1222 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1223 | tv_update_date(0, 1); |
| 1224 | lua->run_time += now_ms - lua->start_time; |
| 1225 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1226 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1227 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1228 | break; |
| 1229 | } |
| 1230 | /* Process the forced yield. if the general yield is not allowed or |
| 1231 | * if no task were associated this the current Lua execution |
| 1232 | * coroutine, we resume the execution. Else we want to return in the |
| 1233 | * scheduler and we want to be waked up again, to continue the |
| 1234 | * current Lua execution. So we schedule our own task. |
| 1235 | */ |
| 1236 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1237 | if (!yield_allowed || !lua->task) |
| 1238 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1239 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1240 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1241 | if (!yield_allowed) { |
| 1242 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1243 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1244 | break; |
| 1245 | } |
| 1246 | ret = HLUA_E_AGAIN; |
| 1247 | break; |
| 1248 | |
| 1249 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1250 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1251 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1252 | * because the errors ares the only one mean to return immediately |
| 1253 | * from and lua execution. |
| 1254 | */ |
| 1255 | if (lua->flags & HLUA_EXIT) { |
| 1256 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1257 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1258 | break; |
| 1259 | } |
| 1260 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1261 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1262 | if (!lua_checkstack(lua->T, 1)) { |
| 1263 | ret = HLUA_E_ERR; |
| 1264 | break; |
| 1265 | } |
| 1266 | msg = lua_tostring(lua->T, -1); |
| 1267 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1268 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1269 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1270 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1271 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1272 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1273 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1274 | ret = HLUA_E_ERRMSG; |
| 1275 | break; |
| 1276 | |
| 1277 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1278 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1279 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1280 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1281 | break; |
| 1282 | |
| 1283 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1284 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1285 | if (!lua_checkstack(lua->T, 1)) { |
| 1286 | ret = HLUA_E_ERR; |
| 1287 | break; |
| 1288 | } |
| 1289 | msg = lua_tostring(lua->T, -1); |
| 1290 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1291 | lua_pop(lua->T, 1); |
| 1292 | if (msg) |
| 1293 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1294 | else |
| 1295 | lua_pushfstring(lua->T, "message handler error"); |
| 1296 | ret = HLUA_E_ERRMSG; |
| 1297 | break; |
| 1298 | |
| 1299 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1300 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1301 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1302 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | switch (ret) { |
| 1307 | case HLUA_E_AGAIN: |
| 1308 | break; |
| 1309 | |
| 1310 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1311 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1312 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1313 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1314 | break; |
| 1315 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1316 | case HLUA_E_ETMOUT: |
| 1317 | case HLUA_E_NOMEM: |
| 1318 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1319 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1320 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1321 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1322 | hlua_ctx_renew(lua, 0); |
| 1323 | break; |
| 1324 | |
| 1325 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1326 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1327 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1328 | break; |
| 1329 | } |
| 1330 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1331 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1332 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1333 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1334 | return ret; |
| 1335 | } |
| 1336 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1337 | /* This function exit the current code. */ |
| 1338 | __LJMP static int hlua_done(lua_State *L) |
| 1339 | { |
| 1340 | struct hlua *hlua = hlua_gethlua(L); |
| 1341 | |
| 1342 | hlua->flags |= HLUA_EXIT; |
| 1343 | WILL_LJMP(lua_error(L)); |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1348 | /* This function is an LUA binding. It provides a function |
| 1349 | * for deleting ACL from a referenced ACL file. |
| 1350 | */ |
| 1351 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1352 | { |
| 1353 | const char *name; |
| 1354 | const char *key; |
| 1355 | struct pat_ref *ref; |
| 1356 | |
| 1357 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1358 | |
| 1359 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1360 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1361 | |
| 1362 | ref = pat_ref_lookup(name); |
| 1363 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1364 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1365 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1366 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1367 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1368 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | /* This function is an LUA binding. It provides a function |
| 1373 | * for deleting map entry from a referenced map file. |
| 1374 | */ |
| 1375 | static int hlua_del_map(lua_State *L) |
| 1376 | { |
| 1377 | const char *name; |
| 1378 | const char *key; |
| 1379 | struct pat_ref *ref; |
| 1380 | |
| 1381 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1382 | |
| 1383 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1384 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1385 | |
| 1386 | ref = pat_ref_lookup(name); |
| 1387 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1388 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1389 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1390 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1391 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1392 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | /* This function is an LUA binding. It provides a function |
| 1397 | * for adding ACL pattern from a referenced ACL file. |
| 1398 | */ |
| 1399 | static int hlua_add_acl(lua_State *L) |
| 1400 | { |
| 1401 | const char *name; |
| 1402 | const char *key; |
| 1403 | struct pat_ref *ref; |
| 1404 | |
| 1405 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1406 | |
| 1407 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1408 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1409 | |
| 1410 | ref = pat_ref_lookup(name); |
| 1411 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1412 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1413 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1414 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1415 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1416 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1417 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1418 | return 0; |
| 1419 | } |
| 1420 | |
| 1421 | /* This function is an LUA binding. It provides a function |
| 1422 | * for setting map pattern and sample from a referenced map |
| 1423 | * file. |
| 1424 | */ |
| 1425 | static int hlua_set_map(lua_State *L) |
| 1426 | { |
| 1427 | const char *name; |
| 1428 | const char *key; |
| 1429 | const char *value; |
| 1430 | struct pat_ref *ref; |
| 1431 | |
| 1432 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1433 | |
| 1434 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1435 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1436 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1437 | |
| 1438 | ref = pat_ref_lookup(name); |
| 1439 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1440 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1441 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1442 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1443 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1444 | pat_ref_set(ref, key, value, NULL); |
| 1445 | else |
| 1446 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1447 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1448 | return 0; |
| 1449 | } |
| 1450 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1451 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1452 | * 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] | 1453 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1454 | * the name of the object (_G[<name>] = <metable> ). |
| 1455 | * |
| 1456 | * A metable is a table that modify the standard behavior of a standard |
| 1457 | * access to the associated data. The entries of this new metatable are |
| 1458 | * defined as is: |
| 1459 | * |
| 1460 | * http://lua-users.org/wiki/MetatableEvents |
| 1461 | * |
| 1462 | * __index |
| 1463 | * |
| 1464 | * we access an absent field in a table, the result is nil. This is |
| 1465 | * true, but it is not the whole truth. Actually, such access triggers |
| 1466 | * the interpreter to look for an __index metamethod: If there is no |
| 1467 | * such method, as usually happens, then the access results in nil; |
| 1468 | * otherwise, the metamethod will provide the result. |
| 1469 | * |
| 1470 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1471 | * the key does not appear in the table, but the metatable has an __index |
| 1472 | * property: |
| 1473 | * |
| 1474 | * - if the value is a function, the function is called, passing in the |
| 1475 | * table and the key; the return value of that function is returned as |
| 1476 | * the result. |
| 1477 | * |
| 1478 | * - if the value is another table, the value of the key in that table is |
| 1479 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1480 | * table's metatable has an __index property, then it continues on up) |
| 1481 | * |
| 1482 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1483 | * |
| 1484 | * http://www.lua.org/pil/13.4.1.html |
| 1485 | * |
| 1486 | * __newindex |
| 1487 | * |
| 1488 | * Like __index, but control property assignment. |
| 1489 | * |
| 1490 | * __mode - Control weak references. A string value with one or both |
| 1491 | * of the characters 'k' and 'v' which specifies that the the |
| 1492 | * keys and/or values in the table are weak references. |
| 1493 | * |
| 1494 | * __call - Treat a table like a function. When a table is followed by |
| 1495 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1496 | * a __call key pointing to a function, that function is invoked |
| 1497 | * (passing any specified arguments) and the return value is |
| 1498 | * returned. |
| 1499 | * |
| 1500 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1501 | * called, if the metatable for myTable has a __metatable |
| 1502 | * key, the value of that key is returned instead of the |
| 1503 | * actual metatable. |
| 1504 | * |
| 1505 | * __tostring - Control string representation. When the builtin |
| 1506 | * "tostring( myTable )" function is called, if the metatable |
| 1507 | * for myTable has a __tostring property set to a function, |
| 1508 | * that function is invoked (passing myTable to it) and the |
| 1509 | * return value is used as the string representation. |
| 1510 | * |
| 1511 | * __len - Control table length. When the table length is requested using |
| 1512 | * the length operator ( '#' ), if the metatable for myTable has |
| 1513 | * a __len key pointing to a function, that function is invoked |
| 1514 | * (passing myTable to it) and the return value used as the value |
| 1515 | * of "#myTable". |
| 1516 | * |
| 1517 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1518 | * collected, if the metatable has a __gc field pointing to a |
| 1519 | * function, that function is first invoked, passing the userdata |
| 1520 | * to it. The __gc metamethod is not called for tables. |
| 1521 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1522 | * |
| 1523 | * Special metamethods for redefining standard operators: |
| 1524 | * http://www.lua.org/pil/13.1.html |
| 1525 | * |
| 1526 | * __add "+" |
| 1527 | * __sub "-" |
| 1528 | * __mul "*" |
| 1529 | * __div "/" |
| 1530 | * __unm "!" |
| 1531 | * __pow "^" |
| 1532 | * __concat ".." |
| 1533 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1534 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1535 | * http://www.lua.org/pil/13.2.html |
| 1536 | * |
| 1537 | * __eq "==" |
| 1538 | * __lt "<" |
| 1539 | * __le "<=" |
| 1540 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1541 | |
| 1542 | /* |
| 1543 | * |
| 1544 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1545 | * Class Map |
| 1546 | * |
| 1547 | * |
| 1548 | */ |
| 1549 | |
| 1550 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1551 | * a class session, otherwise it throws an error. |
| 1552 | */ |
| 1553 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1554 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1555 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | /* This function is the map constructor. It don't need |
| 1559 | * the class Map object. It creates and return a new Map |
| 1560 | * object. It must be called only during "body" or "init" |
| 1561 | * context because it process some filesystem accesses. |
| 1562 | */ |
| 1563 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1564 | { |
| 1565 | const char *fn; |
| 1566 | int match = PAT_MATCH_STR; |
| 1567 | struct sample_conv conv; |
| 1568 | const char *file = ""; |
| 1569 | int line = 0; |
| 1570 | lua_Debug ar; |
| 1571 | char *err = NULL; |
| 1572 | struct arg args[2]; |
| 1573 | |
| 1574 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1575 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1576 | |
| 1577 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1578 | |
| 1579 | if (lua_gettop(L) >= 2) { |
| 1580 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1581 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1582 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1583 | } |
| 1584 | |
| 1585 | /* Get Lua filename and line number. */ |
| 1586 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1587 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1588 | if (ar.currentline > 0) { /* is there info? */ |
| 1589 | file = ar.short_src; |
| 1590 | line = ar.currentline; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /* fill fake sample_conv struct. */ |
| 1595 | conv.kw = ""; /* unused. */ |
| 1596 | conv.process = NULL; /* unused. */ |
| 1597 | conv.arg_mask = 0; /* unused. */ |
| 1598 | conv.val_args = NULL; /* unused. */ |
| 1599 | conv.out_type = SMP_T_STR; |
| 1600 | conv.private = (void *)(long)match; |
| 1601 | switch (match) { |
| 1602 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1603 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1604 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1605 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1606 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1607 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1608 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1609 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1610 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1611 | default: |
| 1612 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1613 | } |
| 1614 | |
| 1615 | /* fill fake args. */ |
| 1616 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1617 | args[0].data.str.area = strdup(fn); |
| 1618 | args[0].data.str.data = strlen(fn); |
| 1619 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1620 | args[1].type = ARGT_STOP; |
| 1621 | |
| 1622 | /* load the map. */ |
| 1623 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1624 | /* error case: we cant use luaL_error because we must |
| 1625 | * free the err variable. |
| 1626 | */ |
| 1627 | luaL_where(L, 1); |
| 1628 | lua_pushfstring(L, "'new': %s.", err); |
| 1629 | lua_concat(L, 2); |
| 1630 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1631 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1632 | WILL_LJMP(lua_error(L)); |
| 1633 | } |
| 1634 | |
| 1635 | /* create the lua object. */ |
| 1636 | lua_newtable(L); |
| 1637 | lua_pushlightuserdata(L, args[0].data.map); |
| 1638 | lua_rawseti(L, -2, 0); |
| 1639 | |
| 1640 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1641 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1642 | lua_setmetatable(L, -2); |
| 1643 | |
| 1644 | |
| 1645 | return 1; |
| 1646 | } |
| 1647 | |
| 1648 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1649 | { |
| 1650 | struct map_descriptor *desc; |
| 1651 | struct pattern *pat; |
| 1652 | struct sample smp; |
| 1653 | |
| 1654 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1655 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1656 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1657 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1658 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1659 | } |
| 1660 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1661 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1662 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1663 | 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] | 1664 | } |
| 1665 | |
| 1666 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1667 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1668 | if (str) |
| 1669 | lua_pushstring(L, ""); |
| 1670 | else |
| 1671 | lua_pushnil(L); |
| 1672 | return 1; |
| 1673 | } |
| 1674 | |
| 1675 | /* 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] | 1676 | 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] | 1677 | return 1; |
| 1678 | } |
| 1679 | |
| 1680 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1681 | { |
| 1682 | return _hlua_map_lookup(L, 0); |
| 1683 | } |
| 1684 | |
| 1685 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1686 | { |
| 1687 | return _hlua_map_lookup(L, 1); |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * |
| 1692 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1693 | * Class Socket |
| 1694 | * |
| 1695 | * |
| 1696 | */ |
| 1697 | |
| 1698 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1699 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1700 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | /* 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] | 1704 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | * received. |
| 1706 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1707 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1708 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1709 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1710 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1711 | if (appctx->ctx.hlua_cosocket.die) { |
| 1712 | si_shutw(si); |
| 1713 | si_shutr(si); |
| 1714 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1715 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1716 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1717 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1718 | } |
| 1719 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1720 | /* If we cant write, wakeup the pending write signals. */ |
| 1721 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1722 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1723 | |
| 1724 | /* If we cant read, wakeup the pending read signals. */ |
| 1725 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1726 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1727 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1728 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1729 | * to be notified whenever the connection completes. |
| 1730 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1731 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1732 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1733 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1734 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1735 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1736 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1737 | |
| 1738 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1739 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1740 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1741 | /* 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] | 1742 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1743 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1744 | |
| 1745 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1746 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1747 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1748 | |
| 1749 | /* Some data were injected in the buffer, notify the stream |
| 1750 | * interface. |
| 1751 | */ |
| 1752 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1753 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1754 | |
| 1755 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1756 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1757 | */ |
| 1758 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1759 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1760 | } |
| 1761 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1762 | /* This function is called when the "struct stream" is destroyed. |
| 1763 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1764 | * Wake all the pending signals. |
| 1765 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1766 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1767 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1768 | struct xref *peer; |
| 1769 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1770 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1771 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1772 | if (peer) |
| 1773 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1774 | |
| 1775 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1776 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1777 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1781 | * uses this object. If the stream does not exists, just quit. |
| 1782 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | * pending signal can rest in the read and write lists. destroy |
| 1784 | * it. |
| 1785 | */ |
| 1786 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1787 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1788 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1790 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1792 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1793 | |
| 1794 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1795 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1796 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1797 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1798 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1800 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1801 | appctx->ctx.hlua_cosocket.die = 1; |
| 1802 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1803 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1804 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1805 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1810 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1811 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1812 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1813 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1814 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1816 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1817 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1818 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1819 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1820 | |
| 1821 | /* Check if we run on the same thread than the xreator thread. |
| 1822 | * We cannot access to the socket if the thread is different. |
| 1823 | */ |
| 1824 | if (socket->tid != tid) |
| 1825 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1826 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1827 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1828 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1830 | |
| 1831 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1832 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1834 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1835 | appctx->ctx.hlua_cosocket.die = 1; |
| 1836 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1838 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1839 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | return 0; |
| 1841 | } |
| 1842 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1843 | /* The close function calls close_helper. |
| 1844 | */ |
| 1845 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1846 | { |
| 1847 | MAY_LJMP(check_args(L, 1, "close")); |
| 1848 | return hlua_socket_close_helper(L); |
| 1849 | } |
| 1850 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1851 | /* This Lua function assumes that the stack contain three parameters. |
| 1852 | * 1 - USERDATA containing a struct socket |
| 1853 | * 2 - INTEGER with values of the macro defined below |
| 1854 | * If the integer is -1, we must read at most one line. |
| 1855 | * If the integer is -2, we ust read all the data until the |
| 1856 | * end of the stream. |
| 1857 | * If the integer is positive value, we must read a number of |
| 1858 | * bytes corresponding to this value. |
| 1859 | */ |
| 1860 | #define HLSR_READ_LINE (-1) |
| 1861 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1862 | __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] | 1863 | { |
| 1864 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1865 | int wanted = lua_tointeger(L, 2); |
| 1866 | struct hlua *hlua = hlua_gethlua(L); |
| 1867 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1868 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1870 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1871 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1872 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1873 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1874 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1875 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1876 | struct stream_interface *si; |
| 1877 | struct stream *s; |
| 1878 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1879 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | |
| 1881 | /* Check if this lua stack is schedulable. */ |
| 1882 | if (!hlua || !hlua->task) |
| 1883 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1884 | "'frontend', 'backend' or 'task'")); |
| 1885 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1886 | /* Check if we run on the same thread than the xreator thread. |
| 1887 | * We cannot access to the socket if the thread is different. |
| 1888 | */ |
| 1889 | if (socket->tid != tid) |
| 1890 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1891 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1892 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1893 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1894 | if (!peer) |
| 1895 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1896 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1897 | si = appctx->owner; |
| 1898 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1900 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1903 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | if (nblk < 0) /* Connection close. */ |
| 1905 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1906 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1908 | |
| 1909 | /* remove final \r\n. */ |
| 1910 | if (nblk == 1) { |
| 1911 | if (blk1[len1-1] == '\n') { |
| 1912 | len1--; |
| 1913 | skip_at_end++; |
| 1914 | if (blk1[len1-1] == '\r') { |
| 1915 | len1--; |
| 1916 | skip_at_end++; |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | else { |
| 1921 | if (blk2[len2-1] == '\n') { |
| 1922 | len2--; |
| 1923 | skip_at_end++; |
| 1924 | if (blk2[len2-1] == '\r') { |
| 1925 | len2--; |
| 1926 | skip_at_end++; |
| 1927 | } |
| 1928 | } |
| 1929 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1934 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1935 | if (nblk < 0) /* Connection close. */ |
| 1936 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1937 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1938 | goto connection_empty; |
| 1939 | } |
| 1940 | |
| 1941 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1942 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1943 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1944 | if (nblk < 0) /* Connection close. */ |
| 1945 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1946 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | goto connection_empty; |
| 1948 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1949 | missing_bytes = wanted - socket->b.n; |
| 1950 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1951 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1952 | len1 = missing_bytes; |
| 1953 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1954 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | len = len1; |
| 1958 | |
| 1959 | luaL_addlstring(&socket->b, blk1, len1); |
| 1960 | if (nblk == 2) { |
| 1961 | len += len2; |
| 1962 | luaL_addlstring(&socket->b, blk2, len2); |
| 1963 | } |
| 1964 | |
| 1965 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1966 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | |
| 1968 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1969 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | |
| 1971 | /* If the pattern reclaim to read all the data |
| 1972 | * in the connection, got out. |
| 1973 | */ |
| 1974 | if (wanted == HLSR_READ_ALL) |
| 1975 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1976 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | goto connection_empty; |
| 1978 | |
| 1979 | /* Return result. */ |
| 1980 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1981 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | return 1; |
| 1983 | |
| 1984 | connection_closed: |
| 1985 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1986 | xref_unlock(&socket->xref, peer); |
| 1987 | |
| 1988 | no_peer: |
| 1989 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1990 | /* If the buffer containds data. */ |
| 1991 | if (socket->b.n > 0) { |
| 1992 | luaL_pushresult(&socket->b); |
| 1993 | return 1; |
| 1994 | } |
| 1995 | lua_pushnil(L); |
| 1996 | lua_pushstring(L, "connection closed."); |
| 1997 | return 2; |
| 1998 | |
| 1999 | connection_empty: |
| 2000 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2001 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2002 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2004 | } |
| 2005 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2006 | 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] | 2007 | return 0; |
| 2008 | } |
| 2009 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2010 | /* This Lua function gets two parameters. The first one can be string |
| 2011 | * or a number. If the string is "*l", the user requires one line. If |
| 2012 | * 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] | 2013 | * If the value is a number, the user require a number of bytes equal |
| 2014 | * to the value. The default value is "*l" (a line). |
| 2015 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2016 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2017 | * integer takes this values: |
| 2018 | * -1 : read a line |
| 2019 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2020 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2022 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2023 | * concatenated with the read data. |
| 2024 | */ |
| 2025 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2026 | { |
| 2027 | int wanted = HLSR_READ_LINE; |
| 2028 | const char *pattern; |
| 2029 | int type; |
| 2030 | char *error; |
| 2031 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2032 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2033 | |
| 2034 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2035 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2036 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2037 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2039 | /* Check if we run on the same thread than the xreator thread. |
| 2040 | * We cannot access to the socket if the thread is different. |
| 2041 | */ |
| 2042 | if (socket->tid != tid) |
| 2043 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2044 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | /* check for pattern. */ |
| 2046 | if (lua_gettop(L) >= 2) { |
| 2047 | type = lua_type(L, 2); |
| 2048 | if (type == LUA_TSTRING) { |
| 2049 | pattern = lua_tostring(L, 2); |
| 2050 | if (strcmp(pattern, "*a") == 0) |
| 2051 | wanted = HLSR_READ_ALL; |
| 2052 | else if (strcmp(pattern, "*l") == 0) |
| 2053 | wanted = HLSR_READ_LINE; |
| 2054 | else { |
| 2055 | wanted = strtoll(pattern, &error, 10); |
| 2056 | if (*error != '\0') |
| 2057 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2058 | } |
| 2059 | } |
| 2060 | else if (type == LUA_TNUMBER) { |
| 2061 | wanted = lua_tointeger(L, 2); |
| 2062 | if (wanted < 0) |
| 2063 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | /* Set pattern. */ |
| 2068 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2069 | |
| 2070 | /* Check if we would replace the top by itself. */ |
| 2071 | if (lua_gettop(L) != 2) |
| 2072 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2074 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | luaL_buffinit(L, &socket->b); |
| 2076 | |
| 2077 | /* Check prefix. */ |
| 2078 | if (lua_gettop(L) >= 3) { |
| 2079 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2080 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2081 | pattern = lua_tolstring(L, 3, &len); |
| 2082 | luaL_addlstring(&socket->b, pattern, len); |
| 2083 | } |
| 2084 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2085 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2089 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2091 | 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] | 2092 | { |
| 2093 | struct hlua_socket *socket; |
| 2094 | struct hlua *hlua = hlua_gethlua(L); |
| 2095 | struct appctx *appctx; |
| 2096 | size_t buf_len; |
| 2097 | const char *buf; |
| 2098 | int len; |
| 2099 | int send_len; |
| 2100 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2101 | struct xref *peer; |
| 2102 | struct stream_interface *si; |
| 2103 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2104 | |
| 2105 | /* Check if this lua stack is schedulable. */ |
| 2106 | if (!hlua || !hlua->task) |
| 2107 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2108 | "'frontend', 'backend' or 'task'")); |
| 2109 | |
| 2110 | /* Get object */ |
| 2111 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2112 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2113 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2114 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2115 | /* Check if we run on the same thread than the xreator thread. |
| 2116 | * We cannot access to the socket if the thread is different. |
| 2117 | */ |
| 2118 | if (socket->tid != tid) |
| 2119 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2120 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2121 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2122 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2123 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2124 | lua_pushinteger(L, -1); |
| 2125 | return 1; |
| 2126 | } |
| 2127 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2128 | si = appctx->owner; |
| 2129 | s = si_strm(si); |
| 2130 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2131 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2132 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2133 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | lua_pushinteger(L, -1); |
| 2135 | return 1; |
| 2136 | } |
| 2137 | |
| 2138 | /* Update the input buffer data. */ |
| 2139 | buf += sent; |
| 2140 | send_len = buf_len - sent; |
| 2141 | |
| 2142 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2143 | if (sent >= buf_len) { |
| 2144 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2146 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2147 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2148 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2149 | * the request buffer if its not required. |
| 2150 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2151 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2152 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2153 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2154 | } |
| 2155 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2156 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2157 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2158 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2159 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2160 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | |
| 2162 | /* send data */ |
| 2163 | if (len < send_len) |
| 2164 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2165 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | |
| 2167 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2168 | * the data" (-2) are not expected because the available length |
| 2169 | * is tested. |
| 2170 | * Other unknown error are also not expected. |
| 2171 | */ |
| 2172 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2173 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2174 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2175 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2176 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2178 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2179 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | return 1; |
| 2181 | } |
| 2182 | |
| 2183 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2184 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2185 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2186 | s->req.rex = TICK_ETERNITY; |
| 2187 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | |
| 2189 | /* Update length sent. */ |
| 2190 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2191 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | |
| 2193 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2194 | if (sent + len >= buf_len) { |
| 2195 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2196 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2197 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | |
| 2199 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2200 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2201 | xref_unlock(&socket->xref, peer); |
| 2202 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2203 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2204 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2205 | 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] | 2206 | return 0; |
| 2207 | } |
| 2208 | |
| 2209 | /* This function initiate the send of data. It just check the input |
| 2210 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2211 | * 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] | 2212 | * "hlua_socket_write_yield" that can yield. |
| 2213 | * |
| 2214 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2215 | * the associated object. The second is a string buffer. The third is |
| 2216 | * a facultative integer that represents where is the buffer position |
| 2217 | * of the start of the data that can send. The first byte is the |
| 2218 | * position "1". The default value is "1". The fourth argument is a |
| 2219 | * facultative integer that represents where is the buffer position |
| 2220 | * of the end of the data that can send. The default is the last byte. |
| 2221 | */ |
| 2222 | static int hlua_socket_send(struct lua_State *L) |
| 2223 | { |
| 2224 | int i; |
| 2225 | int j; |
| 2226 | const char *buf; |
| 2227 | size_t buf_len; |
| 2228 | |
| 2229 | /* Check number of arguments. */ |
| 2230 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2231 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2232 | |
| 2233 | /* Get the string. */ |
| 2234 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2235 | |
| 2236 | /* Get and check j. */ |
| 2237 | if (lua_gettop(L) == 4) { |
| 2238 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2239 | if (j < 0) |
| 2240 | j = buf_len + j + 1; |
| 2241 | if (j > buf_len) |
| 2242 | j = buf_len + 1; |
| 2243 | lua_pop(L, 1); |
| 2244 | } |
| 2245 | else |
| 2246 | j = buf_len; |
| 2247 | |
| 2248 | /* Get and check i. */ |
| 2249 | if (lua_gettop(L) == 3) { |
| 2250 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2251 | if (i < 0) |
| 2252 | i = buf_len + i + 1; |
| 2253 | if (i > buf_len) |
| 2254 | i = buf_len + 1; |
| 2255 | lua_pop(L, 1); |
| 2256 | } else |
| 2257 | i = 1; |
| 2258 | |
| 2259 | /* Check bth i and j. */ |
| 2260 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2261 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | return 1; |
| 2263 | } |
| 2264 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2265 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | return 1; |
| 2267 | } |
| 2268 | if (i == 0) |
| 2269 | i = 1; |
| 2270 | if (j == 0) |
| 2271 | j = 1; |
| 2272 | |
| 2273 | /* Pop the string. */ |
| 2274 | lua_pop(L, 1); |
| 2275 | |
| 2276 | /* Update the buffer length. */ |
| 2277 | buf += i - 1; |
| 2278 | buf_len = j - i + 1; |
| 2279 | lua_pushlstring(L, buf, buf_len); |
| 2280 | |
| 2281 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2282 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2284 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | } |
| 2286 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2287 | #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] | 2288 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2289 | { |
| 2290 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2291 | int ret; |
| 2292 | int len; |
| 2293 | char *p; |
| 2294 | |
| 2295 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2296 | if (ret <= 0) { |
| 2297 | lua_pushnil(L); |
| 2298 | return 1; |
| 2299 | } |
| 2300 | |
| 2301 | if (ret == AF_UNIX) { |
| 2302 | lua_pushstring(L, buffer+1); |
| 2303 | return 1; |
| 2304 | } |
| 2305 | else if (ret == AF_INET6) { |
| 2306 | buffer[0] = '['; |
| 2307 | len = strlen(buffer); |
| 2308 | buffer[len] = ']'; |
| 2309 | len++; |
| 2310 | buffer[len] = ':'; |
| 2311 | len++; |
| 2312 | p = buffer; |
| 2313 | } |
| 2314 | else if (ret == AF_INET) { |
| 2315 | p = buffer + 1; |
| 2316 | len = strlen(p); |
| 2317 | p[len] = ':'; |
| 2318 | len++; |
| 2319 | } |
| 2320 | else { |
| 2321 | lua_pushnil(L); |
| 2322 | return 1; |
| 2323 | } |
| 2324 | |
| 2325 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2326 | lua_pushnil(L); |
| 2327 | return 1; |
| 2328 | } |
| 2329 | |
| 2330 | lua_pushstring(L, p); |
| 2331 | return 1; |
| 2332 | } |
| 2333 | |
| 2334 | /* Returns information about the peer of the connection. */ |
| 2335 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2336 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2337 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | struct xref *peer; |
| 2339 | struct appctx *appctx; |
| 2340 | struct stream_interface *si; |
| 2341 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2342 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2343 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2344 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2345 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2346 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2348 | /* Check if we run on the same thread than the xreator thread. |
| 2349 | * We cannot access to the socket if the thread is different. |
| 2350 | */ |
| 2351 | if (socket->tid != tid) |
| 2352 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2353 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2354 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2355 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2356 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2357 | lua_pushnil(L); |
| 2358 | return 1; |
| 2359 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2360 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2361 | si = appctx->owner; |
| 2362 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2364 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2365 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2366 | lua_pushnil(L); |
| 2367 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2370 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2371 | xref_unlock(&socket->xref, peer); |
| 2372 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | /* Returns information about my connection side. */ |
| 2376 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2377 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2378 | struct hlua_socket *socket; |
| 2379 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2380 | struct appctx *appctx; |
| 2381 | struct xref *peer; |
| 2382 | struct stream_interface *si; |
| 2383 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2384 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2385 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2387 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2388 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2390 | /* Check if we run on the same thread than the xreator thread. |
| 2391 | * We cannot access to the socket if the thread is different. |
| 2392 | */ |
| 2393 | if (socket->tid != tid) |
| 2394 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2395 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2396 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2397 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2398 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | lua_pushnil(L); |
| 2400 | return 1; |
| 2401 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2402 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2403 | si = appctx->owner; |
| 2404 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2406 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2407 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2408 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | lua_pushnil(L); |
| 2410 | return 1; |
| 2411 | } |
| 2412 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2413 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2414 | xref_unlock(&socket->xref, peer); |
| 2415 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2419 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2420 | .obj_type = OBJ_TYPE_APPLET, |
| 2421 | .name = "<LUA_TCP>", |
| 2422 | .fct = hlua_socket_handler, |
| 2423 | .release = hlua_socket_release, |
| 2424 | }; |
| 2425 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2426 | __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] | 2427 | { |
| 2428 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2429 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2430 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2432 | struct stream_interface *si; |
| 2433 | struct stream *s; |
| 2434 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2435 | /* Check if we run on the same thread than the xreator thread. |
| 2436 | * We cannot access to the socket if the thread is different. |
| 2437 | */ |
| 2438 | if (socket->tid != tid) |
| 2439 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2440 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2441 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2442 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2443 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2444 | lua_pushnil(L); |
| 2445 | lua_pushstring(L, "Can't connect"); |
| 2446 | return 2; |
| 2447 | } |
| 2448 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2449 | si = appctx->owner; |
| 2450 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2451 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2452 | /* Check if we run on the same thread than the xreator thread. |
| 2453 | * We cannot access to the socket if the thread is different. |
| 2454 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2455 | if (socket->tid != tid) { |
| 2456 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2457 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2458 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2459 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2461 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2462 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2463 | lua_pushnil(L); |
| 2464 | lua_pushstring(L, "Can't connect"); |
| 2465 | return 2; |
| 2466 | } |
| 2467 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2468 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | |
| 2470 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2471 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2472 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2473 | lua_pushinteger(L, 1); |
| 2474 | return 1; |
| 2475 | } |
| 2476 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2477 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2478 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2479 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2480 | } |
| 2481 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2482 | 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] | 2483 | return 0; |
| 2484 | } |
| 2485 | |
| 2486 | /* This function fail or initite the connection. */ |
| 2487 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2488 | { |
| 2489 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2490 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2491 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2492 | struct hlua *hlua; |
| 2493 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2494 | int low, high; |
| 2495 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2496 | struct xref *peer; |
| 2497 | struct stream_interface *si; |
| 2498 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2500 | if (lua_gettop(L) < 2) |
| 2501 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | |
| 2503 | /* Get args. */ |
| 2504 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2505 | |
| 2506 | /* Check if we run on the same thread than the xreator thread. |
| 2507 | * We cannot access to the socket if the thread is different. |
| 2508 | */ |
| 2509 | if (socket->tid != tid) |
| 2510 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2511 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2512 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2513 | if (lua_gettop(L) >= 3) { |
| 2514 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2515 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2517 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2518 | * that are not enclosed within square brackets. |
| 2519 | */ |
| 2520 | if (port > 0) { |
| 2521 | luaL_buffinit(L, &b); |
| 2522 | luaL_addstring(&b, ip); |
| 2523 | luaL_addchar(&b, ':'); |
| 2524 | luaL_pushresult(&b); |
| 2525 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2526 | } |
| 2527 | } |
| 2528 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2529 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2530 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2531 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2532 | lua_pushnil(L); |
| 2533 | return 1; |
| 2534 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2535 | |
| 2536 | /* Parse ip address. */ |
Willy Tarreau | 8095876 | 2020-09-15 10:30:39 +0200 | [diff] [blame] | 2537 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, PA_O_PORT_OK); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2538 | if (!addr) { |
| 2539 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2540 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2541 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2542 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2543 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2544 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2545 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2546 | if (port == -1) { |
| 2547 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2548 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2550 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2551 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2552 | if (port == -1) { |
| 2553 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2554 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2555 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2556 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2557 | } |
| 2558 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2559 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2560 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2561 | si = appctx->owner; |
| 2562 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2563 | |
| 2564 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2565 | xref_unlock(&socket->xref, peer); |
| 2566 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2567 | } |
| 2568 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2569 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2571 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2572 | |
| 2573 | /* inform the stream that we want to be notified whenever the |
| 2574 | * connection completes. |
| 2575 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2576 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2577 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2578 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2579 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2580 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2581 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2582 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2583 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2584 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2585 | } |
| 2586 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2587 | |
| 2588 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2589 | /* Return yield waiting for connection. */ |
| 2590 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2591 | 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] | 2592 | |
| 2593 | return 0; |
| 2594 | } |
| 2595 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2596 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2597 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2598 | { |
| 2599 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2600 | struct xref *peer; |
| 2601 | struct appctx *appctx; |
| 2602 | struct stream_interface *si; |
| 2603 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | |
| 2605 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2606 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2607 | |
| 2608 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2609 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2610 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2611 | lua_pushnil(L); |
| 2612 | return 1; |
| 2613 | } |
| 2614 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2615 | si = appctx->owner; |
| 2616 | s = si_strm(si); |
| 2617 | |
| 2618 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2619 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2621 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2622 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | |
| 2624 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2625 | { |
| 2626 | return 0; |
| 2627 | } |
| 2628 | |
| 2629 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2630 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2631 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2632 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2633 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2634 | struct xref *peer; |
| 2635 | struct appctx *appctx; |
| 2636 | struct stream_interface *si; |
| 2637 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2638 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2639 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2640 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2641 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2642 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2643 | /* convert the timeout to millis */ |
| 2644 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2646 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2647 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2648 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2649 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2650 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2651 | 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] | 2652 | |
| 2653 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2654 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2655 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2656 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2657 | /* Check if we run on the same thread than the xreator thread. |
| 2658 | * We cannot access to the socket if the thread is different. |
| 2659 | */ |
| 2660 | if (socket->tid != tid) |
| 2661 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2662 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2663 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2664 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2665 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2666 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2667 | WILL_LJMP(lua_error(L)); |
| 2668 | return 0; |
| 2669 | } |
| 2670 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2671 | si = appctx->owner; |
| 2672 | s = si_strm(si); |
| 2673 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2674 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2675 | s->req.rto = tmout; |
| 2676 | s->req.wto = tmout; |
| 2677 | s->res.rto = tmout; |
| 2678 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2679 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2680 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2681 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2682 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2683 | |
| 2684 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2685 | task_queue(s->task); |
| 2686 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2687 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2688 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2689 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2690 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2694 | { |
| 2695 | struct hlua_socket *socket; |
| 2696 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2697 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2698 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2699 | |
| 2700 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2701 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | hlua_pusherror(L, "socket: full stack"); |
| 2703 | goto out_fail_conf; |
| 2704 | } |
| 2705 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2706 | /* Create the object: obj[0] = userdata. */ |
| 2707 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2708 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2709 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2711 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2712 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2713 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2714 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2715 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2716 | goto out_fail_conf; |
| 2717 | } |
| 2718 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2719 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2720 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2721 | lua_setmetatable(L, -2); |
| 2722 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2723 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2724 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2725 | if (!appctx) { |
| 2726 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2727 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2728 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2729 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2730 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2731 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2732 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2733 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2734 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2735 | /* Now create a session, task and stream for this applet */ |
| 2736 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2737 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2738 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2739 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2740 | } |
| 2741 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2742 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2743 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2744 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2745 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2746 | } |
| 2747 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2748 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2749 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2750 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2751 | /* Configure "right" stream interface. this "si" is used to connect |
| 2752 | * and retrieve data from the server. The connection is initialized |
| 2753 | * with the "struct server". |
| 2754 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2755 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2756 | |
| 2757 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2758 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2759 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2760 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2761 | return 1; |
| 2762 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2763 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2764 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2765 | out_fail_sess: |
| 2766 | appctx_free(appctx); |
| 2767 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2768 | WILL_LJMP(lua_error(L)); |
| 2769 | return 0; |
| 2770 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2771 | |
| 2772 | /* |
| 2773 | * |
| 2774 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2775 | * Class Channel |
| 2776 | * |
| 2777 | * |
| 2778 | */ |
| 2779 | |
| 2780 | /* Returns the struct hlua_channel join to the class channel in the |
| 2781 | * stack entry "ud" or throws an argument error. |
| 2782 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2783 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2784 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2785 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2786 | } |
| 2787 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2788 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2789 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2790 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2791 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2793 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2794 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2795 | return 0; |
| 2796 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2797 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2798 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2799 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2802 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2803 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2804 | return 1; |
| 2805 | } |
| 2806 | |
| 2807 | /* Duplicate all the data present in the input channel and put it |
| 2808 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2809 | * the stack if the channel is closed and all the data are consumed, |
| 2810 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2811 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2812 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2813 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2814 | { |
| 2815 | char *blk1; |
| 2816 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2817 | size_t len1; |
| 2818 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2819 | int ret; |
| 2820 | luaL_Buffer b; |
| 2821 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2822 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2823 | if (unlikely(ret == 0)) |
| 2824 | return 0; |
| 2825 | |
| 2826 | if (unlikely(ret < 0)) { |
| 2827 | lua_pushnil(L); |
| 2828 | return -1; |
| 2829 | } |
| 2830 | |
| 2831 | luaL_buffinit(L, &b); |
| 2832 | luaL_addlstring(&b, blk1, len1); |
| 2833 | if (unlikely(ret == 2)) |
| 2834 | luaL_addlstring(&b, blk2, len2); |
| 2835 | luaL_pushresult(&b); |
| 2836 | |
| 2837 | if (unlikely(ret == 2)) |
| 2838 | return len1 + len2; |
| 2839 | return len1; |
| 2840 | } |
| 2841 | |
| 2842 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2843 | * a yield. This function keep the data in the buffer. |
| 2844 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2845 | __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] | 2846 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2847 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2848 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2849 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2850 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2851 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2852 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2853 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2854 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2855 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2856 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2857 | 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] | 2858 | return 1; |
| 2859 | } |
| 2860 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2861 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2862 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2863 | { |
| 2864 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2865 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2866 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2867 | } |
| 2868 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2869 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2870 | * a yield. This function consumes the data in the buffer. It returns |
| 2871 | * a string containing the data or a nil pointer if no data are available |
| 2872 | * and the channel is closed. |
| 2873 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2874 | __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] | 2875 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2876 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2877 | int ret; |
| 2878 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2879 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2880 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2881 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2882 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2883 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2884 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2885 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2886 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2887 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2888 | 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] | 2889 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | if (unlikely(ret == -1)) |
| 2891 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2893 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | return 1; |
| 2895 | } |
| 2896 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2897 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2898 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2899 | { |
| 2900 | MAY_LJMP(check_args(L, 1, "get")); |
| 2901 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2902 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2903 | } |
| 2904 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2905 | /* This functions consumes and returns one line. If the channel is closed, |
| 2906 | * 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] | 2907 | * 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] | 2908 | * value. |
| 2909 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2910 | __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] | 2911 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | char *blk1; |
| 2913 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2914 | size_t len1; |
| 2915 | size_t len2; |
| 2916 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2917 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | int ret; |
| 2919 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2920 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2921 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2922 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2923 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2924 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2925 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2926 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2927 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2928 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2929 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2930 | 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] | 2931 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | if (ret == -1) { |
| 2933 | lua_pushnil(L); |
| 2934 | return 1; |
| 2935 | } |
| 2936 | |
| 2937 | luaL_buffinit(L, &b); |
| 2938 | luaL_addlstring(&b, blk1, len1); |
| 2939 | len = len1; |
| 2940 | if (unlikely(ret == 2)) { |
| 2941 | luaL_addlstring(&b, blk2, len2); |
| 2942 | len += len2; |
| 2943 | } |
| 2944 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2945 | 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] | 2946 | return 1; |
| 2947 | } |
| 2948 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2949 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2950 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2951 | { |
| 2952 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2953 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2954 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2955 | } |
| 2956 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2957 | /* This function takes a string as input, and append it at the |
| 2958 | * input side of channel. If the data is too big, but a space |
| 2959 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2960 | * yields. If the data is bigger than the buffer, or if the |
| 2961 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2962 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2963 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2964 | __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] | 2965 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2966 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2967 | size_t len; |
| 2968 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2969 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2970 | int ret; |
| 2971 | int max; |
| 2972 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2973 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2974 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2975 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2976 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2977 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2978 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2979 | * the request buffer if its not required. |
| 2980 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2981 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2982 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2983 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2984 | } |
| 2985 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2986 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2987 | if (max > len - l) |
| 2988 | max = len - l; |
| 2989 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2990 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | if (ret == -2 || ret == -3) { |
| 2992 | lua_pushinteger(L, -1); |
| 2993 | return 1; |
| 2994 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2995 | if (ret == -1) { |
| 2996 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2997 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2998 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | l += ret; |
| 3000 | lua_pop(L, 1); |
| 3001 | lua_pushinteger(L, l); |
| 3002 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3003 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3004 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3005 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3007 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3008 | */ |
| 3009 | return 1; |
| 3010 | } |
| 3011 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3012 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | return 1; |
| 3014 | } |
| 3015 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3016 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3017 | * of the written string, or -1 if the channel is closed or if the |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | * buffer size is too little for the data. |
| 3019 | */ |
| 3020 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3021 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3022 | size_t len; |
| 3023 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3025 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3026 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3027 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3028 | lua_pushinteger(L, 0); |
| 3029 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3030 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | } |
| 3032 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3033 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3035 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3036 | * or -1 if the channel is closed or if the buffer size is too |
| 3037 | * little for the data. |
| 3038 | */ |
| 3039 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3040 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3041 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3042 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3043 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3044 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3045 | lua_pushinteger(L, 0); |
| 3046 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3047 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3048 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3049 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3050 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3051 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3052 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3054 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3055 | } |
| 3056 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3057 | /* Append data in the output side of the buffer. This data is immediately |
| 3058 | * sent. The function returns the amount of data written. If the buffer |
| 3059 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | * if the channel is closed. |
| 3061 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3062 | __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] | 3063 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3064 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | size_t len; |
| 3066 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3067 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3068 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3069 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3070 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3071 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3072 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3073 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3074 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3075 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3076 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3077 | lua_pushinteger(L, -1); |
| 3078 | return 1; |
| 3079 | } |
| 3080 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3081 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3082 | * the request buffer if its not required. |
| 3083 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3084 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3085 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3086 | 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] | 3087 | } |
| 3088 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3089 | /* The written data will be immediately sent, so we can check |
| 3090 | * the available space without taking in account the reserve. |
| 3091 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3092 | * data, because the buffer will be flushed. |
| 3093 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3094 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3095 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3096 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3097 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3098 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3099 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3100 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3101 | return 1; |
| 3102 | |
| 3103 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3104 | if (max > len - l) |
| 3105 | max = len - l; |
| 3106 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3107 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3108 | * detects a non contiguous buffer and realign it. |
| 3109 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3110 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3111 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3112 | |
| 3113 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3114 | 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] | 3115 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | /* buffer replace considers that the input part is filled. |
| 3117 | * so, I must forward these new data in the output part. |
| 3118 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3119 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3120 | |
| 3121 | l += max; |
| 3122 | lua_pop(L, 1); |
| 3123 | lua_pushinteger(L, l); |
| 3124 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3125 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3126 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3127 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3128 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3129 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3130 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3131 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3132 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3133 | if (l < len) { |
| 3134 | /* If we are waiting for space in the response buffer, we |
| 3135 | * must set the flag WAKERESWR. This flag required the task |
| 3136 | * wake up if any activity is detected on the response buffer. |
| 3137 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3138 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3139 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3140 | else |
| 3141 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3142 | 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] | 3143 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3144 | |
| 3145 | return 1; |
| 3146 | } |
| 3147 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3148 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3149 | * yield the LUA process, and resume it without checking the |
| 3150 | * input arguments. |
| 3151 | */ |
| 3152 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3153 | { |
| 3154 | MAY_LJMP(check_args(L, 2, "send")); |
| 3155 | lua_pushinteger(L, 0); |
| 3156 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3157 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | /* This function forward and amount of butes. The data pass from |
| 3161 | * the input side of the buffer to the output side, and can be |
| 3162 | * forwarded. This function never fails. |
| 3163 | * |
| 3164 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3165 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3166 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3167 | __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] | 3168 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3169 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | int len; |
| 3171 | int l; |
| 3172 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3173 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | |
| 3175 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3176 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3177 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3178 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3179 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3180 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3181 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3182 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3183 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3184 | |
| 3185 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3186 | if (max > ci_data(chn)) |
| 3187 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3188 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | l += max; |
| 3190 | |
| 3191 | lua_pop(L, 1); |
| 3192 | lua_pushinteger(L, l); |
| 3193 | |
| 3194 | /* Check if it miss bytes to forward. */ |
| 3195 | if (l < len) { |
| 3196 | /* The the input channel or the output channel are closed, we |
| 3197 | * must return the amount of data forwarded. |
| 3198 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3199 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3200 | return 1; |
| 3201 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3202 | /* If we are waiting for space data in the response buffer, we |
| 3203 | * must set the flag WAKERESWR. This flag required the task |
| 3204 | * wake up if any activity is detected on the response buffer. |
| 3205 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3206 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3207 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3208 | else |
| 3209 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3210 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3211 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3212 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | return 1; |
| 3216 | } |
| 3217 | |
| 3218 | /* Just check the input and prepare the stack for the previous |
| 3219 | * function "hlua_channel_forward_yield" |
| 3220 | */ |
| 3221 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3222 | { |
| 3223 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3224 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3225 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3226 | |
| 3227 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3228 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | /* Just returns the number of bytes available in the input |
| 3232 | * side of the buffer. This function never fails. |
| 3233 | */ |
| 3234 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3235 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3236 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3237 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3238 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3239 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3240 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3241 | struct htx *htx = htxbuf(&chn->buf); |
| 3242 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3243 | } |
| 3244 | else |
| 3245 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3246 | return 1; |
| 3247 | } |
| 3248 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3249 | /* Returns true if the channel is full. */ |
| 3250 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3251 | { |
| 3252 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3253 | |
| 3254 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3255 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3256 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3257 | * applet). |
| 3258 | */ |
| 3259 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3260 | return 1; |
| 3261 | } |
| 3262 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3263 | /* Returns true if the channel is the response channel. */ |
| 3264 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3265 | { |
| 3266 | struct channel *chn; |
| 3267 | |
| 3268 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3269 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3270 | |
| 3271 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3272 | return 1; |
| 3273 | } |
| 3274 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3275 | /* Just returns the number of bytes available in the output |
| 3276 | * side of the buffer. This function never fails. |
| 3277 | */ |
| 3278 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3279 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3280 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3281 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3282 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3283 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3284 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3285 | return 1; |
| 3286 | } |
| 3287 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3288 | /* |
| 3289 | * |
| 3290 | * |
| 3291 | * Class Fetches |
| 3292 | * |
| 3293 | * |
| 3294 | */ |
| 3295 | |
| 3296 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3297 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3298 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3299 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3300 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3301 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | /* This function creates and push in the stack a fetch object according |
| 3305 | * with a current TXN. |
| 3306 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3307 | 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] | 3308 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3309 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3310 | |
| 3311 | /* Check stack size. */ |
| 3312 | if (!lua_checkstack(L, 3)) |
| 3313 | return 0; |
| 3314 | |
| 3315 | /* Create the object: obj[0] = userdata. |
| 3316 | * Note that the base of the Fetches object is the |
| 3317 | * transaction object. |
| 3318 | */ |
| 3319 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3320 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3321 | lua_rawseti(L, -2, 0); |
| 3322 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3323 | hsmp->s = txn->s; |
| 3324 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3325 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3326 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3327 | |
| 3328 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3329 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3330 | lua_setmetatable(L, -2); |
| 3331 | |
| 3332 | return 1; |
| 3333 | } |
| 3334 | |
| 3335 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3336 | * It uses closure argument to store the associated sample-fetch. It |
| 3337 | * returns only one argument or throws an error. An error is thrown |
| 3338 | * only if an error is encountered during the argument parsing. If |
| 3339 | * the "sample-fetch" function fails, nil is returned. |
| 3340 | */ |
| 3341 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3342 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3343 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3344 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3345 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3346 | int i; |
| 3347 | struct sample smp; |
| 3348 | |
| 3349 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3350 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3351 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3352 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3353 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3354 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3355 | /* Check execution authorization. */ |
| 3356 | if (f->use & SMP_USE_HTTP_ANY && |
| 3357 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3358 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3359 | "is not available in Lua services", f->kw); |
| 3360 | WILL_LJMP(lua_error(L)); |
| 3361 | } |
| 3362 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3363 | /* Get extra arguments. */ |
| 3364 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3365 | if (i >= ARGM_NBARGS) |
| 3366 | break; |
| 3367 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3368 | } |
| 3369 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3370 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3371 | |
| 3372 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3373 | 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] | 3374 | |
| 3375 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3376 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3377 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3378 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | /* Initialise the sample. */ |
| 3382 | memset(&smp, 0, sizeof(smp)); |
| 3383 | |
| 3384 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3385 | 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] | 3386 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3387 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3388 | lua_pushstring(L, ""); |
| 3389 | else |
| 3390 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3391 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3395 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3396 | hlua_smp2lua_str(L, &smp); |
| 3397 | else |
| 3398 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3399 | |
| 3400 | end: |
| 3401 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3402 | if (args[i].type == ARGT_STR) |
| 3403 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3404 | else if (args[i].type == ARGT_REG) |
| 3405 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3406 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3407 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3408 | |
| 3409 | error: |
| 3410 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3411 | if (args[i].type == ARGT_STR) |
| 3412 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3413 | else if (args[i].type == ARGT_REG) |
| 3414 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3415 | } |
| 3416 | WILL_LJMP(lua_error(L)); |
| 3417 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3418 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3419 | |
| 3420 | /* |
| 3421 | * |
| 3422 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3423 | * Class Converters |
| 3424 | * |
| 3425 | * |
| 3426 | */ |
| 3427 | |
| 3428 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3429 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3430 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3431 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3432 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3433 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | /* This function creates and push in the stack a Converters object |
| 3437 | * according with a current TXN. |
| 3438 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3439 | 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] | 3440 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3441 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3442 | |
| 3443 | /* Check stack size. */ |
| 3444 | if (!lua_checkstack(L, 3)) |
| 3445 | return 0; |
| 3446 | |
| 3447 | /* Create the object: obj[0] = userdata. |
| 3448 | * Note that the base of the Converters object is the |
| 3449 | * same than the TXN object. |
| 3450 | */ |
| 3451 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3452 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3453 | lua_rawseti(L, -2, 0); |
| 3454 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3455 | hsmp->s = txn->s; |
| 3456 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3457 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3458 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3459 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3460 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3461 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3462 | lua_setmetatable(L, -2); |
| 3463 | |
| 3464 | return 1; |
| 3465 | } |
| 3466 | |
| 3467 | /* This function is an LUA binding. It is called with each converter. |
| 3468 | * It uses closure argument to store the associated converter. It |
| 3469 | * returns only one argument or throws an error. An error is thrown |
| 3470 | * only if an error is encountered during the argument parsing. If |
| 3471 | * the converter function function fails, nil is returned. |
| 3472 | */ |
| 3473 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3474 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3475 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3476 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3477 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3478 | int i; |
| 3479 | struct sample smp; |
| 3480 | |
| 3481 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3482 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3483 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3484 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3485 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3486 | |
| 3487 | /* Get extra arguments. */ |
| 3488 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3489 | if (i >= ARGM_NBARGS) |
| 3490 | break; |
| 3491 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3492 | } |
| 3493 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3494 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3495 | |
| 3496 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3497 | 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] | 3498 | |
| 3499 | /* Run the special args checker. */ |
| 3500 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3501 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3502 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | /* Initialise the sample. */ |
| 3506 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3507 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3508 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3509 | } |
| 3510 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3511 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3512 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3513 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3514 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3515 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3516 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3517 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3518 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3519 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3520 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3521 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3522 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3526 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3527 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3528 | lua_pushstring(L, ""); |
| 3529 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3530 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3531 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3535 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3536 | hlua_smp2lua_str(L, &smp); |
| 3537 | else |
| 3538 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3539 | end: |
| 3540 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3541 | if (args[i].type == ARGT_STR) |
| 3542 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3543 | else if (args[i].type == ARGT_REG) |
| 3544 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3545 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3546 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3547 | |
| 3548 | error: |
| 3549 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3550 | if (args[i].type == ARGT_STR) |
| 3551 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3552 | else if (args[i].type == ARGT_REG) |
| 3553 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3554 | } |
| 3555 | WILL_LJMP(lua_error(L)); |
| 3556 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3557 | } |
| 3558 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3559 | /* |
| 3560 | * |
| 3561 | * |
| 3562 | * Class AppletTCP |
| 3563 | * |
| 3564 | * |
| 3565 | */ |
| 3566 | |
| 3567 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3568 | * a class stream, otherwise it throws an error. |
| 3569 | */ |
| 3570 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3571 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3572 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | /* This function creates and push in the stack an Applet object |
| 3576 | * according with a current TXN. |
| 3577 | */ |
| 3578 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3579 | { |
| 3580 | struct hlua_appctx *appctx; |
| 3581 | struct stream_interface *si = ctx->owner; |
| 3582 | struct stream *s = si_strm(si); |
| 3583 | struct proxy *p = s->be; |
| 3584 | |
| 3585 | /* Check stack size. */ |
| 3586 | if (!lua_checkstack(L, 3)) |
| 3587 | return 0; |
| 3588 | |
| 3589 | /* Create the object: obj[0] = userdata. |
| 3590 | * Note that the base of the Converters object is the |
| 3591 | * same than the TXN object. |
| 3592 | */ |
| 3593 | lua_newtable(L); |
| 3594 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3595 | lua_rawseti(L, -2, 0); |
| 3596 | appctx->appctx = ctx; |
| 3597 | appctx->htxn.s = s; |
| 3598 | appctx->htxn.p = p; |
| 3599 | |
| 3600 | /* Create the "f" field that contains a list of fetches. */ |
| 3601 | lua_pushstring(L, "f"); |
| 3602 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3603 | return 0; |
| 3604 | lua_settable(L, -3); |
| 3605 | |
| 3606 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3607 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3608 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3609 | return 0; |
| 3610 | lua_settable(L, -3); |
| 3611 | |
| 3612 | /* Create the "c" field that contains a list of converters. */ |
| 3613 | lua_pushstring(L, "c"); |
| 3614 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3615 | return 0; |
| 3616 | lua_settable(L, -3); |
| 3617 | |
| 3618 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3619 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3620 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3621 | return 0; |
| 3622 | lua_settable(L, -3); |
| 3623 | |
| 3624 | /* Pop a class stream metatable and affect it to the table. */ |
| 3625 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3626 | lua_setmetatable(L, -2); |
| 3627 | |
| 3628 | return 1; |
| 3629 | } |
| 3630 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3631 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3632 | { |
| 3633 | struct hlua_appctx *appctx; |
| 3634 | struct stream *s; |
| 3635 | const char *name; |
| 3636 | size_t len; |
| 3637 | struct sample smp; |
| 3638 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3639 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3640 | 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] | 3641 | |
| 3642 | /* It is useles to retrieve the stream, but this function |
| 3643 | * runs only in a stream context. |
| 3644 | */ |
| 3645 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3646 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3647 | s = appctx->htxn.s; |
| 3648 | |
| 3649 | /* Converts the third argument in a sample. */ |
| 3650 | hlua_lua2smp(L, 3, &smp); |
| 3651 | |
| 3652 | /* Store the sample in a variable. */ |
| 3653 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3654 | |
| 3655 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3656 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3657 | else |
| 3658 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3659 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3660 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3664 | { |
| 3665 | struct hlua_appctx *appctx; |
| 3666 | struct stream *s; |
| 3667 | const char *name; |
| 3668 | size_t len; |
| 3669 | struct sample smp; |
| 3670 | |
| 3671 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3672 | |
| 3673 | /* It is useles to retrieve the stream, but this function |
| 3674 | * runs only in a stream context. |
| 3675 | */ |
| 3676 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3677 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3678 | s = appctx->htxn.s; |
| 3679 | |
| 3680 | /* Unset the variable. */ |
| 3681 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3682 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3683 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3687 | { |
| 3688 | struct hlua_appctx *appctx; |
| 3689 | struct stream *s; |
| 3690 | const char *name; |
| 3691 | size_t len; |
| 3692 | struct sample smp; |
| 3693 | |
| 3694 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3695 | |
| 3696 | /* It is useles to retrieve the stream, but this function |
| 3697 | * runs only in a stream context. |
| 3698 | */ |
| 3699 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3700 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3701 | s = appctx->htxn.s; |
| 3702 | |
| 3703 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3704 | if (!vars_get_by_name(name, len, &smp)) { |
| 3705 | lua_pushnil(L); |
| 3706 | return 1; |
| 3707 | } |
| 3708 | |
| 3709 | return hlua_smp2lua(L, &smp); |
| 3710 | } |
| 3711 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3712 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3713 | { |
| 3714 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3715 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3716 | struct hlua *hlua; |
| 3717 | |
| 3718 | /* 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] | 3719 | if (!s->hlua) |
| 3720 | return 0; |
| 3721 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3722 | |
| 3723 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3724 | |
| 3725 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3726 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3727 | |
| 3728 | /* Get and store new value. */ |
| 3729 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3730 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3731 | |
| 3732 | return 0; |
| 3733 | } |
| 3734 | |
| 3735 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3736 | { |
| 3737 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3738 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3739 | struct hlua *hlua; |
| 3740 | |
| 3741 | /* 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] | 3742 | if (!s->hlua) { |
| 3743 | lua_pushnil(L); |
| 3744 | return 1; |
| 3745 | } |
| 3746 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3747 | |
| 3748 | /* Push configuration index in the stack. */ |
| 3749 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3750 | |
| 3751 | return 1; |
| 3752 | } |
| 3753 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3754 | /* If expected data not yet available, it returns a yield. This function |
| 3755 | * consumes the data in the buffer. It returns a string containing the |
| 3756 | * data. This string can be empty. |
| 3757 | */ |
| 3758 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3759 | { |
| 3760 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3761 | struct stream_interface *si = appctx->appctx->owner; |
| 3762 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3763 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3764 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3765 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3766 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3767 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3768 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3769 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3770 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3771 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3772 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3773 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3774 | 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] | 3775 | } |
| 3776 | |
| 3777 | /* End of data: commit the total strings and return. */ |
| 3778 | if (ret < 0) { |
| 3779 | luaL_pushresult(&appctx->b); |
| 3780 | return 1; |
| 3781 | } |
| 3782 | |
| 3783 | /* Ensure that the block 2 length is usable. */ |
| 3784 | if (ret == 1) |
| 3785 | len2 = 0; |
| 3786 | |
| 3787 | /* dont check the max length read and dont check. */ |
| 3788 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3789 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3790 | |
| 3791 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3792 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3793 | luaL_pushresult(&appctx->b); |
| 3794 | return 1; |
| 3795 | } |
| 3796 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3797 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3798 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3799 | { |
| 3800 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3801 | |
| 3802 | /* Initialise the string catenation. */ |
| 3803 | luaL_buffinit(L, &appctx->b); |
| 3804 | |
| 3805 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3806 | } |
| 3807 | |
| 3808 | /* If expected data not yet available, it returns a yield. This function |
| 3809 | * consumes the data in the buffer. It returns a string containing the |
| 3810 | * data. This string can be empty. |
| 3811 | */ |
| 3812 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3813 | { |
| 3814 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3815 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3816 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3817 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3818 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3819 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3820 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3821 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3822 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3823 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3824 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3825 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3826 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3827 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3828 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3829 | 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] | 3830 | } |
| 3831 | |
| 3832 | /* End of data: commit the total strings and return. */ |
| 3833 | if (ret < 0) { |
| 3834 | luaL_pushresult(&appctx->b); |
| 3835 | return 1; |
| 3836 | } |
| 3837 | |
| 3838 | /* Ensure that the block 2 length is usable. */ |
| 3839 | if (ret == 1) |
| 3840 | len2 = 0; |
| 3841 | |
| 3842 | if (len == -1) { |
| 3843 | |
| 3844 | /* If len == -1, catenate all the data avalaile and |
| 3845 | * yield because we want to get all the data until |
| 3846 | * the end of data stream. |
| 3847 | */ |
| 3848 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3849 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3850 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3851 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3852 | 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] | 3853 | |
| 3854 | } else { |
| 3855 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3856 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3857 | if (len1 > len) |
| 3858 | len1 = len; |
| 3859 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3860 | len -= len1; |
| 3861 | |
| 3862 | /* Copy the second block. */ |
| 3863 | if (len2 > len) |
| 3864 | len2 = len; |
| 3865 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3866 | len -= len2; |
| 3867 | |
| 3868 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3869 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3870 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3871 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3872 | if (len > 0) { |
| 3873 | lua_pushinteger(L, len); |
| 3874 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3875 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3876 | 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] | 3877 | } |
| 3878 | |
| 3879 | /* return the result. */ |
| 3880 | luaL_pushresult(&appctx->b); |
| 3881 | return 1; |
| 3882 | } |
| 3883 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3884 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3885 | hlua_pusherror(L, "Lua: internal error"); |
| 3886 | WILL_LJMP(lua_error(L)); |
| 3887 | return 0; |
| 3888 | } |
| 3889 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3890 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3891 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3892 | { |
| 3893 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3894 | int len = -1; |
| 3895 | |
| 3896 | if (lua_gettop(L) > 2) |
| 3897 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3898 | if (lua_gettop(L) >= 2) { |
| 3899 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3900 | lua_pop(L, 1); |
| 3901 | } |
| 3902 | |
| 3903 | /* Confirm or set the required length */ |
| 3904 | lua_pushinteger(L, len); |
| 3905 | |
| 3906 | /* Initialise the string catenation. */ |
| 3907 | luaL_buffinit(L, &appctx->b); |
| 3908 | |
| 3909 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3910 | } |
| 3911 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3912 | /* Append data in the output side of the buffer. This data is immediately |
| 3913 | * sent. The function returns the amount of data written. If the buffer |
| 3914 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3915 | * if the channel is closed. |
| 3916 | */ |
| 3917 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3918 | { |
| 3919 | size_t len; |
| 3920 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3921 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3922 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3923 | struct stream_interface *si = appctx->appctx->owner; |
| 3924 | struct channel *chn = si_ic(si); |
| 3925 | int max; |
| 3926 | |
| 3927 | /* Get the max amount of data which can write as input in the channel. */ |
| 3928 | max = channel_recv_max(chn); |
| 3929 | if (max > (len - l)) |
| 3930 | max = len - l; |
| 3931 | |
| 3932 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3933 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3934 | |
| 3935 | /* update counters. */ |
| 3936 | l += max; |
| 3937 | lua_pop(L, 1); |
| 3938 | lua_pushinteger(L, l); |
| 3939 | |
| 3940 | /* If some data is not send, declares the situation to the |
| 3941 | * applet, and returns a yield. |
| 3942 | */ |
| 3943 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3944 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3945 | 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] | 3946 | } |
| 3947 | |
| 3948 | return 1; |
| 3949 | } |
| 3950 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3951 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3952 | * yield the LUA process, and resume it without checking the |
| 3953 | * input arguments. |
| 3954 | */ |
| 3955 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3956 | { |
| 3957 | MAY_LJMP(check_args(L, 2, "send")); |
| 3958 | lua_pushinteger(L, 0); |
| 3959 | |
| 3960 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3961 | } |
| 3962 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3963 | /* |
| 3964 | * |
| 3965 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3966 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3967 | * |
| 3968 | * |
| 3969 | */ |
| 3970 | |
| 3971 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3972 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3973 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3974 | __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] | 3975 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3976 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3977 | } |
| 3978 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3979 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3980 | * according with a current TXN. |
| 3981 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3982 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3983 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3984 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3985 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3986 | struct stream_interface *si = ctx->owner; |
| 3987 | struct stream *s = si_strm(si); |
| 3988 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3989 | struct htx *htx; |
| 3990 | struct htx_blk *blk; |
| 3991 | struct htx_sl *sl; |
| 3992 | struct ist path; |
| 3993 | unsigned long long len = 0; |
| 3994 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3995 | |
| 3996 | /* Check stack size. */ |
| 3997 | if (!lua_checkstack(L, 3)) |
| 3998 | return 0; |
| 3999 | |
| 4000 | /* Create the object: obj[0] = userdata. |
| 4001 | * Note that the base of the Converters object is the |
| 4002 | * same than the TXN object. |
| 4003 | */ |
| 4004 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4005 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4006 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4007 | appctx->appctx = ctx; |
| 4008 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4009 | 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] | 4010 | appctx->htxn.s = s; |
| 4011 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4012 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4013 | /* Create the "f" field that contains a list of fetches. */ |
| 4014 | lua_pushstring(L, "f"); |
| 4015 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4016 | return 0; |
| 4017 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4018 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4019 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4020 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4021 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4022 | return 0; |
| 4023 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4024 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4025 | /* Create the "c" field that contains a list of converters. */ |
| 4026 | lua_pushstring(L, "c"); |
| 4027 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4028 | return 0; |
| 4029 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4030 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4031 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4032 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4033 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4034 | return 0; |
| 4035 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4036 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4037 | htx = htxbuf(&s->req.buf); |
| 4038 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4039 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4040 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4041 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4042 | /* Stores the request method. */ |
| 4043 | lua_pushstring(L, "method"); |
| 4044 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4045 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4046 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4047 | /* Stores the http version. */ |
| 4048 | lua_pushstring(L, "version"); |
| 4049 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4050 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4051 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4052 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4053 | * the array on the top of the stack. |
| 4054 | */ |
| 4055 | lua_pushstring(L, "headers"); |
| 4056 | htxn.s = s; |
| 4057 | htxn.p = px; |
| 4058 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4059 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4060 | return 0; |
| 4061 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4062 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4063 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4064 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4065 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4066 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4067 | p = path.ptr; |
| 4068 | end = path.ptr + path.len; |
| 4069 | q = p; |
| 4070 | while (q < end && *q != '?') |
| 4071 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4072 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4073 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4074 | lua_pushstring(L, "path"); |
| 4075 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4076 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4077 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4078 | /* Stores the query string. */ |
| 4079 | lua_pushstring(L, "qs"); |
| 4080 | if (*q == '?') |
| 4081 | q++; |
| 4082 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4083 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4084 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4085 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4086 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4087 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4088 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4089 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4090 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 4091 | break; |
| 4092 | if (type == HTX_BLK_DATA) |
| 4093 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4094 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4095 | if (htx->extra != ULLONG_MAX) |
| 4096 | len += htx->extra; |
| 4097 | |
| 4098 | /* Stores the request path. */ |
| 4099 | lua_pushstring(L, "length"); |
| 4100 | lua_pushinteger(L, len); |
| 4101 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4102 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4103 | /* Create an empty array of HTTP request headers. */ |
| 4104 | lua_pushstring(L, "response"); |
| 4105 | lua_newtable(L); |
| 4106 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4107 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4108 | /* Pop a class stream metatable and affect it to the table. */ |
| 4109 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4110 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4111 | |
| 4112 | return 1; |
| 4113 | } |
| 4114 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4115 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4116 | { |
| 4117 | struct hlua_appctx *appctx; |
| 4118 | struct stream *s; |
| 4119 | const char *name; |
| 4120 | size_t len; |
| 4121 | struct sample smp; |
| 4122 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4123 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4124 | 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] | 4125 | |
| 4126 | /* It is useles to retrieve the stream, but this function |
| 4127 | * runs only in a stream context. |
| 4128 | */ |
| 4129 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4130 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4131 | s = appctx->htxn.s; |
| 4132 | |
| 4133 | /* Converts the third argument in a sample. */ |
| 4134 | hlua_lua2smp(L, 3, &smp); |
| 4135 | |
| 4136 | /* Store the sample in a variable. */ |
| 4137 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4138 | |
| 4139 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4140 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4141 | else |
| 4142 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4143 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4144 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4148 | { |
| 4149 | struct hlua_appctx *appctx; |
| 4150 | struct stream *s; |
| 4151 | const char *name; |
| 4152 | size_t len; |
| 4153 | struct sample smp; |
| 4154 | |
| 4155 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4156 | |
| 4157 | /* It is useles to retrieve the stream, but this function |
| 4158 | * runs only in a stream context. |
| 4159 | */ |
| 4160 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4161 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4162 | s = appctx->htxn.s; |
| 4163 | |
| 4164 | /* Unset the variable. */ |
| 4165 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4166 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4167 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4171 | { |
| 4172 | struct hlua_appctx *appctx; |
| 4173 | struct stream *s; |
| 4174 | const char *name; |
| 4175 | size_t len; |
| 4176 | struct sample smp; |
| 4177 | |
| 4178 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4179 | |
| 4180 | /* It is useles to retrieve the stream, but this function |
| 4181 | * runs only in a stream context. |
| 4182 | */ |
| 4183 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4184 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4185 | s = appctx->htxn.s; |
| 4186 | |
| 4187 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4188 | if (!vars_get_by_name(name, len, &smp)) { |
| 4189 | lua_pushnil(L); |
| 4190 | return 1; |
| 4191 | } |
| 4192 | |
| 4193 | return hlua_smp2lua(L, &smp); |
| 4194 | } |
| 4195 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4196 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4197 | { |
| 4198 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4199 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4200 | struct hlua *hlua; |
| 4201 | |
| 4202 | /* 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] | 4203 | if (!s->hlua) |
| 4204 | return 0; |
| 4205 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4206 | |
| 4207 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4208 | |
| 4209 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4210 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4211 | |
| 4212 | /* Get and store new value. */ |
| 4213 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4214 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4215 | |
| 4216 | return 0; |
| 4217 | } |
| 4218 | |
| 4219 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4220 | { |
| 4221 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4222 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4223 | struct hlua *hlua; |
| 4224 | |
| 4225 | /* 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] | 4226 | if (!s->hlua) { |
| 4227 | lua_pushnil(L); |
| 4228 | return 1; |
| 4229 | } |
| 4230 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4231 | |
| 4232 | /* Push configuration index in the stack. */ |
| 4233 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4234 | |
| 4235 | return 1; |
| 4236 | } |
| 4237 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4238 | /* If expected data not yet available, it returns a yield. This function |
| 4239 | * consumes the data in the buffer. It returns a string containing the |
| 4240 | * data. This string can be empty. |
| 4241 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4242 | __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] | 4243 | { |
| 4244 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4245 | struct stream_interface *si = appctx->appctx->owner; |
| 4246 | struct channel *req = si_oc(si); |
| 4247 | struct htx *htx; |
| 4248 | struct htx_blk *blk; |
| 4249 | size_t count; |
| 4250 | int stop = 0; |
| 4251 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4252 | htx = htx_from_buf(&req->buf); |
| 4253 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4254 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4255 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4256 | while (count && !stop && blk) { |
| 4257 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4258 | uint32_t sz = htx_get_blksz(blk); |
| 4259 | struct ist v; |
| 4260 | uint32_t vlen; |
| 4261 | char *nl; |
| 4262 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4263 | if (type == HTX_BLK_EOM) { |
| 4264 | stop = 1; |
| 4265 | break; |
| 4266 | } |
| 4267 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4268 | vlen = sz; |
| 4269 | if (vlen > count) { |
| 4270 | if (type != HTX_BLK_DATA) |
| 4271 | break; |
| 4272 | vlen = count; |
| 4273 | } |
| 4274 | |
| 4275 | switch (type) { |
| 4276 | case HTX_BLK_UNUSED: |
| 4277 | break; |
| 4278 | |
| 4279 | case HTX_BLK_DATA: |
| 4280 | v = htx_get_blk_value(htx, blk); |
| 4281 | v.len = vlen; |
| 4282 | nl = istchr(v, '\n'); |
| 4283 | if (nl != NULL) { |
| 4284 | stop = 1; |
| 4285 | vlen = nl - v.ptr + 1; |
| 4286 | } |
| 4287 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4288 | break; |
| 4289 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4290 | case HTX_BLK_TLR: |
| 4291 | case HTX_BLK_EOM: |
| 4292 | stop = 1; |
| 4293 | break; |
| 4294 | |
| 4295 | default: |
| 4296 | break; |
| 4297 | } |
| 4298 | |
| 4299 | co_set_data(req, co_data(req) - vlen); |
| 4300 | count -= vlen; |
| 4301 | if (sz == vlen) |
| 4302 | blk = htx_remove_blk(htx, blk); |
| 4303 | else { |
| 4304 | htx_cut_data_blk(htx, blk, vlen); |
| 4305 | break; |
| 4306 | } |
| 4307 | } |
| 4308 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4309 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4310 | if (!stop) { |
| 4311 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4312 | 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] | 4313 | } |
| 4314 | |
| 4315 | /* return the result. */ |
| 4316 | luaL_pushresult(&appctx->b); |
| 4317 | return 1; |
| 4318 | } |
| 4319 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4320 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4321 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4322 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4323 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4324 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
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 | /* Initialise the string catenation. */ |
| 4327 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4328 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4329 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4330 | } |
| 4331 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4332 | /* If expected data not yet available, it returns a yield. This function |
| 4333 | * consumes the data in the buffer. It returns a string containing the |
| 4334 | * data. This string can be empty. |
| 4335 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4336 | __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] | 4337 | { |
| 4338 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4339 | struct stream_interface *si = appctx->appctx->owner; |
| 4340 | struct channel *req = si_oc(si); |
| 4341 | struct htx *htx; |
| 4342 | struct htx_blk *blk; |
| 4343 | size_t count; |
| 4344 | int len; |
| 4345 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4346 | htx = htx_from_buf(&req->buf); |
| 4347 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4348 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4349 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4350 | while (count && len && blk) { |
| 4351 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4352 | uint32_t sz = htx_get_blksz(blk); |
| 4353 | struct ist v; |
| 4354 | uint32_t vlen; |
| 4355 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4356 | if (type == HTX_BLK_EOM) { |
| 4357 | len = 0; |
| 4358 | break; |
| 4359 | } |
| 4360 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4361 | vlen = sz; |
| 4362 | if (len > 0 && vlen > len) |
| 4363 | vlen = len; |
| 4364 | if (vlen > count) { |
| 4365 | if (type != HTX_BLK_DATA) |
| 4366 | break; |
| 4367 | vlen = count; |
| 4368 | } |
| 4369 | |
| 4370 | switch (type) { |
| 4371 | case HTX_BLK_UNUSED: |
| 4372 | break; |
| 4373 | |
| 4374 | case HTX_BLK_DATA: |
| 4375 | v = htx_get_blk_value(htx, blk); |
| 4376 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4377 | break; |
| 4378 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4379 | case HTX_BLK_TLR: |
| 4380 | case HTX_BLK_EOM: |
| 4381 | len = 0; |
| 4382 | break; |
| 4383 | |
| 4384 | default: |
| 4385 | break; |
| 4386 | } |
| 4387 | |
| 4388 | co_set_data(req, co_data(req) - vlen); |
| 4389 | count -= vlen; |
| 4390 | if (len > 0) |
| 4391 | len -= vlen; |
| 4392 | if (sz == vlen) |
| 4393 | blk = htx_remove_blk(htx, blk); |
| 4394 | else { |
| 4395 | htx_cut_data_blk(htx, blk, vlen); |
| 4396 | break; |
| 4397 | } |
| 4398 | } |
| 4399 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4400 | htx_to_buf(htx, &req->buf); |
| 4401 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4402 | /* If we are no other data available, yield waiting for new data. */ |
| 4403 | if (len) { |
| 4404 | if (len > 0) { |
| 4405 | lua_pushinteger(L, len); |
| 4406 | lua_replace(L, 2); |
| 4407 | } |
| 4408 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4409 | 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] | 4410 | } |
| 4411 | |
| 4412 | /* return the result. */ |
| 4413 | luaL_pushresult(&appctx->b); |
| 4414 | return 1; |
| 4415 | } |
| 4416 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4417 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4418 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4419 | { |
| 4420 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4421 | int len = -1; |
| 4422 | |
| 4423 | /* Check arguments. */ |
| 4424 | if (lua_gettop(L) > 2) |
| 4425 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4426 | if (lua_gettop(L) >= 2) { |
| 4427 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4428 | lua_pop(L, 1); |
| 4429 | } |
| 4430 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4431 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4432 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4433 | /* Initialise the string catenation. */ |
| 4434 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4435 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4436 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | /* Append data in the output side of the buffer. This data is immediately |
| 4440 | * sent. The function returns the amount of data written. If the buffer |
| 4441 | * cannot contain the data, the function yields. The function returns -1 |
| 4442 | * if the channel is closed. |
| 4443 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4444 | __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] | 4445 | { |
| 4446 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4447 | struct stream_interface *si = appctx->appctx->owner; |
| 4448 | struct channel *res = si_ic(si); |
| 4449 | struct htx *htx = htx_from_buf(&res->buf); |
| 4450 | const char *data; |
| 4451 | size_t len; |
| 4452 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4453 | int max; |
| 4454 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4455 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4456 | if (!max) |
| 4457 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4458 | |
| 4459 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4460 | |
| 4461 | /* Get the max amount of data which can write as input in the channel. */ |
| 4462 | if (max > (len - l)) |
| 4463 | max = len - l; |
| 4464 | |
| 4465 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4466 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4467 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4468 | |
| 4469 | /* update counters. */ |
| 4470 | l += max; |
| 4471 | lua_pop(L, 1); |
| 4472 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4473 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4474 | /* If some data is not send, declares the situation to the |
| 4475 | * applet, and returns a yield. |
| 4476 | */ |
| 4477 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4478 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4479 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4480 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4481 | 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] | 4482 | } |
| 4483 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4484 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4485 | return 1; |
| 4486 | } |
| 4487 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4488 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4489 | * yield the LUA process, and resume it without checking the |
| 4490 | * input arguments. |
| 4491 | */ |
| 4492 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4493 | { |
| 4494 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4495 | |
| 4496 | /* We want to send some data. Headers must be sent. */ |
| 4497 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4498 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4499 | WILL_LJMP(lua_error(L)); |
| 4500 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4501 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4502 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4503 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4504 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4505 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4509 | { |
| 4510 | const char *name; |
| 4511 | int ret; |
| 4512 | |
| 4513 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4514 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4515 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4516 | |
| 4517 | /* Push in the stack the "response" entry. */ |
| 4518 | ret = lua_getfield(L, 1, "response"); |
| 4519 | if (ret != LUA_TTABLE) { |
| 4520 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4521 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4522 | WILL_LJMP(lua_error(L)); |
| 4523 | } |
| 4524 | |
| 4525 | /* check if the header is already registered if it is not |
| 4526 | * the case, register it. |
| 4527 | */ |
| 4528 | ret = lua_getfield(L, -1, name); |
| 4529 | if (ret == LUA_TNIL) { |
| 4530 | |
| 4531 | /* Entry not found. */ |
| 4532 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4533 | |
| 4534 | /* Insert the new header name in the array in the top of the stack. |
| 4535 | * It left the new array in the top of the stack. |
| 4536 | */ |
| 4537 | lua_newtable(L); |
| 4538 | lua_pushvalue(L, 2); |
| 4539 | lua_pushvalue(L, -2); |
| 4540 | lua_settable(L, -4); |
| 4541 | |
| 4542 | } else if (ret != LUA_TTABLE) { |
| 4543 | |
| 4544 | /* corruption error. */ |
| 4545 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4546 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4547 | WILL_LJMP(lua_error(L)); |
| 4548 | } |
| 4549 | |
| 4550 | /* Now the top od thestack is an array of values. We push |
| 4551 | * the header value as new entry. |
| 4552 | */ |
| 4553 | lua_pushvalue(L, 3); |
| 4554 | ret = lua_rawlen(L, -2); |
| 4555 | lua_rawseti(L, -2, ret + 1); |
| 4556 | lua_pushboolean(L, 1); |
| 4557 | return 1; |
| 4558 | } |
| 4559 | |
| 4560 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4561 | { |
| 4562 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4563 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4564 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4565 | |
| 4566 | if (status < 100 || status > 599) { |
| 4567 | lua_pushboolean(L, 0); |
| 4568 | return 1; |
| 4569 | } |
| 4570 | |
| 4571 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4572 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4573 | lua_pushboolean(L, 1); |
| 4574 | return 1; |
| 4575 | } |
| 4576 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4577 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4578 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4579 | { |
| 4580 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4581 | struct stream_interface *si = appctx->appctx->owner; |
| 4582 | struct channel *res = si_ic(si); |
| 4583 | struct htx *htx; |
| 4584 | struct htx_sl *sl; |
| 4585 | struct h1m h1m; |
| 4586 | const char *status, *reason; |
| 4587 | const char *name, *value; |
| 4588 | size_t nlen, vlen; |
| 4589 | unsigned int flags; |
| 4590 | |
| 4591 | /* Send the message at once. */ |
| 4592 | htx = htx_from_buf(&res->buf); |
| 4593 | h1m_init_res(&h1m); |
| 4594 | |
| 4595 | /* Use the same http version than the request. */ |
| 4596 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4597 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4598 | if (reason == NULL) |
| 4599 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4600 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4601 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4602 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4603 | } |
| 4604 | else { |
| 4605 | flags = HTX_SL_F_IS_RESP; |
| 4606 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4607 | } |
| 4608 | if (!sl) { |
| 4609 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4610 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4611 | WILL_LJMP(lua_error(L)); |
| 4612 | } |
| 4613 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4614 | |
| 4615 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4616 | lua_pushvalue(L, 0); |
| 4617 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4618 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4619 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4620 | WILL_LJMP(lua_error(L)); |
| 4621 | } |
| 4622 | |
| 4623 | /* Browse the list of headers. */ |
| 4624 | lua_pushnil(L); |
| 4625 | while(lua_next(L, -2) != 0) { |
| 4626 | /* We expect a string as -2. */ |
| 4627 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4628 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4629 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4630 | lua_typename(L, lua_type(L, -2))); |
| 4631 | WILL_LJMP(lua_error(L)); |
| 4632 | } |
| 4633 | name = lua_tolstring(L, -2, &nlen); |
| 4634 | |
| 4635 | /* We expect an array as -1. */ |
| 4636 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4637 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4638 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4639 | name, |
| 4640 | lua_typename(L, lua_type(L, -1))); |
| 4641 | WILL_LJMP(lua_error(L)); |
| 4642 | } |
| 4643 | |
| 4644 | /* Browse the table who is on the top of the stack. */ |
| 4645 | lua_pushnil(L); |
| 4646 | while(lua_next(L, -2) != 0) { |
| 4647 | int id; |
| 4648 | |
| 4649 | /* We expect a number as -2. */ |
| 4650 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4651 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4652 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4653 | name, |
| 4654 | lua_typename(L, lua_type(L, -2))); |
| 4655 | WILL_LJMP(lua_error(L)); |
| 4656 | } |
| 4657 | id = lua_tointeger(L, -2); |
| 4658 | |
| 4659 | /* We expect a string as -2. */ |
| 4660 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4661 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4662 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4663 | name, id, |
| 4664 | lua_typename(L, lua_type(L, -1))); |
| 4665 | WILL_LJMP(lua_error(L)); |
| 4666 | } |
| 4667 | value = lua_tolstring(L, -1, &vlen); |
| 4668 | |
| 4669 | /* Simple Protocol checks. */ |
| 4670 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4671 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4672 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4673 | struct ist v = ist2(value, vlen); |
| 4674 | int ret; |
| 4675 | |
| 4676 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4677 | if (ret < 0) { |
| 4678 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4679 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4680 | name); |
| 4681 | WILL_LJMP(lua_error(L)); |
| 4682 | } |
| 4683 | else if (ret == 0) |
| 4684 | goto next; /* Skip it */ |
| 4685 | } |
| 4686 | |
| 4687 | /* Add a new header */ |
| 4688 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4689 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4690 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4691 | name); |
| 4692 | WILL_LJMP(lua_error(L)); |
| 4693 | } |
| 4694 | next: |
| 4695 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4696 | lua_pop(L, 1); |
| 4697 | } |
| 4698 | |
| 4699 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4700 | lua_pop(L, 1); |
| 4701 | } |
| 4702 | |
| 4703 | if (h1m.flags & H1_MF_CHNK) |
| 4704 | h1m.flags &= ~H1_MF_CLEN; |
| 4705 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4706 | h1m.flags |= H1_MF_XFER_LEN; |
| 4707 | |
| 4708 | /* Uset HTX start-line flags */ |
| 4709 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4710 | flags |= HTX_SL_F_XFER_ENC; |
| 4711 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4712 | flags |= HTX_SL_F_XFER_LEN; |
| 4713 | if (h1m.flags & H1_MF_CHNK) |
| 4714 | flags |= HTX_SL_F_CHNK; |
| 4715 | else if (h1m.flags & H1_MF_CLEN) |
| 4716 | flags |= HTX_SL_F_CLEN; |
| 4717 | if (h1m.body_len == 0) |
| 4718 | flags |= HTX_SL_F_BODYLESS; |
| 4719 | } |
| 4720 | sl->flags |= flags; |
| 4721 | |
| 4722 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4723 | * and the status code implies the presence of a message body, we must |
| 4724 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4725 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4726 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4727 | */ |
| 4728 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4729 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4730 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4731 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4732 | /* Add a new header */ |
| 4733 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4734 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4735 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4736 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4737 | WILL_LJMP(lua_error(L)); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | /* Finalize headers. */ |
| 4742 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4743 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4744 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4745 | WILL_LJMP(lua_error(L)); |
| 4746 | } |
| 4747 | |
| 4748 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4749 | b_reset(&res->buf); |
| 4750 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4751 | WILL_LJMP(lua_error(L)); |
| 4752 | } |
| 4753 | |
| 4754 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4755 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4756 | |
| 4757 | /* Headers sent, set the flag. */ |
| 4758 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4759 | return 0; |
| 4760 | |
| 4761 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4762 | /* We will build the status line and the headers of the HTTP response. |
| 4763 | * We will try send at once if its not possible, we give back the hand |
| 4764 | * waiting for more room. |
| 4765 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4766 | __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] | 4767 | { |
| 4768 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4769 | struct stream_interface *si = appctx->appctx->owner; |
| 4770 | struct channel *res = si_ic(si); |
| 4771 | |
| 4772 | if (co_data(res)) { |
| 4773 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4774 | 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] | 4775 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4776 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4780 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4781 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4782 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4783 | } |
| 4784 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4785 | /* |
| 4786 | * |
| 4787 | * |
| 4788 | * Class HTTP |
| 4789 | * |
| 4790 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4791 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4792 | |
| 4793 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4794 | * a class stream, otherwise it throws an error. |
| 4795 | */ |
| 4796 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4797 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4798 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4799 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4800 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4801 | /* This function creates and push in the stack a HTTP object |
| 4802 | * according with a current TXN. |
| 4803 | */ |
| 4804 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4805 | { |
| 4806 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4807 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4808 | /* Check stack size. */ |
| 4809 | if (!lua_checkstack(L, 3)) |
| 4810 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4811 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4812 | /* Create the object: obj[0] = userdata. |
| 4813 | * Note that the base of the Converters object is the |
| 4814 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4815 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4816 | lua_newtable(L); |
| 4817 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4818 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4819 | |
| 4820 | htxn->s = txn->s; |
| 4821 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4822 | htxn->dir = txn->dir; |
| 4823 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4824 | |
| 4825 | /* Pop a class stream metatable and affect it to the table. */ |
| 4826 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4827 | lua_setmetatable(L, -2); |
| 4828 | |
| 4829 | return 1; |
| 4830 | } |
| 4831 | |
| 4832 | /* This function creates ans returns an array of HTTP headers. |
| 4833 | * This function does not fails. It is used as wrapper with the |
| 4834 | * 2 following functions. |
| 4835 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4836 | __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] | 4837 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4838 | struct htx *htx; |
| 4839 | int32_t pos; |
| 4840 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4841 | /* Create the table. */ |
| 4842 | lua_newtable(L); |
| 4843 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4844 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4845 | htx = htxbuf(&msg->chn->buf); |
| 4846 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4847 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4848 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4849 | struct ist n, v; |
| 4850 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4851 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4852 | if (type == HTX_BLK_HDR) { |
| 4853 | n = htx_get_blk_name(htx,blk); |
| 4854 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4855 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4856 | else if (type == HTX_BLK_EOH) |
| 4857 | break; |
| 4858 | else |
| 4859 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4860 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4861 | /* Check for existing entry: |
| 4862 | * assume that the table is on the top of the stack, and |
| 4863 | * push the key in the stack, the function lua_gettable() |
| 4864 | * perform the lookup. |
| 4865 | */ |
| 4866 | lua_pushlstring(L, n.ptr, n.len); |
| 4867 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4868 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4869 | switch (lua_type(L, -1)) { |
| 4870 | case LUA_TNIL: |
| 4871 | /* Table not found, create it. */ |
| 4872 | lua_pop(L, 1); /* remove the nil value. */ |
| 4873 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4874 | lua_newtable(L); /* create and push empty table. */ |
| 4875 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4876 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4877 | 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] | 4878 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4879 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4880 | case LUA_TTABLE: |
| 4881 | /* Entry found: push the value in the table. */ |
| 4882 | len = lua_rawlen(L, -1); |
| 4883 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4884 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4885 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4886 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4887 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4888 | default: |
| 4889 | /* Other cases are errors. */ |
| 4890 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4891 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4892 | } |
| 4893 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4894 | return 1; |
| 4895 | } |
| 4896 | |
| 4897 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4898 | { |
| 4899 | struct hlua_txn *htxn; |
| 4900 | |
| 4901 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4902 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4903 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4904 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4905 | WILL_LJMP(lua_error(L)); |
| 4906 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4907 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4911 | { |
| 4912 | struct hlua_txn *htxn; |
| 4913 | |
| 4914 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4915 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4916 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4917 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4918 | WILL_LJMP(lua_error(L)); |
| 4919 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4920 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | /* This function replace full header, or just a value in |
| 4924 | * the request or in the response. It is a wrapper fir the |
| 4925 | * 4 following functions. |
| 4926 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4927 | __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] | 4928 | { |
| 4929 | size_t name_len; |
| 4930 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4931 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4932 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4933 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4934 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4935 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4936 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4937 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4938 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4939 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4940 | 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] | 4941 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4942 | return 0; |
| 4943 | } |
| 4944 | |
| 4945 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4946 | { |
| 4947 | struct hlua_txn *htxn; |
| 4948 | |
| 4949 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4950 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4951 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4952 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4953 | WILL_LJMP(lua_error(L)); |
| 4954 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4955 | 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] | 4956 | } |
| 4957 | |
| 4958 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4959 | { |
| 4960 | struct hlua_txn *htxn; |
| 4961 | |
| 4962 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4963 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4964 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4965 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4966 | WILL_LJMP(lua_error(L)); |
| 4967 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4968 | 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] | 4969 | } |
| 4970 | |
| 4971 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4972 | { |
| 4973 | struct hlua_txn *htxn; |
| 4974 | |
| 4975 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4976 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4977 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4978 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4979 | WILL_LJMP(lua_error(L)); |
| 4980 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4981 | 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] | 4982 | } |
| 4983 | |
| 4984 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4985 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4986 | struct hlua_txn *htxn; |
| 4987 | |
| 4988 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4989 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4990 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4991 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4992 | WILL_LJMP(lua_error(L)); |
| 4993 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4994 | 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] | 4995 | } |
| 4996 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4997 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4998 | * It is a wrapper for the 2 following functions. |
| 4999 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5000 | __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] | 5001 | { |
| 5002 | size_t len; |
| 5003 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5004 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5005 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5006 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5007 | ctx.blk = NULL; |
| 5008 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5009 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5010 | return 0; |
| 5011 | } |
| 5012 | |
| 5013 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5014 | { |
| 5015 | struct hlua_txn *htxn; |
| 5016 | |
| 5017 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5018 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5019 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5020 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5021 | WILL_LJMP(lua_error(L)); |
| 5022 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5023 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5027 | { |
| 5028 | struct hlua_txn *htxn; |
| 5029 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5030 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5031 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5032 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5033 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5034 | WILL_LJMP(lua_error(L)); |
| 5035 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5036 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | /* This function adds an header. It is a wrapper used by |
| 5040 | * the 2 following functions. |
| 5041 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5042 | __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] | 5043 | { |
| 5044 | size_t name_len; |
| 5045 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5046 | size_t value_len; |
| 5047 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5048 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5049 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5050 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5051 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5052 | return 0; |
| 5053 | } |
| 5054 | |
| 5055 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5056 | { |
| 5057 | struct hlua_txn *htxn; |
| 5058 | |
| 5059 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5060 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5061 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5062 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5063 | WILL_LJMP(lua_error(L)); |
| 5064 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5065 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5069 | { |
| 5070 | struct hlua_txn *htxn; |
| 5071 | |
| 5072 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5073 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5074 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5075 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5076 | WILL_LJMP(lua_error(L)); |
| 5077 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5078 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5079 | } |
| 5080 | |
| 5081 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5082 | { |
| 5083 | struct hlua_txn *htxn; |
| 5084 | |
| 5085 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5086 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5087 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5088 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5089 | WILL_LJMP(lua_error(L)); |
| 5090 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5091 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5092 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5096 | { |
| 5097 | struct hlua_txn *htxn; |
| 5098 | |
| 5099 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5100 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5101 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5102 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5103 | WILL_LJMP(lua_error(L)); |
| 5104 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5105 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5106 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | /* This function set the method. */ |
| 5110 | static int hlua_http_req_set_meth(lua_State *L) |
| 5111 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5112 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5113 | size_t name_len; |
| 5114 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5115 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5116 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5117 | WILL_LJMP(lua_error(L)); |
| 5118 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5119 | 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] | 5120 | return 1; |
| 5121 | } |
| 5122 | |
| 5123 | /* This function set the method. */ |
| 5124 | static int hlua_http_req_set_path(lua_State *L) |
| 5125 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5126 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5127 | size_t name_len; |
| 5128 | 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] | 5129 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5130 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5131 | WILL_LJMP(lua_error(L)); |
| 5132 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5133 | 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] | 5134 | return 1; |
| 5135 | } |
| 5136 | |
| 5137 | /* This function set the query-string. */ |
| 5138 | static int hlua_http_req_set_query(lua_State *L) |
| 5139 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5140 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5141 | size_t name_len; |
| 5142 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5143 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5144 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5145 | WILL_LJMP(lua_error(L)); |
| 5146 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5147 | /* Check length. */ |
| 5148 | if (name_len > trash.size - 1) { |
| 5149 | lua_pushboolean(L, 0); |
| 5150 | return 1; |
| 5151 | } |
| 5152 | |
| 5153 | /* Add the mark question as prefix. */ |
| 5154 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5155 | trash.area[trash.data++] = '?'; |
| 5156 | memcpy(trash.area + trash.data, name, name_len); |
| 5157 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5158 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5159 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5160 | 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] | 5161 | return 1; |
| 5162 | } |
| 5163 | |
| 5164 | /* This function set the uri. */ |
| 5165 | static int hlua_http_req_set_uri(lua_State *L) |
| 5166 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5167 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5168 | size_t name_len; |
| 5169 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5170 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5171 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5172 | WILL_LJMP(lua_error(L)); |
| 5173 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5174 | 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] | 5175 | return 1; |
| 5176 | } |
| 5177 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5178 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5179 | static int hlua_http_res_set_status(lua_State *L) |
| 5180 | { |
| 5181 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5182 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5183 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5184 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5185 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5186 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5187 | WILL_LJMP(lua_error(L)); |
| 5188 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5189 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5190 | return 0; |
| 5191 | } |
| 5192 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5193 | /* |
| 5194 | * |
| 5195 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5196 | * Class TXN |
| 5197 | * |
| 5198 | * |
| 5199 | */ |
| 5200 | |
| 5201 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5202 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5203 | */ |
| 5204 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5205 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5206 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5207 | } |
| 5208 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5209 | __LJMP static int hlua_set_var(lua_State *L) |
| 5210 | { |
| 5211 | struct hlua_txn *htxn; |
| 5212 | const char *name; |
| 5213 | size_t len; |
| 5214 | struct sample smp; |
| 5215 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5216 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5217 | 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] | 5218 | |
| 5219 | /* It is useles to retrieve the stream, but this function |
| 5220 | * runs only in a stream context. |
| 5221 | */ |
| 5222 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5223 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5224 | |
| 5225 | /* Converts the third argument in a sample. */ |
| 5226 | hlua_lua2smp(L, 3, &smp); |
| 5227 | |
| 5228 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5229 | 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] | 5230 | |
| 5231 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5232 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5233 | else |
| 5234 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5235 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5236 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5237 | } |
| 5238 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5239 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5240 | { |
| 5241 | struct hlua_txn *htxn; |
| 5242 | const char *name; |
| 5243 | size_t len; |
| 5244 | struct sample smp; |
| 5245 | |
| 5246 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5247 | |
| 5248 | /* It is useles to retrieve the stream, but this function |
| 5249 | * runs only in a stream context. |
| 5250 | */ |
| 5251 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5252 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5253 | |
| 5254 | /* Unset the variable. */ |
| 5255 | 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] | 5256 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5257 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5258 | } |
| 5259 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5260 | __LJMP static int hlua_get_var(lua_State *L) |
| 5261 | { |
| 5262 | struct hlua_txn *htxn; |
| 5263 | const char *name; |
| 5264 | size_t len; |
| 5265 | struct sample smp; |
| 5266 | |
| 5267 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5268 | |
| 5269 | /* It is useles to retrieve the stream, but this function |
| 5270 | * runs only in a stream context. |
| 5271 | */ |
| 5272 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5273 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5274 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5275 | 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] | 5276 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5277 | lua_pushnil(L); |
| 5278 | return 1; |
| 5279 | } |
| 5280 | |
| 5281 | return hlua_smp2lua(L, &smp); |
| 5282 | } |
| 5283 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5284 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5285 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5286 | struct hlua *hlua; |
| 5287 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5288 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5289 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5290 | /* It is useles to retrieve the stream, but this function |
| 5291 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5292 | */ |
| 5293 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5294 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5295 | |
| 5296 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5297 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5298 | |
| 5299 | /* Get and store new value. */ |
| 5300 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5301 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5302 | |
| 5303 | return 0; |
| 5304 | } |
| 5305 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5306 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5307 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5308 | struct hlua *hlua; |
| 5309 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5310 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5311 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5312 | /* It is useles to retrieve the stream, but this function |
| 5313 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5314 | */ |
| 5315 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5316 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5317 | |
| 5318 | /* Push configuration index in the stack. */ |
| 5319 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5320 | |
| 5321 | return 1; |
| 5322 | } |
| 5323 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5324 | /* Create stack entry containing a class TXN. This function |
| 5325 | * return 0 if the stack does not contains free slots, |
| 5326 | * otherwise it returns 1. |
| 5327 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5328 | 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] | 5329 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5330 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5331 | |
| 5332 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5333 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5334 | return 0; |
| 5335 | |
| 5336 | /* NOTE: The allocation never fails. The failure |
| 5337 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5338 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5339 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5340 | /* Create the object: obj[0] = userdata. */ |
| 5341 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5342 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5343 | lua_rawseti(L, -2, 0); |
| 5344 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5345 | htxn->s = s; |
| 5346 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5347 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5348 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5349 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5350 | /* Create the "f" field that contains a list of fetches. */ |
| 5351 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5352 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5353 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5354 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5355 | |
| 5356 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5357 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5358 | 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] | 5359 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5360 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5361 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5362 | /* Create the "c" field that contains a list of converters. */ |
| 5363 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5364 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5365 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5366 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5367 | |
| 5368 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5369 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5370 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5371 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5372 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5373 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5374 | /* Create the "req" field that contains the request channel object. */ |
| 5375 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5376 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5377 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5378 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5379 | |
| 5380 | /* Create the "res" field that contains the response channel object. */ |
| 5381 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5382 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5383 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5384 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5385 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5386 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5387 | lua_pushstring(L, "http"); |
| 5388 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5389 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5390 | return 0; |
| 5391 | } |
| 5392 | else |
| 5393 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5394 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5395 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5396 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5397 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5398 | lua_setmetatable(L, -2); |
| 5399 | |
| 5400 | return 1; |
| 5401 | } |
| 5402 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5403 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5404 | { |
| 5405 | const char *msg; |
| 5406 | struct hlua_txn *htxn; |
| 5407 | |
| 5408 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5409 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5410 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5411 | |
| 5412 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
| 5416 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5417 | { |
| 5418 | int level; |
| 5419 | const char *msg; |
| 5420 | struct hlua_txn *htxn; |
| 5421 | |
| 5422 | MAY_LJMP(check_args(L, 3, "log")); |
| 5423 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5424 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5425 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5426 | |
| 5427 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5428 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5429 | |
| 5430 | hlua_sendlog(htxn->s->be, level, msg); |
| 5431 | return 0; |
| 5432 | } |
| 5433 | |
| 5434 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5435 | { |
| 5436 | const char *msg; |
| 5437 | struct hlua_txn *htxn; |
| 5438 | |
| 5439 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5440 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5441 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5442 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5443 | return 0; |
| 5444 | } |
| 5445 | |
| 5446 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5447 | { |
| 5448 | const char *msg; |
| 5449 | struct hlua_txn *htxn; |
| 5450 | |
| 5451 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5452 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5453 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5454 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5455 | return 0; |
| 5456 | } |
| 5457 | |
| 5458 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5459 | { |
| 5460 | const char *msg; |
| 5461 | struct hlua_txn *htxn; |
| 5462 | |
| 5463 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5464 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5465 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5466 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5467 | return 0; |
| 5468 | } |
| 5469 | |
| 5470 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5471 | { |
| 5472 | const char *msg; |
| 5473 | struct hlua_txn *htxn; |
| 5474 | |
| 5475 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5476 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5477 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5478 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5479 | return 0; |
| 5480 | } |
| 5481 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5482 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5483 | { |
| 5484 | struct hlua_txn *htxn; |
| 5485 | int ll; |
| 5486 | |
| 5487 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5488 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5489 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5490 | |
| 5491 | if (ll < 0 || ll > 7) |
| 5492 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5493 | |
| 5494 | htxn->s->logs.level = ll; |
| 5495 | return 0; |
| 5496 | } |
| 5497 | |
| 5498 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5499 | { |
| 5500 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5501 | int tos; |
| 5502 | |
| 5503 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5504 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5505 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5506 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5507 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5508 | return 0; |
| 5509 | } |
| 5510 | |
| 5511 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5512 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5513 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5514 | int mark; |
| 5515 | |
| 5516 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5517 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5518 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5519 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5520 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5521 | return 0; |
| 5522 | } |
| 5523 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5524 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5525 | { |
| 5526 | struct hlua_txn *htxn; |
| 5527 | |
| 5528 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5529 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5530 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
| 5534 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5535 | { |
| 5536 | struct hlua_txn *htxn; |
| 5537 | |
| 5538 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5539 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5540 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5541 | return 0; |
| 5542 | } |
| 5543 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5544 | /* 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] | 5545 | * 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] | 5546 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5547 | * error. The Reply must be on top of the stack. |
| 5548 | */ |
| 5549 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5550 | { |
| 5551 | struct htx *htx; |
| 5552 | struct htx_sl *sl; |
| 5553 | struct h1m h1m; |
| 5554 | const char *status, *reason, *body; |
| 5555 | size_t status_len, reason_len, body_len; |
| 5556 | int ret, code, flags; |
| 5557 | |
| 5558 | code = 200; |
| 5559 | status = "200"; |
| 5560 | status_len = 3; |
| 5561 | ret = lua_getfield(L, -1, "status"); |
| 5562 | if (ret == LUA_TNUMBER) { |
| 5563 | code = lua_tointeger(L, -1); |
| 5564 | status = lua_tolstring(L, -1, &status_len); |
| 5565 | } |
| 5566 | lua_pop(L, 1); |
| 5567 | |
| 5568 | reason = http_get_reason(code); |
| 5569 | reason_len = strlen(reason); |
| 5570 | ret = lua_getfield(L, -1, "reason"); |
| 5571 | if (ret == LUA_TSTRING) |
| 5572 | reason = lua_tolstring(L, -1, &reason_len); |
| 5573 | lua_pop(L, 1); |
| 5574 | |
| 5575 | body = NULL; |
| 5576 | body_len = 0; |
| 5577 | ret = lua_getfield(L, -1, "body"); |
| 5578 | if (ret == LUA_TSTRING) |
| 5579 | body = lua_tolstring(L, -1, &body_len); |
| 5580 | lua_pop(L, 1); |
| 5581 | |
| 5582 | /* Prepare the response before inserting the headers */ |
| 5583 | h1m_init_res(&h1m); |
| 5584 | htx = htx_from_buf(&s->res.buf); |
| 5585 | channel_htx_truncate(&s->res, htx); |
| 5586 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5587 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5588 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5589 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5590 | } |
| 5591 | else { |
| 5592 | flags = HTX_SL_F_IS_RESP; |
| 5593 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5594 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5595 | } |
| 5596 | if (!sl) |
| 5597 | goto fail; |
| 5598 | sl->info.res.status = code; |
| 5599 | |
| 5600 | /* Push in the stack the "headers" entry. */ |
| 5601 | ret = lua_getfield(L, -1, "headers"); |
| 5602 | if (ret != LUA_TTABLE) |
| 5603 | goto skip_headers; |
| 5604 | |
| 5605 | lua_pushnil(L); |
| 5606 | while (lua_next(L, -2) != 0) { |
| 5607 | struct ist name, value; |
| 5608 | const char *n, *v; |
| 5609 | size_t nlen, vlen; |
| 5610 | |
| 5611 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5612 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5613 | goto next_hdr; |
| 5614 | } |
| 5615 | |
| 5616 | n = lua_tolstring(L, -2, &nlen); |
| 5617 | name = ist2(n, nlen); |
| 5618 | if (isteqi(name, ist("content-length"))) { |
| 5619 | /* Always skip content-length header. It will be added |
| 5620 | * later with the correct len |
| 5621 | */ |
| 5622 | goto next_hdr; |
| 5623 | } |
| 5624 | |
| 5625 | /* Loop on header's values */ |
| 5626 | lua_pushnil(L); |
| 5627 | while (lua_next(L, -2)) { |
| 5628 | if (!lua_isstring(L, -1)) { |
| 5629 | /* Skip the value if it is not a string */ |
| 5630 | goto next_value; |
| 5631 | } |
| 5632 | |
| 5633 | v = lua_tolstring(L, -1, &vlen); |
| 5634 | value = ist2(v, vlen); |
| 5635 | |
| 5636 | if (isteqi(name, ist("transfer-encoding"))) |
| 5637 | h1_parse_xfer_enc_header(&h1m, value); |
| 5638 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5639 | goto fail; |
| 5640 | |
| 5641 | next_value: |
| 5642 | lua_pop(L, 1); |
| 5643 | } |
| 5644 | |
| 5645 | next_hdr: |
| 5646 | lua_pop(L, 1); |
| 5647 | } |
| 5648 | skip_headers: |
| 5649 | lua_pop(L, 1); |
| 5650 | |
| 5651 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5652 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5653 | const char *clen = ultoa(body_len); |
| 5654 | |
| 5655 | h1m.flags |= H1_MF_CLEN; |
| 5656 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5657 | goto fail; |
| 5658 | } |
| 5659 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5660 | h1m.flags |= H1_MF_XFER_LEN; |
| 5661 | |
| 5662 | /* Update HTX start-line flags */ |
| 5663 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5664 | flags |= HTX_SL_F_XFER_ENC; |
| 5665 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5666 | flags |= HTX_SL_F_XFER_LEN; |
| 5667 | if (h1m.flags & H1_MF_CHNK) |
| 5668 | flags |= HTX_SL_F_CHNK; |
| 5669 | else if (h1m.flags & H1_MF_CLEN) |
| 5670 | flags |= HTX_SL_F_CLEN; |
| 5671 | if (h1m.body_len == 0) |
| 5672 | flags |= HTX_SL_F_BODYLESS; |
| 5673 | } |
| 5674 | sl->flags |= flags; |
| 5675 | |
| 5676 | |
| 5677 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5678 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5679 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5680 | goto fail; |
| 5681 | |
| 5682 | /* Now, forward the response and terminate the transaction */ |
| 5683 | s->txn->status = code; |
| 5684 | htx_to_buf(htx, &s->res.buf); |
| 5685 | if (!http_forward_proxy_resp(s, 1)) |
| 5686 | goto fail; |
| 5687 | |
| 5688 | return 1; |
| 5689 | |
| 5690 | fail: |
| 5691 | channel_htx_truncate(&s->res, htx); |
| 5692 | return 0; |
| 5693 | } |
| 5694 | |
| 5695 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5696 | * processing is just aborted. Nothing is returned to the client and all |
| 5697 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5698 | * is forwarded to the client before terminating the transaction. On success, |
| 5699 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5700 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5701 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5702 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5703 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5704 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5705 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5706 | struct stream *s; |
| 5707 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5708 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5709 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5710 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5711 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5712 | * just end the execution of the current lua code. */ |
| 5713 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5714 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5715 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5716 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5717 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5718 | struct channel *req = &s->req; |
| 5719 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5720 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5721 | channel_auto_read(req); |
| 5722 | channel_abort(req); |
| 5723 | channel_auto_close(req); |
| 5724 | channel_erase(req); |
| 5725 | |
| 5726 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5727 | channel_auto_read(res); |
| 5728 | channel_auto_close(res); |
| 5729 | channel_shutr_now(res); |
| 5730 | |
| 5731 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5732 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5733 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5734 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5735 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5736 | /* No reply or invalid reply */ |
| 5737 | s->txn->status = 0; |
| 5738 | http_reply_and_close(s, 0, NULL); |
| 5739 | } |
| 5740 | else { |
| 5741 | /* Remove extra args to have the reply on top of the stack */ |
| 5742 | if (lua_gettop(L) > 2) |
| 5743 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5744 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5745 | if (!hlua_txn_forward_reply(L, s)) { |
| 5746 | if (!(s->flags & SF_ERR_MASK)) |
| 5747 | s->flags |= SF_ERR_PRXCOND; |
| 5748 | lua_pushinteger(L, ACT_RET_ERR); |
| 5749 | WILL_LJMP(hlua_done(L)); |
| 5750 | return 0; /* Never reached */ |
| 5751 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5752 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5753 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5754 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5755 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5756 | /* let's log the request time */ |
| 5757 | s->logs.tv_request = now; |
| 5758 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5759 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5760 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5761 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5762 | done: |
| 5763 | if (!(s->flags & SF_ERR_MASK)) |
| 5764 | s->flags |= SF_ERR_LOCAL; |
| 5765 | if (!(s->flags & SF_FINST_MASK)) |
| 5766 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5767 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5768 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5769 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5770 | return 0; |
| 5771 | } |
| 5772 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5773 | /* |
| 5774 | * |
| 5775 | * |
| 5776 | * Class REPLY |
| 5777 | * |
| 5778 | * |
| 5779 | */ |
| 5780 | |
| 5781 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5782 | * free slots, the function fails and returns 0; |
| 5783 | */ |
| 5784 | static int hlua_txn_reply_new(lua_State *L) |
| 5785 | { |
| 5786 | struct hlua_txn *htxn; |
| 5787 | const char *reason, *body = NULL; |
| 5788 | int ret, status; |
| 5789 | |
| 5790 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5791 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5792 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5793 | WILL_LJMP(lua_error(L)); |
| 5794 | } |
| 5795 | |
| 5796 | /* Default value */ |
| 5797 | status = 200; |
| 5798 | reason = http_get_reason(status); |
| 5799 | |
| 5800 | if (lua_istable(L, 2)) { |
| 5801 | /* load status and reason from the table argument at index 2 */ |
| 5802 | ret = lua_getfield(L, 2, "status"); |
| 5803 | if (ret == LUA_TNIL) |
| 5804 | goto reason; |
| 5805 | else if (ret != LUA_TNUMBER) { |
| 5806 | /* invalid status: ignore the reason */ |
| 5807 | goto body; |
| 5808 | } |
| 5809 | status = lua_tointeger(L, -1); |
| 5810 | |
| 5811 | reason: |
| 5812 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5813 | ret = lua_getfield(L, 2, "reason"); |
| 5814 | if (ret == LUA_TSTRING) |
| 5815 | reason = lua_tostring(L, -1); |
| 5816 | |
| 5817 | body: |
| 5818 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5819 | ret = lua_getfield(L, 2, "body"); |
| 5820 | if (ret == LUA_TSTRING) |
| 5821 | body = lua_tostring(L, -1); |
| 5822 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5823 | } |
| 5824 | |
| 5825 | /* Create the Reply table */ |
| 5826 | lua_newtable(L); |
| 5827 | |
| 5828 | /* Add status element */ |
| 5829 | lua_pushstring(L, "status"); |
| 5830 | lua_pushinteger(L, status); |
| 5831 | lua_settable(L, -3); |
| 5832 | |
| 5833 | /* Add reason element */ |
| 5834 | reason = http_get_reason(status); |
| 5835 | lua_pushstring(L, "reason"); |
| 5836 | lua_pushstring(L, reason); |
| 5837 | lua_settable(L, -3); |
| 5838 | |
| 5839 | /* Add body element, nil if undefined */ |
| 5840 | lua_pushstring(L, "body"); |
| 5841 | if (body) |
| 5842 | lua_pushstring(L, body); |
| 5843 | else |
| 5844 | lua_pushnil(L); |
| 5845 | lua_settable(L, -3); |
| 5846 | |
| 5847 | /* Add headers element */ |
| 5848 | lua_pushstring(L, "headers"); |
| 5849 | lua_newtable(L); |
| 5850 | |
| 5851 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5852 | if (lua_istable(L, 2)) { |
| 5853 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5854 | ret = lua_getfield(L, 2, "headers"); |
| 5855 | if (ret == LUA_TTABLE) { |
| 5856 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5857 | lua_pushnil(L); |
| 5858 | while (lua_next(L, -2) != 0) { |
| 5859 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5860 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5861 | /* invalid value type, skip it */ |
| 5862 | lua_pop(L, 1); |
| 5863 | continue; |
| 5864 | } |
| 5865 | |
| 5866 | |
| 5867 | /* Duplicate the key and swap it with the value. */ |
| 5868 | lua_pushvalue(L, -2); |
| 5869 | lua_insert(L, -2); |
| 5870 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5871 | |
| 5872 | lua_newtable(L); |
| 5873 | lua_insert(L, -2); |
| 5874 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5875 | |
| 5876 | if (lua_isstring(L, -1)) { |
| 5877 | /* push the value in the inner table */ |
| 5878 | lua_rawseti(L, -2, 1); |
| 5879 | } |
| 5880 | else { /* table */ |
| 5881 | lua_pushnil(L); |
| 5882 | while (lua_next(L, -2) != 0) { |
| 5883 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5884 | if (!lua_isstring(L, -1)) { |
| 5885 | /* invalid value type, skip it*/ |
| 5886 | lua_pop(L, 1); |
| 5887 | continue; |
| 5888 | } |
| 5889 | /* push the value in the inner table */ |
| 5890 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5891 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5892 | } |
| 5893 | lua_pop(L, 1); |
| 5894 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5895 | } |
| 5896 | |
| 5897 | /* push (k,v) on the stack in the headers table: |
| 5898 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5899 | */ |
| 5900 | lua_settable(L, -5); |
| 5901 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5902 | } |
| 5903 | } |
| 5904 | lua_pop(L, 1); |
| 5905 | } |
| 5906 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5907 | lua_settable(L, -3); |
| 5908 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5909 | |
| 5910 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5911 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5912 | lua_setmetatable(L, -2); |
| 5913 | return 1; |
| 5914 | } |
| 5915 | |
| 5916 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5917 | * provided, the default one corresponding to the status code is used. |
| 5918 | */ |
| 5919 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5920 | { |
| 5921 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5922 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5923 | |
| 5924 | /* First argument (self) must be a table */ |
| 5925 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5926 | |
| 5927 | if (status < 100 || status > 599) { |
| 5928 | lua_pushboolean(L, 0); |
| 5929 | return 1; |
| 5930 | } |
| 5931 | if (!reason) |
| 5932 | reason = http_get_reason(status); |
| 5933 | |
| 5934 | lua_pushinteger(L, status); |
| 5935 | lua_setfield(L, 1, "status"); |
| 5936 | |
| 5937 | lua_pushstring(L, reason); |
| 5938 | lua_setfield(L, 1, "reason"); |
| 5939 | |
| 5940 | lua_pushboolean(L, 1); |
| 5941 | return 1; |
| 5942 | } |
| 5943 | |
| 5944 | /* Add a header into the reply object. Each header name is associated to an |
| 5945 | * array of values in the "headers" table. If the header name is not found, a |
| 5946 | * new entry is created. |
| 5947 | */ |
| 5948 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5949 | { |
| 5950 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5951 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5952 | int ret; |
| 5953 | |
| 5954 | /* First argument (self) must be a table */ |
| 5955 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5956 | |
| 5957 | /* Push in the stack the "headers" entry. */ |
| 5958 | ret = lua_getfield(L, 1, "headers"); |
| 5959 | if (ret != LUA_TTABLE) { |
| 5960 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5961 | WILL_LJMP(lua_error(L)); |
| 5962 | } |
| 5963 | |
| 5964 | /* check if the header is already registered. If not, register it. */ |
| 5965 | ret = lua_getfield(L, -1, name); |
| 5966 | if (ret == LUA_TNIL) { |
| 5967 | /* Entry not found. */ |
| 5968 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5969 | |
| 5970 | /* Insert the new header name in the array in the top of the stack. |
| 5971 | * It left the new array in the top of the stack. |
| 5972 | */ |
| 5973 | lua_newtable(L); |
| 5974 | lua_pushstring(L, name); |
| 5975 | lua_pushvalue(L, -2); |
| 5976 | lua_settable(L, -4); |
| 5977 | } |
| 5978 | else if (ret != LUA_TTABLE) { |
| 5979 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5980 | WILL_LJMP(lua_error(L)); |
| 5981 | } |
| 5982 | |
| 5983 | /* Now the top od thestack is an array of values. We push |
| 5984 | * the header value as new entry. |
| 5985 | */ |
| 5986 | lua_pushstring(L, value); |
| 5987 | ret = lua_rawlen(L, -2); |
| 5988 | lua_rawseti(L, -2, ret + 1); |
| 5989 | |
| 5990 | lua_pushboolean(L, 1); |
| 5991 | return 1; |
| 5992 | } |
| 5993 | |
| 5994 | /* Remove all occurrences of a given header name. */ |
| 5995 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5996 | { |
| 5997 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5998 | int ret; |
| 5999 | |
| 6000 | /* First argument (self) must be a table */ |
| 6001 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6002 | |
| 6003 | /* Push in the stack the "headers" entry. */ |
| 6004 | ret = lua_getfield(L, 1, "headers"); |
| 6005 | if (ret != LUA_TTABLE) { |
| 6006 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6007 | WILL_LJMP(lua_error(L)); |
| 6008 | } |
| 6009 | |
| 6010 | lua_pushstring(L, name); |
| 6011 | lua_pushnil(L); |
| 6012 | lua_settable(L, -3); |
| 6013 | |
| 6014 | lua_pushboolean(L, 1); |
| 6015 | return 1; |
| 6016 | } |
| 6017 | |
| 6018 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6019 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6020 | { |
| 6021 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6022 | |
| 6023 | /* First argument (self) must be a table */ |
| 6024 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6025 | |
| 6026 | lua_pushstring(L, payload); |
| 6027 | lua_setfield(L, 1, "body"); |
| 6028 | |
| 6029 | lua_pushboolean(L, 1); |
| 6030 | return 1; |
| 6031 | } |
| 6032 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6033 | __LJMP static int hlua_log(lua_State *L) |
| 6034 | { |
| 6035 | int level; |
| 6036 | const char *msg; |
| 6037 | |
| 6038 | MAY_LJMP(check_args(L, 2, "log")); |
| 6039 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6040 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6041 | |
| 6042 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6043 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6044 | |
| 6045 | hlua_sendlog(NULL, level, msg); |
| 6046 | return 0; |
| 6047 | } |
| 6048 | |
| 6049 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6050 | { |
| 6051 | const char *msg; |
| 6052 | |
| 6053 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6054 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6055 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6056 | return 0; |
| 6057 | } |
| 6058 | |
| 6059 | __LJMP static int hlua_log_info(lua_State *L) |
| 6060 | { |
| 6061 | const char *msg; |
| 6062 | |
| 6063 | MAY_LJMP(check_args(L, 1, "info")); |
| 6064 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6065 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6066 | return 0; |
| 6067 | } |
| 6068 | |
| 6069 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6070 | { |
| 6071 | const char *msg; |
| 6072 | |
| 6073 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6074 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6075 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6076 | return 0; |
| 6077 | } |
| 6078 | |
| 6079 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6080 | { |
| 6081 | const char *msg; |
| 6082 | |
| 6083 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6084 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6085 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6086 | return 0; |
| 6087 | } |
| 6088 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6089 | __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] | 6090 | { |
| 6091 | int wakeup_ms = lua_tointeger(L, -1); |
| 6092 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6093 | 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] | 6094 | return 0; |
| 6095 | } |
| 6096 | |
| 6097 | __LJMP static int hlua_sleep(lua_State *L) |
| 6098 | { |
| 6099 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6100 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6101 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6102 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6103 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6104 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6105 | wakeup_ms = tick_add(now_ms, delay); |
| 6106 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6107 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6108 | 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] | 6109 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6110 | } |
| 6111 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6112 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6113 | { |
| 6114 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6115 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6116 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6117 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6118 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6119 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6120 | wakeup_ms = tick_add(now_ms, delay); |
| 6121 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6122 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6123 | 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] | 6124 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6125 | } |
| 6126 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6127 | /* This functionis an LUA binding. it permits to give back |
| 6128 | * the hand at the HAProxy scheduler. It is used when the |
| 6129 | * LUA processing consumes a lot of time. |
| 6130 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6131 | __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] | 6132 | { |
| 6133 | return 0; |
| 6134 | } |
| 6135 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6136 | __LJMP static int hlua_yield(lua_State *L) |
| 6137 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6138 | 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] | 6139 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6140 | } |
| 6141 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6142 | /* This function change the nice of the currently executed |
| 6143 | * task. It is used set low or high priority at the current |
| 6144 | * task. |
| 6145 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6146 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6147 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6148 | struct hlua *hlua; |
| 6149 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6150 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6151 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6152 | hlua = hlua_gethlua(L); |
| 6153 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6154 | |
| 6155 | /* If he task is not set, I'm in a start mode. */ |
| 6156 | if (!hlua || !hlua->task) |
| 6157 | return 0; |
| 6158 | |
| 6159 | if (nice < -1024) |
| 6160 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6161 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6162 | nice = 1024; |
| 6163 | |
| 6164 | hlua->task->nice = nice; |
| 6165 | return 0; |
| 6166 | } |
| 6167 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6168 | /* 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] | 6169 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6170 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6171 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6172 | * |
| 6173 | * Task wrapper are longjmp safe because the only one Lua code |
| 6174 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6175 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6176 | 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] | 6177 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6178 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6179 | enum hlua_exec status; |
| 6180 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6181 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6182 | task_set_affinity(task, tid_bit); |
| 6183 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6184 | /* If it is the first call to the task, we must initialize the |
| 6185 | * execution timeouts. |
| 6186 | */ |
| 6187 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6188 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6189 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6190 | /* Execute the Lua code. */ |
| 6191 | status = hlua_ctx_resume(hlua, 1); |
| 6192 | |
| 6193 | switch (status) { |
| 6194 | /* finished or yield */ |
| 6195 | case HLUA_E_OK: |
| 6196 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6197 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6198 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6199 | break; |
| 6200 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6201 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6202 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6203 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6204 | break; |
| 6205 | |
| 6206 | /* finished with error. */ |
| 6207 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6208 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6209 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6210 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6211 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6212 | break; |
| 6213 | |
| 6214 | case HLUA_E_ERR: |
| 6215 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6216 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6217 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6218 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6219 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6220 | break; |
| 6221 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6222 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6223 | } |
| 6224 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6225 | /* This function is an LUA binding that register LUA function to be |
| 6226 | * executed after the HAProxy configuration parsing and before the |
| 6227 | * HAProxy scheduler starts. This function expect only one LUA |
| 6228 | * argument that is a function. This function returns nothing, but |
| 6229 | * throws if an error is encountered. |
| 6230 | */ |
| 6231 | __LJMP static int hlua_register_init(lua_State *L) |
| 6232 | { |
| 6233 | struct hlua_init_function *init; |
| 6234 | int ref; |
| 6235 | |
| 6236 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6237 | |
| 6238 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6239 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6240 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6241 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6242 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6243 | |
| 6244 | init->function_ref = ref; |
| 6245 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6246 | return 0; |
| 6247 | } |
| 6248 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6249 | /* This functio is an LUA binding. It permits to register a task |
| 6250 | * executed in parallel of the main HAroxy activity. The task is |
| 6251 | * created and it is set in the HAProxy scheduler. It can be called |
| 6252 | * from the "init" section, "post init" or during the runtime. |
| 6253 | * |
| 6254 | * Lua prototype: |
| 6255 | * |
| 6256 | * <none> core.register_task(<function>) |
| 6257 | */ |
| 6258 | static int hlua_register_task(lua_State *L) |
| 6259 | { |
| 6260 | struct hlua *hlua; |
| 6261 | struct task *task; |
| 6262 | int ref; |
| 6263 | |
| 6264 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6265 | |
| 6266 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6267 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6268 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6269 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6270 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6271 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6272 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6273 | if (!task) |
| 6274 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6275 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6276 | task->context = hlua; |
| 6277 | task->process = hlua_process_task; |
| 6278 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6279 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6280 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6281 | |
| 6282 | /* Restore the function in the stack. */ |
| 6283 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6284 | hlua->nargs = 0; |
| 6285 | |
| 6286 | /* Schedule task. */ |
| 6287 | task_schedule(task, now_ms); |
| 6288 | |
| 6289 | return 0; |
| 6290 | } |
| 6291 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6292 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6293 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6294 | * resume converters. |
| 6295 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6296 | 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] | 6297 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6298 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6299 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6300 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6301 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6302 | if (!stream) |
| 6303 | return 0; |
| 6304 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6305 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6306 | * Lua context can be not initialized. This behavior |
| 6307 | * permits to save performances because a systematic |
| 6308 | * Lua initialization cause 5% performances loss. |
| 6309 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6310 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6311 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6312 | if (!stream->hlua) { |
| 6313 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6314 | return 0; |
| 6315 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6316 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6317 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6318 | return 0; |
| 6319 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6320 | } |
| 6321 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6322 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6323 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6324 | |
| 6325 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6326 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6327 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6328 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6329 | else |
| 6330 | error = "critical error"; |
| 6331 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6332 | return 0; |
| 6333 | } |
| 6334 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6335 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6336 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6337 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6338 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6339 | return 0; |
| 6340 | } |
| 6341 | |
| 6342 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6343 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6344 | |
| 6345 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6346 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6347 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6348 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6349 | return 0; |
| 6350 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6351 | hlua_smp2lua(stream->hlua->T, smp); |
| 6352 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6353 | |
| 6354 | /* push keywords in the stack. */ |
| 6355 | if (arg_p) { |
| 6356 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6357 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6358 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6359 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6360 | return 0; |
| 6361 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6362 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6363 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6364 | } |
| 6365 | } |
| 6366 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6367 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6368 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6369 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6370 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6371 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6375 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6376 | /* finished. */ |
| 6377 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6378 | /* If the stack is empty, the function fails. */ |
| 6379 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6380 | return 0; |
| 6381 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6382 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6383 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6384 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6385 | return 1; |
| 6386 | |
| 6387 | /* yield. */ |
| 6388 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6389 | 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] | 6390 | return 0; |
| 6391 | |
| 6392 | /* finished with error. */ |
| 6393 | case HLUA_E_ERRMSG: |
| 6394 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6395 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6396 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6397 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6398 | return 0; |
| 6399 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6400 | case HLUA_E_ETMOUT: |
| 6401 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6402 | return 0; |
| 6403 | |
| 6404 | case HLUA_E_NOMEM: |
| 6405 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6406 | return 0; |
| 6407 | |
| 6408 | case HLUA_E_YIELD: |
| 6409 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6410 | return 0; |
| 6411 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6412 | case HLUA_E_ERR: |
| 6413 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6414 | 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] | 6415 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6416 | |
| 6417 | default: |
| 6418 | return 0; |
| 6419 | } |
| 6420 | } |
| 6421 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6422 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6423 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6424 | * resume sample-fetches. This function will be called by the sample |
| 6425 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6426 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6427 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6428 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6429 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6430 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6431 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6432 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6433 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6434 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6435 | if (!stream) |
| 6436 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6437 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6438 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6439 | * Lua context can be not initialized. This behavior |
| 6440 | * permits to save performances because a systematic |
| 6441 | * Lua initialization cause 5% performances loss. |
| 6442 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6443 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6444 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6445 | if (!stream->hlua) { |
| 6446 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6447 | return 0; |
| 6448 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6449 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6450 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6451 | return 0; |
| 6452 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6453 | } |
| 6454 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6455 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6456 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6457 | |
| 6458 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6459 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6460 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6461 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6462 | else |
| 6463 | error = "critical error"; |
| 6464 | 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] | 6465 | return 0; |
| 6466 | } |
| 6467 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6468 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6469 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6470 | 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] | 6471 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6472 | return 0; |
| 6473 | } |
| 6474 | |
| 6475 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6476 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6477 | |
| 6478 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6479 | 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] | 6480 | 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] | 6481 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6482 | return 0; |
| 6483 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6484 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6485 | |
| 6486 | /* push keywords in the stack. */ |
| 6487 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6488 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6489 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6490 | 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] | 6491 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6492 | return 0; |
| 6493 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6494 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6495 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6496 | } |
| 6497 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6498 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6499 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6500 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6501 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6502 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6506 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6507 | /* finished. */ |
| 6508 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6509 | /* If the stack is empty, the function fails. */ |
| 6510 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6511 | return 0; |
| 6512 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6513 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6514 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6515 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6516 | |
| 6517 | /* Set the end of execution flag. */ |
| 6518 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6519 | return 1; |
| 6520 | |
| 6521 | /* yield. */ |
| 6522 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6523 | 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] | 6524 | return 0; |
| 6525 | |
| 6526 | /* finished with error. */ |
| 6527 | case HLUA_E_ERRMSG: |
| 6528 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6529 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6530 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6531 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6532 | return 0; |
| 6533 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6534 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6535 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6536 | return 0; |
| 6537 | |
| 6538 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6539 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6540 | return 0; |
| 6541 | |
| 6542 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6543 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6544 | return 0; |
| 6545 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6546 | case HLUA_E_ERR: |
| 6547 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6548 | 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] | 6549 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6550 | |
| 6551 | default: |
| 6552 | return 0; |
| 6553 | } |
| 6554 | } |
| 6555 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6556 | /* This function is an LUA binding used for registering |
| 6557 | * "sample-conv" functions. It expects a converter name used |
| 6558 | * in the haproxy configuration file, and an LUA function. |
| 6559 | */ |
| 6560 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6561 | { |
| 6562 | struct sample_conv_kw_list *sck; |
| 6563 | const char *name; |
| 6564 | int ref; |
| 6565 | int len; |
| 6566 | struct hlua_function *fcn; |
| 6567 | |
| 6568 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6569 | |
| 6570 | /* First argument : converter name. */ |
| 6571 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6572 | |
| 6573 | /* Second argument : lua function. */ |
| 6574 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6575 | |
| 6576 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6577 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6578 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6579 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6580 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6581 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6582 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6583 | |
| 6584 | /* Fill fcn. */ |
| 6585 | fcn->name = strdup(name); |
| 6586 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6587 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6588 | fcn->function_ref = ref; |
| 6589 | |
| 6590 | /* List head */ |
| 6591 | sck->list.n = sck->list.p = NULL; |
| 6592 | |
| 6593 | /* converter keyword. */ |
| 6594 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6595 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6596 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6597 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6598 | |
| 6599 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6600 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6601 | 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] | 6602 | sck->kw[0].val_args = NULL; |
| 6603 | sck->kw[0].in_type = SMP_T_STR; |
| 6604 | sck->kw[0].out_type = SMP_T_STR; |
| 6605 | sck->kw[0].private = fcn; |
| 6606 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6607 | /* Register this new converter */ |
| 6608 | sample_register_convs(sck); |
| 6609 | |
| 6610 | return 0; |
| 6611 | } |
| 6612 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6613 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6614 | * "sample-fetch" functions. It expects a converter name used |
| 6615 | * in the haproxy configuration file, and an LUA function. |
| 6616 | */ |
| 6617 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6618 | { |
| 6619 | const char *name; |
| 6620 | int ref; |
| 6621 | int len; |
| 6622 | struct sample_fetch_kw_list *sfk; |
| 6623 | struct hlua_function *fcn; |
| 6624 | |
| 6625 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6626 | |
| 6627 | /* First argument : sample-fetch name. */ |
| 6628 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6629 | |
| 6630 | /* Second argument : lua function. */ |
| 6631 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6632 | |
| 6633 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6634 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6635 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6636 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6637 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6638 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6639 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6640 | |
| 6641 | /* Fill fcn. */ |
| 6642 | fcn->name = strdup(name); |
| 6643 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6644 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6645 | fcn->function_ref = ref; |
| 6646 | |
| 6647 | /* List head */ |
| 6648 | sfk->list.n = sfk->list.p = NULL; |
| 6649 | |
| 6650 | /* sample-fetch keyword. */ |
| 6651 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6652 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6653 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6654 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6655 | |
| 6656 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6657 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6658 | 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] | 6659 | sfk->kw[0].val_args = NULL; |
| 6660 | sfk->kw[0].out_type = SMP_T_STR; |
| 6661 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6662 | sfk->kw[0].val = 0; |
| 6663 | sfk->kw[0].private = fcn; |
| 6664 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6665 | /* Register this new fetch. */ |
| 6666 | sample_register_fetches(sfk); |
| 6667 | |
| 6668 | return 0; |
| 6669 | } |
| 6670 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6671 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6672 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6673 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6674 | { |
| 6675 | struct hlua *hlua = hlua_gethlua(L); |
| 6676 | unsigned int delay; |
| 6677 | unsigned int wakeup_ms; |
| 6678 | |
| 6679 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6680 | |
| 6681 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6682 | wakeup_ms = tick_add(now_ms, delay); |
| 6683 | hlua->wake_time = wakeup_ms; |
| 6684 | return 0; |
| 6685 | } |
| 6686 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6687 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6688 | * wrapper during the initialisation period. This function may return any |
| 6689 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6690 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6691 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6692 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6693 | 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] | 6694 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6695 | { |
| 6696 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6697 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6698 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6699 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6700 | |
| 6701 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6702 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6703 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6704 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6705 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6706 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6707 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6708 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6709 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6710 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6711 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6712 | * Lua context can be not initialized. This behavior |
| 6713 | * permits to save performances because a systematic |
| 6714 | * Lua initialization cause 5% performances loss. |
| 6715 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6716 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6717 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6718 | if (!s->hlua) { |
| 6719 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6720 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6721 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6722 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6723 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6724 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6725 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6726 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6727 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6728 | } |
| 6729 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6730 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6731 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6732 | |
| 6733 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6734 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6735 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6736 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6737 | else |
| 6738 | error = "critical error"; |
| 6739 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6740 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6741 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6742 | } |
| 6743 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6744 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6745 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6746 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6747 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6748 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6749 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6750 | } |
| 6751 | |
| 6752 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6753 | 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] | 6754 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6755 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6756 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6757 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6758 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6759 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6760 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6761 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6762 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6763 | |
| 6764 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6765 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6766 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6767 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6768 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6769 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6770 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6771 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6772 | lua_pushstring(s->hlua->T, *arg); |
| 6773 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6774 | } |
| 6775 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6776 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6777 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6778 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6779 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6780 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6784 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6785 | /* finished. */ |
| 6786 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6787 | /* Catch the return value */ |
| 6788 | if (lua_gettop(s->hlua->T) > 0) |
| 6789 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6790 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6791 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6792 | if (act_ret == ACT_RET_YIELD) { |
| 6793 | if (flags & ACT_OPT_FINAL) |
| 6794 | goto err_yield; |
| 6795 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6796 | if (dir == SMP_OPT_DIR_REQ) |
| 6797 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6798 | s->hlua->wake_time); |
| 6799 | else |
| 6800 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6801 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6802 | } |
| 6803 | goto end; |
| 6804 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6805 | /* yield. */ |
| 6806 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6807 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6808 | if (dir == SMP_OPT_DIR_REQ) |
| 6809 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6810 | s->hlua->wake_time); |
| 6811 | else |
| 6812 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6813 | s->hlua->wake_time); |
| 6814 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6815 | /* Some actions can be wake up when a "write" event |
| 6816 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6817 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6818 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6819 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6820 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6821 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6822 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6823 | act_ret = ACT_RET_YIELD; |
| 6824 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6825 | |
| 6826 | /* finished with error. */ |
| 6827 | case HLUA_E_ERRMSG: |
| 6828 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6829 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6830 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6831 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6832 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6833 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6834 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6835 | 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] | 6836 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6837 | |
| 6838 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6839 | 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] | 6840 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6841 | |
| 6842 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6843 | err_yield: |
| 6844 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6845 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6846 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6847 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6848 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6849 | case HLUA_E_ERR: |
| 6850 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6851 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6852 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6853 | |
| 6854 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6855 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6856 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6857 | |
| 6858 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6859 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6860 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6861 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6862 | } |
| 6863 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6864 | 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] | 6865 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6866 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6867 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6868 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6869 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6870 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6871 | } |
| 6872 | |
| 6873 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6874 | { |
| 6875 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6876 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6877 | struct task *task; |
| 6878 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6879 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6880 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6881 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6882 | if (!hlua) { |
| 6883 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6884 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6885 | return 0; |
| 6886 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6887 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6888 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6889 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6890 | |
| 6891 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6892 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6893 | if (!task) { |
| 6894 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6895 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6896 | return 0; |
| 6897 | } |
| 6898 | task->nice = 0; |
| 6899 | task->context = ctx; |
| 6900 | task->process = hlua_applet_wakeup; |
| 6901 | ctx->ctx.hlua_apptcp.task = task; |
| 6902 | |
| 6903 | /* In the execution wrappers linked with a stream, the |
| 6904 | * Lua context can be not initialized. This behavior |
| 6905 | * permits to save performances because a systematic |
| 6906 | * Lua initialization cause 5% performances loss. |
| 6907 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6908 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6909 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6910 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6911 | return 0; |
| 6912 | } |
| 6913 | |
| 6914 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6915 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6916 | |
| 6917 | /* The following Lua calls can fail. */ |
| 6918 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6919 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6920 | error = lua_tostring(hlua->T, -1); |
| 6921 | else |
| 6922 | error = "critical error"; |
| 6923 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6924 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6925 | return 0; |
| 6926 | } |
| 6927 | |
| 6928 | /* Check stack available size. */ |
| 6929 | if (!lua_checkstack(hlua->T, 1)) { |
| 6930 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6931 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6932 | RESET_SAFE_LJMP(hlua->T); |
| 6933 | return 0; |
| 6934 | } |
| 6935 | |
| 6936 | /* Restore the function in the stack. */ |
| 6937 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6938 | |
| 6939 | /* Create and and push object stream in the stack. */ |
| 6940 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6941 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6942 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6943 | RESET_SAFE_LJMP(hlua->T); |
| 6944 | return 0; |
| 6945 | } |
| 6946 | hlua->nargs = 1; |
| 6947 | |
| 6948 | /* push keywords in the stack. */ |
| 6949 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6950 | if (!lua_checkstack(hlua->T, 1)) { |
| 6951 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6952 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6953 | RESET_SAFE_LJMP(hlua->T); |
| 6954 | return 0; |
| 6955 | } |
| 6956 | lua_pushstring(hlua->T, *arg); |
| 6957 | hlua->nargs++; |
| 6958 | } |
| 6959 | |
| 6960 | RESET_SAFE_LJMP(hlua->T); |
| 6961 | |
| 6962 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6963 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6964 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6965 | |
| 6966 | return 1; |
| 6967 | } |
| 6968 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6969 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6970 | { |
| 6971 | struct stream_interface *si = ctx->owner; |
| 6972 | struct stream *strm = si_strm(si); |
| 6973 | struct channel *res = si_ic(si); |
| 6974 | struct act_rule *rule = ctx->rule; |
| 6975 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6976 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6977 | |
| 6978 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6979 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6980 | /* eat the whole request */ |
| 6981 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6982 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6983 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6984 | |
| 6985 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6986 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6987 | return; |
| 6988 | |
| 6989 | /* Execute the function. */ |
| 6990 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6991 | /* finished. */ |
| 6992 | case HLUA_E_OK: |
| 6993 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6994 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6995 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6996 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6997 | res->flags |= CF_READ_NULL; |
| 6998 | si_shutr(si); |
| 6999 | return; |
| 7000 | |
| 7001 | /* yield. */ |
| 7002 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7003 | if (hlua->wake_time != TICK_ETERNITY) |
| 7004 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7005 | return; |
| 7006 | |
| 7007 | /* finished with error. */ |
| 7008 | case HLUA_E_ERRMSG: |
| 7009 | /* Display log. */ |
| 7010 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7011 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7012 | lua_pop(hlua->T, 1); |
| 7013 | goto error; |
| 7014 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7015 | case HLUA_E_ETMOUT: |
| 7016 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7017 | rule->arg.hlua_rule->fcn.name); |
| 7018 | goto error; |
| 7019 | |
| 7020 | case HLUA_E_NOMEM: |
| 7021 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7022 | rule->arg.hlua_rule->fcn.name); |
| 7023 | goto error; |
| 7024 | |
| 7025 | case HLUA_E_YIELD: /* unexpected */ |
| 7026 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7027 | rule->arg.hlua_rule->fcn.name); |
| 7028 | goto error; |
| 7029 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7030 | case HLUA_E_ERR: |
| 7031 | /* Display log. */ |
| 7032 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7033 | rule->arg.hlua_rule->fcn.name); |
| 7034 | goto error; |
| 7035 | |
| 7036 | default: |
| 7037 | goto error; |
| 7038 | } |
| 7039 | |
| 7040 | error: |
| 7041 | |
| 7042 | /* For all other cases, just close the stream. */ |
| 7043 | si_shutw(si); |
| 7044 | si_shutr(si); |
| 7045 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7046 | } |
| 7047 | |
| 7048 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7049 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7050 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7051 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7052 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7053 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7054 | } |
| 7055 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7056 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7057 | * an errors occurs and -1 if more data are required for initializing |
| 7058 | * the applet. |
| 7059 | */ |
| 7060 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7061 | { |
| 7062 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7063 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7064 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7065 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7066 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7067 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7068 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7069 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7070 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7071 | if (!hlua) { |
| 7072 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7073 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7074 | return 0; |
| 7075 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7076 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7077 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7078 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7079 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7080 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7081 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7082 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7083 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7084 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7085 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7086 | if (!task) { |
| 7087 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7088 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7089 | return 0; |
| 7090 | } |
| 7091 | task->nice = 0; |
| 7092 | task->context = ctx; |
| 7093 | task->process = hlua_applet_wakeup; |
| 7094 | ctx->ctx.hlua_apphttp.task = task; |
| 7095 | |
| 7096 | /* In the execution wrappers linked with a stream, the |
| 7097 | * Lua context can be not initialized. This behavior |
| 7098 | * permits to save performances because a systematic |
| 7099 | * Lua initialization cause 5% performances loss. |
| 7100 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7101 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7102 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7103 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7104 | return 0; |
| 7105 | } |
| 7106 | |
| 7107 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7108 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7109 | |
| 7110 | /* The following Lua calls can fail. */ |
| 7111 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7112 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7113 | error = lua_tostring(hlua->T, -1); |
| 7114 | else |
| 7115 | error = "critical error"; |
| 7116 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7117 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7118 | return 0; |
| 7119 | } |
| 7120 | |
| 7121 | /* Check stack available size. */ |
| 7122 | if (!lua_checkstack(hlua->T, 1)) { |
| 7123 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7124 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7125 | RESET_SAFE_LJMP(hlua->T); |
| 7126 | return 0; |
| 7127 | } |
| 7128 | |
| 7129 | /* Restore the function in the stack. */ |
| 7130 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7131 | |
| 7132 | /* Create and and push object stream in the stack. */ |
| 7133 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7134 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7135 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7136 | RESET_SAFE_LJMP(hlua->T); |
| 7137 | return 0; |
| 7138 | } |
| 7139 | hlua->nargs = 1; |
| 7140 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7141 | /* push keywords in the stack. */ |
| 7142 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7143 | if (!lua_checkstack(hlua->T, 1)) { |
| 7144 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7145 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7146 | RESET_SAFE_LJMP(hlua->T); |
| 7147 | return 0; |
| 7148 | } |
| 7149 | lua_pushstring(hlua->T, *arg); |
| 7150 | hlua->nargs++; |
| 7151 | } |
| 7152 | |
| 7153 | RESET_SAFE_LJMP(hlua->T); |
| 7154 | |
| 7155 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7156 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7157 | |
| 7158 | return 1; |
| 7159 | } |
| 7160 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7161 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7162 | { |
| 7163 | struct stream_interface *si = ctx->owner; |
| 7164 | struct stream *strm = si_strm(si); |
| 7165 | struct channel *req = si_oc(si); |
| 7166 | struct channel *res = si_ic(si); |
| 7167 | struct act_rule *rule = ctx->rule; |
| 7168 | struct proxy *px = strm->be; |
| 7169 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7170 | struct htx *req_htx, *res_htx; |
| 7171 | |
| 7172 | res_htx = htx_from_buf(&res->buf); |
| 7173 | |
| 7174 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7175 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7176 | goto out; |
| 7177 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7178 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7179 | if (!b_size(&res->buf)) { |
| 7180 | si_rx_room_blk(si); |
| 7181 | goto out; |
| 7182 | } |
| 7183 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7184 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7185 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7186 | |
| 7187 | /* Set the currently running flag. */ |
| 7188 | if (!HLUA_IS_RUNNING(hlua) && |
| 7189 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7190 | struct htx_blk *blk; |
| 7191 | size_t count = co_data(req); |
| 7192 | |
| 7193 | if (!count) { |
| 7194 | si_cant_get(si); |
| 7195 | goto out; |
| 7196 | } |
| 7197 | |
| 7198 | /* We need to flush the request header. This left the body for |
| 7199 | * the Lua. |
| 7200 | */ |
| 7201 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7202 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7203 | while (count && blk) { |
| 7204 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7205 | uint32_t sz = htx_get_blksz(blk); |
| 7206 | |
| 7207 | if (sz > count) { |
| 7208 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7209 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7210 | goto out; |
| 7211 | } |
| 7212 | |
| 7213 | count -= sz; |
| 7214 | co_set_data(req, co_data(req) - sz); |
| 7215 | blk = htx_remove_blk(req_htx, blk); |
| 7216 | |
| 7217 | if (type == HTX_BLK_EOH) |
| 7218 | break; |
| 7219 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7220 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7221 | } |
| 7222 | |
| 7223 | /* Executes The applet if it is not done. */ |
| 7224 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7225 | |
| 7226 | /* Execute the function. */ |
| 7227 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7228 | /* finished. */ |
| 7229 | case HLUA_E_OK: |
| 7230 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7231 | break; |
| 7232 | |
| 7233 | /* yield. */ |
| 7234 | case HLUA_E_AGAIN: |
| 7235 | if (hlua->wake_time != TICK_ETERNITY) |
| 7236 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7237 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7238 | |
| 7239 | /* finished with error. */ |
| 7240 | case HLUA_E_ERRMSG: |
| 7241 | /* Display log. */ |
| 7242 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7243 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7244 | lua_pop(hlua->T, 1); |
| 7245 | goto error; |
| 7246 | |
| 7247 | case HLUA_E_ETMOUT: |
| 7248 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7249 | rule->arg.hlua_rule->fcn.name); |
| 7250 | goto error; |
| 7251 | |
| 7252 | case HLUA_E_NOMEM: |
| 7253 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7254 | rule->arg.hlua_rule->fcn.name); |
| 7255 | goto error; |
| 7256 | |
| 7257 | case HLUA_E_YIELD: /* unexpected */ |
| 7258 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7259 | rule->arg.hlua_rule->fcn.name); |
| 7260 | goto error; |
| 7261 | |
| 7262 | case HLUA_E_ERR: |
| 7263 | /* Display log. */ |
| 7264 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7265 | rule->arg.hlua_rule->fcn.name); |
| 7266 | goto error; |
| 7267 | |
| 7268 | default: |
| 7269 | goto error; |
| 7270 | } |
| 7271 | } |
| 7272 | |
| 7273 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7274 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7275 | goto done; |
| 7276 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7277 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7278 | goto error; |
| 7279 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7280 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7281 | 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] | 7282 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7283 | si_rx_room_blk(si); |
| 7284 | goto out; |
| 7285 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7286 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7287 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7288 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7289 | } |
| 7290 | |
| 7291 | done: |
| 7292 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7293 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7294 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7295 | si_shutr(si); |
| 7296 | } |
| 7297 | |
| 7298 | /* eat the whole request */ |
| 7299 | if (co_data(req)) { |
| 7300 | req_htx = htx_from_buf(&req->buf); |
| 7301 | co_htx_skip(req, req_htx, co_data(req)); |
| 7302 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7303 | } |
| 7304 | } |
| 7305 | |
| 7306 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7307 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7308 | return; |
| 7309 | |
| 7310 | error: |
| 7311 | |
| 7312 | /* If we are in HTTP mode, and we are not send any |
| 7313 | * data, return a 500 server error in best effort: |
| 7314 | * if there is no room available in the buffer, |
| 7315 | * just close the connection. |
| 7316 | */ |
| 7317 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7318 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7319 | |
| 7320 | channel_erase(res); |
| 7321 | res->buf.data = b_data(err); |
| 7322 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7323 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7324 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7325 | } |
| 7326 | if (!(strm->flags & SF_ERR_MASK)) |
| 7327 | strm->flags |= SF_ERR_RESOURCE; |
| 7328 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7329 | goto done; |
| 7330 | } |
| 7331 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7332 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7333 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7334 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7335 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7336 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7337 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7338 | } |
| 7339 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7340 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7341 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7342 | * |
| 7343 | * This function can fail with an abort() due to an Lua critical error. |
| 7344 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7345 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7346 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7347 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7348 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7349 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7350 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7351 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7352 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7353 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7354 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7355 | if (!rule->arg.hlua_rule) { |
| 7356 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7357 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7358 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7359 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7360 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7361 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7362 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7363 | if (!rule->arg.hlua_rule->args) { |
| 7364 | memprintf(err, "out of memory error"); |
| 7365 | return ACT_RET_PRS_ERR; |
| 7366 | } |
| 7367 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7368 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7369 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7370 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7371 | /* Expect some arguments */ |
| 7372 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7373 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7374 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7375 | return ACT_RET_PRS_ERR; |
| 7376 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7377 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7378 | if (!rule->arg.hlua_rule->args[i]) { |
| 7379 | memprintf(err, "out of memory error"); |
| 7380 | return ACT_RET_PRS_ERR; |
| 7381 | } |
| 7382 | (*cur_arg)++; |
| 7383 | } |
| 7384 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7385 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7386 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7387 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7388 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7389 | } |
| 7390 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7391 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7392 | struct act_rule *rule, char **err) |
| 7393 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7394 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7395 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7396 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7397 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7398 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7399 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7400 | * the call of this analyzer. |
| 7401 | */ |
| 7402 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7403 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7404 | return ACT_RET_PRS_ERR; |
| 7405 | } |
| 7406 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7407 | /* Memory for the rule. */ |
| 7408 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7409 | if (!rule->arg.hlua_rule) { |
| 7410 | memprintf(err, "out of memory error"); |
| 7411 | return ACT_RET_PRS_ERR; |
| 7412 | } |
| 7413 | |
| 7414 | /* Reference the Lua function and store the reference. */ |
| 7415 | rule->arg.hlua_rule->fcn = *fcn; |
| 7416 | |
| 7417 | /* TODO: later accept arguments. */ |
| 7418 | rule->arg.hlua_rule->args = NULL; |
| 7419 | |
| 7420 | /* Add applet pointer in the rule. */ |
| 7421 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7422 | rule->applet.name = fcn->name; |
| 7423 | rule->applet.init = hlua_applet_http_init; |
| 7424 | rule->applet.fct = hlua_applet_http_fct; |
| 7425 | rule->applet.release = hlua_applet_http_release; |
| 7426 | rule->applet.timeout = hlua_timeout_applet; |
| 7427 | |
| 7428 | return ACT_RET_PRS_OK; |
| 7429 | } |
| 7430 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7431 | /* This function is an LUA binding used for registering |
| 7432 | * "sample-conv" functions. It expects a converter name used |
| 7433 | * in the haproxy configuration file, and an LUA function. |
| 7434 | */ |
| 7435 | __LJMP static int hlua_register_action(lua_State *L) |
| 7436 | { |
| 7437 | struct action_kw_list *akl; |
| 7438 | const char *name; |
| 7439 | int ref; |
| 7440 | int len; |
| 7441 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7442 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7443 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7444 | /* Initialise the number of expected arguments at 0. */ |
| 7445 | nargs = 0; |
| 7446 | |
| 7447 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7448 | 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] | 7449 | |
| 7450 | /* First argument : converter name. */ |
| 7451 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7452 | |
| 7453 | /* Second argument : environment. */ |
| 7454 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7455 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7456 | |
| 7457 | /* Third argument : lua function. */ |
| 7458 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7459 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7460 | /* 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] | 7461 | if (lua_gettop(L) >= 4) |
| 7462 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7463 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7464 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7465 | lua_pushnil(L); |
| 7466 | while (lua_next(L, 2) != 0) { |
| 7467 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7468 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7469 | |
| 7470 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7471 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7472 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7473 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7474 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7475 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7476 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7477 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7478 | |
| 7479 | /* Fill fcn. */ |
| 7480 | fcn->name = strdup(name); |
| 7481 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7482 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7483 | fcn->function_ref = ref; |
| 7484 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7485 | /* Set the expected number od arguments. */ |
| 7486 | fcn->nargs = nargs; |
| 7487 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7488 | /* List head */ |
| 7489 | akl->list.n = akl->list.p = NULL; |
| 7490 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7491 | /* action keyword. */ |
| 7492 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7493 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7494 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7495 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7496 | |
| 7497 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7498 | |
| 7499 | akl->kw[0].match_pfx = 0; |
| 7500 | akl->kw[0].private = fcn; |
| 7501 | akl->kw[0].parse = action_register_lua; |
| 7502 | |
| 7503 | /* select the action registering point. */ |
| 7504 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7505 | tcp_req_cont_keywords_register(akl); |
| 7506 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7507 | tcp_res_cont_keywords_register(akl); |
| 7508 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7509 | http_req_keywords_register(akl); |
| 7510 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7511 | http_res_keywords_register(akl); |
| 7512 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7513 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7514 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7515 | "are expected.", lua_tostring(L, -1))); |
| 7516 | |
| 7517 | /* pop the environment string. */ |
| 7518 | lua_pop(L, 1); |
| 7519 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7520 | return ACT_RET_PRS_OK; |
| 7521 | } |
| 7522 | |
| 7523 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7524 | struct act_rule *rule, char **err) |
| 7525 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7526 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7527 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7528 | if (px->mode == PR_MODE_HTTP) { |
| 7529 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7530 | return ACT_RET_PRS_ERR; |
| 7531 | } |
| 7532 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7533 | /* Memory for the rule. */ |
| 7534 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7535 | if (!rule->arg.hlua_rule) { |
| 7536 | memprintf(err, "out of memory error"); |
| 7537 | return ACT_RET_PRS_ERR; |
| 7538 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7539 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7540 | /* Reference the Lua function and store the reference. */ |
| 7541 | rule->arg.hlua_rule->fcn = *fcn; |
| 7542 | |
| 7543 | /* TODO: later accept arguments. */ |
| 7544 | rule->arg.hlua_rule->args = NULL; |
| 7545 | |
| 7546 | /* Add applet pointer in the rule. */ |
| 7547 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7548 | rule->applet.name = fcn->name; |
| 7549 | rule->applet.init = hlua_applet_tcp_init; |
| 7550 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7551 | rule->applet.release = hlua_applet_tcp_release; |
| 7552 | rule->applet.timeout = hlua_timeout_applet; |
| 7553 | |
| 7554 | return 0; |
| 7555 | } |
| 7556 | |
| 7557 | /* This function is an LUA binding used for registering |
| 7558 | * "sample-conv" functions. It expects a converter name used |
| 7559 | * in the haproxy configuration file, and an LUA function. |
| 7560 | */ |
| 7561 | __LJMP static int hlua_register_service(lua_State *L) |
| 7562 | { |
| 7563 | struct action_kw_list *akl; |
| 7564 | const char *name; |
| 7565 | const char *env; |
| 7566 | int ref; |
| 7567 | int len; |
| 7568 | struct hlua_function *fcn; |
| 7569 | |
| 7570 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7571 | |
| 7572 | /* First argument : converter name. */ |
| 7573 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7574 | |
| 7575 | /* Second argument : environment. */ |
| 7576 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7577 | |
| 7578 | /* Third argument : lua function. */ |
| 7579 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7580 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7581 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7582 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7583 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7584 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7585 | fcn = calloc(1, sizeof(*fcn)); |
| 7586 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7587 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7588 | |
| 7589 | /* Fill fcn. */ |
| 7590 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7591 | fcn->name = calloc(1, len); |
| 7592 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7593 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7594 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7595 | fcn->function_ref = ref; |
| 7596 | |
| 7597 | /* List head */ |
| 7598 | akl->list.n = akl->list.p = NULL; |
| 7599 | |
| 7600 | /* converter keyword. */ |
| 7601 | len = strlen("lua.") + strlen(name) + 1; |
| 7602 | akl->kw[0].kw = calloc(1, len); |
| 7603 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7604 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7605 | |
| 7606 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7607 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7608 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7609 | if (strcmp(env, "tcp") == 0) |
| 7610 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7611 | else if (strcmp(env, "http") == 0) |
| 7612 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7613 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7614 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7615 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7616 | |
| 7617 | akl->kw[0].match_pfx = 0; |
| 7618 | akl->kw[0].private = fcn; |
| 7619 | |
| 7620 | /* End of array. */ |
| 7621 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7622 | |
| 7623 | /* Register this new converter */ |
| 7624 | service_keywords_register(akl); |
| 7625 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7626 | return 0; |
| 7627 | } |
| 7628 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7629 | /* This function initialises Lua cli handler. It copies the |
| 7630 | * arguments in the Lua stack and create channel IO objects. |
| 7631 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7632 | 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] | 7633 | { |
| 7634 | struct hlua *hlua; |
| 7635 | struct hlua_function *fcn; |
| 7636 | int i; |
| 7637 | const char *error; |
| 7638 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7639 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7640 | appctx->ctx.hlua_cli.fcn = private; |
| 7641 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7642 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7643 | if (!hlua) { |
| 7644 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7645 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7646 | } |
| 7647 | HLUA_INIT(hlua); |
| 7648 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7649 | |
| 7650 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7651 | * 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] | 7652 | * applet_http. It is absolutely compatible. |
| 7653 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7654 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7655 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7656 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7657 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7658 | } |
| 7659 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7660 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7661 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7662 | |
| 7663 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7664 | 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] | 7665 | 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] | 7666 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7667 | } |
| 7668 | |
| 7669 | /* The following Lua calls can fail. */ |
| 7670 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7671 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7672 | error = lua_tostring(hlua->T, -1); |
| 7673 | else |
| 7674 | error = "critical error"; |
| 7675 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7676 | goto error; |
| 7677 | } |
| 7678 | |
| 7679 | /* Check stack available size. */ |
| 7680 | if (!lua_checkstack(hlua->T, 2)) { |
| 7681 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7682 | goto error; |
| 7683 | } |
| 7684 | |
| 7685 | /* Restore the function in the stack. */ |
| 7686 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7687 | |
| 7688 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7689 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7690 | */ |
| 7691 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7692 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7693 | goto error; |
| 7694 | } |
| 7695 | hlua->nargs = 1; |
| 7696 | |
| 7697 | /* push keywords in the stack. */ |
| 7698 | for (i = 0; *args[i]; i++) { |
| 7699 | /* Check stack available size. */ |
| 7700 | if (!lua_checkstack(hlua->T, 1)) { |
| 7701 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7702 | goto error; |
| 7703 | } |
| 7704 | lua_pushstring(hlua->T, args[i]); |
| 7705 | hlua->nargs++; |
| 7706 | } |
| 7707 | |
| 7708 | /* We must initialize the execution timeouts. */ |
| 7709 | hlua->max_time = hlua_timeout_session; |
| 7710 | |
| 7711 | /* At this point the execution is safe. */ |
| 7712 | RESET_SAFE_LJMP(hlua->T); |
| 7713 | |
| 7714 | /* It's ok */ |
| 7715 | return 0; |
| 7716 | |
| 7717 | /* It's not ok. */ |
| 7718 | error: |
| 7719 | RESET_SAFE_LJMP(hlua->T); |
| 7720 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7721 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7722 | return 1; |
| 7723 | } |
| 7724 | |
| 7725 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7726 | { |
| 7727 | struct hlua *hlua; |
| 7728 | struct stream_interface *si; |
| 7729 | struct hlua_function *fcn; |
| 7730 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7731 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7732 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7733 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7734 | |
| 7735 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7736 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7737 | return 1; |
| 7738 | |
| 7739 | /* Execute the function. */ |
| 7740 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7741 | |
| 7742 | /* finished. */ |
| 7743 | case HLUA_E_OK: |
| 7744 | return 1; |
| 7745 | |
| 7746 | /* yield. */ |
| 7747 | case HLUA_E_AGAIN: |
| 7748 | /* We want write. */ |
| 7749 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7750 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7751 | /* Set the timeout. */ |
| 7752 | if (hlua->wake_time != TICK_ETERNITY) |
| 7753 | task_schedule(hlua->task, hlua->wake_time); |
| 7754 | return 0; |
| 7755 | |
| 7756 | /* finished with error. */ |
| 7757 | case HLUA_E_ERRMSG: |
| 7758 | /* Display log. */ |
| 7759 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7760 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7761 | lua_pop(hlua->T, 1); |
| 7762 | return 1; |
| 7763 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7764 | case HLUA_E_ETMOUT: |
| 7765 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7766 | fcn->name); |
| 7767 | return 1; |
| 7768 | |
| 7769 | case HLUA_E_NOMEM: |
| 7770 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7771 | fcn->name); |
| 7772 | return 1; |
| 7773 | |
| 7774 | case HLUA_E_YIELD: /* unexpected */ |
| 7775 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7776 | fcn->name); |
| 7777 | return 1; |
| 7778 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7779 | case HLUA_E_ERR: |
| 7780 | /* Display log. */ |
| 7781 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7782 | fcn->name); |
| 7783 | return 1; |
| 7784 | |
| 7785 | default: |
| 7786 | return 1; |
| 7787 | } |
| 7788 | |
| 7789 | return 1; |
| 7790 | } |
| 7791 | |
| 7792 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7793 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7794 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7795 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7796 | } |
| 7797 | |
| 7798 | /* This function is an LUA binding used for registering |
| 7799 | * new keywords in the cli. It expects a list of keywords |
| 7800 | * which are the "path". It is limited to 5 keywords. A |
| 7801 | * description of the command, a function to be executed |
| 7802 | * for the parsing and a function for io handlers. |
| 7803 | */ |
| 7804 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7805 | { |
| 7806 | struct cli_kw_list *cli_kws; |
| 7807 | const char *message; |
| 7808 | int ref_io; |
| 7809 | int len; |
| 7810 | struct hlua_function *fcn; |
| 7811 | int index; |
| 7812 | int i; |
| 7813 | |
| 7814 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7815 | |
| 7816 | /* First argument : an array of maximum 5 keywords. */ |
| 7817 | if (!lua_istable(L, 1)) |
| 7818 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7819 | |
| 7820 | /* Second argument : string with contextual message. */ |
| 7821 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7822 | |
| 7823 | /* Third and fourth argument : lua function. */ |
| 7824 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7825 | |
| 7826 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7827 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7828 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7829 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7830 | fcn = calloc(1, sizeof(*fcn)); |
| 7831 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7832 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7833 | |
| 7834 | /* Fill path. */ |
| 7835 | index = 0; |
| 7836 | lua_pushnil(L); |
| 7837 | while(lua_next(L, 1) != 0) { |
| 7838 | if (index >= 5) |
| 7839 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7840 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7841 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7842 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7843 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7844 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7845 | index++; |
| 7846 | lua_pop(L, 1); |
| 7847 | } |
| 7848 | |
| 7849 | /* Copy help message. */ |
| 7850 | cli_kws->kw[0].usage = strdup(message); |
| 7851 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7852 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7853 | |
| 7854 | /* Fill fcn io handler. */ |
| 7855 | len = strlen("<lua.cli>") + 1; |
| 7856 | for (i = 0; i < index; i++) |
| 7857 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7858 | fcn->name = calloc(1, len); |
| 7859 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7860 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7861 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7862 | for (i = 0; i < index; i++) { |
| 7863 | strncat((char *)fcn->name, ".", len); |
| 7864 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7865 | } |
| 7866 | strncat((char *)fcn->name, ">", len); |
| 7867 | fcn->function_ref = ref_io; |
| 7868 | |
| 7869 | /* Fill last entries. */ |
| 7870 | cli_kws->kw[0].private = fcn; |
| 7871 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7872 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7873 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7874 | |
| 7875 | /* Register this new converter */ |
| 7876 | cli_register_kw(cli_kws); |
| 7877 | |
| 7878 | return 0; |
| 7879 | } |
| 7880 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7881 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7882 | struct proxy *defpx, const char *file, int line, |
| 7883 | char **err, unsigned int *timeout) |
| 7884 | { |
| 7885 | const char *error; |
| 7886 | |
| 7887 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7888 | if (error == PARSE_TIME_OVER) { |
| 7889 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7890 | args[1], args[0]); |
| 7891 | return -1; |
| 7892 | } |
| 7893 | else if (error == PARSE_TIME_UNDER) { |
| 7894 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7895 | args[1], args[0]); |
| 7896 | return -1; |
| 7897 | } |
| 7898 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7899 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7900 | return -1; |
| 7901 | } |
| 7902 | return 0; |
| 7903 | } |
| 7904 | |
| 7905 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7906 | struct proxy *defpx, const char *file, int line, |
| 7907 | char **err) |
| 7908 | { |
| 7909 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7910 | file, line, err, &hlua_timeout_session); |
| 7911 | } |
| 7912 | |
| 7913 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7914 | struct proxy *defpx, const char *file, int line, |
| 7915 | char **err) |
| 7916 | { |
| 7917 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7918 | file, line, err, &hlua_timeout_task); |
| 7919 | } |
| 7920 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7921 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7922 | struct proxy *defpx, const char *file, int line, |
| 7923 | char **err) |
| 7924 | { |
| 7925 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7926 | file, line, err, &hlua_timeout_applet); |
| 7927 | } |
| 7928 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7929 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7930 | struct proxy *defpx, const char *file, int line, |
| 7931 | char **err) |
| 7932 | { |
| 7933 | char *error; |
| 7934 | |
| 7935 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7936 | if (*error != '\0') { |
| 7937 | memprintf(err, "%s: invalid number", args[0]); |
| 7938 | return -1; |
| 7939 | } |
| 7940 | return 0; |
| 7941 | } |
| 7942 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7943 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7944 | struct proxy *defpx, const char *file, int line, |
| 7945 | char **err) |
| 7946 | { |
| 7947 | char *error; |
| 7948 | |
| 7949 | if (*(args[1]) == 0) { |
| 7950 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7951 | return -1; |
| 7952 | } |
| 7953 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7954 | if (*error != '\0') { |
| 7955 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7956 | return -1; |
| 7957 | } |
| 7958 | return 0; |
| 7959 | } |
| 7960 | |
| 7961 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7962 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7963 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7964 | * the main lua entry point. |
| 7965 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7966 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7967 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7968 | * |
| 7969 | * In some error case, LUA set an error message in top of the stack. This function |
| 7970 | * 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] | 7971 | * |
| 7972 | * This function can fail with an abort() due to an Lua critical error. |
| 7973 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7974 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7975 | */ |
| 7976 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7977 | struct proxy *defpx, const char *file, int line, |
| 7978 | char **err) |
| 7979 | { |
| 7980 | int error; |
| 7981 | |
| 7982 | /* Just load and compile the file. */ |
| 7983 | error = luaL_loadfile(gL.T, args[1]); |
| 7984 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7985 | 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] | 7986 | lua_pop(gL.T, 1); |
| 7987 | return -1; |
| 7988 | } |
| 7989 | |
| 7990 | /* If no syntax error where detected, execute the code. */ |
| 7991 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7992 | switch (error) { |
| 7993 | case LUA_OK: |
| 7994 | break; |
| 7995 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7996 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7997 | lua_pop(gL.T, 1); |
| 7998 | return -1; |
| 7999 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8000 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8001 | return -1; |
| 8002 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8003 | 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] | 8004 | lua_pop(gL.T, 1); |
| 8005 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8006 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8007 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8008 | 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] | 8009 | lua_pop(gL.T, 1); |
| 8010 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8011 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8012 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8013 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8014 | lua_pop(gL.T, 1); |
| 8015 | return -1; |
| 8016 | } |
| 8017 | |
| 8018 | return 0; |
| 8019 | } |
| 8020 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8021 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8022 | * in the given <ctx>. |
| 8023 | */ |
| 8024 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 8025 | { |
| 8026 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 8027 | lua_pushstring(ctx.T, path); /* push given path */ |
| 8028 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 8029 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 8030 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 8031 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 8032 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 8033 | |
| 8034 | return 0; |
| 8035 | } |
| 8036 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8037 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 8038 | struct proxy *defpx, const char *file, int line, |
| 8039 | char **err) |
| 8040 | { |
| 8041 | char *path; |
| 8042 | char *type = "path"; |
| 8043 | if (too_many_args(2, args, err, NULL)) { |
| 8044 | return -1; |
| 8045 | } |
| 8046 | |
| 8047 | if (!(*args[1])) { |
| 8048 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 8049 | return -1; |
| 8050 | } |
| 8051 | path = args[1]; |
| 8052 | |
| 8053 | if (*args[2]) { |
| 8054 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8055 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 8056 | return -1; |
| 8057 | } |
| 8058 | type = args[2]; |
| 8059 | } |
| 8060 | |
| 8061 | return hlua_prepend_path(gL, type, path); |
| 8062 | } |
| 8063 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8064 | /* configuration keywords declaration */ |
| 8065 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8066 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8067 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8068 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8069 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8070 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8071 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8072 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8073 | { 0, NULL, NULL }, |
| 8074 | }}; |
| 8075 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8076 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8077 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8078 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8079 | /* This function can fail with an abort() due to an Lua critical error. |
| 8080 | * We are in the initialisation process of HAProxy, this abort() is |
| 8081 | * tolerated. |
| 8082 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8083 | int hlua_post_init() |
| 8084 | { |
| 8085 | struct hlua_init_function *init; |
| 8086 | const char *msg; |
| 8087 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8088 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8089 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8090 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8091 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8092 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8093 | error = lua_tostring(gL.T, -1); |
| 8094 | else |
| 8095 | error = "critical error"; |
| 8096 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8097 | exit(1); |
| 8098 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8099 | |
| 8100 | #if USE_OPENSSL |
| 8101 | /* Initialize SSL server. */ |
| 8102 | if (socket_ssl.xprt->prepare_srv) { |
| 8103 | int saved_used_backed = global.ssl_used_backend; |
| 8104 | // don't affect maxconn automatic computation |
| 8105 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8106 | global.ssl_used_backend = saved_used_backed; |
| 8107 | } |
| 8108 | #endif |
| 8109 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8110 | hlua_fcn_post_init(gL.T); |
| 8111 | RESET_SAFE_LJMP(gL.T); |
| 8112 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8113 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8114 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8115 | ret = hlua_ctx_resume(&gL, 0); |
| 8116 | switch (ret) { |
| 8117 | case HLUA_E_OK: |
| 8118 | lua_pop(gL.T, -1); |
| 8119 | return 1; |
| 8120 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8121 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8122 | return 0; |
| 8123 | case HLUA_E_ERRMSG: |
| 8124 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8125 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8126 | return 0; |
| 8127 | case HLUA_E_ERR: |
| 8128 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8129 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8130 | return 0; |
| 8131 | } |
| 8132 | } |
| 8133 | return 1; |
| 8134 | } |
| 8135 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8136 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8137 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8138 | * is the previously allocated size or the kind of object in case of a new |
| 8139 | * allocation. <nsize> is the requested new size. |
| 8140 | */ |
| 8141 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8142 | { |
| 8143 | struct hlua_mem_allocator *zone = ud; |
| 8144 | |
| 8145 | if (nsize == 0) { |
| 8146 | /* it's a free */ |
| 8147 | if (ptr) |
| 8148 | zone->allocated -= osize; |
| 8149 | free(ptr); |
| 8150 | return NULL; |
| 8151 | } |
| 8152 | |
| 8153 | if (!ptr) { |
| 8154 | /* it's a new allocation */ |
| 8155 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8156 | return NULL; |
| 8157 | |
| 8158 | ptr = malloc(nsize); |
| 8159 | if (ptr) |
| 8160 | zone->allocated += nsize; |
| 8161 | return ptr; |
| 8162 | } |
| 8163 | |
| 8164 | /* it's a realloc */ |
| 8165 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8166 | return NULL; |
| 8167 | |
| 8168 | ptr = realloc(ptr, nsize); |
| 8169 | if (ptr) |
| 8170 | zone->allocated += nsize - osize; |
| 8171 | return ptr; |
| 8172 | } |
| 8173 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8174 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8175 | * We are in the initialisation process of HAProxy, this abort() is |
| 8176 | * tolerated. |
| 8177 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8178 | void hlua_init(void) |
| 8179 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8180 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8181 | int idx; |
| 8182 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8183 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8184 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8185 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8186 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8187 | struct srv_kw *kw; |
| 8188 | int tmp_error; |
| 8189 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8190 | char *args[] = { /* SSL client configuration. */ |
| 8191 | "ssl", |
| 8192 | "verify", |
| 8193 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8194 | NULL |
| 8195 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8196 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8197 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8198 | /* Init main lua stack. */ |
| 8199 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8200 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8201 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8202 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8203 | hlua_sethlua(&gL); |
| 8204 | gL.Tref = LUA_REFNIL; |
| 8205 | gL.task = NULL; |
| 8206 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8207 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8208 | * the Lua function can fail with an abort. We are in the initialisation |
| 8209 | * process of HAProxy, this abort() is tolerated. |
| 8210 | */ |
| 8211 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8212 | /* Initialise lua. */ |
| 8213 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8214 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8215 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8216 | #ifdef HLUA_PREPEND_PATH |
| 8217 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8218 | #endif |
| 8219 | #ifdef HLUA_PREPEND_CPATH |
| 8220 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8221 | #endif |
| 8222 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8223 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8224 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8225 | /* Set safe environment for the initialisation. */ |
| 8226 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8227 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8228 | error_msg = lua_tostring(gL.T, -1); |
| 8229 | else |
| 8230 | error_msg = "critical error"; |
| 8231 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8232 | exit(1); |
| 8233 | } |
| 8234 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8235 | /* |
| 8236 | * |
| 8237 | * Create "core" object. |
| 8238 | * |
| 8239 | */ |
| 8240 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8241 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8242 | lua_newtable(gL.T); |
| 8243 | |
| 8244 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8245 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8246 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8247 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8248 | /* Register special functions. */ |
| 8249 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8250 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8251 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8252 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8253 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8254 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8255 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8256 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8257 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8258 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8259 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8260 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8261 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8262 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8263 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8264 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8265 | hlua_class_function(gL.T, "log", hlua_log); |
| 8266 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8267 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8268 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8269 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8270 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8271 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8272 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8273 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8274 | |
| 8275 | /* |
| 8276 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8277 | * Create "act" object. |
| 8278 | * |
| 8279 | */ |
| 8280 | |
| 8281 | /* This table entry is the object "act" base. */ |
| 8282 | lua_newtable(gL.T); |
| 8283 | |
| 8284 | /* push action return constants */ |
| 8285 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8286 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8287 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8288 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8289 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8290 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8291 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8292 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8293 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8294 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8295 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8296 | lua_setglobal(gL.T, "act"); |
| 8297 | |
| 8298 | /* |
| 8299 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8300 | * Register class Map |
| 8301 | * |
| 8302 | */ |
| 8303 | |
| 8304 | /* This table entry is the object "Map" base. */ |
| 8305 | lua_newtable(gL.T); |
| 8306 | |
| 8307 | /* register pattern types. */ |
| 8308 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8309 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8310 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8311 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8312 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8313 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8314 | |
| 8315 | /* register constructor. */ |
| 8316 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8317 | |
| 8318 | /* Create and fill the metatable. */ |
| 8319 | lua_newtable(gL.T); |
| 8320 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8321 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8322 | lua_pushstring(gL.T, "__index"); |
| 8323 | lua_newtable(gL.T); |
| 8324 | |
| 8325 | /* Register . */ |
| 8326 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8327 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8328 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8329 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8330 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8331 | /* Register previous table in the registry with reference and named entry. |
| 8332 | * The function hlua_register_metatable() pops the stack, so we |
| 8333 | * previously create a copy of the table. |
| 8334 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8335 | 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] | 8336 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8337 | |
| 8338 | /* Assign the metatable to the mai Map object. */ |
| 8339 | lua_setmetatable(gL.T, -2); |
| 8340 | |
| 8341 | /* Set a name to the table. */ |
| 8342 | lua_setglobal(gL.T, "Map"); |
| 8343 | |
| 8344 | /* |
| 8345 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8346 | * Register class Channel |
| 8347 | * |
| 8348 | */ |
| 8349 | |
| 8350 | /* Create and fill the metatable. */ |
| 8351 | lua_newtable(gL.T); |
| 8352 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8353 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8354 | lua_pushstring(gL.T, "__index"); |
| 8355 | lua_newtable(gL.T); |
| 8356 | |
| 8357 | /* Register . */ |
| 8358 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8359 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8360 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8361 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8362 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8363 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8364 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8365 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8366 | 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] | 8367 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8368 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8369 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8370 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8371 | |
| 8372 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8373 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8374 | |
| 8375 | /* |
| 8376 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8377 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8378 | * |
| 8379 | */ |
| 8380 | |
| 8381 | /* Create and fill the metatable. */ |
| 8382 | lua_newtable(gL.T); |
| 8383 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8384 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8385 | lua_pushstring(gL.T, "__index"); |
| 8386 | lua_newtable(gL.T); |
| 8387 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8388 | /* Browse existing fetches and create the associated |
| 8389 | * object method. |
| 8390 | */ |
| 8391 | sf = NULL; |
| 8392 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8393 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8394 | * by an underscore. |
| 8395 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8396 | strncpy(trash.area, sf->kw, trash.size); |
| 8397 | trash.area[trash.size - 1] = '\0'; |
| 8398 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8399 | if (*p == '.' || *p == '-' || *p == '+') |
| 8400 | *p = '_'; |
| 8401 | |
| 8402 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8403 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8404 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8405 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8406 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8407 | } |
| 8408 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8409 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8410 | |
| 8411 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8412 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8413 | |
| 8414 | /* |
| 8415 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8416 | * Register class Converters |
| 8417 | * |
| 8418 | */ |
| 8419 | |
| 8420 | /* Create and fill the metatable. */ |
| 8421 | lua_newtable(gL.T); |
| 8422 | |
| 8423 | /* Create and fill the __index entry. */ |
| 8424 | lua_pushstring(gL.T, "__index"); |
| 8425 | lua_newtable(gL.T); |
| 8426 | |
| 8427 | /* Browse existing converters and create the associated |
| 8428 | * object method. |
| 8429 | */ |
| 8430 | sc = NULL; |
| 8431 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8432 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8433 | * by an underscore. |
| 8434 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8435 | strncpy(trash.area, sc->kw, trash.size); |
| 8436 | trash.area[trash.size - 1] = '\0'; |
| 8437 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8438 | if (*p == '.' || *p == '-' || *p == '+') |
| 8439 | *p = '_'; |
| 8440 | |
| 8441 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8442 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8443 | lua_pushlightuserdata(gL.T, sc); |
| 8444 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8445 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8446 | } |
| 8447 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8448 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8449 | |
| 8450 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8451 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8452 | |
| 8453 | /* |
| 8454 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8455 | * Register class HTTP |
| 8456 | * |
| 8457 | */ |
| 8458 | |
| 8459 | /* Create and fill the metatable. */ |
| 8460 | lua_newtable(gL.T); |
| 8461 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8462 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8463 | lua_pushstring(gL.T, "__index"); |
| 8464 | lua_newtable(gL.T); |
| 8465 | |
| 8466 | /* Register Lua functions. */ |
| 8467 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8468 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8469 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8470 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8471 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8472 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8473 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8474 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8475 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8476 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8477 | |
| 8478 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8479 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8480 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8481 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8482 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8483 | 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] | 8484 | 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] | 8485 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8486 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8487 | |
| 8488 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8489 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8490 | |
| 8491 | /* |
| 8492 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8493 | * Register class AppletTCP |
| 8494 | * |
| 8495 | */ |
| 8496 | |
| 8497 | /* Create and fill the metatable. */ |
| 8498 | lua_newtable(gL.T); |
| 8499 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8500 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8501 | lua_pushstring(gL.T, "__index"); |
| 8502 | lua_newtable(gL.T); |
| 8503 | |
| 8504 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8505 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8506 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8507 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8508 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8509 | 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] | 8510 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8511 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8512 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8513 | |
| 8514 | lua_settable(gL.T, -3); |
| 8515 | |
| 8516 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8517 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8518 | |
| 8519 | /* |
| 8520 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8521 | * Register class AppletHTTP |
| 8522 | * |
| 8523 | */ |
| 8524 | |
| 8525 | /* Create and fill the metatable. */ |
| 8526 | lua_newtable(gL.T); |
| 8527 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8528 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8529 | lua_pushstring(gL.T, "__index"); |
| 8530 | lua_newtable(gL.T); |
| 8531 | |
| 8532 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8533 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8534 | 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] | 8535 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8536 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8537 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8538 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8539 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8540 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8541 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8542 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8543 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8544 | |
| 8545 | lua_settable(gL.T, -3); |
| 8546 | |
| 8547 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8548 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8549 | |
| 8550 | /* |
| 8551 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8552 | * Register class TXN |
| 8553 | * |
| 8554 | */ |
| 8555 | |
| 8556 | /* Create and fill the metatable. */ |
| 8557 | lua_newtable(gL.T); |
| 8558 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8559 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8560 | lua_pushstring(gL.T, "__index"); |
| 8561 | lua_newtable(gL.T); |
| 8562 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8563 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8564 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8565 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8566 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8567 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8568 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8569 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8570 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8571 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8572 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8573 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8574 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8575 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8576 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8577 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8578 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8579 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8580 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8581 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8582 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8583 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8584 | |
| 8585 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8586 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8587 | |
| 8588 | /* |
| 8589 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8590 | * Register class reply |
| 8591 | * |
| 8592 | */ |
| 8593 | lua_newtable(gL.T); |
| 8594 | lua_pushstring(gL.T, "__index"); |
| 8595 | lua_newtable(gL.T); |
| 8596 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8597 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8598 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8599 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8600 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8601 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8602 | |
| 8603 | |
| 8604 | /* |
| 8605 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8606 | * Register class Socket |
| 8607 | * |
| 8608 | */ |
| 8609 | |
| 8610 | /* Create and fill the metatable. */ |
| 8611 | lua_newtable(gL.T); |
| 8612 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8613 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8614 | lua_pushstring(gL.T, "__index"); |
| 8615 | lua_newtable(gL.T); |
| 8616 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8617 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8618 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8619 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8620 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8621 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8622 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8623 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8624 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8625 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8626 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8627 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8628 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8629 | 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] | 8630 | |
| 8631 | /* Register the garbage collector entry. */ |
| 8632 | lua_pushstring(gL.T, "__gc"); |
| 8633 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8634 | 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] | 8635 | |
| 8636 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8637 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8638 | |
| 8639 | /* Proxy and server configuration initialisation. */ |
| 8640 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8641 | init_new_proxy(&socket_proxy); |
| 8642 | socket_proxy.parent = NULL; |
| 8643 | socket_proxy.last_change = now.tv_sec; |
| 8644 | socket_proxy.id = "LUA-SOCKET"; |
| 8645 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8646 | socket_proxy.maxconn = 0; |
| 8647 | socket_proxy.accept = NULL; |
| 8648 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8649 | socket_proxy.srv = NULL; |
| 8650 | socket_proxy.conn_retries = 0; |
| 8651 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8652 | |
| 8653 | /* Init TCP server: unchanged parameters */ |
| 8654 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8655 | socket_tcp.next = NULL; |
| 8656 | socket_tcp.proxy = &socket_proxy; |
| 8657 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8658 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8659 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8660 | socket_tcp.idle_conns = NULL; |
| 8661 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8662 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8663 | socket_tcp.last_change = 0; |
| 8664 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8665 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8666 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8667 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8668 | |
| 8669 | /* XXX: Copy default parameter from default server, |
| 8670 | * but the default server is not initialized. |
| 8671 | */ |
| 8672 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8673 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8674 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8675 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8676 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8677 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8678 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8679 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8680 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8681 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8682 | |
| 8683 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8684 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8685 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8686 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8687 | socket_tcp.check.server = &socket_tcp; |
| 8688 | |
| 8689 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8690 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8691 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8692 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8693 | socket_tcp.agent.server = &socket_tcp; |
| 8694 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8695 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8696 | |
| 8697 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8698 | /* Init TCP server: unchanged parameters */ |
| 8699 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8700 | socket_ssl.next = NULL; |
| 8701 | socket_ssl.proxy = &socket_proxy; |
| 8702 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8703 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8704 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8705 | socket_ssl.idle_conns = NULL; |
| 8706 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8707 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8708 | socket_ssl.last_change = 0; |
| 8709 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8710 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8711 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8712 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8713 | |
| 8714 | /* XXX: Copy default parameter from default server, |
| 8715 | * but the default server is not initialized. |
| 8716 | */ |
| 8717 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8718 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8719 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8720 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8721 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8722 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8723 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8724 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8725 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8726 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8727 | |
| 8728 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8729 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8730 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8731 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8732 | socket_ssl.check.server = &socket_ssl; |
| 8733 | |
| 8734 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8735 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8736 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8737 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8738 | socket_ssl.agent.server = &socket_ssl; |
| 8739 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8740 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8741 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8742 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8743 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8744 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8745 | /* |
| 8746 | * |
| 8747 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8748 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8749 | * features like client certificates and ssl_verify. |
| 8750 | * |
| 8751 | */ |
| 8752 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8753 | if (tmp_error != 0) { |
| 8754 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8755 | abort(); /* This must be never arrives because the command line |
| 8756 | not editable by the user. */ |
| 8757 | } |
| 8758 | idx += kw->skip; |
| 8759 | } |
| 8760 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8761 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8762 | |
| 8763 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8764 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8765 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8766 | static void hlua_deinit() |
| 8767 | { |
| 8768 | lua_close(gL.T); |
| 8769 | } |
| 8770 | |
| 8771 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8772 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8773 | static void hlua_register_build_options(void) |
| 8774 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8775 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8776 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8777 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8778 | hap_register_build_opts(ptr, 1); |
| 8779 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8780 | |
| 8781 | INITCALL0(STG_REGISTER, hlua_register_build_options); |