Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | 3dfbd27 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 35 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 36 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 37 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 39 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 40 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 41 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 42 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 43 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 44 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 45 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 46 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 47 | #include <haproxy/payload.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 48 | #include <haproxy/proxy-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 49 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 50 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 51 | #include <haproxy/server-t.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 52 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 53 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 54 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 55 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 56 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 57 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 59 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 60 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 61 | #include <haproxy/xref.h> |
| 62 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 63 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 64 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 65 | * macro is used only for identifying the function that can |
| 66 | * not return because a longjmp is executed. |
| 67 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 68 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 69 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 70 | */ |
| 71 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 72 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 73 | #define MAY_LJMP(func) func |
| 74 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 75 | /* This couple of function executes securely some Lua calls outside of |
| 76 | * the lua runtime environment. Each Lua call can return a longjmp |
| 77 | * if it encounter a memory error. |
| 78 | * |
| 79 | * Lua documentation extract: |
| 80 | * |
| 81 | * If an error happens outside any protected environment, Lua calls |
| 82 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 83 | * exiting the host application. Your panic function can avoid this |
| 84 | * exit by never returning (e.g., doing a long jump to your own |
| 85 | * recovery point outside Lua). |
| 86 | * |
| 87 | * The panic function runs as if it were a message handler (see |
| 88 | * §2.3); in particular, the error message is at the top of the |
| 89 | * stack. However, there is no guarantee about stack space. To push |
| 90 | * anything on the stack, the panic function must first check the |
| 91 | * available space (see §4.2). |
| 92 | * |
| 93 | * We must check all the Lua entry point. This includes: |
| 94 | * - The include/proto/hlua.h exported functions |
| 95 | * - the task wrapper function |
| 96 | * - The action wrapper function |
| 97 | * - The converters wrapper function |
| 98 | * - The sample-fetch wrapper functions |
| 99 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 100 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 101 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 102 | * |
| 103 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 104 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 105 | * because they must be exists in the program stack when the longjmp |
| 106 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 107 | * |
| 108 | * Note that the Lua processing is not really thread safe. It provides |
| 109 | * heavy system which consists to add our own lock function in the Lua |
| 110 | * code and recompile the library. This system will probably not accepted |
| 111 | * by maintainers of various distribs. |
| 112 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 113 | * 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] | 114 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 115 | * of function and a lua_unlock() at the end of the function. So I |
| 116 | * conclude that the Lua thread safe mode just perform a mutex around |
| 117 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 118 | * easier for distro maintainers. |
| 119 | * |
| 120 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 121 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 122 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 124 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 125 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 126 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 127 | 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] | 128 | |
| 129 | #define SET_SAFE_LJMP(__L) \ |
| 130 | ({ \ |
| 131 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 132 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 133 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 134 | lua_atpanic(__L, hlua_panic_safe); \ |
| 135 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 136 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 137 | } else { \ |
| 138 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 139 | ret = 1; \ |
| 140 | } \ |
| 141 | ret; \ |
| 142 | }) |
| 143 | |
| 144 | /* If we are the last function catching Lua errors, we |
| 145 | * must reset the panic function. |
| 146 | */ |
| 147 | #define RESET_SAFE_LJMP(__L) \ |
| 148 | do { \ |
| 149 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 150 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 151 | } while(0) |
| 152 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 153 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 154 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 155 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 156 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 157 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 158 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 159 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 160 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 161 | /* The main Lua execution context. */ |
| 162 | struct hlua gL; |
| 163 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 164 | /* This is the memory pool containing struct lua for applets |
| 165 | * (including cli). |
| 166 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 167 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 168 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 169 | /* Used for Socket connection. */ |
| 170 | static struct proxy socket_proxy; |
| 171 | static struct server socket_tcp; |
| 172 | #ifdef USE_OPENSSL |
| 173 | static struct server socket_ssl; |
| 174 | #endif |
| 175 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 176 | /* List head of the function called at the initialisation time. */ |
| 177 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 178 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 179 | /* The following variables contains the reference of the different |
| 180 | * Lua classes. These references are useful for identify metadata |
| 181 | * associated with an object. |
| 182 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 183 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 184 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 185 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 186 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 187 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 188 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 189 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 190 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 191 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 192 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 193 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 194 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 195 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 196 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 197 | * because a task may remain alive during all the haproxy execution. |
| 198 | */ |
| 199 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 200 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 201 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 202 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 203 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 204 | * it is used for preventing infinite loops. |
| 205 | * |
| 206 | * I test the scheer with an infinite loop containing one incrementation |
| 207 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 208 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 209 | * to one interrupt each 10 000 instructions. |
| 210 | * |
| 211 | * configured | Number of |
| 212 | * instructions | loops executed |
| 213 | * between two | in milions |
| 214 | * forced yields | |
| 215 | * ---------------+--------------- |
| 216 | * 10 | 160 |
| 217 | * 500 | 670 |
| 218 | * 1000 | 680 |
| 219 | * 5000 | 700 |
| 220 | * 7000 | 700 |
| 221 | * 8000 | 700 |
| 222 | * 9000 | 710 <- ceil |
| 223 | * 10000 | 710 |
| 224 | * 100000 | 710 |
| 225 | * 1000000 | 710 |
| 226 | * |
| 227 | */ |
| 228 | static unsigned int hlua_nb_instruction = 10000; |
| 229 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 230 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 231 | * be enforced on any memory allocation. |
| 232 | */ |
| 233 | struct hlua_mem_allocator { |
| 234 | size_t allocated; |
| 235 | size_t limit; |
| 236 | }; |
| 237 | |
| 238 | static struct hlua_mem_allocator hlua_global_allocator; |
| 239 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 240 | /* These functions converts types between HAProxy internal args or |
| 241 | * sample and LUA types. Another function permits to check if the |
| 242 | * LUA stack contains arguments according with an required ARG_T |
| 243 | * format. |
| 244 | */ |
| 245 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 246 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 247 | __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] | 248 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 249 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 250 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 251 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 252 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 253 | __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] | 254 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 255 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 256 | do { \ |
| 257 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 258 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 259 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 260 | } while (0) |
| 261 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 262 | /* Used to check an Lua function type in the stack. It creates and |
| 263 | * returns a reference of the function. This function throws an |
| 264 | * error if the rgument is not a "function". |
| 265 | */ |
| 266 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 267 | { |
| 268 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 269 | 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] | 270 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 271 | } |
| 272 | lua_pushvalue(L, argno); |
| 273 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 274 | } |
| 275 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 276 | /* Return the string that is of the top of the stack. */ |
| 277 | const char *hlua_get_top_error_string(lua_State *L) |
| 278 | { |
| 279 | if (lua_gettop(L) < 1) |
| 280 | return "unknown error"; |
| 281 | if (lua_type(L, -1) != LUA_TSTRING) |
| 282 | return "unknown error"; |
| 283 | return lua_tostring(L, -1); |
| 284 | } |
| 285 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 286 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 287 | { |
| 288 | lua_Debug ar; |
| 289 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 290 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 291 | int filled = 0; |
| 292 | |
| 293 | while (lua_getstack(L, level++, &ar)) { |
| 294 | |
| 295 | /* Add separator */ |
| 296 | if (filled) |
| 297 | chunk_appendf(msg, ", "); |
| 298 | filled = 1; |
| 299 | |
| 300 | /* Fill fields: |
| 301 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 302 | * 'l': fills in the field currentline; |
| 303 | * 'n': fills in the field name and namewhat; |
| 304 | * 't': fills in the field istailcall; |
| 305 | */ |
| 306 | lua_getinfo(L, "Slnt", &ar); |
| 307 | |
| 308 | /* Append code localisation */ |
| 309 | if (ar.currentline > 0) |
| 310 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 311 | else |
| 312 | chunk_appendf(msg, "%s ", ar.short_src); |
| 313 | |
| 314 | /* |
| 315 | * Get function name |
| 316 | * |
| 317 | * if namewhat is no empty, name is defined. |
| 318 | * what contains "Lua" for Lua function, "C" for C function, |
| 319 | * or "main" for main code. |
| 320 | */ |
| 321 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 322 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 323 | |
| 324 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 325 | chunk_appendf(msg, "main chunk"); |
| 326 | |
| 327 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 328 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 329 | |
| 330 | else /* nothing left... */ |
| 331 | chunk_appendf(msg, "?"); |
| 332 | |
| 333 | |
| 334 | /* Display tailed call */ |
| 335 | if (ar.istailcall) |
| 336 | chunk_appendf(msg, " ..."); |
| 337 | } |
| 338 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 339 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 343 | /* This function check the number of arguments available in the |
| 344 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 345 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 346 | */ |
| 347 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 348 | { |
| 349 | if (lua_gettop(L) == nb) |
| 350 | return; |
| 351 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 352 | } |
| 353 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 354 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 355 | * and the line number where the error is encountered. |
| 356 | */ |
| 357 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 358 | { |
| 359 | va_list argp; |
| 360 | va_start(argp, fmt); |
| 361 | luaL_where(L, 1); |
| 362 | lua_pushvfstring(L, fmt, argp); |
| 363 | va_end(argp); |
| 364 | lua_concat(L, 2); |
| 365 | return 1; |
| 366 | } |
| 367 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 368 | /* This functions is used with sample fetch and converters. It |
| 369 | * converts the HAProxy configuration argument in a lua stack |
| 370 | * values. |
| 371 | * |
| 372 | * It takes an array of "arg", and each entry of the array is |
| 373 | * converted and pushed in the LUA stack. |
| 374 | */ |
| 375 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 376 | { |
| 377 | switch (arg->type) { |
| 378 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | case ARGT_TIME: |
| 380 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 381 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 385 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case ARGT_IPV4: |
| 389 | case ARGT_IPV6: |
| 390 | case ARGT_MSK4: |
| 391 | case ARGT_MSK6: |
| 392 | case ARGT_FE: |
| 393 | case ARGT_BE: |
| 394 | case ARGT_TAB: |
| 395 | case ARGT_SRV: |
| 396 | case ARGT_USR: |
| 397 | case ARGT_MAP: |
| 398 | default: |
| 399 | lua_pushnil(L); |
| 400 | break; |
| 401 | } |
| 402 | return 1; |
| 403 | } |
| 404 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 405 | /* 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] | 406 | * 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] | 407 | * with sample fetch wrappers. The input arguments are given to the |
| 408 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 409 | */ |
| 410 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 411 | { |
| 412 | switch (lua_type(L, ud)) { |
| 413 | |
| 414 | case LUA_TNUMBER: |
| 415 | case LUA_TBOOLEAN: |
| 416 | arg->type = ARGT_SINT; |
| 417 | arg->data.sint = lua_tointeger(L, ud); |
| 418 | break; |
| 419 | |
| 420 | case LUA_TSTRING: |
| 421 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 422 | 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] | 423 | /* 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] | 424 | 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] | 425 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 426 | break; |
| 427 | |
| 428 | case LUA_TUSERDATA: |
| 429 | case LUA_TNIL: |
| 430 | case LUA_TTABLE: |
| 431 | case LUA_TFUNCTION: |
| 432 | case LUA_TTHREAD: |
| 433 | case LUA_TLIGHTUSERDATA: |
| 434 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 435 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | return 1; |
| 439 | } |
| 440 | |
| 441 | /* the following functions are used to convert a struct sample |
| 442 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 443 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 444 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 445 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 447 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 450 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case SMP_T_BIN: |
| 454 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 455 | 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] | 456 | break; |
| 457 | |
| 458 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 459 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 460 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 461 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 462 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 463 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 464 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 465 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 466 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 467 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 468 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 469 | 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] | 470 | break; |
| 471 | default: |
| 472 | lua_pushnil(L); |
| 473 | break; |
| 474 | } |
| 475 | break; |
| 476 | |
| 477 | case SMP_T_IPV4: |
| 478 | case SMP_T_IPV6: |
| 479 | 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] | 480 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 481 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 482 | 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] | 483 | else |
| 484 | lua_pushnil(L); |
| 485 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 486 | default: |
| 487 | lua_pushnil(L); |
| 488 | break; |
| 489 | } |
| 490 | return 1; |
| 491 | } |
| 492 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 493 | /* the following functions are used to convert a struct sample |
| 494 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 495 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 496 | */ |
| 497 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 498 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 499 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 500 | |
| 501 | case SMP_T_BIN: |
| 502 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 503 | 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] | 504 | break; |
| 505 | |
| 506 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 507 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 508 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 509 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 510 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 511 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 512 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 513 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 514 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 515 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 516 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 517 | 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] | 518 | break; |
| 519 | default: |
| 520 | lua_pushstring(L, ""); |
| 521 | break; |
| 522 | } |
| 523 | break; |
| 524 | |
| 525 | case SMP_T_SINT: |
| 526 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 527 | case SMP_T_IPV4: |
| 528 | case SMP_T_IPV6: |
| 529 | 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] | 530 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 531 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 532 | 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] | 533 | else |
| 534 | lua_pushstring(L, ""); |
| 535 | break; |
| 536 | default: |
| 537 | lua_pushstring(L, ""); |
| 538 | break; |
| 539 | } |
| 540 | return 1; |
| 541 | } |
| 542 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 543 | /* the following functions are used to convert an Lua type in a |
| 544 | * struct sample. This is useful to provide data from a converter |
| 545 | * to the LUA code. |
| 546 | */ |
| 547 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 548 | { |
| 549 | switch (lua_type(L, ud)) { |
| 550 | |
| 551 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 552 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 553 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 554 | break; |
| 555 | |
| 556 | |
| 557 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 558 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 559 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 560 | break; |
| 561 | |
| 562 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 563 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 565 | 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] | 566 | /* 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] | 567 | 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] | 568 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 569 | break; |
| 570 | |
| 571 | case LUA_TUSERDATA: |
| 572 | case LUA_TNIL: |
| 573 | case LUA_TTABLE: |
| 574 | case LUA_TFUNCTION: |
| 575 | case LUA_TTHREAD: |
| 576 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 577 | case LUA_TNONE: |
| 578 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 579 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 580 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 581 | break; |
| 582 | } |
| 583 | return 1; |
| 584 | } |
| 585 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 586 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 587 | * 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] | 588 | * 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] | 589 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 590 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 591 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 593 | __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] | 594 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | { |
| 596 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 597 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 598 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 599 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 600 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 601 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 602 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 603 | |
| 604 | idx = 0; |
| 605 | min_arg = ARGM(mask); |
| 606 | mask >>= ARGM_BITS; |
| 607 | |
| 608 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 609 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | |
| 611 | /* Check oversize. */ |
| 612 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 613 | msg = "Malformed argument mask"; |
| 614 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* Check for mandatory arguments. */ |
| 618 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 619 | if (idx < min_arg) { |
| 620 | |
| 621 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 622 | if (idx > 0) { |
| 623 | msg = "Mandatory argument expected"; |
| 624 | goto error; |
| 625 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 626 | |
| 627 | /* If first argument have a certain type, some default values |
| 628 | * may be used. See the function smp_resolve_args(). |
| 629 | */ |
| 630 | switch (mask & ARGT_MASK) { |
| 631 | |
| 632 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 633 | if (!(p->cap & PR_CAP_FE)) { |
| 634 | msg = "Mandatory argument expected"; |
| 635 | goto error; |
| 636 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 637 | argp[idx].data.prx = p; |
| 638 | argp[idx].type = ARGT_FE; |
| 639 | argp[idx+1].type = ARGT_STOP; |
| 640 | break; |
| 641 | |
| 642 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 643 | if (!(p->cap & PR_CAP_BE)) { |
| 644 | msg = "Mandatory argument expected"; |
| 645 | goto error; |
| 646 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 647 | argp[idx].data.prx = p; |
| 648 | argp[idx].type = ARGT_BE; |
| 649 | argp[idx+1].type = ARGT_STOP; |
| 650 | break; |
| 651 | |
| 652 | case ARGT_TAB: |
| 653 | argp[idx].data.prx = p; |
| 654 | argp[idx].type = ARGT_TAB; |
| 655 | argp[idx+1].type = ARGT_STOP; |
| 656 | break; |
| 657 | |
| 658 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 659 | msg = "Mandatory argument expected"; |
| 660 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 664 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 665 | } |
| 666 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 667 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 668 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 669 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 670 | msg = "Last argument expected"; |
| 671 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 675 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 676 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 679 | /* Convert some argument types. All string in argp[] are for not |
| 680 | * duplicated yet. |
| 681 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 682 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 683 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 684 | if (argp[idx].type != ARGT_SINT) { |
| 685 | msg = "integer expected"; |
| 686 | goto error; |
| 687 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 688 | argp[idx].type = ARGT_SINT; |
| 689 | break; |
| 690 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 691 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 692 | if (argp[idx].type != ARGT_SINT) { |
| 693 | msg = "integer expected"; |
| 694 | goto error; |
| 695 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 696 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 697 | break; |
| 698 | |
| 699 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 700 | if (argp[idx].type != ARGT_SINT) { |
| 701 | msg = "integer expected"; |
| 702 | goto error; |
| 703 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 704 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 705 | break; |
| 706 | |
| 707 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 708 | if (argp[idx].type != ARGT_STR) { |
| 709 | msg = "string expected"; |
| 710 | goto error; |
| 711 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 712 | 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] | 713 | if (!argp[idx].data.prx) { |
| 714 | msg = "frontend doesn't exist"; |
| 715 | goto error; |
| 716 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 717 | argp[idx].type = ARGT_FE; |
| 718 | break; |
| 719 | |
| 720 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 721 | if (argp[idx].type != ARGT_STR) { |
| 722 | msg = "string expected"; |
| 723 | goto error; |
| 724 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 725 | 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] | 726 | if (!argp[idx].data.prx) { |
| 727 | msg = "backend doesn't exist"; |
| 728 | goto error; |
| 729 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 730 | argp[idx].type = ARGT_BE; |
| 731 | break; |
| 732 | |
| 733 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 734 | if (argp[idx].type != ARGT_STR) { |
| 735 | msg = "string expected"; |
| 736 | goto error; |
| 737 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 738 | 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] | 739 | if (!argp[idx].data.t) { |
| 740 | msg = "table doesn't exist"; |
| 741 | goto error; |
| 742 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 743 | argp[idx].type = ARGT_TAB; |
| 744 | break; |
| 745 | |
| 746 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 747 | if (argp[idx].type != ARGT_STR) { |
| 748 | msg = "string expected"; |
| 749 | goto error; |
| 750 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 751 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 752 | if (sname) { |
| 753 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 754 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 755 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 756 | if (!px) { |
| 757 | msg = "backend doesn't exist"; |
| 758 | goto error; |
| 759 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 760 | } |
| 761 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 762 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 764 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 765 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 766 | if (!argp[idx].data.srv) { |
| 767 | msg = "server doesn't exist"; |
| 768 | goto error; |
| 769 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | argp[idx].type = ARGT_SRV; |
| 771 | break; |
| 772 | |
| 773 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 774 | if (argp[idx].type != ARGT_STR) { |
| 775 | msg = "string expected"; |
| 776 | goto error; |
| 777 | } |
| 778 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 779 | msg = "invalid IPv4 address"; |
| 780 | goto error; |
| 781 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 782 | argp[idx].type = ARGT_IPV4; |
| 783 | break; |
| 784 | |
| 785 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 786 | if (argp[idx].type == ARGT_SINT) |
| 787 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 788 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 789 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 790 | msg = "invalid IPv4 mask"; |
| 791 | goto error; |
| 792 | } |
| 793 | } |
| 794 | else { |
| 795 | msg = "integer or string expected"; |
| 796 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 797 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 798 | argp[idx].type = ARGT_MSK4; |
| 799 | break; |
| 800 | |
| 801 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 802 | if (argp[idx].type != ARGT_STR) { |
| 803 | msg = "string expected"; |
| 804 | goto error; |
| 805 | } |
| 806 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 807 | msg = "invalid IPv6 address"; |
| 808 | goto error; |
| 809 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 810 | argp[idx].type = ARGT_IPV6; |
| 811 | break; |
| 812 | |
| 813 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 814 | if (argp[idx].type == ARGT_SINT) |
| 815 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 816 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 817 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 818 | msg = "invalid IPv6 mask"; |
| 819 | goto error; |
| 820 | } |
| 821 | } |
| 822 | else { |
| 823 | msg = "integer or string expected"; |
| 824 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 825 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 826 | argp[idx].type = ARGT_MSK6; |
| 827 | break; |
| 828 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 829 | case ARGT_REG: |
| 830 | if (argp[idx].type != ARGT_STR) { |
| 831 | msg = "string expected"; |
| 832 | goto error; |
| 833 | } |
| 834 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 835 | if (!reg) { |
| 836 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 837 | argp[idx].data.str.area, err); |
| 838 | free(err); |
| 839 | goto error; |
| 840 | } |
| 841 | argp[idx].type = ARGT_REG; |
| 842 | argp[idx].data.reg = reg; |
| 843 | break; |
| 844 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 845 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 846 | if (argp[idx].type != ARGT_STR) { |
| 847 | msg = "string expected"; |
| 848 | goto error; |
| 849 | } |
| 850 | if (p->uri_auth && p->uri_auth->userlist && |
| 851 | !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area)) |
| 852 | ul = p->uri_auth->userlist; |
| 853 | else |
| 854 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 855 | |
| 856 | if (!ul) { |
| 857 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 858 | goto error; |
| 859 | } |
| 860 | argp[idx].type = ARGT_USR; |
| 861 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 862 | break; |
| 863 | |
| 864 | case ARGT_STR: |
| 865 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 866 | msg = "unable to duplicate string arg"; |
| 867 | goto error; |
| 868 | } |
| 869 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 870 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 871 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 872 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 873 | msg = "type not yet supported"; |
| 874 | goto error; |
| 875 | break; |
| 876 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /* Check for type of argument. */ |
| 880 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 881 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 882 | arg_type_names[(mask & ARGT_MASK)], |
| 883 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 884 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* Next argument. */ |
| 888 | mask >>= ARGT_BITS; |
| 889 | idx++; |
| 890 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 891 | return 0; |
| 892 | |
| 893 | error: |
| 894 | for (i = 0; i < idx; i++) { |
| 895 | if (argp[i].type == ARGT_STR) |
| 896 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 897 | else if (argp[i].type == ARGT_REG) |
| 898 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 899 | } |
| 900 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 901 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 902 | } |
| 903 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 904 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 905 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 906 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | * |
| 908 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 909 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 910 | */ |
| 911 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 912 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 913 | struct hlua **hlua = lua_getextraspace(L); |
| 914 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 915 | } |
| 916 | static inline void hlua_sethlua(struct hlua *hlua) |
| 917 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 918 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 919 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 920 | } |
| 921 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 922 | /* This function is used to send logs. It try to send on screen (stderr) |
| 923 | * and on the default syslog server. |
| 924 | */ |
| 925 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 926 | { |
| 927 | struct tm tm; |
| 928 | char *p; |
| 929 | |
| 930 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 931 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 932 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 933 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 934 | /* Break the message if exceed the buffer size. */ |
| 935 | *(p-4) = ' '; |
| 936 | *(p-3) = '.'; |
| 937 | *(p-2) = '.'; |
| 938 | *(p-1) = '.'; |
| 939 | break; |
| 940 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 941 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 942 | *p = *msg; |
| 943 | else |
| 944 | *p = '.'; |
| 945 | } |
| 946 | *p = '\0'; |
| 947 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 948 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 949 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 950 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 951 | return; |
| 952 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 953 | get_localtime(date.tv_sec, &tm); |
| 954 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 955 | 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] | 956 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 957 | fflush(stderr); |
| 958 | } |
| 959 | } |
| 960 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 961 | /* This function just ensure that the yield will be always |
| 962 | * returned with a timeout and permit to set some flags |
| 963 | */ |
| 964 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 965 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 966 | { |
| 967 | struct hlua *hlua = hlua_gethlua(L); |
| 968 | |
| 969 | /* Set the wake timeout. If timeout is required, we set |
| 970 | * the expiration time. |
| 971 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 972 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 973 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 974 | hlua->flags |= flags; |
| 975 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 976 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 977 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 978 | } |
| 979 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 980 | /* This function initialises the Lua environment stored in the stream. |
| 981 | * 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] | 982 | * 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] | 983 | * |
| 984 | * This function is particular. it initialises a new Lua thread. If the |
| 985 | * initialisation fails (example: out of memory error), the lua function |
| 986 | * throws an error (longjmp). |
| 987 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 988 | * 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] | 989 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 990 | * threads appear, the safe environment set a lock to ensure only one |
| 991 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 992 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 993 | * |
| 994 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 995 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 996 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 997 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 998 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 999 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1000 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1001 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1002 | 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] | 1003 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1004 | if (!already_safe) { |
| 1005 | if (!SET_SAFE_LJMP(gL.T)) { |
| 1006 | lua->Tref = LUA_REFNIL; |
| 1007 | return 0; |
| 1008 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1009 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1010 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1011 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1012 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1013 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1014 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1015 | lua->T = lua_newthread(gL.T); |
| 1016 | if (!lua->T) { |
| 1017 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1018 | if (!already_safe) |
| 1019 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1020 | return 0; |
| 1021 | } |
| 1022 | hlua_sethlua(lua); |
| 1023 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1024 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1025 | if (!already_safe) |
| 1026 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1027 | return 1; |
| 1028 | } |
| 1029 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1030 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1031 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1032 | * is not freed. |
| 1033 | */ |
| 1034 | void hlua_ctx_destroy(struct hlua *lua) |
| 1035 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1036 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1037 | return; |
| 1038 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1039 | if (!lua->T) |
| 1040 | goto end; |
| 1041 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1042 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1043 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1044 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1045 | if (!SET_SAFE_LJMP(lua->T)) |
| 1046 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1047 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1048 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1049 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1050 | if (!SET_SAFE_LJMP(gL.T)) |
| 1051 | return; |
| 1052 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1053 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1054 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1055 | * without error, we run the GC on the thread pointer. Its freed all |
| 1056 | * the unused memory. |
| 1057 | * If the thread is finnish with an error or is currently yielded, |
| 1058 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1059 | * so e run the GC on the main thread. |
| 1060 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1061 | * the garbage collection. |
| 1062 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1063 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1064 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1065 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1066 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 1067 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1068 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1069 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1070 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1071 | |
| 1072 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1073 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /* This function is used to restore the Lua context when a coroutine |
| 1077 | * fails. This function copy the common memory between old coroutine |
| 1078 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1079 | * replaced by the new coroutine. |
| 1080 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1081 | * as string error message and it is copied in the new stack. |
| 1082 | */ |
| 1083 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1084 | { |
| 1085 | lua_State *T; |
| 1086 | int new_ref; |
| 1087 | |
| 1088 | /* Renew the main LUA stack doesn't have sense. */ |
| 1089 | if (lua == &gL) |
| 1090 | return 0; |
| 1091 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1092 | /* New Lua coroutine. */ |
| 1093 | T = lua_newthread(gL.T); |
| 1094 | if (!T) |
| 1095 | return 0; |
| 1096 | |
| 1097 | /* Copy last error message. */ |
| 1098 | if (keep_msg) |
| 1099 | lua_xmove(lua->T, T, 1); |
| 1100 | |
| 1101 | /* Copy data between the coroutines. */ |
| 1102 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1103 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1104 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | |
| 1106 | /* Destroy old data. */ |
| 1107 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1108 | |
| 1109 | /* The thread is garbage collected by Lua. */ |
| 1110 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1111 | |
| 1112 | /* Fill the struct with the new coroutine values. */ |
| 1113 | lua->Mref = new_ref; |
| 1114 | lua->T = T; |
| 1115 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1116 | |
| 1117 | /* Set context. */ |
| 1118 | hlua_sethlua(lua); |
| 1119 | |
| 1120 | return 1; |
| 1121 | } |
| 1122 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1123 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1124 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1125 | struct hlua *hlua = hlua_gethlua(L); |
| 1126 | |
| 1127 | /* Lua cannot yield when its returning from a function, |
| 1128 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1129 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1130 | */ |
| 1131 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1132 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | /* restore the interrupt condition. */ |
| 1137 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1138 | |
| 1139 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1140 | * If the state is not yieldable, trying yield causes an error. |
| 1141 | */ |
| 1142 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1143 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1144 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1145 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1146 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1147 | hlua->run_time += now_ms - hlua->start_time; |
| 1148 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1149 | lua_pushfstring(L, "execution timeout"); |
| 1150 | WILL_LJMP(lua_error(L)); |
| 1151 | } |
| 1152 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1153 | /* Update the start time. */ |
| 1154 | hlua->start_time = now_ms; |
| 1155 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1156 | /* Try to interrupt the process at the end of the current |
| 1157 | * unyieldable function. |
| 1158 | */ |
| 1159 | 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] | 1160 | } |
| 1161 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | /* This function start or resumes the Lua stack execution. If the flag |
| 1163 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1164 | * The function return an error. |
| 1165 | * |
| 1166 | * The function can returns 4 values: |
| 1167 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1168 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1169 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1170 | * - 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] | 1171 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1172 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1174 | * 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] | 1175 | * LUA code. |
| 1176 | */ |
| 1177 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1178 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1179 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1180 | int nres; |
| 1181 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | int ret; |
| 1183 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1184 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1185 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1186 | /* Initialise run time counter. */ |
| 1187 | if (!HLUA_IS_RUNNING(lua)) |
| 1188 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1189 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1190 | /* Lock the whole Lua execution. This lock must be before the |
| 1191 | * label "resume_execution". |
| 1192 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1193 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1194 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1195 | resume_execution: |
| 1196 | |
| 1197 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1198 | * instructions. it is used for preventing infinite loops. |
| 1199 | */ |
| 1200 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1201 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1202 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1203 | HLUA_SET_RUN(lua); |
| 1204 | HLUA_CLR_CTRLYIELD(lua); |
| 1205 | HLUA_CLR_WAKERESWR(lua); |
| 1206 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1207 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1208 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1209 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1210 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1211 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1213 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1214 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1215 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1217 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | switch (ret) { |
| 1219 | |
| 1220 | case LUA_OK: |
| 1221 | ret = HLUA_E_OK; |
| 1222 | break; |
| 1223 | |
| 1224 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1225 | /* Check if the execution timeout is expired. It it is the case, we |
| 1226 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1227 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1228 | tv_update_date(0, 1); |
| 1229 | lua->run_time += now_ms - lua->start_time; |
| 1230 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1231 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1232 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1233 | break; |
| 1234 | } |
| 1235 | /* Process the forced yield. if the general yield is not allowed or |
| 1236 | * if no task were associated this the current Lua execution |
| 1237 | * coroutine, we resume the execution. Else we want to return in the |
| 1238 | * scheduler and we want to be waked up again, to continue the |
| 1239 | * current Lua execution. So we schedule our own task. |
| 1240 | */ |
| 1241 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1242 | if (!yield_allowed || !lua->task) |
| 1243 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1244 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1245 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1246 | if (!yield_allowed) { |
| 1247 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1248 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1249 | break; |
| 1250 | } |
| 1251 | ret = HLUA_E_AGAIN; |
| 1252 | break; |
| 1253 | |
| 1254 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1255 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1256 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1257 | * because the errors ares the only one mean to return immediately |
| 1258 | * from and lua execution. |
| 1259 | */ |
| 1260 | if (lua->flags & HLUA_EXIT) { |
| 1261 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1262 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1263 | break; |
| 1264 | } |
| 1265 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1266 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1267 | if (!lua_checkstack(lua->T, 1)) { |
| 1268 | ret = HLUA_E_ERR; |
| 1269 | break; |
| 1270 | } |
| 1271 | msg = lua_tostring(lua->T, -1); |
| 1272 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1273 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1274 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1275 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1276 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1277 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1278 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1279 | ret = HLUA_E_ERRMSG; |
| 1280 | break; |
| 1281 | |
| 1282 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1283 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1284 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1285 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1286 | break; |
| 1287 | |
| 1288 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1289 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1290 | if (!lua_checkstack(lua->T, 1)) { |
| 1291 | ret = HLUA_E_ERR; |
| 1292 | break; |
| 1293 | } |
| 1294 | msg = lua_tostring(lua->T, -1); |
| 1295 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1296 | lua_pop(lua->T, 1); |
| 1297 | if (msg) |
| 1298 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1299 | else |
| 1300 | lua_pushfstring(lua->T, "message handler error"); |
| 1301 | ret = HLUA_E_ERRMSG; |
| 1302 | break; |
| 1303 | |
| 1304 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1305 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1306 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1307 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1308 | break; |
| 1309 | } |
| 1310 | |
| 1311 | switch (ret) { |
| 1312 | case HLUA_E_AGAIN: |
| 1313 | break; |
| 1314 | |
| 1315 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1316 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1317 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1318 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1319 | break; |
| 1320 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1321 | case HLUA_E_ETMOUT: |
| 1322 | case HLUA_E_NOMEM: |
| 1323 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1324 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1325 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1326 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1327 | hlua_ctx_renew(lua, 0); |
| 1328 | break; |
| 1329 | |
| 1330 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1331 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1332 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1333 | break; |
| 1334 | } |
| 1335 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1336 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1337 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1338 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1339 | return ret; |
| 1340 | } |
| 1341 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1342 | /* This function exit the current code. */ |
| 1343 | __LJMP static int hlua_done(lua_State *L) |
| 1344 | { |
| 1345 | struct hlua *hlua = hlua_gethlua(L); |
| 1346 | |
| 1347 | hlua->flags |= HLUA_EXIT; |
| 1348 | WILL_LJMP(lua_error(L)); |
| 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1353 | /* This function is an LUA binding. It provides a function |
| 1354 | * for deleting ACL from a referenced ACL file. |
| 1355 | */ |
| 1356 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1357 | { |
| 1358 | const char *name; |
| 1359 | const char *key; |
| 1360 | struct pat_ref *ref; |
| 1361 | |
| 1362 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1363 | |
| 1364 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1365 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1366 | |
| 1367 | ref = pat_ref_lookup(name); |
| 1368 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1369 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1370 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1371 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1372 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1373 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1374 | return 0; |
| 1375 | } |
| 1376 | |
| 1377 | /* This function is an LUA binding. It provides a function |
| 1378 | * for deleting map entry from a referenced map file. |
| 1379 | */ |
| 1380 | static int hlua_del_map(lua_State *L) |
| 1381 | { |
| 1382 | const char *name; |
| 1383 | const char *key; |
| 1384 | struct pat_ref *ref; |
| 1385 | |
| 1386 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1387 | |
| 1388 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1389 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1390 | |
| 1391 | ref = pat_ref_lookup(name); |
| 1392 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1393 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1394 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1395 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1396 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1397 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | /* This function is an LUA binding. It provides a function |
| 1402 | * for adding ACL pattern from a referenced ACL file. |
| 1403 | */ |
| 1404 | static int hlua_add_acl(lua_State *L) |
| 1405 | { |
| 1406 | const char *name; |
| 1407 | const char *key; |
| 1408 | struct pat_ref *ref; |
| 1409 | |
| 1410 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1411 | |
| 1412 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1413 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1414 | |
| 1415 | ref = pat_ref_lookup(name); |
| 1416 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1417 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1418 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1419 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1420 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1421 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1422 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
| 1426 | /* This function is an LUA binding. It provides a function |
| 1427 | * for setting map pattern and sample from a referenced map |
| 1428 | * file. |
| 1429 | */ |
| 1430 | static int hlua_set_map(lua_State *L) |
| 1431 | { |
| 1432 | const char *name; |
| 1433 | const char *key; |
| 1434 | const char *value; |
| 1435 | struct pat_ref *ref; |
| 1436 | |
| 1437 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1438 | |
| 1439 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1440 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1441 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1442 | |
| 1443 | ref = pat_ref_lookup(name); |
| 1444 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1445 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1446 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1447 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1448 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1449 | pat_ref_set(ref, key, value, NULL); |
| 1450 | else |
| 1451 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1452 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1456 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1457 | * 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] | 1458 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1459 | * the name of the object (_G[<name>] = <metable> ). |
| 1460 | * |
| 1461 | * A metable is a table that modify the standard behavior of a standard |
| 1462 | * access to the associated data. The entries of this new metatable are |
| 1463 | * defined as is: |
| 1464 | * |
| 1465 | * http://lua-users.org/wiki/MetatableEvents |
| 1466 | * |
| 1467 | * __index |
| 1468 | * |
| 1469 | * we access an absent field in a table, the result is nil. This is |
| 1470 | * true, but it is not the whole truth. Actually, such access triggers |
| 1471 | * the interpreter to look for an __index metamethod: If there is no |
| 1472 | * such method, as usually happens, then the access results in nil; |
| 1473 | * otherwise, the metamethod will provide the result. |
| 1474 | * |
| 1475 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1476 | * the key does not appear in the table, but the metatable has an __index |
| 1477 | * property: |
| 1478 | * |
| 1479 | * - if the value is a function, the function is called, passing in the |
| 1480 | * table and the key; the return value of that function is returned as |
| 1481 | * the result. |
| 1482 | * |
| 1483 | * - if the value is another table, the value of the key in that table is |
| 1484 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1485 | * table's metatable has an __index property, then it continues on up) |
| 1486 | * |
| 1487 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1488 | * |
| 1489 | * http://www.lua.org/pil/13.4.1.html |
| 1490 | * |
| 1491 | * __newindex |
| 1492 | * |
| 1493 | * Like __index, but control property assignment. |
| 1494 | * |
| 1495 | * __mode - Control weak references. A string value with one or both |
| 1496 | * of the characters 'k' and 'v' which specifies that the the |
| 1497 | * keys and/or values in the table are weak references. |
| 1498 | * |
| 1499 | * __call - Treat a table like a function. When a table is followed by |
| 1500 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1501 | * a __call key pointing to a function, that function is invoked |
| 1502 | * (passing any specified arguments) and the return value is |
| 1503 | * returned. |
| 1504 | * |
| 1505 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1506 | * called, if the metatable for myTable has a __metatable |
| 1507 | * key, the value of that key is returned instead of the |
| 1508 | * actual metatable. |
| 1509 | * |
| 1510 | * __tostring - Control string representation. When the builtin |
| 1511 | * "tostring( myTable )" function is called, if the metatable |
| 1512 | * for myTable has a __tostring property set to a function, |
| 1513 | * that function is invoked (passing myTable to it) and the |
| 1514 | * return value is used as the string representation. |
| 1515 | * |
| 1516 | * __len - Control table length. When the table length is requested using |
| 1517 | * the length operator ( '#' ), if the metatable for myTable has |
| 1518 | * a __len key pointing to a function, that function is invoked |
| 1519 | * (passing myTable to it) and the return value used as the value |
| 1520 | * of "#myTable". |
| 1521 | * |
| 1522 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1523 | * collected, if the metatable has a __gc field pointing to a |
| 1524 | * function, that function is first invoked, passing the userdata |
| 1525 | * to it. The __gc metamethod is not called for tables. |
| 1526 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1527 | * |
| 1528 | * Special metamethods for redefining standard operators: |
| 1529 | * http://www.lua.org/pil/13.1.html |
| 1530 | * |
| 1531 | * __add "+" |
| 1532 | * __sub "-" |
| 1533 | * __mul "*" |
| 1534 | * __div "/" |
| 1535 | * __unm "!" |
| 1536 | * __pow "^" |
| 1537 | * __concat ".." |
| 1538 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1539 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1540 | * http://www.lua.org/pil/13.2.html |
| 1541 | * |
| 1542 | * __eq "==" |
| 1543 | * __lt "<" |
| 1544 | * __le "<=" |
| 1545 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1546 | |
| 1547 | /* |
| 1548 | * |
| 1549 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1550 | * Class Map |
| 1551 | * |
| 1552 | * |
| 1553 | */ |
| 1554 | |
| 1555 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1556 | * a class session, otherwise it throws an error. |
| 1557 | */ |
| 1558 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1559 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1560 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | /* This function is the map constructor. It don't need |
| 1564 | * the class Map object. It creates and return a new Map |
| 1565 | * object. It must be called only during "body" or "init" |
| 1566 | * context because it process some filesystem accesses. |
| 1567 | */ |
| 1568 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1569 | { |
| 1570 | const char *fn; |
| 1571 | int match = PAT_MATCH_STR; |
| 1572 | struct sample_conv conv; |
| 1573 | const char *file = ""; |
| 1574 | int line = 0; |
| 1575 | lua_Debug ar; |
| 1576 | char *err = NULL; |
| 1577 | struct arg args[2]; |
| 1578 | |
| 1579 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1580 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1581 | |
| 1582 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1583 | |
| 1584 | if (lua_gettop(L) >= 2) { |
| 1585 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1586 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1587 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1588 | } |
| 1589 | |
| 1590 | /* Get Lua filename and line number. */ |
| 1591 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1592 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1593 | if (ar.currentline > 0) { /* is there info? */ |
| 1594 | file = ar.short_src; |
| 1595 | line = ar.currentline; |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | /* fill fake sample_conv struct. */ |
| 1600 | conv.kw = ""; /* unused. */ |
| 1601 | conv.process = NULL; /* unused. */ |
| 1602 | conv.arg_mask = 0; /* unused. */ |
| 1603 | conv.val_args = NULL; /* unused. */ |
| 1604 | conv.out_type = SMP_T_STR; |
| 1605 | conv.private = (void *)(long)match; |
| 1606 | switch (match) { |
| 1607 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1608 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1609 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1610 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1611 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1612 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1613 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1614 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1615 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1616 | default: |
| 1617 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1618 | } |
| 1619 | |
| 1620 | /* fill fake args. */ |
| 1621 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1622 | args[0].data.str.area = strdup(fn); |
| 1623 | args[0].data.str.data = strlen(fn); |
| 1624 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1625 | args[1].type = ARGT_STOP; |
| 1626 | |
| 1627 | /* load the map. */ |
| 1628 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1629 | /* error case: we cant use luaL_error because we must |
| 1630 | * free the err variable. |
| 1631 | */ |
| 1632 | luaL_where(L, 1); |
| 1633 | lua_pushfstring(L, "'new': %s.", err); |
| 1634 | lua_concat(L, 2); |
| 1635 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1636 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1637 | WILL_LJMP(lua_error(L)); |
| 1638 | } |
| 1639 | |
| 1640 | /* create the lua object. */ |
| 1641 | lua_newtable(L); |
| 1642 | lua_pushlightuserdata(L, args[0].data.map); |
| 1643 | lua_rawseti(L, -2, 0); |
| 1644 | |
| 1645 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1646 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1647 | lua_setmetatable(L, -2); |
| 1648 | |
| 1649 | |
| 1650 | return 1; |
| 1651 | } |
| 1652 | |
| 1653 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1654 | { |
| 1655 | struct map_descriptor *desc; |
| 1656 | struct pattern *pat; |
| 1657 | struct sample smp; |
| 1658 | |
| 1659 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1660 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1661 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1662 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1663 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1664 | } |
| 1665 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1666 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1667 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1668 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry Fournier | 66e9ff5 | 2020-11-10 20:38:20 +0100 | [diff] [blame] | 1669 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1673 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1674 | if (str) |
| 1675 | lua_pushstring(L, ""); |
| 1676 | else |
| 1677 | lua_pushnil(L); |
| 1678 | return 1; |
| 1679 | } |
| 1680 | |
| 1681 | /* 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] | 1682 | 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] | 1683 | return 1; |
| 1684 | } |
| 1685 | |
| 1686 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1687 | { |
| 1688 | return _hlua_map_lookup(L, 0); |
| 1689 | } |
| 1690 | |
| 1691 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1692 | { |
| 1693 | return _hlua_map_lookup(L, 1); |
| 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * |
| 1698 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | * Class Socket |
| 1700 | * |
| 1701 | * |
| 1702 | */ |
| 1703 | |
| 1704 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1705 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1706 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | /* 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] | 1710 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1711 | * received. |
| 1712 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1713 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1715 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1717 | if (appctx->ctx.hlua_cosocket.die) { |
| 1718 | si_shutw(si); |
| 1719 | si_shutr(si); |
| 1720 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1721 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1722 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1723 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1724 | } |
| 1725 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1726 | /* If we cant write, wakeup the pending write signals. */ |
| 1727 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1728 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1729 | |
| 1730 | /* If we cant read, wakeup the pending read signals. */ |
| 1731 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1732 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1733 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1734 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1735 | * to be notified whenever the connection completes. |
| 1736 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1737 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1738 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1739 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1740 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1741 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1742 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1743 | |
| 1744 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1745 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1746 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1747 | /* 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] | 1748 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1749 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1750 | |
| 1751 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1752 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1753 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1754 | |
| 1755 | /* Some data were injected in the buffer, notify the stream |
| 1756 | * interface. |
| 1757 | */ |
| 1758 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1759 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1760 | |
| 1761 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1762 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1763 | */ |
| 1764 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1765 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1766 | } |
| 1767 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1768 | /* This function is called when the "struct stream" is destroyed. |
| 1769 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1770 | * Wake all the pending signals. |
| 1771 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1772 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1773 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1774 | struct xref *peer; |
| 1775 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1776 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1777 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1778 | if (peer) |
| 1779 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1780 | |
| 1781 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1782 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1783 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1787 | * uses this object. If the stream does not exists, just quit. |
| 1788 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | * pending signal can rest in the read and write lists. destroy |
| 1790 | * it. |
| 1791 | */ |
| 1792 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1793 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1794 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1795 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1796 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1797 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1798 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1799 | |
| 1800 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1801 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1802 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1803 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1804 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1805 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1806 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1807 | appctx->ctx.hlua_cosocket.die = 1; |
| 1808 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1809 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1810 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1811 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1812 | return 0; |
| 1813 | } |
| 1814 | |
| 1815 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1816 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1818 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1820 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1821 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1822 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1823 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1824 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1825 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1826 | |
| 1827 | /* Check if we run on the same thread than the xreator thread. |
| 1828 | * We cannot access to the socket if the thread is different. |
| 1829 | */ |
| 1830 | if (socket->tid != tid) |
| 1831 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1832 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1833 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1834 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1836 | |
| 1837 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1838 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1839 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1840 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1841 | appctx->ctx.hlua_cosocket.die = 1; |
| 1842 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1844 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1845 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1846 | return 0; |
| 1847 | } |
| 1848 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1849 | /* The close function calls close_helper. |
| 1850 | */ |
| 1851 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1852 | { |
| 1853 | MAY_LJMP(check_args(L, 1, "close")); |
| 1854 | return hlua_socket_close_helper(L); |
| 1855 | } |
| 1856 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1857 | /* This Lua function assumes that the stack contain three parameters. |
| 1858 | * 1 - USERDATA containing a struct socket |
| 1859 | * 2 - INTEGER with values of the macro defined below |
| 1860 | * If the integer is -1, we must read at most one line. |
| 1861 | * If the integer is -2, we ust read all the data until the |
| 1862 | * end of the stream. |
| 1863 | * If the integer is positive value, we must read a number of |
| 1864 | * bytes corresponding to this value. |
| 1865 | */ |
| 1866 | #define HLSR_READ_LINE (-1) |
| 1867 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1868 | __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] | 1869 | { |
| 1870 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1871 | int wanted = lua_tointeger(L, 2); |
| 1872 | struct hlua *hlua = hlua_gethlua(L); |
| 1873 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1874 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1876 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1877 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1878 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1879 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1880 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1881 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1882 | struct stream_interface *si; |
| 1883 | struct stream *s; |
| 1884 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1885 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1886 | |
| 1887 | /* Check if this lua stack is schedulable. */ |
| 1888 | if (!hlua || !hlua->task) |
| 1889 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1890 | "'frontend', 'backend' or 'task'")); |
| 1891 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1892 | /* Check if we run on the same thread than the xreator thread. |
| 1893 | * We cannot access to the socket if the thread is different. |
| 1894 | */ |
| 1895 | if (socket->tid != tid) |
| 1896 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1897 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1898 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1899 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1900 | if (!peer) |
| 1901 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1902 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1903 | si = appctx->owner; |
| 1904 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1906 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1909 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | if (nblk < 0) /* Connection close. */ |
| 1911 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1912 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1913 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1914 | |
| 1915 | /* remove final \r\n. */ |
| 1916 | if (nblk == 1) { |
| 1917 | if (blk1[len1-1] == '\n') { |
| 1918 | len1--; |
| 1919 | skip_at_end++; |
| 1920 | if (blk1[len1-1] == '\r') { |
| 1921 | len1--; |
| 1922 | skip_at_end++; |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | else { |
| 1927 | if (blk2[len2-1] == '\n') { |
| 1928 | len2--; |
| 1929 | skip_at_end++; |
| 1930 | if (blk2[len2-1] == '\r') { |
| 1931 | len2--; |
| 1932 | skip_at_end++; |
| 1933 | } |
| 1934 | } |
| 1935 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1939 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1940 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | if (nblk < 0) /* Connection close. */ |
| 1942 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1943 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1944 | goto connection_empty; |
| 1945 | } |
| 1946 | |
| 1947 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1948 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1949 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1950 | if (nblk < 0) /* Connection close. */ |
| 1951 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1952 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1953 | goto connection_empty; |
| 1954 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1955 | missing_bytes = wanted - socket->b.n; |
| 1956 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1958 | len1 = missing_bytes; |
| 1959 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1960 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | len = len1; |
| 1964 | |
| 1965 | luaL_addlstring(&socket->b, blk1, len1); |
| 1966 | if (nblk == 2) { |
| 1967 | len += len2; |
| 1968 | luaL_addlstring(&socket->b, blk2, len2); |
| 1969 | } |
| 1970 | |
| 1971 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1972 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | |
| 1974 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1975 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | |
| 1977 | /* If the pattern reclaim to read all the data |
| 1978 | * in the connection, got out. |
| 1979 | */ |
| 1980 | if (wanted == HLSR_READ_ALL) |
| 1981 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1982 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1983 | goto connection_empty; |
| 1984 | |
| 1985 | /* Return result. */ |
| 1986 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1987 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | return 1; |
| 1989 | |
| 1990 | connection_closed: |
| 1991 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1992 | xref_unlock(&socket->xref, peer); |
| 1993 | |
| 1994 | no_peer: |
| 1995 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | /* If the buffer containds data. */ |
| 1997 | if (socket->b.n > 0) { |
| 1998 | luaL_pushresult(&socket->b); |
| 1999 | return 1; |
| 2000 | } |
| 2001 | lua_pushnil(L); |
| 2002 | lua_pushstring(L, "connection closed."); |
| 2003 | return 2; |
| 2004 | |
| 2005 | connection_empty: |
| 2006 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2007 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2008 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2009 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2010 | } |
| 2011 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2012 | 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] | 2013 | return 0; |
| 2014 | } |
| 2015 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2016 | /* This Lua function gets two parameters. The first one can be string |
| 2017 | * or a number. If the string is "*l", the user requires one line. If |
| 2018 | * 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] | 2019 | * If the value is a number, the user require a number of bytes equal |
| 2020 | * to the value. The default value is "*l" (a line). |
| 2021 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2022 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2023 | * integer takes this values: |
| 2024 | * -1 : read a line |
| 2025 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2026 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2028 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | * concatenated with the read data. |
| 2030 | */ |
| 2031 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2032 | { |
| 2033 | int wanted = HLSR_READ_LINE; |
| 2034 | const char *pattern; |
| 2035 | int type; |
| 2036 | char *error; |
| 2037 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2038 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2039 | |
| 2040 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2041 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2042 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2043 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2045 | /* Check if we run on the same thread than the xreator thread. |
| 2046 | * We cannot access to the socket if the thread is different. |
| 2047 | */ |
| 2048 | if (socket->tid != tid) |
| 2049 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2050 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | /* check for pattern. */ |
| 2052 | if (lua_gettop(L) >= 2) { |
| 2053 | type = lua_type(L, 2); |
| 2054 | if (type == LUA_TSTRING) { |
| 2055 | pattern = lua_tostring(L, 2); |
| 2056 | if (strcmp(pattern, "*a") == 0) |
| 2057 | wanted = HLSR_READ_ALL; |
| 2058 | else if (strcmp(pattern, "*l") == 0) |
| 2059 | wanted = HLSR_READ_LINE; |
| 2060 | else { |
| 2061 | wanted = strtoll(pattern, &error, 10); |
| 2062 | if (*error != '\0') |
| 2063 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2064 | } |
| 2065 | } |
| 2066 | else if (type == LUA_TNUMBER) { |
| 2067 | wanted = lua_tointeger(L, 2); |
| 2068 | if (wanted < 0) |
| 2069 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | /* Set pattern. */ |
| 2074 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2075 | |
| 2076 | /* Check if we would replace the top by itself. */ |
| 2077 | if (lua_gettop(L) != 2) |
| 2078 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2080 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | luaL_buffinit(L, &socket->b); |
| 2082 | |
| 2083 | /* Check prefix. */ |
| 2084 | if (lua_gettop(L) >= 3) { |
| 2085 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2086 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2087 | pattern = lua_tolstring(L, 3, &len); |
| 2088 | luaL_addlstring(&socket->b, pattern, len); |
| 2089 | } |
| 2090 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2091 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2095 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2097 | 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] | 2098 | { |
| 2099 | struct hlua_socket *socket; |
| 2100 | struct hlua *hlua = hlua_gethlua(L); |
| 2101 | struct appctx *appctx; |
| 2102 | size_t buf_len; |
| 2103 | const char *buf; |
| 2104 | int len; |
| 2105 | int send_len; |
| 2106 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2107 | struct xref *peer; |
| 2108 | struct stream_interface *si; |
| 2109 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | |
| 2111 | /* Check if this lua stack is schedulable. */ |
| 2112 | if (!hlua || !hlua->task) |
| 2113 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2114 | "'frontend', 'backend' or 'task'")); |
| 2115 | |
| 2116 | /* Get object */ |
| 2117 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2118 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2119 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2121 | /* Check if we run on the same thread than the xreator thread. |
| 2122 | * We cannot access to the socket if the thread is different. |
| 2123 | */ |
| 2124 | if (socket->tid != tid) |
| 2125 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2126 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2127 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2128 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2129 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2130 | lua_pushinteger(L, -1); |
| 2131 | return 1; |
| 2132 | } |
| 2133 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2134 | si = appctx->owner; |
| 2135 | s = si_strm(si); |
| 2136 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2138 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2139 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | lua_pushinteger(L, -1); |
| 2141 | return 1; |
| 2142 | } |
| 2143 | |
| 2144 | /* Update the input buffer data. */ |
| 2145 | buf += sent; |
| 2146 | send_len = buf_len - sent; |
| 2147 | |
| 2148 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2149 | if (sent >= buf_len) { |
| 2150 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2152 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2154 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2155 | * the request buffer if its not required. |
| 2156 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2157 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2158 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2159 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2160 | } |
| 2161 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2162 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2163 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2164 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2166 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | |
| 2168 | /* send data */ |
| 2169 | if (len < send_len) |
| 2170 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2171 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | |
| 2173 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2174 | * the data" (-2) are not expected because the available length |
| 2175 | * is tested. |
| 2176 | * Other unknown error are also not expected. |
| 2177 | */ |
| 2178 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2179 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2180 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2181 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2182 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2184 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2185 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | return 1; |
| 2187 | } |
| 2188 | |
| 2189 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2190 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2191 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2192 | s->req.rex = TICK_ETERNITY; |
| 2193 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | |
| 2195 | /* Update length sent. */ |
| 2196 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2197 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | |
| 2199 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2200 | if (sent + len >= buf_len) { |
| 2201 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2203 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | |
| 2205 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2206 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2207 | xref_unlock(&socket->xref, peer); |
| 2208 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2209 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2210 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2211 | 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] | 2212 | return 0; |
| 2213 | } |
| 2214 | |
| 2215 | /* This function initiate the send of data. It just check the input |
| 2216 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2217 | * 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] | 2218 | * "hlua_socket_write_yield" that can yield. |
| 2219 | * |
| 2220 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2221 | * the associated object. The second is a string buffer. The third is |
| 2222 | * a facultative integer that represents where is the buffer position |
| 2223 | * of the start of the data that can send. The first byte is the |
| 2224 | * position "1". The default value is "1". The fourth argument is a |
| 2225 | * facultative integer that represents where is the buffer position |
| 2226 | * of the end of the data that can send. The default is the last byte. |
| 2227 | */ |
| 2228 | static int hlua_socket_send(struct lua_State *L) |
| 2229 | { |
| 2230 | int i; |
| 2231 | int j; |
| 2232 | const char *buf; |
| 2233 | size_t buf_len; |
| 2234 | |
| 2235 | /* Check number of arguments. */ |
| 2236 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2237 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2238 | |
| 2239 | /* Get the string. */ |
| 2240 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2241 | |
| 2242 | /* Get and check j. */ |
| 2243 | if (lua_gettop(L) == 4) { |
| 2244 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2245 | if (j < 0) |
| 2246 | j = buf_len + j + 1; |
| 2247 | if (j > buf_len) |
| 2248 | j = buf_len + 1; |
| 2249 | lua_pop(L, 1); |
| 2250 | } |
| 2251 | else |
| 2252 | j = buf_len; |
| 2253 | |
| 2254 | /* Get and check i. */ |
| 2255 | if (lua_gettop(L) == 3) { |
| 2256 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2257 | if (i < 0) |
| 2258 | i = buf_len + i + 1; |
| 2259 | if (i > buf_len) |
| 2260 | i = buf_len + 1; |
| 2261 | lua_pop(L, 1); |
| 2262 | } else |
| 2263 | i = 1; |
| 2264 | |
| 2265 | /* Check bth i and j. */ |
| 2266 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2267 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2268 | return 1; |
| 2269 | } |
| 2270 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2271 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | return 1; |
| 2273 | } |
| 2274 | if (i == 0) |
| 2275 | i = 1; |
| 2276 | if (j == 0) |
| 2277 | j = 1; |
| 2278 | |
| 2279 | /* Pop the string. */ |
| 2280 | lua_pop(L, 1); |
| 2281 | |
| 2282 | /* Update the buffer length. */ |
| 2283 | buf += i - 1; |
| 2284 | buf_len = j - i + 1; |
| 2285 | lua_pushlstring(L, buf, buf_len); |
| 2286 | |
| 2287 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2288 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2290 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | } |
| 2292 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2293 | #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] | 2294 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2295 | { |
| 2296 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2297 | int ret; |
| 2298 | int len; |
| 2299 | char *p; |
| 2300 | |
| 2301 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2302 | if (ret <= 0) { |
| 2303 | lua_pushnil(L); |
| 2304 | return 1; |
| 2305 | } |
| 2306 | |
| 2307 | if (ret == AF_UNIX) { |
| 2308 | lua_pushstring(L, buffer+1); |
| 2309 | return 1; |
| 2310 | } |
| 2311 | else if (ret == AF_INET6) { |
| 2312 | buffer[0] = '['; |
| 2313 | len = strlen(buffer); |
| 2314 | buffer[len] = ']'; |
| 2315 | len++; |
| 2316 | buffer[len] = ':'; |
| 2317 | len++; |
| 2318 | p = buffer; |
| 2319 | } |
| 2320 | else if (ret == AF_INET) { |
| 2321 | p = buffer + 1; |
| 2322 | len = strlen(p); |
| 2323 | p[len] = ':'; |
| 2324 | len++; |
| 2325 | } |
| 2326 | else { |
| 2327 | lua_pushnil(L); |
| 2328 | return 1; |
| 2329 | } |
| 2330 | |
| 2331 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2332 | lua_pushnil(L); |
| 2333 | return 1; |
| 2334 | } |
| 2335 | |
| 2336 | lua_pushstring(L, p); |
| 2337 | return 1; |
| 2338 | } |
| 2339 | |
| 2340 | /* Returns information about the peer of the connection. */ |
| 2341 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2342 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2343 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2344 | struct xref *peer; |
| 2345 | struct appctx *appctx; |
| 2346 | struct stream_interface *si; |
| 2347 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2348 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2349 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2351 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2352 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2353 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2354 | /* Check if we run on the same thread than the xreator thread. |
| 2355 | * We cannot access to the socket if the thread is different. |
| 2356 | */ |
| 2357 | if (socket->tid != tid) |
| 2358 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2359 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2360 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2361 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2362 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | lua_pushnil(L); |
| 2364 | return 1; |
| 2365 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2366 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2367 | si = appctx->owner; |
| 2368 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2369 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2370 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2371 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2372 | lua_pushnil(L); |
| 2373 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | } |
| 2375 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2376 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2377 | xref_unlock(&socket->xref, peer); |
| 2378 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | /* Returns information about my connection side. */ |
| 2382 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2383 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2384 | struct hlua_socket *socket; |
| 2385 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2386 | struct appctx *appctx; |
| 2387 | struct xref *peer; |
| 2388 | struct stream_interface *si; |
| 2389 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2390 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2391 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2392 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2393 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2394 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2396 | /* Check if we run on the same thread than the xreator thread. |
| 2397 | * We cannot access to the socket if the thread is different. |
| 2398 | */ |
| 2399 | if (socket->tid != tid) |
| 2400 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2401 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2402 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2403 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2404 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | lua_pushnil(L); |
| 2406 | return 1; |
| 2407 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2408 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2409 | si = appctx->owner; |
| 2410 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2412 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2413 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2414 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | lua_pushnil(L); |
| 2416 | return 1; |
| 2417 | } |
| 2418 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2419 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | xref_unlock(&socket->xref, peer); |
| 2421 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2425 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2426 | .obj_type = OBJ_TYPE_APPLET, |
| 2427 | .name = "<LUA_TCP>", |
| 2428 | .fct = hlua_socket_handler, |
| 2429 | .release = hlua_socket_release, |
| 2430 | }; |
| 2431 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2432 | __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] | 2433 | { |
| 2434 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2435 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2436 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2437 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2438 | struct stream_interface *si; |
| 2439 | struct stream *s; |
| 2440 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2441 | /* Check if we run on the same thread than the xreator thread. |
| 2442 | * We cannot access to the socket if the thread is different. |
| 2443 | */ |
| 2444 | if (socket->tid != tid) |
| 2445 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2446 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2447 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2448 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2449 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2450 | lua_pushnil(L); |
| 2451 | lua_pushstring(L, "Can't connect"); |
| 2452 | return 2; |
| 2453 | } |
| 2454 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2455 | si = appctx->owner; |
| 2456 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2457 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2458 | /* Check if we run on the same thread than the xreator thread. |
| 2459 | * We cannot access to the socket if the thread is different. |
| 2460 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2461 | if (socket->tid != tid) { |
| 2462 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2463 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2464 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2465 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2466 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2467 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2468 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | lua_pushnil(L); |
| 2470 | lua_pushstring(L, "Can't connect"); |
| 2471 | return 2; |
| 2472 | } |
| 2473 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2474 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
| 2476 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2477 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2478 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2479 | lua_pushinteger(L, 1); |
| 2480 | return 1; |
| 2481 | } |
| 2482 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2484 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2485 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2486 | } |
| 2487 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2488 | 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] | 2489 | return 0; |
| 2490 | } |
| 2491 | |
| 2492 | /* This function fail or initite the connection. */ |
| 2493 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2494 | { |
| 2495 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2496 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2497 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2498 | struct hlua *hlua; |
| 2499 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2500 | int low, high; |
| 2501 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2502 | struct xref *peer; |
| 2503 | struct stream_interface *si; |
| 2504 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2505 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2506 | if (lua_gettop(L) < 2) |
| 2507 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | |
| 2509 | /* Get args. */ |
| 2510 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2511 | |
| 2512 | /* Check if we run on the same thread than the xreator thread. |
| 2513 | * We cannot access to the socket if the thread is different. |
| 2514 | */ |
| 2515 | if (socket->tid != tid) |
| 2516 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2517 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2518 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2519 | if (lua_gettop(L) >= 3) { |
| 2520 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2521 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2523 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2524 | * that are not enclosed within square brackets. |
| 2525 | */ |
| 2526 | if (port > 0) { |
| 2527 | luaL_buffinit(L, &b); |
| 2528 | luaL_addstring(&b, ip); |
| 2529 | luaL_addchar(&b, ':'); |
| 2530 | luaL_pushresult(&b); |
| 2531 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2532 | } |
| 2533 | } |
| 2534 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2535 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2536 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2537 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2538 | lua_pushnil(L); |
| 2539 | return 1; |
| 2540 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2541 | |
| 2542 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2543 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2544 | if (!addr) { |
| 2545 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2546 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2547 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2548 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2550 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2551 | if (addr->ss_family == AF_INET) { |
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_in *)addr)->sin_port = htons(port); |
| 2557 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2558 | if (port == -1) { |
| 2559 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2560 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2561 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2562 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2563 | } |
| 2564 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2565 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2566 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2567 | si = appctx->owner; |
| 2568 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2569 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2570 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2571 | xref_unlock(&socket->xref, peer); |
| 2572 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2573 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2574 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2575 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2576 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2577 | |
| 2578 | /* inform the stream that we want to be notified whenever the |
| 2579 | * connection completes. |
| 2580 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2581 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2582 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2583 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2584 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2585 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2586 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2587 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2588 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2589 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2590 | } |
| 2591 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2592 | |
| 2593 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2594 | /* Return yield waiting for connection. */ |
| 2595 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2596 | 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] | 2597 | |
| 2598 | return 0; |
| 2599 | } |
| 2600 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2601 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2602 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2603 | { |
| 2604 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2605 | struct xref *peer; |
| 2606 | struct appctx *appctx; |
| 2607 | struct stream_interface *si; |
| 2608 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | |
| 2610 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2611 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2612 | |
| 2613 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2614 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2615 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2616 | lua_pushnil(L); |
| 2617 | return 1; |
| 2618 | } |
| 2619 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2620 | si = appctx->owner; |
| 2621 | s = si_strm(si); |
| 2622 | |
| 2623 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2624 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2625 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2626 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2627 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2628 | |
| 2629 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2630 | { |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2635 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2636 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2637 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2638 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | struct xref *peer; |
| 2640 | struct appctx *appctx; |
| 2641 | struct stream_interface *si; |
| 2642 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2643 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2645 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2646 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2647 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2648 | /* convert the timeout to millis */ |
| 2649 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2651 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2652 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2653 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2654 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2655 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2656 | 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] | 2657 | |
| 2658 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2659 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2660 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2661 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2662 | /* Check if we run on the same thread than the xreator thread. |
| 2663 | * We cannot access to the socket if the thread is different. |
| 2664 | */ |
| 2665 | if (socket->tid != tid) |
| 2666 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2667 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2668 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2669 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2670 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2671 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2672 | WILL_LJMP(lua_error(L)); |
| 2673 | return 0; |
| 2674 | } |
| 2675 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2676 | si = appctx->owner; |
| 2677 | s = si_strm(si); |
| 2678 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2679 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2680 | s->req.rto = tmout; |
| 2681 | s->req.wto = tmout; |
| 2682 | s->res.rto = tmout; |
| 2683 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2684 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2685 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2686 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2687 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2688 | |
| 2689 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2690 | task_queue(s->task); |
| 2691 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2692 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2693 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2694 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2695 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2699 | { |
| 2700 | struct hlua_socket *socket; |
| 2701 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2702 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2703 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2704 | |
| 2705 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2706 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | hlua_pusherror(L, "socket: full stack"); |
| 2708 | goto out_fail_conf; |
| 2709 | } |
| 2710 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2711 | /* Create the object: obj[0] = userdata. */ |
| 2712 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2714 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2716 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2717 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2718 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2719 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2720 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2721 | goto out_fail_conf; |
| 2722 | } |
| 2723 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2724 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2725 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2726 | lua_setmetatable(L, -2); |
| 2727 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2728 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2729 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2730 | if (!appctx) { |
| 2731 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2732 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2733 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2734 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2735 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2736 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2737 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2738 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2739 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2740 | /* Now create a session, task and stream for this applet */ |
| 2741 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2742 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2743 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2744 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2745 | } |
| 2746 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2747 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2748 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2749 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2750 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2751 | } |
| 2752 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2753 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2754 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2755 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2756 | /* Configure "right" stream interface. this "si" is used to connect |
| 2757 | * and retrieve data from the server. The connection is initialized |
| 2758 | * with the "struct server". |
| 2759 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2760 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2761 | |
| 2762 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2763 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2764 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2765 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2766 | return 1; |
| 2767 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2768 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2769 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2770 | out_fail_sess: |
| 2771 | appctx_free(appctx); |
| 2772 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2773 | WILL_LJMP(lua_error(L)); |
| 2774 | return 0; |
| 2775 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2776 | |
| 2777 | /* |
| 2778 | * |
| 2779 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2780 | * Class Channel |
| 2781 | * |
| 2782 | * |
| 2783 | */ |
| 2784 | |
| 2785 | /* Returns the struct hlua_channel join to the class channel in the |
| 2786 | * stack entry "ud" or throws an argument error. |
| 2787 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2788 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2789 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2790 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2791 | } |
| 2792 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2793 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2794 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2795 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2796 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2798 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2799 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | return 0; |
| 2801 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2802 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2803 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2804 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2805 | |
| 2806 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2807 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2808 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2809 | return 1; |
| 2810 | } |
| 2811 | |
| 2812 | /* Duplicate all the data present in the input channel and put it |
| 2813 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2814 | * the stack if the channel is closed and all the data are consumed, |
| 2815 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2816 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2817 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2818 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2819 | { |
| 2820 | char *blk1; |
| 2821 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2822 | size_t len1; |
| 2823 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2824 | int ret; |
| 2825 | luaL_Buffer b; |
| 2826 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2827 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2828 | if (unlikely(ret == 0)) |
| 2829 | return 0; |
| 2830 | |
| 2831 | if (unlikely(ret < 0)) { |
| 2832 | lua_pushnil(L); |
| 2833 | return -1; |
| 2834 | } |
| 2835 | |
| 2836 | luaL_buffinit(L, &b); |
| 2837 | luaL_addlstring(&b, blk1, len1); |
| 2838 | if (unlikely(ret == 2)) |
| 2839 | luaL_addlstring(&b, blk2, len2); |
| 2840 | luaL_pushresult(&b); |
| 2841 | |
| 2842 | if (unlikely(ret == 2)) |
| 2843 | return len1 + len2; |
| 2844 | return len1; |
| 2845 | } |
| 2846 | |
| 2847 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2848 | * a yield. This function keep the data in the buffer. |
| 2849 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2850 | __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] | 2851 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2852 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2853 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2854 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2855 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2856 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2857 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2858 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2859 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2860 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2861 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2862 | 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] | 2863 | return 1; |
| 2864 | } |
| 2865 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2866 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2867 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2868 | { |
| 2869 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2870 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2871 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2872 | } |
| 2873 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2874 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2875 | * a yield. This function consumes the data in the buffer. It returns |
| 2876 | * a string containing the data or a nil pointer if no data are available |
| 2877 | * and the channel is closed. |
| 2878 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2879 | __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] | 2880 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2881 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2882 | int ret; |
| 2883 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2884 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2885 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2886 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2887 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2888 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2889 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2890 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2891 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2893 | 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] | 2894 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2895 | if (unlikely(ret == -1)) |
| 2896 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2898 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | return 1; |
| 2900 | } |
| 2901 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2902 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2903 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2904 | { |
| 2905 | MAY_LJMP(check_args(L, 1, "get")); |
| 2906 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2907 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2908 | } |
| 2909 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2910 | /* This functions consumes and returns one line. If the channel is closed, |
| 2911 | * 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] | 2912 | * 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] | 2913 | * value. |
| 2914 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2915 | __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] | 2916 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2917 | char *blk1; |
| 2918 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2919 | size_t len1; |
| 2920 | size_t len2; |
| 2921 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2922 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2923 | int ret; |
| 2924 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2925 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2926 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2927 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2928 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2929 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2930 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2931 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2932 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2933 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2935 | 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] | 2936 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | if (ret == -1) { |
| 2938 | lua_pushnil(L); |
| 2939 | return 1; |
| 2940 | } |
| 2941 | |
| 2942 | luaL_buffinit(L, &b); |
| 2943 | luaL_addlstring(&b, blk1, len1); |
| 2944 | len = len1; |
| 2945 | if (unlikely(ret == 2)) { |
| 2946 | luaL_addlstring(&b, blk2, len2); |
| 2947 | len += len2; |
| 2948 | } |
| 2949 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2950 | 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] | 2951 | return 1; |
| 2952 | } |
| 2953 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2954 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2955 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2956 | { |
| 2957 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2958 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2959 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2960 | } |
| 2961 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2962 | /* This function takes a string as input, and append it at the |
| 2963 | * input side of channel. If the data is too big, but a space |
| 2964 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2965 | * yields. If the data is bigger than the buffer, or if the |
| 2966 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2967 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2969 | __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] | 2970 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2971 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2972 | size_t len; |
| 2973 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2974 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2975 | int ret; |
| 2976 | int max; |
| 2977 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2978 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2979 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2980 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2981 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2982 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2983 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2984 | * the request buffer if its not required. |
| 2985 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2986 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2987 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2988 | 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] | 2989 | } |
| 2990 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2991 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2992 | if (max > len - l) |
| 2993 | max = len - l; |
| 2994 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2995 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | if (ret == -2 || ret == -3) { |
| 2997 | lua_pushinteger(L, -1); |
| 2998 | return 1; |
| 2999 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3000 | if (ret == -1) { |
| 3001 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3002 | 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] | 3003 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | l += ret; |
| 3005 | lua_pop(L, 1); |
| 3006 | lua_pushinteger(L, l); |
| 3007 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3008 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3009 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3010 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3012 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | */ |
| 3014 | return 1; |
| 3015 | } |
| 3016 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3017 | 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] | 3018 | return 1; |
| 3019 | } |
| 3020 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3021 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3022 | * 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] | 3023 | * buffer size is too little for the data. |
| 3024 | */ |
| 3025 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3026 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3027 | size_t len; |
| 3028 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3029 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3030 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3031 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3032 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | lua_pushinteger(L, 0); |
| 3034 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3035 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3036 | } |
| 3037 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3038 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3039 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3040 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3041 | * or -1 if the channel is closed or if the buffer size is too |
| 3042 | * little for the data. |
| 3043 | */ |
| 3044 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3045 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3046 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3047 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3048 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3049 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3050 | lua_pushinteger(L, 0); |
| 3051 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3052 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3053 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3054 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3055 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3056 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3057 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3058 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3059 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | } |
| 3061 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3062 | /* Append data in the output side of the buffer. This data is immediately |
| 3063 | * sent. The function returns the amount of data written. If the buffer |
| 3064 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | * if the channel is closed. |
| 3066 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3067 | __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] | 3068 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3069 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3070 | size_t len; |
| 3071 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3072 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3073 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3074 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3075 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3076 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3077 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3078 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3079 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3080 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3081 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3082 | lua_pushinteger(L, -1); |
| 3083 | return 1; |
| 3084 | } |
| 3085 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3086 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3087 | * the request buffer if its not required. |
| 3088 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3089 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3090 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3091 | 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] | 3092 | } |
| 3093 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3094 | /* The written data will be immediately sent, so we can check |
| 3095 | * the available space without taking in account the reserve. |
| 3096 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3097 | * data, because the buffer will be flushed. |
| 3098 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3099 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3100 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3101 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3102 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3103 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3104 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3105 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3106 | return 1; |
| 3107 | |
| 3108 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3109 | if (max > len - l) |
| 3110 | max = len - l; |
| 3111 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3112 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3113 | * detects a non contiguous buffer and realign it. |
| 3114 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3115 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3116 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3117 | |
| 3118 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3119 | 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] | 3120 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3121 | /* buffer replace considers that the input part is filled. |
| 3122 | * so, I must forward these new data in the output part. |
| 3123 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3124 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | |
| 3126 | l += max; |
| 3127 | lua_pop(L, 1); |
| 3128 | lua_pushinteger(L, l); |
| 3129 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3130 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3131 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3132 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3133 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3134 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3135 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3136 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3137 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3138 | if (l < len) { |
| 3139 | /* If we are waiting for space in the response buffer, we |
| 3140 | * must set the flag WAKERESWR. This flag required the task |
| 3141 | * wake up if any activity is detected on the response buffer. |
| 3142 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3143 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3144 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3145 | else |
| 3146 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3147 | 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] | 3148 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3149 | |
| 3150 | return 1; |
| 3151 | } |
| 3152 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3153 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3154 | * yield the LUA process, and resume it without checking the |
| 3155 | * input arguments. |
| 3156 | */ |
| 3157 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3158 | { |
| 3159 | MAY_LJMP(check_args(L, 2, "send")); |
| 3160 | lua_pushinteger(L, 0); |
| 3161 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3162 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | /* This function forward and amount of butes. The data pass from |
| 3166 | * the input side of the buffer to the output side, and can be |
| 3167 | * forwarded. This function never fails. |
| 3168 | * |
| 3169 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3170 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3171 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3172 | __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] | 3173 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3174 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3175 | int len; |
| 3176 | int l; |
| 3177 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3178 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3179 | |
| 3180 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3181 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3182 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3183 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3184 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3185 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3186 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3187 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3188 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3189 | |
| 3190 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3191 | if (max > ci_data(chn)) |
| 3192 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3193 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3194 | l += max; |
| 3195 | |
| 3196 | lua_pop(L, 1); |
| 3197 | lua_pushinteger(L, l); |
| 3198 | |
| 3199 | /* Check if it miss bytes to forward. */ |
| 3200 | if (l < len) { |
| 3201 | /* The the input channel or the output channel are closed, we |
| 3202 | * must return the amount of data forwarded. |
| 3203 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3204 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3205 | return 1; |
| 3206 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3207 | /* If we are waiting for space data in the response buffer, we |
| 3208 | * must set the flag WAKERESWR. This flag required the task |
| 3209 | * wake up if any activity is detected on the response buffer. |
| 3210 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3211 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3212 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3213 | else |
| 3214 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3215 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3216 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3217 | 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] | 3218 | } |
| 3219 | |
| 3220 | return 1; |
| 3221 | } |
| 3222 | |
| 3223 | /* Just check the input and prepare the stack for the previous |
| 3224 | * function "hlua_channel_forward_yield" |
| 3225 | */ |
| 3226 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3227 | { |
| 3228 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3229 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3230 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3231 | |
| 3232 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3233 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | /* Just returns the number of bytes available in the input |
| 3237 | * side of the buffer. This function never fails. |
| 3238 | */ |
| 3239 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3240 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3241 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3242 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3243 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3244 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3245 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3246 | struct htx *htx = htxbuf(&chn->buf); |
| 3247 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3248 | } |
| 3249 | else |
| 3250 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3251 | return 1; |
| 3252 | } |
| 3253 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3254 | /* Returns true if the channel is full. */ |
| 3255 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3256 | { |
| 3257 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3258 | |
| 3259 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3260 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3261 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3262 | * applet). |
| 3263 | */ |
| 3264 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3265 | return 1; |
| 3266 | } |
| 3267 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3268 | /* Returns true if the channel is the response channel. */ |
| 3269 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3270 | { |
| 3271 | struct channel *chn; |
| 3272 | |
| 3273 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3274 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3275 | |
| 3276 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3277 | return 1; |
| 3278 | } |
| 3279 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3280 | /* Just returns the number of bytes available in the output |
| 3281 | * side of the buffer. This function never fails. |
| 3282 | */ |
| 3283 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3284 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3285 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3286 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3287 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3288 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3289 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3290 | return 1; |
| 3291 | } |
| 3292 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3293 | /* |
| 3294 | * |
| 3295 | * |
| 3296 | * Class Fetches |
| 3297 | * |
| 3298 | * |
| 3299 | */ |
| 3300 | |
| 3301 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3302 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3303 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3304 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3305 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3306 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | /* This function creates and push in the stack a fetch object according |
| 3310 | * with a current TXN. |
| 3311 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3312 | 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] | 3313 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3314 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3315 | |
| 3316 | /* Check stack size. */ |
| 3317 | if (!lua_checkstack(L, 3)) |
| 3318 | return 0; |
| 3319 | |
| 3320 | /* Create the object: obj[0] = userdata. |
| 3321 | * Note that the base of the Fetches object is the |
| 3322 | * transaction object. |
| 3323 | */ |
| 3324 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3325 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3326 | lua_rawseti(L, -2, 0); |
| 3327 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3328 | hsmp->s = txn->s; |
| 3329 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3330 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3331 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3332 | |
| 3333 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3334 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3335 | lua_setmetatable(L, -2); |
| 3336 | |
| 3337 | return 1; |
| 3338 | } |
| 3339 | |
| 3340 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3341 | * It uses closure argument to store the associated sample-fetch. It |
| 3342 | * returns only one argument or throws an error. An error is thrown |
| 3343 | * only if an error is encountered during the argument parsing. If |
| 3344 | * the "sample-fetch" function fails, nil is returned. |
| 3345 | */ |
| 3346 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3347 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3348 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3349 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3350 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3351 | int i; |
| 3352 | struct sample smp; |
| 3353 | |
| 3354 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3355 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3356 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3357 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3358 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3359 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3360 | /* Check execution authorization. */ |
| 3361 | if (f->use & SMP_USE_HTTP_ANY && |
| 3362 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3363 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3364 | "is not available in Lua services", f->kw); |
| 3365 | WILL_LJMP(lua_error(L)); |
| 3366 | } |
| 3367 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3368 | /* Get extra arguments. */ |
| 3369 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3370 | if (i >= ARGM_NBARGS) |
| 3371 | break; |
| 3372 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3373 | } |
| 3374 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3375 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3376 | |
| 3377 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3378 | 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] | 3379 | |
| 3380 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3381 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3382 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3383 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | /* Initialise the sample. */ |
| 3387 | memset(&smp, 0, sizeof(smp)); |
| 3388 | |
| 3389 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3390 | 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] | 3391 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3392 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3393 | lua_pushstring(L, ""); |
| 3394 | else |
| 3395 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3396 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3400 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3401 | hlua_smp2lua_str(L, &smp); |
| 3402 | else |
| 3403 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3404 | |
| 3405 | end: |
| 3406 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3407 | if (args[i].type == ARGT_STR) |
| 3408 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3409 | else if (args[i].type == ARGT_REG) |
| 3410 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3411 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3412 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3413 | |
| 3414 | error: |
| 3415 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3416 | if (args[i].type == ARGT_STR) |
| 3417 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3418 | else if (args[i].type == ARGT_REG) |
| 3419 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3420 | } |
| 3421 | WILL_LJMP(lua_error(L)); |
| 3422 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3423 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3424 | |
| 3425 | /* |
| 3426 | * |
| 3427 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3428 | * Class Converters |
| 3429 | * |
| 3430 | * |
| 3431 | */ |
| 3432 | |
| 3433 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3434 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3435 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3436 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3437 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3438 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3439 | } |
| 3440 | |
| 3441 | /* This function creates and push in the stack a Converters object |
| 3442 | * according with a current TXN. |
| 3443 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3444 | 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] | 3445 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3446 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3447 | |
| 3448 | /* Check stack size. */ |
| 3449 | if (!lua_checkstack(L, 3)) |
| 3450 | return 0; |
| 3451 | |
| 3452 | /* Create the object: obj[0] = userdata. |
| 3453 | * Note that the base of the Converters object is the |
| 3454 | * same than the TXN object. |
| 3455 | */ |
| 3456 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3457 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3458 | lua_rawseti(L, -2, 0); |
| 3459 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3460 | hsmp->s = txn->s; |
| 3461 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3462 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3463 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3464 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3465 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3466 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3467 | lua_setmetatable(L, -2); |
| 3468 | |
| 3469 | return 1; |
| 3470 | } |
| 3471 | |
| 3472 | /* This function is an LUA binding. It is called with each converter. |
| 3473 | * It uses closure argument to store the associated converter. It |
| 3474 | * returns only one argument or throws an error. An error is thrown |
| 3475 | * only if an error is encountered during the argument parsing. If |
| 3476 | * the converter function function fails, nil is returned. |
| 3477 | */ |
| 3478 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3479 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3480 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3481 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3482 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3483 | int i; |
| 3484 | struct sample smp; |
| 3485 | |
| 3486 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3487 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3488 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3489 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3490 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3491 | |
| 3492 | /* Get extra arguments. */ |
| 3493 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3494 | if (i >= ARGM_NBARGS) |
| 3495 | break; |
| 3496 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3497 | } |
| 3498 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3499 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3500 | |
| 3501 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3502 | 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] | 3503 | |
| 3504 | /* Run the special args checker. */ |
| 3505 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3506 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3507 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3511 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3512 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3513 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3514 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3515 | } |
| 3516 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3517 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3518 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3519 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3520 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3521 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3522 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3523 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3524 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3525 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3526 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3527 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3528 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3532 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3533 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3534 | lua_pushstring(L, ""); |
| 3535 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3536 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3537 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3541 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3542 | hlua_smp2lua_str(L, &smp); |
| 3543 | else |
| 3544 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3545 | end: |
| 3546 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3547 | if (args[i].type == ARGT_STR) |
| 3548 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3549 | else if (args[i].type == ARGT_REG) |
| 3550 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3551 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3552 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3553 | |
| 3554 | error: |
| 3555 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3556 | if (args[i].type == ARGT_STR) |
| 3557 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3558 | else if (args[i].type == ARGT_REG) |
| 3559 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3560 | } |
| 3561 | WILL_LJMP(lua_error(L)); |
| 3562 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3563 | } |
| 3564 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3565 | /* |
| 3566 | * |
| 3567 | * |
| 3568 | * Class AppletTCP |
| 3569 | * |
| 3570 | * |
| 3571 | */ |
| 3572 | |
| 3573 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3574 | * a class stream, otherwise it throws an error. |
| 3575 | */ |
| 3576 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3577 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3578 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | /* This function creates and push in the stack an Applet object |
| 3582 | * according with a current TXN. |
| 3583 | */ |
| 3584 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3585 | { |
| 3586 | struct hlua_appctx *appctx; |
| 3587 | struct stream_interface *si = ctx->owner; |
| 3588 | struct stream *s = si_strm(si); |
| 3589 | struct proxy *p = s->be; |
| 3590 | |
| 3591 | /* Check stack size. */ |
| 3592 | if (!lua_checkstack(L, 3)) |
| 3593 | return 0; |
| 3594 | |
| 3595 | /* Create the object: obj[0] = userdata. |
| 3596 | * Note that the base of the Converters object is the |
| 3597 | * same than the TXN object. |
| 3598 | */ |
| 3599 | lua_newtable(L); |
| 3600 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3601 | lua_rawseti(L, -2, 0); |
| 3602 | appctx->appctx = ctx; |
| 3603 | appctx->htxn.s = s; |
| 3604 | appctx->htxn.p = p; |
| 3605 | |
| 3606 | /* Create the "f" field that contains a list of fetches. */ |
| 3607 | lua_pushstring(L, "f"); |
| 3608 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3609 | return 0; |
| 3610 | lua_settable(L, -3); |
| 3611 | |
| 3612 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3613 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3614 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3615 | return 0; |
| 3616 | lua_settable(L, -3); |
| 3617 | |
| 3618 | /* Create the "c" field that contains a list of converters. */ |
| 3619 | lua_pushstring(L, "c"); |
| 3620 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3621 | return 0; |
| 3622 | lua_settable(L, -3); |
| 3623 | |
| 3624 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3625 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3626 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3627 | return 0; |
| 3628 | lua_settable(L, -3); |
| 3629 | |
| 3630 | /* Pop a class stream metatable and affect it to the table. */ |
| 3631 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3632 | lua_setmetatable(L, -2); |
| 3633 | |
| 3634 | return 1; |
| 3635 | } |
| 3636 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3637 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3638 | { |
| 3639 | struct hlua_appctx *appctx; |
| 3640 | struct stream *s; |
| 3641 | const char *name; |
| 3642 | size_t len; |
| 3643 | struct sample smp; |
| 3644 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3645 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3646 | 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] | 3647 | |
| 3648 | /* It is useles to retrieve the stream, but this function |
| 3649 | * runs only in a stream context. |
| 3650 | */ |
| 3651 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3652 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3653 | s = appctx->htxn.s; |
| 3654 | |
| 3655 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3656 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3657 | hlua_lua2smp(L, 3, &smp); |
| 3658 | |
| 3659 | /* Store the sample in a variable. */ |
| 3660 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3661 | |
| 3662 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3663 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3664 | else |
| 3665 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3666 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3667 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3671 | { |
| 3672 | struct hlua_appctx *appctx; |
| 3673 | struct stream *s; |
| 3674 | const char *name; |
| 3675 | size_t len; |
| 3676 | struct sample smp; |
| 3677 | |
| 3678 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3679 | |
| 3680 | /* It is useles to retrieve the stream, but this function |
| 3681 | * runs only in a stream context. |
| 3682 | */ |
| 3683 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3684 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3685 | s = appctx->htxn.s; |
| 3686 | |
| 3687 | /* Unset the variable. */ |
| 3688 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3689 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3690 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3694 | { |
| 3695 | struct hlua_appctx *appctx; |
| 3696 | struct stream *s; |
| 3697 | const char *name; |
| 3698 | size_t len; |
| 3699 | struct sample smp; |
| 3700 | |
| 3701 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3702 | |
| 3703 | /* It is useles to retrieve the stream, but this function |
| 3704 | * runs only in a stream context. |
| 3705 | */ |
| 3706 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3707 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3708 | s = appctx->htxn.s; |
| 3709 | |
| 3710 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3711 | if (!vars_get_by_name(name, len, &smp)) { |
| 3712 | lua_pushnil(L); |
| 3713 | return 1; |
| 3714 | } |
| 3715 | |
| 3716 | return hlua_smp2lua(L, &smp); |
| 3717 | } |
| 3718 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3719 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3720 | { |
| 3721 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3722 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3723 | struct hlua *hlua; |
| 3724 | |
| 3725 | /* 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] | 3726 | if (!s->hlua) |
| 3727 | return 0; |
| 3728 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3729 | |
| 3730 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3731 | |
| 3732 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3733 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3734 | |
| 3735 | /* Get and store new value. */ |
| 3736 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3737 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3738 | |
| 3739 | return 0; |
| 3740 | } |
| 3741 | |
| 3742 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3743 | { |
| 3744 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3745 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3746 | struct hlua *hlua; |
| 3747 | |
| 3748 | /* 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] | 3749 | if (!s->hlua) { |
| 3750 | lua_pushnil(L); |
| 3751 | return 1; |
| 3752 | } |
| 3753 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3754 | |
| 3755 | /* Push configuration index in the stack. */ |
| 3756 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3757 | |
| 3758 | return 1; |
| 3759 | } |
| 3760 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3761 | /* If expected data not yet available, it returns a yield. This function |
| 3762 | * consumes the data in the buffer. It returns a string containing the |
| 3763 | * data. This string can be empty. |
| 3764 | */ |
| 3765 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3766 | { |
| 3767 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3768 | struct stream_interface *si = appctx->appctx->owner; |
| 3769 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3770 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3771 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3772 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3773 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3774 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3775 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3776 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3777 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3778 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3779 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3780 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3781 | 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] | 3782 | } |
| 3783 | |
| 3784 | /* End of data: commit the total strings and return. */ |
| 3785 | if (ret < 0) { |
| 3786 | luaL_pushresult(&appctx->b); |
| 3787 | return 1; |
| 3788 | } |
| 3789 | |
| 3790 | /* Ensure that the block 2 length is usable. */ |
| 3791 | if (ret == 1) |
| 3792 | len2 = 0; |
| 3793 | |
| 3794 | /* dont check the max length read and dont check. */ |
| 3795 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3796 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3797 | |
| 3798 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3799 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3800 | luaL_pushresult(&appctx->b); |
| 3801 | return 1; |
| 3802 | } |
| 3803 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3804 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3805 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3806 | { |
| 3807 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3808 | |
| 3809 | /* Initialise the string catenation. */ |
| 3810 | luaL_buffinit(L, &appctx->b); |
| 3811 | |
| 3812 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3813 | } |
| 3814 | |
| 3815 | /* If expected data not yet available, it returns a yield. This function |
| 3816 | * consumes the data in the buffer. It returns a string containing the |
| 3817 | * data. This string can be empty. |
| 3818 | */ |
| 3819 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3820 | { |
| 3821 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3822 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3823 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3824 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3825 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3826 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3827 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3828 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3829 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3830 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3831 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3832 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3833 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3834 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3835 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3836 | 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] | 3837 | } |
| 3838 | |
| 3839 | /* End of data: commit the total strings and return. */ |
| 3840 | if (ret < 0) { |
| 3841 | luaL_pushresult(&appctx->b); |
| 3842 | return 1; |
| 3843 | } |
| 3844 | |
| 3845 | /* Ensure that the block 2 length is usable. */ |
| 3846 | if (ret == 1) |
| 3847 | len2 = 0; |
| 3848 | |
| 3849 | if (len == -1) { |
| 3850 | |
| 3851 | /* If len == -1, catenate all the data avalaile and |
| 3852 | * yield because we want to get all the data until |
| 3853 | * the end of data stream. |
| 3854 | */ |
| 3855 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3856 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3857 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3858 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3859 | 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] | 3860 | |
| 3861 | } else { |
| 3862 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3863 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3864 | if (len1 > len) |
| 3865 | len1 = len; |
| 3866 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3867 | len -= len1; |
| 3868 | |
| 3869 | /* Copy the second block. */ |
| 3870 | if (len2 > len) |
| 3871 | len2 = len; |
| 3872 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3873 | len -= len2; |
| 3874 | |
| 3875 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3876 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3877 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3878 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3879 | if (len > 0) { |
| 3880 | lua_pushinteger(L, len); |
| 3881 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3882 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3883 | 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] | 3884 | } |
| 3885 | |
| 3886 | /* return the result. */ |
| 3887 | luaL_pushresult(&appctx->b); |
| 3888 | return 1; |
| 3889 | } |
| 3890 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3891 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3892 | hlua_pusherror(L, "Lua: internal error"); |
| 3893 | WILL_LJMP(lua_error(L)); |
| 3894 | return 0; |
| 3895 | } |
| 3896 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3897 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3898 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3899 | { |
| 3900 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3901 | int len = -1; |
| 3902 | |
| 3903 | if (lua_gettop(L) > 2) |
| 3904 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3905 | if (lua_gettop(L) >= 2) { |
| 3906 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3907 | lua_pop(L, 1); |
| 3908 | } |
| 3909 | |
| 3910 | /* Confirm or set the required length */ |
| 3911 | lua_pushinteger(L, len); |
| 3912 | |
| 3913 | /* Initialise the string catenation. */ |
| 3914 | luaL_buffinit(L, &appctx->b); |
| 3915 | |
| 3916 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3917 | } |
| 3918 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3919 | /* Append data in the output side of the buffer. This data is immediately |
| 3920 | * sent. The function returns the amount of data written. If the buffer |
| 3921 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3922 | * if the channel is closed. |
| 3923 | */ |
| 3924 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3925 | { |
| 3926 | size_t len; |
| 3927 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3928 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3929 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3930 | struct stream_interface *si = appctx->appctx->owner; |
| 3931 | struct channel *chn = si_ic(si); |
| 3932 | int max; |
| 3933 | |
| 3934 | /* Get the max amount of data which can write as input in the channel. */ |
| 3935 | max = channel_recv_max(chn); |
| 3936 | if (max > (len - l)) |
| 3937 | max = len - l; |
| 3938 | |
| 3939 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3940 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3941 | |
| 3942 | /* update counters. */ |
| 3943 | l += max; |
| 3944 | lua_pop(L, 1); |
| 3945 | lua_pushinteger(L, l); |
| 3946 | |
| 3947 | /* If some data is not send, declares the situation to the |
| 3948 | * applet, and returns a yield. |
| 3949 | */ |
| 3950 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3951 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3952 | 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] | 3953 | } |
| 3954 | |
| 3955 | return 1; |
| 3956 | } |
| 3957 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3958 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3959 | * yield the LUA process, and resume it without checking the |
| 3960 | * input arguments. |
| 3961 | */ |
| 3962 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3963 | { |
| 3964 | MAY_LJMP(check_args(L, 2, "send")); |
| 3965 | lua_pushinteger(L, 0); |
| 3966 | |
| 3967 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3968 | } |
| 3969 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3970 | /* |
| 3971 | * |
| 3972 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3973 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3974 | * |
| 3975 | * |
| 3976 | */ |
| 3977 | |
| 3978 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3979 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3980 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3981 | __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] | 3982 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3983 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3986 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3987 | * according with a current TXN. |
| 3988 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3989 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3990 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3991 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3992 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3993 | struct stream_interface *si = ctx->owner; |
| 3994 | struct stream *s = si_strm(si); |
| 3995 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3996 | struct htx *htx; |
| 3997 | struct htx_blk *blk; |
| 3998 | struct htx_sl *sl; |
| 3999 | struct ist path; |
| 4000 | unsigned long long len = 0; |
| 4001 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4002 | |
| 4003 | /* Check stack size. */ |
| 4004 | if (!lua_checkstack(L, 3)) |
| 4005 | return 0; |
| 4006 | |
| 4007 | /* Create the object: obj[0] = userdata. |
| 4008 | * Note that the base of the Converters object is the |
| 4009 | * same than the TXN object. |
| 4010 | */ |
| 4011 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4012 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4013 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4014 | appctx->appctx = ctx; |
| 4015 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4016 | 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] | 4017 | appctx->htxn.s = s; |
| 4018 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4019 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4020 | /* Create the "f" field that contains a list of fetches. */ |
| 4021 | lua_pushstring(L, "f"); |
| 4022 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4023 | return 0; |
| 4024 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4025 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4026 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4027 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4028 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4029 | return 0; |
| 4030 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4031 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4032 | /* Create the "c" field that contains a list of converters. */ |
| 4033 | lua_pushstring(L, "c"); |
| 4034 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4035 | return 0; |
| 4036 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4037 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4038 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4039 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4040 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4041 | return 0; |
| 4042 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4043 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4044 | htx = htxbuf(&s->req.buf); |
| 4045 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4046 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4047 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4048 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4049 | /* Stores the request method. */ |
| 4050 | lua_pushstring(L, "method"); |
| 4051 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4052 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4053 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4054 | /* Stores the http version. */ |
| 4055 | lua_pushstring(L, "version"); |
| 4056 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4057 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4058 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4059 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4060 | * the array on the top of the stack. |
| 4061 | */ |
| 4062 | lua_pushstring(L, "headers"); |
| 4063 | htxn.s = s; |
| 4064 | htxn.p = px; |
| 4065 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4066 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4067 | return 0; |
| 4068 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4069 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4070 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4071 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4072 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4073 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4074 | p = path.ptr; |
| 4075 | end = path.ptr + path.len; |
| 4076 | q = p; |
| 4077 | while (q < end && *q != '?') |
| 4078 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4079 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4080 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4081 | lua_pushstring(L, "path"); |
| 4082 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4083 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4084 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4085 | /* Stores the query string. */ |
| 4086 | lua_pushstring(L, "qs"); |
| 4087 | if (*q == '?') |
| 4088 | q++; |
| 4089 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4090 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4091 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4092 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4093 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4094 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4095 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4096 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4097 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 4098 | break; |
| 4099 | if (type == HTX_BLK_DATA) |
| 4100 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4101 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4102 | if (htx->extra != ULLONG_MAX) |
| 4103 | len += htx->extra; |
| 4104 | |
| 4105 | /* Stores the request path. */ |
| 4106 | lua_pushstring(L, "length"); |
| 4107 | lua_pushinteger(L, len); |
| 4108 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4109 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4110 | /* Create an empty array of HTTP request headers. */ |
| 4111 | lua_pushstring(L, "response"); |
| 4112 | lua_newtable(L); |
| 4113 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4114 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4115 | /* Pop a class stream metatable and affect it to the table. */ |
| 4116 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4117 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4118 | |
| 4119 | return 1; |
| 4120 | } |
| 4121 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4122 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4123 | { |
| 4124 | struct hlua_appctx *appctx; |
| 4125 | struct stream *s; |
| 4126 | const char *name; |
| 4127 | size_t len; |
| 4128 | struct sample smp; |
| 4129 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4130 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4131 | 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] | 4132 | |
| 4133 | /* It is useles to retrieve the stream, but this function |
| 4134 | * runs only in a stream context. |
| 4135 | */ |
| 4136 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4137 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4138 | s = appctx->htxn.s; |
| 4139 | |
| 4140 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4141 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4142 | hlua_lua2smp(L, 3, &smp); |
| 4143 | |
| 4144 | /* Store the sample in a variable. */ |
| 4145 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4146 | |
| 4147 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4148 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4149 | else |
| 4150 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4151 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4152 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4156 | { |
| 4157 | struct hlua_appctx *appctx; |
| 4158 | struct stream *s; |
| 4159 | const char *name; |
| 4160 | size_t len; |
| 4161 | struct sample smp; |
| 4162 | |
| 4163 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4164 | |
| 4165 | /* It is useles to retrieve the stream, but this function |
| 4166 | * runs only in a stream context. |
| 4167 | */ |
| 4168 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4169 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4170 | s = appctx->htxn.s; |
| 4171 | |
| 4172 | /* Unset the variable. */ |
| 4173 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4174 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4175 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4179 | { |
| 4180 | struct hlua_appctx *appctx; |
| 4181 | struct stream *s; |
| 4182 | const char *name; |
| 4183 | size_t len; |
| 4184 | struct sample smp; |
| 4185 | |
| 4186 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4187 | |
| 4188 | /* It is useles to retrieve the stream, but this function |
| 4189 | * runs only in a stream context. |
| 4190 | */ |
| 4191 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4192 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4193 | s = appctx->htxn.s; |
| 4194 | |
| 4195 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4196 | if (!vars_get_by_name(name, len, &smp)) { |
| 4197 | lua_pushnil(L); |
| 4198 | return 1; |
| 4199 | } |
| 4200 | |
| 4201 | return hlua_smp2lua(L, &smp); |
| 4202 | } |
| 4203 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4204 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4205 | { |
| 4206 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4207 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4208 | struct hlua *hlua; |
| 4209 | |
| 4210 | /* 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] | 4211 | if (!s->hlua) |
| 4212 | return 0; |
| 4213 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4214 | |
| 4215 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4216 | |
| 4217 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4218 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4219 | |
| 4220 | /* Get and store new value. */ |
| 4221 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4222 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4223 | |
| 4224 | return 0; |
| 4225 | } |
| 4226 | |
| 4227 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4228 | { |
| 4229 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4230 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4231 | struct hlua *hlua; |
| 4232 | |
| 4233 | /* 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] | 4234 | if (!s->hlua) { |
| 4235 | lua_pushnil(L); |
| 4236 | return 1; |
| 4237 | } |
| 4238 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4239 | |
| 4240 | /* Push configuration index in the stack. */ |
| 4241 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4242 | |
| 4243 | return 1; |
| 4244 | } |
| 4245 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4246 | /* If expected data not yet available, it returns a yield. This function |
| 4247 | * consumes the data in the buffer. It returns a string containing the |
| 4248 | * data. This string can be empty. |
| 4249 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4250 | __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] | 4251 | { |
| 4252 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4253 | struct stream_interface *si = appctx->appctx->owner; |
| 4254 | struct channel *req = si_oc(si); |
| 4255 | struct htx *htx; |
| 4256 | struct htx_blk *blk; |
| 4257 | size_t count; |
| 4258 | int stop = 0; |
| 4259 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4260 | htx = htx_from_buf(&req->buf); |
| 4261 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4262 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4263 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4264 | while (count && !stop && blk) { |
| 4265 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4266 | uint32_t sz = htx_get_blksz(blk); |
| 4267 | struct ist v; |
| 4268 | uint32_t vlen; |
| 4269 | char *nl; |
| 4270 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4271 | if (type == HTX_BLK_EOM) { |
| 4272 | stop = 1; |
| 4273 | break; |
| 4274 | } |
| 4275 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4276 | vlen = sz; |
| 4277 | if (vlen > count) { |
| 4278 | if (type != HTX_BLK_DATA) |
| 4279 | break; |
| 4280 | vlen = count; |
| 4281 | } |
| 4282 | |
| 4283 | switch (type) { |
| 4284 | case HTX_BLK_UNUSED: |
| 4285 | break; |
| 4286 | |
| 4287 | case HTX_BLK_DATA: |
| 4288 | v = htx_get_blk_value(htx, blk); |
| 4289 | v.len = vlen; |
| 4290 | nl = istchr(v, '\n'); |
| 4291 | if (nl != NULL) { |
| 4292 | stop = 1; |
| 4293 | vlen = nl - v.ptr + 1; |
| 4294 | } |
| 4295 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4296 | break; |
| 4297 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4298 | case HTX_BLK_TLR: |
| 4299 | case HTX_BLK_EOM: |
| 4300 | stop = 1; |
| 4301 | break; |
| 4302 | |
| 4303 | default: |
| 4304 | break; |
| 4305 | } |
| 4306 | |
| 4307 | co_set_data(req, co_data(req) - vlen); |
| 4308 | count -= vlen; |
| 4309 | if (sz == vlen) |
| 4310 | blk = htx_remove_blk(htx, blk); |
| 4311 | else { |
| 4312 | htx_cut_data_blk(htx, blk, vlen); |
| 4313 | break; |
| 4314 | } |
| 4315 | } |
| 4316 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4317 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4318 | if (!stop) { |
| 4319 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4320 | 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] | 4321 | } |
| 4322 | |
| 4323 | /* return the result. */ |
| 4324 | luaL_pushresult(&appctx->b); |
| 4325 | return 1; |
| 4326 | } |
| 4327 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4328 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4329 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4330 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4331 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4332 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4333 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4334 | /* Initialise the string catenation. */ |
| 4335 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4336 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4337 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4338 | } |
| 4339 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4340 | /* If expected data not yet available, it returns a yield. This function |
| 4341 | * consumes the data in the buffer. It returns a string containing the |
| 4342 | * data. This string can be empty. |
| 4343 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4344 | __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] | 4345 | { |
| 4346 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4347 | struct stream_interface *si = appctx->appctx->owner; |
| 4348 | struct channel *req = si_oc(si); |
| 4349 | struct htx *htx; |
| 4350 | struct htx_blk *blk; |
| 4351 | size_t count; |
| 4352 | int len; |
| 4353 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4354 | htx = htx_from_buf(&req->buf); |
| 4355 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4356 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4357 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4358 | while (count && len && blk) { |
| 4359 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4360 | uint32_t sz = htx_get_blksz(blk); |
| 4361 | struct ist v; |
| 4362 | uint32_t vlen; |
| 4363 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4364 | if (type == HTX_BLK_EOM) { |
| 4365 | len = 0; |
| 4366 | break; |
| 4367 | } |
| 4368 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4369 | vlen = sz; |
| 4370 | if (len > 0 && vlen > len) |
| 4371 | vlen = len; |
| 4372 | if (vlen > count) { |
| 4373 | if (type != HTX_BLK_DATA) |
| 4374 | break; |
| 4375 | vlen = count; |
| 4376 | } |
| 4377 | |
| 4378 | switch (type) { |
| 4379 | case HTX_BLK_UNUSED: |
| 4380 | break; |
| 4381 | |
| 4382 | case HTX_BLK_DATA: |
| 4383 | v = htx_get_blk_value(htx, blk); |
| 4384 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4385 | break; |
| 4386 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4387 | case HTX_BLK_TLR: |
| 4388 | case HTX_BLK_EOM: |
| 4389 | len = 0; |
| 4390 | break; |
| 4391 | |
| 4392 | default: |
| 4393 | break; |
| 4394 | } |
| 4395 | |
| 4396 | co_set_data(req, co_data(req) - vlen); |
| 4397 | count -= vlen; |
| 4398 | if (len > 0) |
| 4399 | len -= vlen; |
| 4400 | if (sz == vlen) |
| 4401 | blk = htx_remove_blk(htx, blk); |
| 4402 | else { |
| 4403 | htx_cut_data_blk(htx, blk, vlen); |
| 4404 | break; |
| 4405 | } |
| 4406 | } |
| 4407 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4408 | htx_to_buf(htx, &req->buf); |
| 4409 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4410 | /* If we are no other data available, yield waiting for new data. */ |
| 4411 | if (len) { |
| 4412 | if (len > 0) { |
| 4413 | lua_pushinteger(L, len); |
| 4414 | lua_replace(L, 2); |
| 4415 | } |
| 4416 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4417 | 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] | 4418 | } |
| 4419 | |
| 4420 | /* return the result. */ |
| 4421 | luaL_pushresult(&appctx->b); |
| 4422 | return 1; |
| 4423 | } |
| 4424 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4425 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4426 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4427 | { |
| 4428 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4429 | int len = -1; |
| 4430 | |
| 4431 | /* Check arguments. */ |
| 4432 | if (lua_gettop(L) > 2) |
| 4433 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4434 | if (lua_gettop(L) >= 2) { |
| 4435 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4436 | lua_pop(L, 1); |
| 4437 | } |
| 4438 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4439 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4440 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4441 | /* Initialise the string catenation. */ |
| 4442 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4443 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4444 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | /* Append data in the output side of the buffer. This data is immediately |
| 4448 | * sent. The function returns the amount of data written. If the buffer |
| 4449 | * cannot contain the data, the function yields. The function returns -1 |
| 4450 | * if the channel is closed. |
| 4451 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4452 | __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] | 4453 | { |
| 4454 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4455 | struct stream_interface *si = appctx->appctx->owner; |
| 4456 | struct channel *res = si_ic(si); |
| 4457 | struct htx *htx = htx_from_buf(&res->buf); |
| 4458 | const char *data; |
| 4459 | size_t len; |
| 4460 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4461 | int max; |
| 4462 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4463 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4464 | if (!max) |
| 4465 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4466 | |
| 4467 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4468 | |
| 4469 | /* Get the max amount of data which can write as input in the channel. */ |
| 4470 | if (max > (len - l)) |
| 4471 | max = len - l; |
| 4472 | |
| 4473 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4474 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4475 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4476 | |
| 4477 | /* update counters. */ |
| 4478 | l += max; |
| 4479 | lua_pop(L, 1); |
| 4480 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4481 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4482 | /* If some data is not send, declares the situation to the |
| 4483 | * applet, and returns a yield. |
| 4484 | */ |
| 4485 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4486 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4487 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4488 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4489 | 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] | 4490 | } |
| 4491 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4492 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4493 | return 1; |
| 4494 | } |
| 4495 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4496 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4497 | * yield the LUA process, and resume it without checking the |
| 4498 | * input arguments. |
| 4499 | */ |
| 4500 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4501 | { |
| 4502 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4503 | |
| 4504 | /* We want to send some data. Headers must be sent. */ |
| 4505 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4506 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4507 | WILL_LJMP(lua_error(L)); |
| 4508 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4509 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4510 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4511 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4512 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4513 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4517 | { |
| 4518 | const char *name; |
| 4519 | int ret; |
| 4520 | |
| 4521 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4522 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4523 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4524 | |
| 4525 | /* Push in the stack the "response" entry. */ |
| 4526 | ret = lua_getfield(L, 1, "response"); |
| 4527 | if (ret != LUA_TTABLE) { |
| 4528 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4529 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4530 | WILL_LJMP(lua_error(L)); |
| 4531 | } |
| 4532 | |
| 4533 | /* check if the header is already registered if it is not |
| 4534 | * the case, register it. |
| 4535 | */ |
| 4536 | ret = lua_getfield(L, -1, name); |
| 4537 | if (ret == LUA_TNIL) { |
| 4538 | |
| 4539 | /* Entry not found. */ |
| 4540 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4541 | |
| 4542 | /* Insert the new header name in the array in the top of the stack. |
| 4543 | * It left the new array in the top of the stack. |
| 4544 | */ |
| 4545 | lua_newtable(L); |
| 4546 | lua_pushvalue(L, 2); |
| 4547 | lua_pushvalue(L, -2); |
| 4548 | lua_settable(L, -4); |
| 4549 | |
| 4550 | } else if (ret != LUA_TTABLE) { |
| 4551 | |
| 4552 | /* corruption error. */ |
| 4553 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4554 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4555 | WILL_LJMP(lua_error(L)); |
| 4556 | } |
| 4557 | |
| 4558 | /* Now the top od thestack is an array of values. We push |
| 4559 | * the header value as new entry. |
| 4560 | */ |
| 4561 | lua_pushvalue(L, 3); |
| 4562 | ret = lua_rawlen(L, -2); |
| 4563 | lua_rawseti(L, -2, ret + 1); |
| 4564 | lua_pushboolean(L, 1); |
| 4565 | return 1; |
| 4566 | } |
| 4567 | |
| 4568 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4569 | { |
| 4570 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4571 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4572 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4573 | |
| 4574 | if (status < 100 || status > 599) { |
| 4575 | lua_pushboolean(L, 0); |
| 4576 | return 1; |
| 4577 | } |
| 4578 | |
| 4579 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4580 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4581 | lua_pushboolean(L, 1); |
| 4582 | return 1; |
| 4583 | } |
| 4584 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4585 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4586 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4587 | { |
| 4588 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4589 | struct stream_interface *si = appctx->appctx->owner; |
| 4590 | struct channel *res = si_ic(si); |
| 4591 | struct htx *htx; |
| 4592 | struct htx_sl *sl; |
| 4593 | struct h1m h1m; |
| 4594 | const char *status, *reason; |
| 4595 | const char *name, *value; |
| 4596 | size_t nlen, vlen; |
| 4597 | unsigned int flags; |
| 4598 | |
| 4599 | /* Send the message at once. */ |
| 4600 | htx = htx_from_buf(&res->buf); |
| 4601 | h1m_init_res(&h1m); |
| 4602 | |
| 4603 | /* Use the same http version than the request. */ |
| 4604 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4605 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4606 | if (reason == NULL) |
| 4607 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4608 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4609 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4610 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4611 | } |
| 4612 | else { |
| 4613 | flags = HTX_SL_F_IS_RESP; |
| 4614 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4615 | } |
| 4616 | if (!sl) { |
| 4617 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4618 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4619 | WILL_LJMP(lua_error(L)); |
| 4620 | } |
| 4621 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4622 | |
| 4623 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4624 | lua_pushvalue(L, 0); |
| 4625 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4626 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4627 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4628 | WILL_LJMP(lua_error(L)); |
| 4629 | } |
| 4630 | |
| 4631 | /* Browse the list of headers. */ |
| 4632 | lua_pushnil(L); |
| 4633 | while(lua_next(L, -2) != 0) { |
| 4634 | /* We expect a string as -2. */ |
| 4635 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4636 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4637 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4638 | lua_typename(L, lua_type(L, -2))); |
| 4639 | WILL_LJMP(lua_error(L)); |
| 4640 | } |
| 4641 | name = lua_tolstring(L, -2, &nlen); |
| 4642 | |
| 4643 | /* We expect an array as -1. */ |
| 4644 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4645 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4646 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4647 | name, |
| 4648 | lua_typename(L, lua_type(L, -1))); |
| 4649 | WILL_LJMP(lua_error(L)); |
| 4650 | } |
| 4651 | |
| 4652 | /* Browse the table who is on the top of the stack. */ |
| 4653 | lua_pushnil(L); |
| 4654 | while(lua_next(L, -2) != 0) { |
| 4655 | int id; |
| 4656 | |
| 4657 | /* We expect a number as -2. */ |
| 4658 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4659 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4660 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4661 | name, |
| 4662 | lua_typename(L, lua_type(L, -2))); |
| 4663 | WILL_LJMP(lua_error(L)); |
| 4664 | } |
| 4665 | id = lua_tointeger(L, -2); |
| 4666 | |
| 4667 | /* We expect a string as -2. */ |
| 4668 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4669 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4670 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4671 | name, id, |
| 4672 | lua_typename(L, lua_type(L, -1))); |
| 4673 | WILL_LJMP(lua_error(L)); |
| 4674 | } |
| 4675 | value = lua_tolstring(L, -1, &vlen); |
| 4676 | |
| 4677 | /* Simple Protocol checks. */ |
| 4678 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4679 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4680 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4681 | struct ist v = ist2(value, vlen); |
| 4682 | int ret; |
| 4683 | |
| 4684 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4685 | if (ret < 0) { |
| 4686 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4687 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4688 | name); |
| 4689 | WILL_LJMP(lua_error(L)); |
| 4690 | } |
| 4691 | else if (ret == 0) |
| 4692 | goto next; /* Skip it */ |
| 4693 | } |
| 4694 | |
| 4695 | /* Add a new header */ |
| 4696 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4697 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4698 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4699 | name); |
| 4700 | WILL_LJMP(lua_error(L)); |
| 4701 | } |
| 4702 | next: |
| 4703 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4704 | lua_pop(L, 1); |
| 4705 | } |
| 4706 | |
| 4707 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4708 | lua_pop(L, 1); |
| 4709 | } |
| 4710 | |
| 4711 | if (h1m.flags & H1_MF_CHNK) |
| 4712 | h1m.flags &= ~H1_MF_CLEN; |
| 4713 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4714 | h1m.flags |= H1_MF_XFER_LEN; |
| 4715 | |
| 4716 | /* Uset HTX start-line flags */ |
| 4717 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4718 | flags |= HTX_SL_F_XFER_ENC; |
| 4719 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4720 | flags |= HTX_SL_F_XFER_LEN; |
| 4721 | if (h1m.flags & H1_MF_CHNK) |
| 4722 | flags |= HTX_SL_F_CHNK; |
| 4723 | else if (h1m.flags & H1_MF_CLEN) |
| 4724 | flags |= HTX_SL_F_CLEN; |
| 4725 | if (h1m.body_len == 0) |
| 4726 | flags |= HTX_SL_F_BODYLESS; |
| 4727 | } |
| 4728 | sl->flags |= flags; |
| 4729 | |
| 4730 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4731 | * and the status code implies the presence of a message body, we must |
| 4732 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4733 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4734 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4735 | */ |
| 4736 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4737 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4738 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4739 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4740 | /* Add a new header */ |
| 4741 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4742 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4743 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4744 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4745 | WILL_LJMP(lua_error(L)); |
| 4746 | } |
| 4747 | } |
| 4748 | |
| 4749 | /* Finalize headers. */ |
| 4750 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4751 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4752 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4753 | WILL_LJMP(lua_error(L)); |
| 4754 | } |
| 4755 | |
| 4756 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4757 | b_reset(&res->buf); |
| 4758 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4759 | WILL_LJMP(lua_error(L)); |
| 4760 | } |
| 4761 | |
| 4762 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4763 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4764 | |
| 4765 | /* Headers sent, set the flag. */ |
| 4766 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4767 | return 0; |
| 4768 | |
| 4769 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4770 | /* We will build the status line and the headers of the HTTP response. |
| 4771 | * We will try send at once if its not possible, we give back the hand |
| 4772 | * waiting for more room. |
| 4773 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4774 | __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] | 4775 | { |
| 4776 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4777 | struct stream_interface *si = appctx->appctx->owner; |
| 4778 | struct channel *res = si_ic(si); |
| 4779 | |
| 4780 | if (co_data(res)) { |
| 4781 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4782 | 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] | 4783 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4784 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4788 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4789 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4790 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4791 | } |
| 4792 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4793 | /* |
| 4794 | * |
| 4795 | * |
| 4796 | * Class HTTP |
| 4797 | * |
| 4798 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4799 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4800 | |
| 4801 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4802 | * a class stream, otherwise it throws an error. |
| 4803 | */ |
| 4804 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4805 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4806 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4807 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4808 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4809 | /* This function creates and push in the stack a HTTP object |
| 4810 | * according with a current TXN. |
| 4811 | */ |
| 4812 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4813 | { |
| 4814 | struct hlua_txn *htxn; |
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 | /* Check stack size. */ |
| 4817 | if (!lua_checkstack(L, 3)) |
| 4818 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4819 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4820 | /* Create the object: obj[0] = userdata. |
| 4821 | * Note that the base of the Converters object is the |
| 4822 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4823 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4824 | lua_newtable(L); |
| 4825 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4826 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4827 | |
| 4828 | htxn->s = txn->s; |
| 4829 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4830 | htxn->dir = txn->dir; |
| 4831 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4832 | |
| 4833 | /* Pop a class stream metatable and affect it to the table. */ |
| 4834 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4835 | lua_setmetatable(L, -2); |
| 4836 | |
| 4837 | return 1; |
| 4838 | } |
| 4839 | |
| 4840 | /* This function creates ans returns an array of HTTP headers. |
| 4841 | * This function does not fails. It is used as wrapper with the |
| 4842 | * 2 following functions. |
| 4843 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4844 | __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] | 4845 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4846 | struct htx *htx; |
| 4847 | int32_t pos; |
| 4848 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4849 | /* Create the table. */ |
| 4850 | lua_newtable(L); |
| 4851 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4852 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4853 | htx = htxbuf(&msg->chn->buf); |
| 4854 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4855 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4856 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4857 | struct ist n, v; |
| 4858 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4859 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4860 | if (type == HTX_BLK_HDR) { |
| 4861 | n = htx_get_blk_name(htx,blk); |
| 4862 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4863 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4864 | else if (type == HTX_BLK_EOH) |
| 4865 | break; |
| 4866 | else |
| 4867 | continue; |
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 | /* Check for existing entry: |
| 4870 | * assume that the table is on the top of the stack, and |
| 4871 | * push the key in the stack, the function lua_gettable() |
| 4872 | * perform the lookup. |
| 4873 | */ |
| 4874 | lua_pushlstring(L, n.ptr, n.len); |
| 4875 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4876 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4877 | switch (lua_type(L, -1)) { |
| 4878 | case LUA_TNIL: |
| 4879 | /* Table not found, create it. */ |
| 4880 | lua_pop(L, 1); /* remove the nil value. */ |
| 4881 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4882 | lua_newtable(L); /* create and push empty table. */ |
| 4883 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4884 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4885 | 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] | 4886 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4887 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4888 | case LUA_TTABLE: |
| 4889 | /* Entry found: push the value in the table. */ |
| 4890 | len = lua_rawlen(L, -1); |
| 4891 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4892 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4893 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4894 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4895 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4896 | default: |
| 4897 | /* Other cases are errors. */ |
| 4898 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4899 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4900 | } |
| 4901 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4902 | return 1; |
| 4903 | } |
| 4904 | |
| 4905 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4906 | { |
| 4907 | struct hlua_txn *htxn; |
| 4908 | |
| 4909 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4910 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4911 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4912 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4913 | WILL_LJMP(lua_error(L)); |
| 4914 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4915 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4919 | { |
| 4920 | struct hlua_txn *htxn; |
| 4921 | |
| 4922 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4923 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4924 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4925 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4926 | WILL_LJMP(lua_error(L)); |
| 4927 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4928 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | /* This function replace full header, or just a value in |
| 4932 | * the request or in the response. It is a wrapper fir the |
| 4933 | * 4 following functions. |
| 4934 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4935 | __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] | 4936 | { |
| 4937 | size_t name_len; |
| 4938 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4939 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4940 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4941 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4942 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4943 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4944 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4945 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4946 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4947 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4948 | 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] | 4949 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4950 | return 0; |
| 4951 | } |
| 4952 | |
| 4953 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4954 | { |
| 4955 | struct hlua_txn *htxn; |
| 4956 | |
| 4957 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4958 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4959 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4960 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4961 | WILL_LJMP(lua_error(L)); |
| 4962 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4963 | 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] | 4964 | } |
| 4965 | |
| 4966 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4967 | { |
| 4968 | struct hlua_txn *htxn; |
| 4969 | |
| 4970 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4971 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4972 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4973 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4974 | WILL_LJMP(lua_error(L)); |
| 4975 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4976 | 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] | 4977 | } |
| 4978 | |
| 4979 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4980 | { |
| 4981 | struct hlua_txn *htxn; |
| 4982 | |
| 4983 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4984 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4985 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4986 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4987 | WILL_LJMP(lua_error(L)); |
| 4988 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4989 | 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] | 4990 | } |
| 4991 | |
| 4992 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4993 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4994 | struct hlua_txn *htxn; |
| 4995 | |
| 4996 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4997 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4998 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4999 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5000 | WILL_LJMP(lua_error(L)); |
| 5001 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5002 | 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] | 5003 | } |
| 5004 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5005 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5006 | * It is a wrapper for the 2 following functions. |
| 5007 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5008 | __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] | 5009 | { |
| 5010 | size_t len; |
| 5011 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5012 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5013 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5014 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5015 | ctx.blk = NULL; |
| 5016 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5017 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5018 | return 0; |
| 5019 | } |
| 5020 | |
| 5021 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5022 | { |
| 5023 | struct hlua_txn *htxn; |
| 5024 | |
| 5025 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5026 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5027 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5028 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5029 | WILL_LJMP(lua_error(L)); |
| 5030 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5031 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5035 | { |
| 5036 | struct hlua_txn *htxn; |
| 5037 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5038 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5039 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5040 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5041 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5042 | WILL_LJMP(lua_error(L)); |
| 5043 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5044 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | /* This function adds an header. It is a wrapper used by |
| 5048 | * the 2 following functions. |
| 5049 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5050 | __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] | 5051 | { |
| 5052 | size_t name_len; |
| 5053 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5054 | size_t value_len; |
| 5055 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5056 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5057 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5058 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5059 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5060 | return 0; |
| 5061 | } |
| 5062 | |
| 5063 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5064 | { |
| 5065 | struct hlua_txn *htxn; |
| 5066 | |
| 5067 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5068 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5069 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5070 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5071 | WILL_LJMP(lua_error(L)); |
| 5072 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5073 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5077 | { |
| 5078 | struct hlua_txn *htxn; |
| 5079 | |
| 5080 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5081 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5082 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5083 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5084 | WILL_LJMP(lua_error(L)); |
| 5085 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5086 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5090 | { |
| 5091 | struct hlua_txn *htxn; |
| 5092 | |
| 5093 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5094 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5095 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5096 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5097 | WILL_LJMP(lua_error(L)); |
| 5098 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5099 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5100 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5104 | { |
| 5105 | struct hlua_txn *htxn; |
| 5106 | |
| 5107 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5108 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5109 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5110 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5111 | WILL_LJMP(lua_error(L)); |
| 5112 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5113 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5114 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | /* This function set the method. */ |
| 5118 | static int hlua_http_req_set_meth(lua_State *L) |
| 5119 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5120 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5121 | size_t name_len; |
| 5122 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5123 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5124 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5125 | WILL_LJMP(lua_error(L)); |
| 5126 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5127 | 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] | 5128 | return 1; |
| 5129 | } |
| 5130 | |
| 5131 | /* This function set the method. */ |
| 5132 | static int hlua_http_req_set_path(lua_State *L) |
| 5133 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5134 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5135 | size_t name_len; |
| 5136 | 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] | 5137 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5138 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5139 | WILL_LJMP(lua_error(L)); |
| 5140 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5141 | 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] | 5142 | return 1; |
| 5143 | } |
| 5144 | |
| 5145 | /* This function set the query-string. */ |
| 5146 | static int hlua_http_req_set_query(lua_State *L) |
| 5147 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5148 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5149 | size_t name_len; |
| 5150 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5151 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5152 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5153 | WILL_LJMP(lua_error(L)); |
| 5154 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5155 | /* Check length. */ |
| 5156 | if (name_len > trash.size - 1) { |
| 5157 | lua_pushboolean(L, 0); |
| 5158 | return 1; |
| 5159 | } |
| 5160 | |
| 5161 | /* Add the mark question as prefix. */ |
| 5162 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5163 | trash.area[trash.data++] = '?'; |
| 5164 | memcpy(trash.area + trash.data, name, name_len); |
| 5165 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5166 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5167 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5168 | 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] | 5169 | return 1; |
| 5170 | } |
| 5171 | |
| 5172 | /* This function set the uri. */ |
| 5173 | static int hlua_http_req_set_uri(lua_State *L) |
| 5174 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5175 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5176 | size_t name_len; |
| 5177 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5178 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5179 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5180 | WILL_LJMP(lua_error(L)); |
| 5181 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5182 | 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] | 5183 | return 1; |
| 5184 | } |
| 5185 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5186 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5187 | static int hlua_http_res_set_status(lua_State *L) |
| 5188 | { |
| 5189 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5190 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5191 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5192 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5193 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5194 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5195 | WILL_LJMP(lua_error(L)); |
| 5196 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5197 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5198 | return 0; |
| 5199 | } |
| 5200 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5201 | /* |
| 5202 | * |
| 5203 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5204 | * Class TXN |
| 5205 | * |
| 5206 | * |
| 5207 | */ |
| 5208 | |
| 5209 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5210 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5211 | */ |
| 5212 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5213 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5214 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5215 | } |
| 5216 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5217 | __LJMP static int hlua_set_var(lua_State *L) |
| 5218 | { |
| 5219 | struct hlua_txn *htxn; |
| 5220 | const char *name; |
| 5221 | size_t len; |
| 5222 | struct sample smp; |
| 5223 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5224 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5225 | 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] | 5226 | |
| 5227 | /* It is useles to retrieve the stream, but this function |
| 5228 | * runs only in a stream context. |
| 5229 | */ |
| 5230 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5231 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5232 | |
| 5233 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5234 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5235 | hlua_lua2smp(L, 3, &smp); |
| 5236 | |
| 5237 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5238 | 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] | 5239 | |
| 5240 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5241 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5242 | else |
| 5243 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5244 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5245 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5246 | } |
| 5247 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5248 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5249 | { |
| 5250 | struct hlua_txn *htxn; |
| 5251 | const char *name; |
| 5252 | size_t len; |
| 5253 | struct sample smp; |
| 5254 | |
| 5255 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5256 | |
| 5257 | /* It is useles to retrieve the stream, but this function |
| 5258 | * runs only in a stream context. |
| 5259 | */ |
| 5260 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5261 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5262 | |
| 5263 | /* Unset the variable. */ |
| 5264 | 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] | 5265 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5266 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5267 | } |
| 5268 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5269 | __LJMP static int hlua_get_var(lua_State *L) |
| 5270 | { |
| 5271 | struct hlua_txn *htxn; |
| 5272 | const char *name; |
| 5273 | size_t len; |
| 5274 | struct sample smp; |
| 5275 | |
| 5276 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5277 | |
| 5278 | /* It is useles to retrieve the stream, but this function |
| 5279 | * runs only in a stream context. |
| 5280 | */ |
| 5281 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5282 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5283 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5284 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 5285 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5286 | lua_pushnil(L); |
| 5287 | return 1; |
| 5288 | } |
| 5289 | |
| 5290 | return hlua_smp2lua(L, &smp); |
| 5291 | } |
| 5292 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5293 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5294 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5295 | struct hlua *hlua; |
| 5296 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5297 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5298 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5299 | /* It is useles to retrieve the stream, but this function |
| 5300 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5301 | */ |
| 5302 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5303 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5304 | |
| 5305 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5306 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5307 | |
| 5308 | /* Get and store new value. */ |
| 5309 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5310 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5311 | |
| 5312 | return 0; |
| 5313 | } |
| 5314 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5315 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5316 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5317 | struct hlua *hlua; |
| 5318 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5319 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5320 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5321 | /* It is useles to retrieve the stream, but this function |
| 5322 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5323 | */ |
| 5324 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5325 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5326 | |
| 5327 | /* Push configuration index in the stack. */ |
| 5328 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5329 | |
| 5330 | return 1; |
| 5331 | } |
| 5332 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5333 | /* Create stack entry containing a class TXN. This function |
| 5334 | * return 0 if the stack does not contains free slots, |
| 5335 | * otherwise it returns 1. |
| 5336 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5337 | 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] | 5338 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5339 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5340 | |
| 5341 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5342 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5343 | return 0; |
| 5344 | |
| 5345 | /* NOTE: The allocation never fails. The failure |
| 5346 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5347 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5348 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5349 | /* Create the object: obj[0] = userdata. */ |
| 5350 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5351 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5352 | lua_rawseti(L, -2, 0); |
| 5353 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5354 | htxn->s = s; |
| 5355 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5356 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5357 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5358 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5359 | /* Create the "f" field that contains a list of fetches. */ |
| 5360 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5361 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5362 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5363 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5364 | |
| 5365 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5366 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5367 | 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] | 5368 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5369 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5370 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5371 | /* Create the "c" field that contains a list of converters. */ |
| 5372 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5373 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5374 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5375 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5376 | |
| 5377 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5378 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5379 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5380 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5381 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5382 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5383 | /* Create the "req" field that contains the request channel object. */ |
| 5384 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5385 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5386 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5387 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5388 | |
| 5389 | /* Create the "res" field that contains the response channel object. */ |
| 5390 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5391 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5392 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5393 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5394 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5395 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5396 | lua_pushstring(L, "http"); |
| 5397 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5398 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5399 | return 0; |
| 5400 | } |
| 5401 | else |
| 5402 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5403 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5404 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5405 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5406 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5407 | lua_setmetatable(L, -2); |
| 5408 | |
| 5409 | return 1; |
| 5410 | } |
| 5411 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5412 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5413 | { |
| 5414 | const char *msg; |
| 5415 | struct hlua_txn *htxn; |
| 5416 | |
| 5417 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5418 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5419 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5420 | |
| 5421 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5422 | return 0; |
| 5423 | } |
| 5424 | |
| 5425 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5426 | { |
| 5427 | int level; |
| 5428 | const char *msg; |
| 5429 | struct hlua_txn *htxn; |
| 5430 | |
| 5431 | MAY_LJMP(check_args(L, 3, "log")); |
| 5432 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5433 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5434 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5435 | |
| 5436 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5437 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5438 | |
| 5439 | hlua_sendlog(htxn->s->be, level, msg); |
| 5440 | return 0; |
| 5441 | } |
| 5442 | |
| 5443 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5444 | { |
| 5445 | const char *msg; |
| 5446 | struct hlua_txn *htxn; |
| 5447 | |
| 5448 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5449 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5450 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5451 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5452 | return 0; |
| 5453 | } |
| 5454 | |
| 5455 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5456 | { |
| 5457 | const char *msg; |
| 5458 | struct hlua_txn *htxn; |
| 5459 | |
| 5460 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5461 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5462 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5463 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5464 | return 0; |
| 5465 | } |
| 5466 | |
| 5467 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5468 | { |
| 5469 | const char *msg; |
| 5470 | struct hlua_txn *htxn; |
| 5471 | |
| 5472 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5473 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5474 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5475 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5476 | return 0; |
| 5477 | } |
| 5478 | |
| 5479 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5480 | { |
| 5481 | const char *msg; |
| 5482 | struct hlua_txn *htxn; |
| 5483 | |
| 5484 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5485 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5486 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5487 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5488 | return 0; |
| 5489 | } |
| 5490 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5491 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5492 | { |
| 5493 | struct hlua_txn *htxn; |
| 5494 | int ll; |
| 5495 | |
| 5496 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5497 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5498 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5499 | |
| 5500 | if (ll < 0 || ll > 7) |
| 5501 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5502 | |
| 5503 | htxn->s->logs.level = ll; |
| 5504 | return 0; |
| 5505 | } |
| 5506 | |
| 5507 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5508 | { |
| 5509 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5510 | int tos; |
| 5511 | |
| 5512 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5513 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5514 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5515 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5516 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5517 | return 0; |
| 5518 | } |
| 5519 | |
| 5520 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5521 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5522 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5523 | int mark; |
| 5524 | |
| 5525 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5526 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5527 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5528 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5529 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5530 | return 0; |
| 5531 | } |
| 5532 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5533 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5534 | { |
| 5535 | struct hlua_txn *htxn; |
| 5536 | |
| 5537 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5538 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5539 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5540 | return 0; |
| 5541 | } |
| 5542 | |
| 5543 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5544 | { |
| 5545 | struct hlua_txn *htxn; |
| 5546 | |
| 5547 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5548 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5549 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5550 | return 0; |
| 5551 | } |
| 5552 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5553 | /* 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] | 5554 | * 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] | 5555 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5556 | * error. The Reply must be on top of the stack. |
| 5557 | */ |
| 5558 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5559 | { |
| 5560 | struct htx *htx; |
| 5561 | struct htx_sl *sl; |
| 5562 | struct h1m h1m; |
| 5563 | const char *status, *reason, *body; |
| 5564 | size_t status_len, reason_len, body_len; |
| 5565 | int ret, code, flags; |
| 5566 | |
| 5567 | code = 200; |
| 5568 | status = "200"; |
| 5569 | status_len = 3; |
| 5570 | ret = lua_getfield(L, -1, "status"); |
| 5571 | if (ret == LUA_TNUMBER) { |
| 5572 | code = lua_tointeger(L, -1); |
| 5573 | status = lua_tolstring(L, -1, &status_len); |
| 5574 | } |
| 5575 | lua_pop(L, 1); |
| 5576 | |
| 5577 | reason = http_get_reason(code); |
| 5578 | reason_len = strlen(reason); |
| 5579 | ret = lua_getfield(L, -1, "reason"); |
| 5580 | if (ret == LUA_TSTRING) |
| 5581 | reason = lua_tolstring(L, -1, &reason_len); |
| 5582 | lua_pop(L, 1); |
| 5583 | |
| 5584 | body = NULL; |
| 5585 | body_len = 0; |
| 5586 | ret = lua_getfield(L, -1, "body"); |
| 5587 | if (ret == LUA_TSTRING) |
| 5588 | body = lua_tolstring(L, -1, &body_len); |
| 5589 | lua_pop(L, 1); |
| 5590 | |
| 5591 | /* Prepare the response before inserting the headers */ |
| 5592 | h1m_init_res(&h1m); |
| 5593 | htx = htx_from_buf(&s->res.buf); |
| 5594 | channel_htx_truncate(&s->res, htx); |
| 5595 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5596 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5597 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5598 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5599 | } |
| 5600 | else { |
| 5601 | flags = HTX_SL_F_IS_RESP; |
| 5602 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5603 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5604 | } |
| 5605 | if (!sl) |
| 5606 | goto fail; |
| 5607 | sl->info.res.status = code; |
| 5608 | |
| 5609 | /* Push in the stack the "headers" entry. */ |
| 5610 | ret = lua_getfield(L, -1, "headers"); |
| 5611 | if (ret != LUA_TTABLE) |
| 5612 | goto skip_headers; |
| 5613 | |
| 5614 | lua_pushnil(L); |
| 5615 | while (lua_next(L, -2) != 0) { |
| 5616 | struct ist name, value; |
| 5617 | const char *n, *v; |
| 5618 | size_t nlen, vlen; |
| 5619 | |
| 5620 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5621 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5622 | goto next_hdr; |
| 5623 | } |
| 5624 | |
| 5625 | n = lua_tolstring(L, -2, &nlen); |
| 5626 | name = ist2(n, nlen); |
| 5627 | if (isteqi(name, ist("content-length"))) { |
| 5628 | /* Always skip content-length header. It will be added |
| 5629 | * later with the correct len |
| 5630 | */ |
| 5631 | goto next_hdr; |
| 5632 | } |
| 5633 | |
| 5634 | /* Loop on header's values */ |
| 5635 | lua_pushnil(L); |
| 5636 | while (lua_next(L, -2)) { |
| 5637 | if (!lua_isstring(L, -1)) { |
| 5638 | /* Skip the value if it is not a string */ |
| 5639 | goto next_value; |
| 5640 | } |
| 5641 | |
| 5642 | v = lua_tolstring(L, -1, &vlen); |
| 5643 | value = ist2(v, vlen); |
| 5644 | |
| 5645 | if (isteqi(name, ist("transfer-encoding"))) |
| 5646 | h1_parse_xfer_enc_header(&h1m, value); |
| 5647 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5648 | goto fail; |
| 5649 | |
| 5650 | next_value: |
| 5651 | lua_pop(L, 1); |
| 5652 | } |
| 5653 | |
| 5654 | next_hdr: |
| 5655 | lua_pop(L, 1); |
| 5656 | } |
| 5657 | skip_headers: |
| 5658 | lua_pop(L, 1); |
| 5659 | |
| 5660 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5661 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5662 | const char *clen = ultoa(body_len); |
| 5663 | |
| 5664 | h1m.flags |= H1_MF_CLEN; |
| 5665 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5666 | goto fail; |
| 5667 | } |
| 5668 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5669 | h1m.flags |= H1_MF_XFER_LEN; |
| 5670 | |
| 5671 | /* Update HTX start-line flags */ |
| 5672 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5673 | flags |= HTX_SL_F_XFER_ENC; |
| 5674 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5675 | flags |= HTX_SL_F_XFER_LEN; |
| 5676 | if (h1m.flags & H1_MF_CHNK) |
| 5677 | flags |= HTX_SL_F_CHNK; |
| 5678 | else if (h1m.flags & H1_MF_CLEN) |
| 5679 | flags |= HTX_SL_F_CLEN; |
| 5680 | if (h1m.body_len == 0) |
| 5681 | flags |= HTX_SL_F_BODYLESS; |
| 5682 | } |
| 5683 | sl->flags |= flags; |
| 5684 | |
| 5685 | |
| 5686 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5687 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5688 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5689 | goto fail; |
| 5690 | |
| 5691 | /* Now, forward the response and terminate the transaction */ |
| 5692 | s->txn->status = code; |
| 5693 | htx_to_buf(htx, &s->res.buf); |
| 5694 | if (!http_forward_proxy_resp(s, 1)) |
| 5695 | goto fail; |
| 5696 | |
| 5697 | return 1; |
| 5698 | |
| 5699 | fail: |
| 5700 | channel_htx_truncate(&s->res, htx); |
| 5701 | return 0; |
| 5702 | } |
| 5703 | |
| 5704 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5705 | * processing is just aborted. Nothing is returned to the client and all |
| 5706 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5707 | * is forwarded to the client before terminating the transaction. On success, |
| 5708 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5709 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5710 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5711 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5712 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5713 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5714 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5715 | struct stream *s; |
| 5716 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5717 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5718 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5719 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5720 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5721 | * just end the execution of the current lua code. */ |
| 5722 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5723 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5724 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5725 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5726 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5727 | struct channel *req = &s->req; |
| 5728 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5729 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5730 | channel_auto_read(req); |
| 5731 | channel_abort(req); |
| 5732 | channel_auto_close(req); |
| 5733 | channel_erase(req); |
| 5734 | |
| 5735 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5736 | channel_auto_read(res); |
| 5737 | channel_auto_close(res); |
| 5738 | channel_shutr_now(res); |
| 5739 | |
| 5740 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5741 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5742 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5743 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5744 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5745 | /* No reply or invalid reply */ |
| 5746 | s->txn->status = 0; |
| 5747 | http_reply_and_close(s, 0, NULL); |
| 5748 | } |
| 5749 | else { |
| 5750 | /* Remove extra args to have the reply on top of the stack */ |
| 5751 | if (lua_gettop(L) > 2) |
| 5752 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5753 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5754 | if (!hlua_txn_forward_reply(L, s)) { |
| 5755 | if (!(s->flags & SF_ERR_MASK)) |
| 5756 | s->flags |= SF_ERR_PRXCOND; |
| 5757 | lua_pushinteger(L, ACT_RET_ERR); |
| 5758 | WILL_LJMP(hlua_done(L)); |
| 5759 | return 0; /* Never reached */ |
| 5760 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5761 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5762 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5763 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5764 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5765 | /* let's log the request time */ |
| 5766 | s->logs.tv_request = now; |
| 5767 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5768 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5769 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5770 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5771 | done: |
| 5772 | if (!(s->flags & SF_ERR_MASK)) |
| 5773 | s->flags |= SF_ERR_LOCAL; |
| 5774 | if (!(s->flags & SF_FINST_MASK)) |
| 5775 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5776 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5777 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5778 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5779 | return 0; |
| 5780 | } |
| 5781 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5782 | /* |
| 5783 | * |
| 5784 | * |
| 5785 | * Class REPLY |
| 5786 | * |
| 5787 | * |
| 5788 | */ |
| 5789 | |
| 5790 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5791 | * free slots, the function fails and returns 0; |
| 5792 | */ |
| 5793 | static int hlua_txn_reply_new(lua_State *L) |
| 5794 | { |
| 5795 | struct hlua_txn *htxn; |
| 5796 | const char *reason, *body = NULL; |
| 5797 | int ret, status; |
| 5798 | |
| 5799 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5800 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5801 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5802 | WILL_LJMP(lua_error(L)); |
| 5803 | } |
| 5804 | |
| 5805 | /* Default value */ |
| 5806 | status = 200; |
| 5807 | reason = http_get_reason(status); |
| 5808 | |
| 5809 | if (lua_istable(L, 2)) { |
| 5810 | /* load status and reason from the table argument at index 2 */ |
| 5811 | ret = lua_getfield(L, 2, "status"); |
| 5812 | if (ret == LUA_TNIL) |
| 5813 | goto reason; |
| 5814 | else if (ret != LUA_TNUMBER) { |
| 5815 | /* invalid status: ignore the reason */ |
| 5816 | goto body; |
| 5817 | } |
| 5818 | status = lua_tointeger(L, -1); |
| 5819 | |
| 5820 | reason: |
| 5821 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5822 | ret = lua_getfield(L, 2, "reason"); |
| 5823 | if (ret == LUA_TSTRING) |
| 5824 | reason = lua_tostring(L, -1); |
| 5825 | |
| 5826 | body: |
| 5827 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5828 | ret = lua_getfield(L, 2, "body"); |
| 5829 | if (ret == LUA_TSTRING) |
| 5830 | body = lua_tostring(L, -1); |
| 5831 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5832 | } |
| 5833 | |
| 5834 | /* Create the Reply table */ |
| 5835 | lua_newtable(L); |
| 5836 | |
| 5837 | /* Add status element */ |
| 5838 | lua_pushstring(L, "status"); |
| 5839 | lua_pushinteger(L, status); |
| 5840 | lua_settable(L, -3); |
| 5841 | |
| 5842 | /* Add reason element */ |
| 5843 | reason = http_get_reason(status); |
| 5844 | lua_pushstring(L, "reason"); |
| 5845 | lua_pushstring(L, reason); |
| 5846 | lua_settable(L, -3); |
| 5847 | |
| 5848 | /* Add body element, nil if undefined */ |
| 5849 | lua_pushstring(L, "body"); |
| 5850 | if (body) |
| 5851 | lua_pushstring(L, body); |
| 5852 | else |
| 5853 | lua_pushnil(L); |
| 5854 | lua_settable(L, -3); |
| 5855 | |
| 5856 | /* Add headers element */ |
| 5857 | lua_pushstring(L, "headers"); |
| 5858 | lua_newtable(L); |
| 5859 | |
| 5860 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5861 | if (lua_istable(L, 2)) { |
| 5862 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5863 | ret = lua_getfield(L, 2, "headers"); |
| 5864 | if (ret == LUA_TTABLE) { |
| 5865 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5866 | lua_pushnil(L); |
| 5867 | while (lua_next(L, -2) != 0) { |
| 5868 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5869 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5870 | /* invalid value type, skip it */ |
| 5871 | lua_pop(L, 1); |
| 5872 | continue; |
| 5873 | } |
| 5874 | |
| 5875 | |
| 5876 | /* Duplicate the key and swap it with the value. */ |
| 5877 | lua_pushvalue(L, -2); |
| 5878 | lua_insert(L, -2); |
| 5879 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5880 | |
| 5881 | lua_newtable(L); |
| 5882 | lua_insert(L, -2); |
| 5883 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5884 | |
| 5885 | if (lua_isstring(L, -1)) { |
| 5886 | /* push the value in the inner table */ |
| 5887 | lua_rawseti(L, -2, 1); |
| 5888 | } |
| 5889 | else { /* table */ |
| 5890 | lua_pushnil(L); |
| 5891 | while (lua_next(L, -2) != 0) { |
| 5892 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5893 | if (!lua_isstring(L, -1)) { |
| 5894 | /* invalid value type, skip it*/ |
| 5895 | lua_pop(L, 1); |
| 5896 | continue; |
| 5897 | } |
| 5898 | /* push the value in the inner table */ |
| 5899 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5900 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5901 | } |
| 5902 | lua_pop(L, 1); |
| 5903 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5904 | } |
| 5905 | |
| 5906 | /* push (k,v) on the stack in the headers table: |
| 5907 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5908 | */ |
| 5909 | lua_settable(L, -5); |
| 5910 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5911 | } |
| 5912 | } |
| 5913 | lua_pop(L, 1); |
| 5914 | } |
| 5915 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5916 | lua_settable(L, -3); |
| 5917 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5918 | |
| 5919 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5920 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5921 | lua_setmetatable(L, -2); |
| 5922 | return 1; |
| 5923 | } |
| 5924 | |
| 5925 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5926 | * provided, the default one corresponding to the status code is used. |
| 5927 | */ |
| 5928 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5929 | { |
| 5930 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5931 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5932 | |
| 5933 | /* First argument (self) must be a table */ |
| 5934 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5935 | |
| 5936 | if (status < 100 || status > 599) { |
| 5937 | lua_pushboolean(L, 0); |
| 5938 | return 1; |
| 5939 | } |
| 5940 | if (!reason) |
| 5941 | reason = http_get_reason(status); |
| 5942 | |
| 5943 | lua_pushinteger(L, status); |
| 5944 | lua_setfield(L, 1, "status"); |
| 5945 | |
| 5946 | lua_pushstring(L, reason); |
| 5947 | lua_setfield(L, 1, "reason"); |
| 5948 | |
| 5949 | lua_pushboolean(L, 1); |
| 5950 | return 1; |
| 5951 | } |
| 5952 | |
| 5953 | /* Add a header into the reply object. Each header name is associated to an |
| 5954 | * array of values in the "headers" table. If the header name is not found, a |
| 5955 | * new entry is created. |
| 5956 | */ |
| 5957 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5958 | { |
| 5959 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5960 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5961 | int ret; |
| 5962 | |
| 5963 | /* First argument (self) must be a table */ |
| 5964 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5965 | |
| 5966 | /* Push in the stack the "headers" entry. */ |
| 5967 | ret = lua_getfield(L, 1, "headers"); |
| 5968 | if (ret != LUA_TTABLE) { |
| 5969 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5970 | WILL_LJMP(lua_error(L)); |
| 5971 | } |
| 5972 | |
| 5973 | /* check if the header is already registered. If not, register it. */ |
| 5974 | ret = lua_getfield(L, -1, name); |
| 5975 | if (ret == LUA_TNIL) { |
| 5976 | /* Entry not found. */ |
| 5977 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5978 | |
| 5979 | /* Insert the new header name in the array in the top of the stack. |
| 5980 | * It left the new array in the top of the stack. |
| 5981 | */ |
| 5982 | lua_newtable(L); |
| 5983 | lua_pushstring(L, name); |
| 5984 | lua_pushvalue(L, -2); |
| 5985 | lua_settable(L, -4); |
| 5986 | } |
| 5987 | else if (ret != LUA_TTABLE) { |
| 5988 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5989 | WILL_LJMP(lua_error(L)); |
| 5990 | } |
| 5991 | |
| 5992 | /* Now the top od thestack is an array of values. We push |
| 5993 | * the header value as new entry. |
| 5994 | */ |
| 5995 | lua_pushstring(L, value); |
| 5996 | ret = lua_rawlen(L, -2); |
| 5997 | lua_rawseti(L, -2, ret + 1); |
| 5998 | |
| 5999 | lua_pushboolean(L, 1); |
| 6000 | return 1; |
| 6001 | } |
| 6002 | |
| 6003 | /* Remove all occurrences of a given header name. */ |
| 6004 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6005 | { |
| 6006 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6007 | int ret; |
| 6008 | |
| 6009 | /* First argument (self) must be a table */ |
| 6010 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6011 | |
| 6012 | /* Push in the stack the "headers" entry. */ |
| 6013 | ret = lua_getfield(L, 1, "headers"); |
| 6014 | if (ret != LUA_TTABLE) { |
| 6015 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6016 | WILL_LJMP(lua_error(L)); |
| 6017 | } |
| 6018 | |
| 6019 | lua_pushstring(L, name); |
| 6020 | lua_pushnil(L); |
| 6021 | lua_settable(L, -3); |
| 6022 | |
| 6023 | lua_pushboolean(L, 1); |
| 6024 | return 1; |
| 6025 | } |
| 6026 | |
| 6027 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6028 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6029 | { |
| 6030 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6031 | |
| 6032 | /* First argument (self) must be a table */ |
| 6033 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6034 | |
| 6035 | lua_pushstring(L, payload); |
| 6036 | lua_setfield(L, 1, "body"); |
| 6037 | |
| 6038 | lua_pushboolean(L, 1); |
| 6039 | return 1; |
| 6040 | } |
| 6041 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6042 | __LJMP static int hlua_log(lua_State *L) |
| 6043 | { |
| 6044 | int level; |
| 6045 | const char *msg; |
| 6046 | |
| 6047 | MAY_LJMP(check_args(L, 2, "log")); |
| 6048 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6049 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6050 | |
| 6051 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6052 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6053 | |
| 6054 | hlua_sendlog(NULL, level, msg); |
| 6055 | return 0; |
| 6056 | } |
| 6057 | |
| 6058 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6059 | { |
| 6060 | const char *msg; |
| 6061 | |
| 6062 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6063 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6064 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6065 | return 0; |
| 6066 | } |
| 6067 | |
| 6068 | __LJMP static int hlua_log_info(lua_State *L) |
| 6069 | { |
| 6070 | const char *msg; |
| 6071 | |
| 6072 | MAY_LJMP(check_args(L, 1, "info")); |
| 6073 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6074 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6075 | return 0; |
| 6076 | } |
| 6077 | |
| 6078 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6079 | { |
| 6080 | const char *msg; |
| 6081 | |
| 6082 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6083 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6084 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6085 | return 0; |
| 6086 | } |
| 6087 | |
| 6088 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6089 | { |
| 6090 | const char *msg; |
| 6091 | |
| 6092 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6093 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6094 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6095 | return 0; |
| 6096 | } |
| 6097 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6098 | __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] | 6099 | { |
| 6100 | int wakeup_ms = lua_tointeger(L, -1); |
| 6101 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6102 | 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] | 6103 | return 0; |
| 6104 | } |
| 6105 | |
| 6106 | __LJMP static int hlua_sleep(lua_State *L) |
| 6107 | { |
| 6108 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6109 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6110 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6111 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6112 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6113 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6114 | wakeup_ms = tick_add(now_ms, delay); |
| 6115 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6116 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6117 | 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] | 6118 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6119 | } |
| 6120 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6121 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6122 | { |
| 6123 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6124 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6125 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6126 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6127 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6128 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6129 | wakeup_ms = tick_add(now_ms, delay); |
| 6130 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6131 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6132 | 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] | 6133 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6134 | } |
| 6135 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6136 | /* This functionis an LUA binding. it permits to give back |
| 6137 | * the hand at the HAProxy scheduler. It is used when the |
| 6138 | * LUA processing consumes a lot of time. |
| 6139 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6140 | __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] | 6141 | { |
| 6142 | return 0; |
| 6143 | } |
| 6144 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6145 | __LJMP static int hlua_yield(lua_State *L) |
| 6146 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6147 | 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] | 6148 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6149 | } |
| 6150 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6151 | /* This function change the nice of the currently executed |
| 6152 | * task. It is used set low or high priority at the current |
| 6153 | * task. |
| 6154 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6155 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6156 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6157 | struct hlua *hlua; |
| 6158 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6159 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6160 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6161 | hlua = hlua_gethlua(L); |
| 6162 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6163 | |
| 6164 | /* If he task is not set, I'm in a start mode. */ |
| 6165 | if (!hlua || !hlua->task) |
| 6166 | return 0; |
| 6167 | |
| 6168 | if (nice < -1024) |
| 6169 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6170 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6171 | nice = 1024; |
| 6172 | |
| 6173 | hlua->task->nice = nice; |
| 6174 | return 0; |
| 6175 | } |
| 6176 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6177 | /* 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] | 6178 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6179 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6180 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6181 | * |
| 6182 | * Task wrapper are longjmp safe because the only one Lua code |
| 6183 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6184 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6185 | 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] | 6186 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6187 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6188 | enum hlua_exec status; |
| 6189 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6190 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6191 | task_set_affinity(task, tid_bit); |
| 6192 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6193 | /* If it is the first call to the task, we must initialize the |
| 6194 | * execution timeouts. |
| 6195 | */ |
| 6196 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6197 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6198 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6199 | /* Execute the Lua code. */ |
| 6200 | status = hlua_ctx_resume(hlua, 1); |
| 6201 | |
| 6202 | switch (status) { |
| 6203 | /* finished or yield */ |
| 6204 | case HLUA_E_OK: |
| 6205 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6206 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6207 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6208 | break; |
| 6209 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6210 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6211 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6212 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6213 | break; |
| 6214 | |
| 6215 | /* finished with error. */ |
| 6216 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6217 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6218 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6219 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6220 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6221 | break; |
| 6222 | |
| 6223 | case HLUA_E_ERR: |
| 6224 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6225 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6226 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6227 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6228 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6229 | break; |
| 6230 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6231 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6232 | } |
| 6233 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6234 | /* This function is an LUA binding that register LUA function to be |
| 6235 | * executed after the HAProxy configuration parsing and before the |
| 6236 | * HAProxy scheduler starts. This function expect only one LUA |
| 6237 | * argument that is a function. This function returns nothing, but |
| 6238 | * throws if an error is encountered. |
| 6239 | */ |
| 6240 | __LJMP static int hlua_register_init(lua_State *L) |
| 6241 | { |
| 6242 | struct hlua_init_function *init; |
| 6243 | int ref; |
| 6244 | |
| 6245 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6246 | |
| 6247 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6248 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6249 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6250 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6251 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6252 | |
| 6253 | init->function_ref = ref; |
| 6254 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6255 | return 0; |
| 6256 | } |
| 6257 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6258 | /* This functio is an LUA binding. It permits to register a task |
| 6259 | * executed in parallel of the main HAroxy activity. The task is |
| 6260 | * created and it is set in the HAProxy scheduler. It can be called |
| 6261 | * from the "init" section, "post init" or during the runtime. |
| 6262 | * |
| 6263 | * Lua prototype: |
| 6264 | * |
| 6265 | * <none> core.register_task(<function>) |
| 6266 | */ |
| 6267 | static int hlua_register_task(lua_State *L) |
| 6268 | { |
| 6269 | struct hlua *hlua; |
| 6270 | struct task *task; |
| 6271 | int ref; |
| 6272 | |
| 6273 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6274 | |
| 6275 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6276 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6277 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6278 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6279 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6280 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6281 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6282 | if (!task) |
| 6283 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6284 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6285 | task->context = hlua; |
| 6286 | task->process = hlua_process_task; |
| 6287 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6288 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6289 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6290 | |
| 6291 | /* Restore the function in the stack. */ |
| 6292 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6293 | hlua->nargs = 0; |
| 6294 | |
| 6295 | /* Schedule task. */ |
| 6296 | task_schedule(task, now_ms); |
| 6297 | |
| 6298 | return 0; |
| 6299 | } |
| 6300 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6301 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6302 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6303 | * resume converters. |
| 6304 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6305 | 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] | 6306 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6307 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6308 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6309 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6310 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6311 | if (!stream) |
| 6312 | return 0; |
| 6313 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6314 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6315 | * Lua context can be not initialized. This behavior |
| 6316 | * permits to save performances because a systematic |
| 6317 | * Lua initialization cause 5% performances loss. |
| 6318 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6319 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6320 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6321 | if (!stream->hlua) { |
| 6322 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6323 | return 0; |
| 6324 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6325 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6326 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6327 | return 0; |
| 6328 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6329 | } |
| 6330 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6331 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6332 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6333 | |
| 6334 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6335 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6336 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6337 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6338 | else |
| 6339 | error = "critical error"; |
| 6340 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6341 | return 0; |
| 6342 | } |
| 6343 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6344 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6345 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6346 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6347 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6348 | return 0; |
| 6349 | } |
| 6350 | |
| 6351 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6352 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6353 | |
| 6354 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6355 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6356 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6357 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6358 | return 0; |
| 6359 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6360 | hlua_smp2lua(stream->hlua->T, smp); |
| 6361 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6362 | |
| 6363 | /* push keywords in the stack. */ |
| 6364 | if (arg_p) { |
| 6365 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6366 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6367 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6368 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6369 | return 0; |
| 6370 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6371 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6372 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6373 | } |
| 6374 | } |
| 6375 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6376 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6377 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6378 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6379 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6380 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6381 | } |
| 6382 | |
| 6383 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6384 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6385 | /* finished. */ |
| 6386 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6387 | /* If the stack is empty, the function fails. */ |
| 6388 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6389 | return 0; |
| 6390 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6391 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6392 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6393 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6394 | return 1; |
| 6395 | |
| 6396 | /* yield. */ |
| 6397 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6398 | 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] | 6399 | return 0; |
| 6400 | |
| 6401 | /* finished with error. */ |
| 6402 | case HLUA_E_ERRMSG: |
| 6403 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6404 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6405 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6406 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6407 | return 0; |
| 6408 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6409 | case HLUA_E_ETMOUT: |
| 6410 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6411 | return 0; |
| 6412 | |
| 6413 | case HLUA_E_NOMEM: |
| 6414 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6415 | return 0; |
| 6416 | |
| 6417 | case HLUA_E_YIELD: |
| 6418 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6419 | return 0; |
| 6420 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6421 | case HLUA_E_ERR: |
| 6422 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6423 | 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] | 6424 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6425 | |
| 6426 | default: |
| 6427 | return 0; |
| 6428 | } |
| 6429 | } |
| 6430 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6431 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6432 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6433 | * resume sample-fetches. This function will be called by the sample |
| 6434 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6435 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6436 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6437 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6438 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6439 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6440 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6441 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6442 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6443 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6444 | if (!stream) |
| 6445 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6446 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6447 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6448 | * Lua context can be not initialized. This behavior |
| 6449 | * permits to save performances because a systematic |
| 6450 | * Lua initialization cause 5% performances loss. |
| 6451 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6452 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6453 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6454 | if (!stream->hlua) { |
| 6455 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6456 | return 0; |
| 6457 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6458 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6459 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6460 | return 0; |
| 6461 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6462 | } |
| 6463 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6464 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6465 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6466 | |
| 6467 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6468 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6469 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6470 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6471 | else |
| 6472 | error = "critical error"; |
| 6473 | 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] | 6474 | return 0; |
| 6475 | } |
| 6476 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6477 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6478 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6479 | 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] | 6480 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6481 | return 0; |
| 6482 | } |
| 6483 | |
| 6484 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6485 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6486 | |
| 6487 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6488 | 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] | 6489 | 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] | 6490 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6491 | return 0; |
| 6492 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6493 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6494 | |
| 6495 | /* push keywords in the stack. */ |
| 6496 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6497 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6498 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6499 | 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] | 6500 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6501 | return 0; |
| 6502 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6503 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6504 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6505 | } |
| 6506 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6507 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6508 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6509 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6510 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6511 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6512 | } |
| 6513 | |
| 6514 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6515 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6516 | /* finished. */ |
| 6517 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6518 | /* If the stack is empty, the function fails. */ |
| 6519 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6520 | return 0; |
| 6521 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6522 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6523 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6524 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6525 | |
| 6526 | /* Set the end of execution flag. */ |
| 6527 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6528 | return 1; |
| 6529 | |
| 6530 | /* yield. */ |
| 6531 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6532 | 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] | 6533 | return 0; |
| 6534 | |
| 6535 | /* finished with error. */ |
| 6536 | case HLUA_E_ERRMSG: |
| 6537 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6538 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6539 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6540 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6541 | return 0; |
| 6542 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6543 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6544 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6545 | return 0; |
| 6546 | |
| 6547 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6548 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6549 | return 0; |
| 6550 | |
| 6551 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6552 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6553 | return 0; |
| 6554 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6555 | case HLUA_E_ERR: |
| 6556 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6557 | 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] | 6558 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6559 | |
| 6560 | default: |
| 6561 | return 0; |
| 6562 | } |
| 6563 | } |
| 6564 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6565 | /* This function is an LUA binding used for registering |
| 6566 | * "sample-conv" functions. It expects a converter name used |
| 6567 | * in the haproxy configuration file, and an LUA function. |
| 6568 | */ |
| 6569 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6570 | { |
| 6571 | struct sample_conv_kw_list *sck; |
| 6572 | const char *name; |
| 6573 | int ref; |
| 6574 | int len; |
| 6575 | struct hlua_function *fcn; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6576 | struct sample_conv *sc; |
| 6577 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6578 | |
| 6579 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6580 | |
| 6581 | /* First argument : converter name. */ |
| 6582 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6583 | |
| 6584 | /* Second argument : lua function. */ |
| 6585 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6586 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6587 | /* Check if the converter is already registered */ |
| 6588 | trash = get_trash_chunk(); |
| 6589 | chunk_printf(trash, "lua.%s", name); |
| 6590 | sc = find_sample_conv(trash->area, trash->data); |
| 6591 | if (sc != NULL) { |
| 6592 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6593 | "This will become a hard error in version 2.5.\n", name); |
| 6594 | } |
| 6595 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6596 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6597 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6598 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6599 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6600 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6601 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6602 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6603 | |
| 6604 | /* Fill fcn. */ |
| 6605 | fcn->name = strdup(name); |
| 6606 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6607 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6608 | fcn->function_ref = ref; |
| 6609 | |
| 6610 | /* List head */ |
| 6611 | sck->list.n = sck->list.p = NULL; |
| 6612 | |
| 6613 | /* converter keyword. */ |
| 6614 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6615 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6616 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6617 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6618 | |
| 6619 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6620 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6621 | 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] | 6622 | sck->kw[0].val_args = NULL; |
| 6623 | sck->kw[0].in_type = SMP_T_STR; |
| 6624 | sck->kw[0].out_type = SMP_T_STR; |
| 6625 | sck->kw[0].private = fcn; |
| 6626 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6627 | /* Register this new converter */ |
| 6628 | sample_register_convs(sck); |
| 6629 | |
| 6630 | return 0; |
| 6631 | } |
| 6632 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6633 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6634 | * "sample-fetch" functions. It expects a converter name used |
| 6635 | * in the haproxy configuration file, and an LUA function. |
| 6636 | */ |
| 6637 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6638 | { |
| 6639 | const char *name; |
| 6640 | int ref; |
| 6641 | int len; |
| 6642 | struct sample_fetch_kw_list *sfk; |
| 6643 | struct hlua_function *fcn; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6644 | struct sample_fetch *sf; |
| 6645 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6646 | |
| 6647 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6648 | |
| 6649 | /* First argument : sample-fetch name. */ |
| 6650 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6651 | |
| 6652 | /* Second argument : lua function. */ |
| 6653 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6654 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6655 | /* Check if the sample-fetch is already registered */ |
| 6656 | trash = get_trash_chunk(); |
| 6657 | chunk_printf(trash, "lua.%s", name); |
| 6658 | sf = find_sample_fetch(trash->area, trash->data); |
| 6659 | if (sf != NULL) { |
| 6660 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6661 | "This will become a hard error in version 2.5.\n", name); |
| 6662 | } |
| 6663 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6664 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6665 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6666 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6667 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6668 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6669 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6670 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6671 | |
| 6672 | /* Fill fcn. */ |
| 6673 | fcn->name = strdup(name); |
| 6674 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6675 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6676 | fcn->function_ref = ref; |
| 6677 | |
| 6678 | /* List head */ |
| 6679 | sfk->list.n = sfk->list.p = NULL; |
| 6680 | |
| 6681 | /* sample-fetch keyword. */ |
| 6682 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6683 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6684 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6685 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6686 | |
| 6687 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6688 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6689 | 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] | 6690 | sfk->kw[0].val_args = NULL; |
| 6691 | sfk->kw[0].out_type = SMP_T_STR; |
| 6692 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6693 | sfk->kw[0].val = 0; |
| 6694 | sfk->kw[0].private = fcn; |
| 6695 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6696 | /* Register this new fetch. */ |
| 6697 | sample_register_fetches(sfk); |
| 6698 | |
| 6699 | return 0; |
| 6700 | } |
| 6701 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6702 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6703 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6704 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6705 | { |
| 6706 | struct hlua *hlua = hlua_gethlua(L); |
| 6707 | unsigned int delay; |
| 6708 | unsigned int wakeup_ms; |
| 6709 | |
| 6710 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6711 | |
| 6712 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6713 | wakeup_ms = tick_add(now_ms, delay); |
| 6714 | hlua->wake_time = wakeup_ms; |
| 6715 | return 0; |
| 6716 | } |
| 6717 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6718 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6719 | * wrapper during the initialisation period. This function may return any |
| 6720 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6721 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6722 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6723 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6724 | 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] | 6725 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6726 | { |
| 6727 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6728 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6729 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6730 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6731 | |
| 6732 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6733 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6734 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6735 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6736 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6737 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6738 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6739 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6740 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6741 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6742 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6743 | * Lua context can be not initialized. This behavior |
| 6744 | * permits to save performances because a systematic |
| 6745 | * Lua initialization cause 5% performances loss. |
| 6746 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6747 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6748 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6749 | if (!s->hlua) { |
| 6750 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6751 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6752 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6753 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6754 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6755 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6756 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6757 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6758 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6759 | } |
| 6760 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6761 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6762 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6763 | |
| 6764 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6765 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6766 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6767 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6768 | else |
| 6769 | error = "critical error"; |
| 6770 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6771 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6772 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6773 | } |
| 6774 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6775 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6776 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6777 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6778 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6779 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6780 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6784 | 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] | 6785 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6786 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6787 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6788 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6789 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6790 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6791 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6792 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6793 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6794 | |
| 6795 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6796 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6797 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6798 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6799 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6800 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6801 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6802 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6803 | lua_pushstring(s->hlua->T, *arg); |
| 6804 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6805 | } |
| 6806 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6807 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6808 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6809 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6810 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6811 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6812 | } |
| 6813 | |
| 6814 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6815 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6816 | /* finished. */ |
| 6817 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6818 | /* Catch the return value */ |
| 6819 | if (lua_gettop(s->hlua->T) > 0) |
| 6820 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6821 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6822 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6823 | if (act_ret == ACT_RET_YIELD) { |
| 6824 | if (flags & ACT_OPT_FINAL) |
| 6825 | goto err_yield; |
| 6826 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6827 | if (dir == SMP_OPT_DIR_REQ) |
| 6828 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6829 | s->hlua->wake_time); |
| 6830 | else |
| 6831 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6832 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6833 | } |
| 6834 | goto end; |
| 6835 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6836 | /* yield. */ |
| 6837 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6838 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6839 | if (dir == SMP_OPT_DIR_REQ) |
| 6840 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6841 | s->hlua->wake_time); |
| 6842 | else |
| 6843 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6844 | s->hlua->wake_time); |
| 6845 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6846 | /* Some actions can be wake up when a "write" event |
| 6847 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6848 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6849 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6850 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6851 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6852 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6853 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6854 | act_ret = ACT_RET_YIELD; |
| 6855 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6856 | |
| 6857 | /* finished with error. */ |
| 6858 | case HLUA_E_ERRMSG: |
| 6859 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6860 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6861 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6862 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6863 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6864 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6865 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6866 | 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] | 6867 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6868 | |
| 6869 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6870 | 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] | 6871 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6872 | |
| 6873 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6874 | err_yield: |
| 6875 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6876 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6877 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6878 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6879 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6880 | case HLUA_E_ERR: |
| 6881 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6882 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6883 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6884 | |
| 6885 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6886 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6887 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6888 | |
| 6889 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6890 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6891 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6892 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6893 | } |
| 6894 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6895 | 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] | 6896 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6897 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6898 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6899 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6900 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6901 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6905 | { |
| 6906 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6907 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6908 | struct task *task; |
| 6909 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6910 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6911 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6912 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6913 | if (!hlua) { |
| 6914 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6915 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6916 | return 0; |
| 6917 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6918 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6919 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6920 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6921 | |
| 6922 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6923 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6924 | if (!task) { |
| 6925 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6926 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6927 | return 0; |
| 6928 | } |
| 6929 | task->nice = 0; |
| 6930 | task->context = ctx; |
| 6931 | task->process = hlua_applet_wakeup; |
| 6932 | ctx->ctx.hlua_apptcp.task = task; |
| 6933 | |
| 6934 | /* In the execution wrappers linked with a stream, the |
| 6935 | * Lua context can be not initialized. This behavior |
| 6936 | * permits to save performances because a systematic |
| 6937 | * Lua initialization cause 5% performances loss. |
| 6938 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6939 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6940 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6941 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6942 | return 0; |
| 6943 | } |
| 6944 | |
| 6945 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6946 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6947 | |
| 6948 | /* The following Lua calls can fail. */ |
| 6949 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6950 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6951 | error = lua_tostring(hlua->T, -1); |
| 6952 | else |
| 6953 | error = "critical error"; |
| 6954 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6955 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6956 | return 0; |
| 6957 | } |
| 6958 | |
| 6959 | /* Check stack available size. */ |
| 6960 | if (!lua_checkstack(hlua->T, 1)) { |
| 6961 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6962 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6963 | RESET_SAFE_LJMP(hlua->T); |
| 6964 | return 0; |
| 6965 | } |
| 6966 | |
| 6967 | /* Restore the function in the stack. */ |
| 6968 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6969 | |
| 6970 | /* Create and and push object stream in the stack. */ |
| 6971 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6972 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6973 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6974 | RESET_SAFE_LJMP(hlua->T); |
| 6975 | return 0; |
| 6976 | } |
| 6977 | hlua->nargs = 1; |
| 6978 | |
| 6979 | /* push keywords in the stack. */ |
| 6980 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6981 | if (!lua_checkstack(hlua->T, 1)) { |
| 6982 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6983 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6984 | RESET_SAFE_LJMP(hlua->T); |
| 6985 | return 0; |
| 6986 | } |
| 6987 | lua_pushstring(hlua->T, *arg); |
| 6988 | hlua->nargs++; |
| 6989 | } |
| 6990 | |
| 6991 | RESET_SAFE_LJMP(hlua->T); |
| 6992 | |
| 6993 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6994 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6995 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6996 | |
| 6997 | return 1; |
| 6998 | } |
| 6999 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7000 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7001 | { |
| 7002 | struct stream_interface *si = ctx->owner; |
| 7003 | struct stream *strm = si_strm(si); |
| 7004 | struct channel *res = si_ic(si); |
| 7005 | struct act_rule *rule = ctx->rule; |
| 7006 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7007 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7008 | |
| 7009 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7010 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7011 | /* eat the whole request */ |
| 7012 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7013 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7014 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7015 | |
| 7016 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7017 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7018 | return; |
| 7019 | |
| 7020 | /* Execute the function. */ |
| 7021 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7022 | /* finished. */ |
| 7023 | case HLUA_E_OK: |
| 7024 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7025 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7026 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7027 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7028 | res->flags |= CF_READ_NULL; |
| 7029 | si_shutr(si); |
| 7030 | return; |
| 7031 | |
| 7032 | /* yield. */ |
| 7033 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7034 | if (hlua->wake_time != TICK_ETERNITY) |
| 7035 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7036 | return; |
| 7037 | |
| 7038 | /* finished with error. */ |
| 7039 | case HLUA_E_ERRMSG: |
| 7040 | /* Display log. */ |
| 7041 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7042 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7043 | lua_pop(hlua->T, 1); |
| 7044 | goto error; |
| 7045 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7046 | case HLUA_E_ETMOUT: |
| 7047 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7048 | rule->arg.hlua_rule->fcn.name); |
| 7049 | goto error; |
| 7050 | |
| 7051 | case HLUA_E_NOMEM: |
| 7052 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7053 | rule->arg.hlua_rule->fcn.name); |
| 7054 | goto error; |
| 7055 | |
| 7056 | case HLUA_E_YIELD: /* unexpected */ |
| 7057 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7058 | rule->arg.hlua_rule->fcn.name); |
| 7059 | goto error; |
| 7060 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7061 | case HLUA_E_ERR: |
| 7062 | /* Display log. */ |
| 7063 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7064 | rule->arg.hlua_rule->fcn.name); |
| 7065 | goto error; |
| 7066 | |
| 7067 | default: |
| 7068 | goto error; |
| 7069 | } |
| 7070 | |
| 7071 | error: |
| 7072 | |
| 7073 | /* For all other cases, just close the stream. */ |
| 7074 | si_shutw(si); |
| 7075 | si_shutr(si); |
| 7076 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7077 | } |
| 7078 | |
| 7079 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7080 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7081 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7082 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7083 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7084 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7085 | } |
| 7086 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7087 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7088 | * an errors occurs and -1 if more data are required for initializing |
| 7089 | * the applet. |
| 7090 | */ |
| 7091 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7092 | { |
| 7093 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7094 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7095 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7096 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7097 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7098 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7099 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7100 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7101 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7102 | if (!hlua) { |
| 7103 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7104 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7105 | return 0; |
| 7106 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7107 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7108 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7109 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7110 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7111 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7112 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7113 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7114 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7115 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7116 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7117 | if (!task) { |
| 7118 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7119 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7120 | return 0; |
| 7121 | } |
| 7122 | task->nice = 0; |
| 7123 | task->context = ctx; |
| 7124 | task->process = hlua_applet_wakeup; |
| 7125 | ctx->ctx.hlua_apphttp.task = task; |
| 7126 | |
| 7127 | /* In the execution wrappers linked with a stream, the |
| 7128 | * Lua context can be not initialized. This behavior |
| 7129 | * permits to save performances because a systematic |
| 7130 | * Lua initialization cause 5% performances loss. |
| 7131 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7132 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7133 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7134 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7135 | return 0; |
| 7136 | } |
| 7137 | |
| 7138 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7139 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7140 | |
| 7141 | /* The following Lua calls can fail. */ |
| 7142 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7143 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7144 | error = lua_tostring(hlua->T, -1); |
| 7145 | else |
| 7146 | error = "critical error"; |
| 7147 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7148 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7149 | return 0; |
| 7150 | } |
| 7151 | |
| 7152 | /* Check stack available size. */ |
| 7153 | if (!lua_checkstack(hlua->T, 1)) { |
| 7154 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7155 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7156 | RESET_SAFE_LJMP(hlua->T); |
| 7157 | return 0; |
| 7158 | } |
| 7159 | |
| 7160 | /* Restore the function in the stack. */ |
| 7161 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7162 | |
| 7163 | /* Create and and push object stream in the stack. */ |
| 7164 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7165 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7166 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7167 | RESET_SAFE_LJMP(hlua->T); |
| 7168 | return 0; |
| 7169 | } |
| 7170 | hlua->nargs = 1; |
| 7171 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7172 | /* push keywords in the stack. */ |
| 7173 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7174 | if (!lua_checkstack(hlua->T, 1)) { |
| 7175 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7176 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7177 | RESET_SAFE_LJMP(hlua->T); |
| 7178 | return 0; |
| 7179 | } |
| 7180 | lua_pushstring(hlua->T, *arg); |
| 7181 | hlua->nargs++; |
| 7182 | } |
| 7183 | |
| 7184 | RESET_SAFE_LJMP(hlua->T); |
| 7185 | |
| 7186 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7187 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7188 | |
| 7189 | return 1; |
| 7190 | } |
| 7191 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7192 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7193 | { |
| 7194 | struct stream_interface *si = ctx->owner; |
| 7195 | struct stream *strm = si_strm(si); |
| 7196 | struct channel *req = si_oc(si); |
| 7197 | struct channel *res = si_ic(si); |
| 7198 | struct act_rule *rule = ctx->rule; |
| 7199 | struct proxy *px = strm->be; |
| 7200 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7201 | struct htx *req_htx, *res_htx; |
| 7202 | |
| 7203 | res_htx = htx_from_buf(&res->buf); |
| 7204 | |
| 7205 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7206 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7207 | goto out; |
| 7208 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7209 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7210 | if (!b_size(&res->buf)) { |
| 7211 | si_rx_room_blk(si); |
| 7212 | goto out; |
| 7213 | } |
| 7214 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7215 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7216 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7217 | |
| 7218 | /* Set the currently running flag. */ |
| 7219 | if (!HLUA_IS_RUNNING(hlua) && |
| 7220 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7221 | struct htx_blk *blk; |
| 7222 | size_t count = co_data(req); |
| 7223 | |
| 7224 | if (!count) { |
| 7225 | si_cant_get(si); |
| 7226 | goto out; |
| 7227 | } |
| 7228 | |
| 7229 | /* We need to flush the request header. This left the body for |
| 7230 | * the Lua. |
| 7231 | */ |
| 7232 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7233 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7234 | while (count && blk) { |
| 7235 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7236 | uint32_t sz = htx_get_blksz(blk); |
| 7237 | |
| 7238 | if (sz > count) { |
| 7239 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7240 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7241 | goto out; |
| 7242 | } |
| 7243 | |
| 7244 | count -= sz; |
| 7245 | co_set_data(req, co_data(req) - sz); |
| 7246 | blk = htx_remove_blk(req_htx, blk); |
| 7247 | |
| 7248 | if (type == HTX_BLK_EOH) |
| 7249 | break; |
| 7250 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7251 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7252 | } |
| 7253 | |
| 7254 | /* Executes The applet if it is not done. */ |
| 7255 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7256 | |
| 7257 | /* Execute the function. */ |
| 7258 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7259 | /* finished. */ |
| 7260 | case HLUA_E_OK: |
| 7261 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7262 | break; |
| 7263 | |
| 7264 | /* yield. */ |
| 7265 | case HLUA_E_AGAIN: |
| 7266 | if (hlua->wake_time != TICK_ETERNITY) |
| 7267 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7268 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7269 | |
| 7270 | /* finished with error. */ |
| 7271 | case HLUA_E_ERRMSG: |
| 7272 | /* Display log. */ |
| 7273 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7274 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7275 | lua_pop(hlua->T, 1); |
| 7276 | goto error; |
| 7277 | |
| 7278 | case HLUA_E_ETMOUT: |
| 7279 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7280 | rule->arg.hlua_rule->fcn.name); |
| 7281 | goto error; |
| 7282 | |
| 7283 | case HLUA_E_NOMEM: |
| 7284 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7285 | rule->arg.hlua_rule->fcn.name); |
| 7286 | goto error; |
| 7287 | |
| 7288 | case HLUA_E_YIELD: /* unexpected */ |
| 7289 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7290 | rule->arg.hlua_rule->fcn.name); |
| 7291 | goto error; |
| 7292 | |
| 7293 | case HLUA_E_ERR: |
| 7294 | /* Display log. */ |
| 7295 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7296 | rule->arg.hlua_rule->fcn.name); |
| 7297 | goto error; |
| 7298 | |
| 7299 | default: |
| 7300 | goto error; |
| 7301 | } |
| 7302 | } |
| 7303 | |
| 7304 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7305 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7306 | goto done; |
| 7307 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7308 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7309 | goto error; |
| 7310 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7311 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7312 | 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] | 7313 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7314 | si_rx_room_blk(si); |
| 7315 | goto out; |
| 7316 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7317 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7318 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7319 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7320 | } |
| 7321 | |
| 7322 | done: |
| 7323 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7324 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7325 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7326 | si_shutr(si); |
| 7327 | } |
| 7328 | |
| 7329 | /* eat the whole request */ |
| 7330 | if (co_data(req)) { |
| 7331 | req_htx = htx_from_buf(&req->buf); |
| 7332 | co_htx_skip(req, req_htx, co_data(req)); |
| 7333 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7334 | } |
| 7335 | } |
| 7336 | |
| 7337 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7338 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7339 | return; |
| 7340 | |
| 7341 | error: |
| 7342 | |
| 7343 | /* If we are in HTTP mode, and we are not send any |
| 7344 | * data, return a 500 server error in best effort: |
| 7345 | * if there is no room available in the buffer, |
| 7346 | * just close the connection. |
| 7347 | */ |
| 7348 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7349 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7350 | |
| 7351 | channel_erase(res); |
| 7352 | res->buf.data = b_data(err); |
| 7353 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7354 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7355 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7356 | } |
| 7357 | if (!(strm->flags & SF_ERR_MASK)) |
| 7358 | strm->flags |= SF_ERR_RESOURCE; |
| 7359 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7360 | goto done; |
| 7361 | } |
| 7362 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7363 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7364 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7365 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7366 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7367 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7368 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7369 | } |
| 7370 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7371 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7372 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7373 | * |
| 7374 | * This function can fail with an abort() due to an Lua critical error. |
| 7375 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7376 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7377 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7378 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7379 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7380 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7381 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7382 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7383 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7384 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7385 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7386 | if (!rule->arg.hlua_rule) { |
| 7387 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7388 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7389 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7390 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7391 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7392 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7393 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7394 | if (!rule->arg.hlua_rule->args) { |
| 7395 | memprintf(err, "out of memory error"); |
| 7396 | return ACT_RET_PRS_ERR; |
| 7397 | } |
| 7398 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7399 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7400 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7401 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7402 | /* Expect some arguments */ |
| 7403 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7404 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7405 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7406 | return ACT_RET_PRS_ERR; |
| 7407 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7408 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7409 | if (!rule->arg.hlua_rule->args[i]) { |
| 7410 | memprintf(err, "out of memory error"); |
| 7411 | return ACT_RET_PRS_ERR; |
| 7412 | } |
| 7413 | (*cur_arg)++; |
| 7414 | } |
| 7415 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7416 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7417 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7418 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7419 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7420 | } |
| 7421 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7422 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7423 | struct act_rule *rule, char **err) |
| 7424 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7425 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7426 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7427 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7428 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7429 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7430 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7431 | * the call of this analyzer. |
| 7432 | */ |
| 7433 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7434 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7435 | return ACT_RET_PRS_ERR; |
| 7436 | } |
| 7437 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7438 | /* Memory for the rule. */ |
| 7439 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7440 | if (!rule->arg.hlua_rule) { |
| 7441 | memprintf(err, "out of memory error"); |
| 7442 | return ACT_RET_PRS_ERR; |
| 7443 | } |
| 7444 | |
| 7445 | /* Reference the Lua function and store the reference. */ |
| 7446 | rule->arg.hlua_rule->fcn = *fcn; |
| 7447 | |
| 7448 | /* TODO: later accept arguments. */ |
| 7449 | rule->arg.hlua_rule->args = NULL; |
| 7450 | |
| 7451 | /* Add applet pointer in the rule. */ |
| 7452 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7453 | rule->applet.name = fcn->name; |
| 7454 | rule->applet.init = hlua_applet_http_init; |
| 7455 | rule->applet.fct = hlua_applet_http_fct; |
| 7456 | rule->applet.release = hlua_applet_http_release; |
| 7457 | rule->applet.timeout = hlua_timeout_applet; |
| 7458 | |
| 7459 | return ACT_RET_PRS_OK; |
| 7460 | } |
| 7461 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7462 | /* This function is an LUA binding used for registering |
| 7463 | * "sample-conv" functions. It expects a converter name used |
| 7464 | * in the haproxy configuration file, and an LUA function. |
| 7465 | */ |
| 7466 | __LJMP static int hlua_register_action(lua_State *L) |
| 7467 | { |
| 7468 | struct action_kw_list *akl; |
| 7469 | const char *name; |
| 7470 | int ref; |
| 7471 | int len; |
| 7472 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7473 | int nargs; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7474 | struct buffer *trash; |
| 7475 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7476 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7477 | /* Initialise the number of expected arguments at 0. */ |
| 7478 | nargs = 0; |
| 7479 | |
| 7480 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7481 | 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] | 7482 | |
| 7483 | /* First argument : converter name. */ |
| 7484 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7485 | |
| 7486 | /* Second argument : environment. */ |
| 7487 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7488 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7489 | |
| 7490 | /* Third argument : lua function. */ |
| 7491 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7492 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7493 | /* 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] | 7494 | if (lua_gettop(L) >= 4) |
| 7495 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7496 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7497 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7498 | lua_pushnil(L); |
| 7499 | while (lua_next(L, 2) != 0) { |
| 7500 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7501 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7502 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7503 | /* Check if action exists */ |
| 7504 | trash = get_trash_chunk(); |
| 7505 | chunk_printf(trash, "lua.%s", name); |
| 7506 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7507 | akw = tcp_req_cont_action(trash->area); |
| 7508 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7509 | akw = tcp_res_cont_action(trash->area); |
| 7510 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7511 | akw = action_http_req_custom(trash->area); |
| 7512 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7513 | akw = action_http_res_custom(trash->area); |
| 7514 | } else { |
| 7515 | akw = NULL; |
| 7516 | } |
| 7517 | if (akw != NULL) { |
| 7518 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7519 | "This will become a hard error in version 2.5.\n", name); |
| 7520 | } |
| 7521 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7522 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7523 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7524 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7525 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7526 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7527 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7528 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7529 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7530 | |
| 7531 | /* Fill fcn. */ |
| 7532 | fcn->name = strdup(name); |
| 7533 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7534 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7535 | fcn->function_ref = ref; |
| 7536 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7537 | /* Set the expected number od arguments. */ |
| 7538 | fcn->nargs = nargs; |
| 7539 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7540 | /* List head */ |
| 7541 | akl->list.n = akl->list.p = NULL; |
| 7542 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7543 | /* action keyword. */ |
| 7544 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7545 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7546 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7547 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7548 | |
| 7549 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7550 | |
| 7551 | akl->kw[0].match_pfx = 0; |
| 7552 | akl->kw[0].private = fcn; |
| 7553 | akl->kw[0].parse = action_register_lua; |
| 7554 | |
| 7555 | /* select the action registering point. */ |
| 7556 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7557 | tcp_req_cont_keywords_register(akl); |
| 7558 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7559 | tcp_res_cont_keywords_register(akl); |
| 7560 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7561 | http_req_keywords_register(akl); |
| 7562 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7563 | http_res_keywords_register(akl); |
| 7564 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7565 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7566 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7567 | "are expected.", lua_tostring(L, -1))); |
| 7568 | |
| 7569 | /* pop the environment string. */ |
| 7570 | lua_pop(L, 1); |
| 7571 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7572 | return ACT_RET_PRS_OK; |
| 7573 | } |
| 7574 | |
| 7575 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7576 | struct act_rule *rule, char **err) |
| 7577 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7578 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7579 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7580 | if (px->mode == PR_MODE_HTTP) { |
| 7581 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7582 | return ACT_RET_PRS_ERR; |
| 7583 | } |
| 7584 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7585 | /* Memory for the rule. */ |
| 7586 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7587 | if (!rule->arg.hlua_rule) { |
| 7588 | memprintf(err, "out of memory error"); |
| 7589 | return ACT_RET_PRS_ERR; |
| 7590 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7591 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7592 | /* Reference the Lua function and store the reference. */ |
| 7593 | rule->arg.hlua_rule->fcn = *fcn; |
| 7594 | |
| 7595 | /* TODO: later accept arguments. */ |
| 7596 | rule->arg.hlua_rule->args = NULL; |
| 7597 | |
| 7598 | /* Add applet pointer in the rule. */ |
| 7599 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7600 | rule->applet.name = fcn->name; |
| 7601 | rule->applet.init = hlua_applet_tcp_init; |
| 7602 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7603 | rule->applet.release = hlua_applet_tcp_release; |
| 7604 | rule->applet.timeout = hlua_timeout_applet; |
| 7605 | |
| 7606 | return 0; |
| 7607 | } |
| 7608 | |
| 7609 | /* This function is an LUA binding used for registering |
| 7610 | * "sample-conv" functions. It expects a converter name used |
| 7611 | * in the haproxy configuration file, and an LUA function. |
| 7612 | */ |
| 7613 | __LJMP static int hlua_register_service(lua_State *L) |
| 7614 | { |
| 7615 | struct action_kw_list *akl; |
| 7616 | const char *name; |
| 7617 | const char *env; |
| 7618 | int ref; |
| 7619 | int len; |
| 7620 | struct hlua_function *fcn; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7621 | struct buffer *trash; |
| 7622 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7623 | |
| 7624 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7625 | |
| 7626 | /* First argument : converter name. */ |
| 7627 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7628 | |
| 7629 | /* Second argument : environment. */ |
| 7630 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7631 | |
| 7632 | /* Third argument : lua function. */ |
| 7633 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7634 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7635 | /* Check for service already registered */ |
| 7636 | trash = get_trash_chunk(); |
| 7637 | chunk_printf(trash, "lua.%s", name); |
| 7638 | akw = service_find(trash->area); |
| 7639 | if (akw != NULL) { |
| 7640 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7641 | "This will become a hard error in version 2.5.\n", name); |
| 7642 | } |
| 7643 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7644 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7645 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7646 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7647 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7648 | fcn = calloc(1, sizeof(*fcn)); |
| 7649 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7650 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7651 | |
| 7652 | /* Fill fcn. */ |
| 7653 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7654 | fcn->name = calloc(1, len); |
| 7655 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7656 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7657 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7658 | fcn->function_ref = ref; |
| 7659 | |
| 7660 | /* List head */ |
| 7661 | akl->list.n = akl->list.p = NULL; |
| 7662 | |
| 7663 | /* converter keyword. */ |
| 7664 | len = strlen("lua.") + strlen(name) + 1; |
| 7665 | akl->kw[0].kw = calloc(1, len); |
| 7666 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7667 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7668 | |
| 7669 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7670 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7671 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7672 | if (strcmp(env, "tcp") == 0) |
| 7673 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7674 | else if (strcmp(env, "http") == 0) |
| 7675 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7676 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7677 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7678 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7679 | |
| 7680 | akl->kw[0].match_pfx = 0; |
| 7681 | akl->kw[0].private = fcn; |
| 7682 | |
| 7683 | /* End of array. */ |
| 7684 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7685 | |
| 7686 | /* Register this new converter */ |
| 7687 | service_keywords_register(akl); |
| 7688 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7689 | return 0; |
| 7690 | } |
| 7691 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7692 | /* This function initialises Lua cli handler. It copies the |
| 7693 | * arguments in the Lua stack and create channel IO objects. |
| 7694 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7695 | 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] | 7696 | { |
| 7697 | struct hlua *hlua; |
| 7698 | struct hlua_function *fcn; |
| 7699 | int i; |
| 7700 | const char *error; |
| 7701 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7702 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7703 | appctx->ctx.hlua_cli.fcn = private; |
| 7704 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7705 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7706 | if (!hlua) { |
| 7707 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7708 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7709 | } |
| 7710 | HLUA_INIT(hlua); |
| 7711 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7712 | |
| 7713 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7714 | * 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] | 7715 | * applet_http. It is absolutely compatible. |
| 7716 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7717 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7718 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7719 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7720 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7721 | } |
| 7722 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7723 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7724 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7725 | |
| 7726 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7727 | 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] | 7728 | 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] | 7729 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7730 | } |
| 7731 | |
| 7732 | /* The following Lua calls can fail. */ |
| 7733 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7734 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7735 | error = lua_tostring(hlua->T, -1); |
| 7736 | else |
| 7737 | error = "critical error"; |
| 7738 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7739 | goto error; |
| 7740 | } |
| 7741 | |
| 7742 | /* Check stack available size. */ |
| 7743 | if (!lua_checkstack(hlua->T, 2)) { |
| 7744 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7745 | goto error; |
| 7746 | } |
| 7747 | |
| 7748 | /* Restore the function in the stack. */ |
| 7749 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7750 | |
| 7751 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7752 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7753 | */ |
| 7754 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7755 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7756 | goto error; |
| 7757 | } |
| 7758 | hlua->nargs = 1; |
| 7759 | |
| 7760 | /* push keywords in the stack. */ |
| 7761 | for (i = 0; *args[i]; i++) { |
| 7762 | /* Check stack available size. */ |
| 7763 | if (!lua_checkstack(hlua->T, 1)) { |
| 7764 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7765 | goto error; |
| 7766 | } |
| 7767 | lua_pushstring(hlua->T, args[i]); |
| 7768 | hlua->nargs++; |
| 7769 | } |
| 7770 | |
| 7771 | /* We must initialize the execution timeouts. */ |
| 7772 | hlua->max_time = hlua_timeout_session; |
| 7773 | |
| 7774 | /* At this point the execution is safe. */ |
| 7775 | RESET_SAFE_LJMP(hlua->T); |
| 7776 | |
| 7777 | /* It's ok */ |
| 7778 | return 0; |
| 7779 | |
| 7780 | /* It's not ok. */ |
| 7781 | error: |
| 7782 | RESET_SAFE_LJMP(hlua->T); |
| 7783 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7784 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7785 | return 1; |
| 7786 | } |
| 7787 | |
| 7788 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7789 | { |
| 7790 | struct hlua *hlua; |
| 7791 | struct stream_interface *si; |
| 7792 | struct hlua_function *fcn; |
| 7793 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7794 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7795 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7796 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7797 | |
| 7798 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7799 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7800 | return 1; |
| 7801 | |
| 7802 | /* Execute the function. */ |
| 7803 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7804 | |
| 7805 | /* finished. */ |
| 7806 | case HLUA_E_OK: |
| 7807 | return 1; |
| 7808 | |
| 7809 | /* yield. */ |
| 7810 | case HLUA_E_AGAIN: |
| 7811 | /* We want write. */ |
| 7812 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7813 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7814 | /* Set the timeout. */ |
| 7815 | if (hlua->wake_time != TICK_ETERNITY) |
| 7816 | task_schedule(hlua->task, hlua->wake_time); |
| 7817 | return 0; |
| 7818 | |
| 7819 | /* finished with error. */ |
| 7820 | case HLUA_E_ERRMSG: |
| 7821 | /* Display log. */ |
| 7822 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7823 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7824 | lua_pop(hlua->T, 1); |
| 7825 | return 1; |
| 7826 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7827 | case HLUA_E_ETMOUT: |
| 7828 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7829 | fcn->name); |
| 7830 | return 1; |
| 7831 | |
| 7832 | case HLUA_E_NOMEM: |
| 7833 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7834 | fcn->name); |
| 7835 | return 1; |
| 7836 | |
| 7837 | case HLUA_E_YIELD: /* unexpected */ |
| 7838 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7839 | fcn->name); |
| 7840 | return 1; |
| 7841 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7842 | case HLUA_E_ERR: |
| 7843 | /* Display log. */ |
| 7844 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7845 | fcn->name); |
| 7846 | return 1; |
| 7847 | |
| 7848 | default: |
| 7849 | return 1; |
| 7850 | } |
| 7851 | |
| 7852 | return 1; |
| 7853 | } |
| 7854 | |
| 7855 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7856 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7857 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7858 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7859 | } |
| 7860 | |
| 7861 | /* This function is an LUA binding used for registering |
| 7862 | * new keywords in the cli. It expects a list of keywords |
| 7863 | * which are the "path". It is limited to 5 keywords. A |
| 7864 | * description of the command, a function to be executed |
| 7865 | * for the parsing and a function for io handlers. |
| 7866 | */ |
| 7867 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7868 | { |
| 7869 | struct cli_kw_list *cli_kws; |
| 7870 | const char *message; |
| 7871 | int ref_io; |
| 7872 | int len; |
| 7873 | struct hlua_function *fcn; |
| 7874 | int index; |
| 7875 | int i; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7876 | struct buffer *trash; |
| 7877 | const char *kw[5]; |
| 7878 | struct cli_kw *cli_kw; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7879 | |
| 7880 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7881 | |
| 7882 | /* First argument : an array of maximum 5 keywords. */ |
| 7883 | if (!lua_istable(L, 1)) |
| 7884 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7885 | |
| 7886 | /* Second argument : string with contextual message. */ |
| 7887 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7888 | |
| 7889 | /* Third and fourth argument : lua function. */ |
| 7890 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7891 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7892 | /* Check for CLI service already registered */ |
| 7893 | trash = get_trash_chunk(); |
| 7894 | index = 0; |
| 7895 | lua_pushnil(L); |
| 7896 | memset(kw, 0, sizeof(kw)); |
| 7897 | while (lua_next(L, 1) != 0) { |
| 7898 | if (index >= CLI_PREFIX_KW_NB) |
| 7899 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7900 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7901 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7902 | kw[index] = lua_tostring(L, -1); |
| 7903 | if (index == 0) |
| 7904 | chunk_printf(trash, "%s", kw[index]); |
| 7905 | else |
| 7906 | chunk_appendf(trash, " %s", kw[index]); |
| 7907 | index++; |
| 7908 | lua_pop(L, 1); |
| 7909 | } |
| 7910 | cli_kw = cli_find_kw_exact((char **)kw); |
| 7911 | if (cli_kw != NULL) { |
| 7912 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 7913 | "This will become a hard error in version 2.5.\n", trash->area); |
| 7914 | } |
| 7915 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7916 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7917 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7918 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7919 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7920 | fcn = calloc(1, sizeof(*fcn)); |
| 7921 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7922 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7923 | |
| 7924 | /* Fill path. */ |
| 7925 | index = 0; |
| 7926 | lua_pushnil(L); |
| 7927 | while(lua_next(L, 1) != 0) { |
| 7928 | if (index >= 5) |
| 7929 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7930 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7931 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7932 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7933 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7934 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7935 | index++; |
| 7936 | lua_pop(L, 1); |
| 7937 | } |
| 7938 | |
| 7939 | /* Copy help message. */ |
| 7940 | cli_kws->kw[0].usage = strdup(message); |
| 7941 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7942 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7943 | |
| 7944 | /* Fill fcn io handler. */ |
| 7945 | len = strlen("<lua.cli>") + 1; |
| 7946 | for (i = 0; i < index; i++) |
| 7947 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7948 | fcn->name = calloc(1, len); |
| 7949 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7950 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7951 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7952 | for (i = 0; i < index; i++) { |
| 7953 | strncat((char *)fcn->name, ".", len); |
| 7954 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7955 | } |
| 7956 | strncat((char *)fcn->name, ">", len); |
| 7957 | fcn->function_ref = ref_io; |
| 7958 | |
| 7959 | /* Fill last entries. */ |
| 7960 | cli_kws->kw[0].private = fcn; |
| 7961 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7962 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7963 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7964 | |
| 7965 | /* Register this new converter */ |
| 7966 | cli_register_kw(cli_kws); |
| 7967 | |
| 7968 | return 0; |
| 7969 | } |
| 7970 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7971 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7972 | struct proxy *defpx, const char *file, int line, |
| 7973 | char **err, unsigned int *timeout) |
| 7974 | { |
| 7975 | const char *error; |
| 7976 | |
| 7977 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7978 | if (error == PARSE_TIME_OVER) { |
| 7979 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7980 | args[1], args[0]); |
| 7981 | return -1; |
| 7982 | } |
| 7983 | else if (error == PARSE_TIME_UNDER) { |
| 7984 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7985 | args[1], args[0]); |
| 7986 | return -1; |
| 7987 | } |
| 7988 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7989 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7990 | return -1; |
| 7991 | } |
| 7992 | return 0; |
| 7993 | } |
| 7994 | |
| 7995 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7996 | struct proxy *defpx, const char *file, int line, |
| 7997 | char **err) |
| 7998 | { |
| 7999 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8000 | file, line, err, &hlua_timeout_session); |
| 8001 | } |
| 8002 | |
| 8003 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8004 | struct proxy *defpx, const char *file, int line, |
| 8005 | char **err) |
| 8006 | { |
| 8007 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8008 | file, line, err, &hlua_timeout_task); |
| 8009 | } |
| 8010 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8011 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8012 | struct proxy *defpx, const char *file, int line, |
| 8013 | char **err) |
| 8014 | { |
| 8015 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8016 | file, line, err, &hlua_timeout_applet); |
| 8017 | } |
| 8018 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8019 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8020 | struct proxy *defpx, const char *file, int line, |
| 8021 | char **err) |
| 8022 | { |
| 8023 | char *error; |
| 8024 | |
| 8025 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8026 | if (*error != '\0') { |
| 8027 | memprintf(err, "%s: invalid number", args[0]); |
| 8028 | return -1; |
| 8029 | } |
| 8030 | return 0; |
| 8031 | } |
| 8032 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8033 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8034 | struct proxy *defpx, const char *file, int line, |
| 8035 | char **err) |
| 8036 | { |
| 8037 | char *error; |
| 8038 | |
| 8039 | if (*(args[1]) == 0) { |
| 8040 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8041 | return -1; |
| 8042 | } |
| 8043 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8044 | if (*error != '\0') { |
| 8045 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8046 | return -1; |
| 8047 | } |
| 8048 | return 0; |
| 8049 | } |
| 8050 | |
| 8051 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8052 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8053 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8054 | * the main lua entry point. |
| 8055 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8056 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8057 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8058 | * |
| 8059 | * In some error case, LUA set an error message in top of the stack. This function |
| 8060 | * 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] | 8061 | * |
| 8062 | * This function can fail with an abort() due to an Lua critical error. |
| 8063 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8064 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8065 | */ |
| 8066 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8067 | struct proxy *defpx, const char *file, int line, |
| 8068 | char **err) |
| 8069 | { |
| 8070 | int error; |
| 8071 | |
Thierry Fournier | ad6e9c5 | 2020-11-29 01:06:24 +0100 | [diff] [blame] | 8072 | if (*(args[1]) == 0) { |
| 8073 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8074 | return -1; |
| 8075 | } |
| 8076 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8077 | /* Just load and compile the file. */ |
| 8078 | error = luaL_loadfile(gL.T, args[1]); |
| 8079 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8080 | 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] | 8081 | lua_pop(gL.T, 1); |
| 8082 | return -1; |
| 8083 | } |
| 8084 | |
| 8085 | /* If no syntax error where detected, execute the code. */ |
| 8086 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8087 | switch (error) { |
| 8088 | case LUA_OK: |
| 8089 | break; |
| 8090 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8091 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8092 | lua_pop(gL.T, 1); |
| 8093 | return -1; |
| 8094 | case LUA_ERRMEM: |
Thierry Fournier | 518017f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8095 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8096 | return -1; |
| 8097 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8098 | 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] | 8099 | lua_pop(gL.T, 1); |
| 8100 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8101 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8102 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8103 | 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] | 8104 | lua_pop(gL.T, 1); |
| 8105 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8106 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8107 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8108 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8109 | lua_pop(gL.T, 1); |
| 8110 | return -1; |
| 8111 | } |
| 8112 | |
| 8113 | return 0; |
| 8114 | } |
| 8115 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8116 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8117 | * in the given <ctx>. |
| 8118 | */ |
| 8119 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 8120 | { |
| 8121 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 8122 | lua_pushstring(ctx.T, path); /* push given path */ |
| 8123 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 8124 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 8125 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 8126 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 8127 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 8128 | |
| 8129 | return 0; |
| 8130 | } |
| 8131 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8132 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 8133 | struct proxy *defpx, const char *file, int line, |
| 8134 | char **err) |
| 8135 | { |
| 8136 | char *path; |
| 8137 | char *type = "path"; |
| 8138 | if (too_many_args(2, args, err, NULL)) { |
| 8139 | return -1; |
| 8140 | } |
| 8141 | |
| 8142 | if (!(*args[1])) { |
| 8143 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 8144 | return -1; |
| 8145 | } |
| 8146 | path = args[1]; |
| 8147 | |
| 8148 | if (*args[2]) { |
| 8149 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8150 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 8151 | return -1; |
| 8152 | } |
| 8153 | type = args[2]; |
| 8154 | } |
| 8155 | |
| 8156 | return hlua_prepend_path(gL, type, path); |
| 8157 | } |
| 8158 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8159 | /* configuration keywords declaration */ |
| 8160 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8161 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8162 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8163 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8164 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8165 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8166 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8167 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8168 | { 0, NULL, NULL }, |
| 8169 | }}; |
| 8170 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8171 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8172 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8173 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8174 | /* This 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 | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8178 | int hlua_post_init() |
| 8179 | { |
| 8180 | struct hlua_init_function *init; |
| 8181 | const char *msg; |
| 8182 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8183 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8184 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8185 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8186 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8187 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8188 | error = lua_tostring(gL.T, -1); |
| 8189 | else |
| 8190 | error = "critical error"; |
| 8191 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8192 | exit(1); |
| 8193 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8194 | |
| 8195 | #if USE_OPENSSL |
| 8196 | /* Initialize SSL server. */ |
| 8197 | if (socket_ssl.xprt->prepare_srv) { |
| 8198 | int saved_used_backed = global.ssl_used_backend; |
| 8199 | // don't affect maxconn automatic computation |
| 8200 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8201 | global.ssl_used_backend = saved_used_backed; |
| 8202 | } |
| 8203 | #endif |
| 8204 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8205 | hlua_fcn_post_init(gL.T); |
| 8206 | RESET_SAFE_LJMP(gL.T); |
| 8207 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8208 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8209 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8210 | ret = hlua_ctx_resume(&gL, 0); |
| 8211 | switch (ret) { |
| 8212 | case HLUA_E_OK: |
| 8213 | lua_pop(gL.T, -1); |
Thierry Fournier | 5a96467 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8214 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8215 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8216 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8217 | return 0; |
| 8218 | case HLUA_E_ERRMSG: |
| 8219 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8220 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8221 | return 0; |
| 8222 | case HLUA_E_ERR: |
| 8223 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8224 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8225 | return 0; |
| 8226 | } |
| 8227 | } |
| 8228 | return 1; |
| 8229 | } |
| 8230 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8231 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8232 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8233 | * is the previously allocated size or the kind of object in case of a new |
| 8234 | * allocation. <nsize> is the requested new size. |
| 8235 | */ |
| 8236 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8237 | { |
| 8238 | struct hlua_mem_allocator *zone = ud; |
| 8239 | |
| 8240 | if (nsize == 0) { |
| 8241 | /* it's a free */ |
| 8242 | if (ptr) |
| 8243 | zone->allocated -= osize; |
| 8244 | free(ptr); |
| 8245 | return NULL; |
| 8246 | } |
| 8247 | |
| 8248 | if (!ptr) { |
| 8249 | /* it's a new allocation */ |
| 8250 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8251 | return NULL; |
| 8252 | |
| 8253 | ptr = malloc(nsize); |
| 8254 | if (ptr) |
| 8255 | zone->allocated += nsize; |
| 8256 | return ptr; |
| 8257 | } |
| 8258 | |
| 8259 | /* it's a realloc */ |
| 8260 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8261 | return NULL; |
| 8262 | |
| 8263 | ptr = realloc(ptr, nsize); |
| 8264 | if (ptr) |
| 8265 | zone->allocated += nsize - osize; |
| 8266 | return ptr; |
| 8267 | } |
| 8268 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8269 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8270 | * We are in the initialisation process of HAProxy, this abort() is |
| 8271 | * tolerated. |
| 8272 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8273 | void hlua_init(void) |
| 8274 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8275 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8276 | int idx; |
| 8277 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8278 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8279 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8280 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8281 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8282 | struct srv_kw *kw; |
| 8283 | int tmp_error; |
| 8284 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8285 | char *args[] = { /* SSL client configuration. */ |
| 8286 | "ssl", |
| 8287 | "verify", |
| 8288 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8289 | NULL |
| 8290 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8291 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8292 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8293 | /* Init main lua stack. */ |
| 8294 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8295 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8296 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8297 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8298 | hlua_sethlua(&gL); |
| 8299 | gL.Tref = LUA_REFNIL; |
| 8300 | gL.task = NULL; |
| 8301 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8302 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8303 | * the Lua function can fail with an abort. We are in the initialisation |
| 8304 | * process of HAProxy, this abort() is tolerated. |
| 8305 | */ |
| 8306 | |
Thierry Fournier | 2b7983c | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8307 | /* Set safe environment for the initialisation. */ |
| 8308 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8309 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8310 | error_msg = lua_tostring(gL.T, -1); |
| 8311 | else |
| 8312 | error_msg = "critical error"; |
| 8313 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8314 | exit(1); |
| 8315 | } |
| 8316 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8317 | /* Initialise lua. */ |
| 8318 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8319 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8320 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8321 | #ifdef HLUA_PREPEND_PATH |
| 8322 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8323 | #endif |
| 8324 | #ifdef HLUA_PREPEND_CPATH |
| 8325 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8326 | #endif |
| 8327 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8328 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8329 | |
| 8330 | /* |
| 8331 | * |
| 8332 | * Create "core" object. |
| 8333 | * |
| 8334 | */ |
| 8335 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8336 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8337 | lua_newtable(gL.T); |
| 8338 | |
| 8339 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8340 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8341 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8342 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8343 | /* Register special functions. */ |
| 8344 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8345 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8346 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8347 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8348 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8349 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8350 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8351 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8352 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8353 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8354 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8355 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8356 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8357 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8358 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8359 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8360 | hlua_class_function(gL.T, "log", hlua_log); |
| 8361 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8362 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8363 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8364 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8365 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8366 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8367 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8368 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8369 | |
| 8370 | /* |
| 8371 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8372 | * Create "act" object. |
| 8373 | * |
| 8374 | */ |
| 8375 | |
| 8376 | /* This table entry is the object "act" base. */ |
| 8377 | lua_newtable(gL.T); |
| 8378 | |
| 8379 | /* push action return constants */ |
| 8380 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8381 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8382 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8383 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8384 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8385 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8386 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8387 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8388 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8389 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8390 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8391 | lua_setglobal(gL.T, "act"); |
| 8392 | |
| 8393 | /* |
| 8394 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8395 | * Register class Map |
| 8396 | * |
| 8397 | */ |
| 8398 | |
| 8399 | /* This table entry is the object "Map" base. */ |
| 8400 | lua_newtable(gL.T); |
| 8401 | |
| 8402 | /* register pattern types. */ |
| 8403 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8404 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8405 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8406 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8407 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8408 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8409 | |
| 8410 | /* register constructor. */ |
| 8411 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8412 | |
| 8413 | /* Create and fill the metatable. */ |
| 8414 | lua_newtable(gL.T); |
| 8415 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8416 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8417 | lua_pushstring(gL.T, "__index"); |
| 8418 | lua_newtable(gL.T); |
| 8419 | |
| 8420 | /* Register . */ |
| 8421 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8422 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8423 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8424 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8425 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8426 | /* Register previous table in the registry with reference and named entry. |
| 8427 | * The function hlua_register_metatable() pops the stack, so we |
| 8428 | * previously create a copy of the table. |
| 8429 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8430 | 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] | 8431 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8432 | |
| 8433 | /* Assign the metatable to the mai Map object. */ |
| 8434 | lua_setmetatable(gL.T, -2); |
| 8435 | |
| 8436 | /* Set a name to the table. */ |
| 8437 | lua_setglobal(gL.T, "Map"); |
| 8438 | |
| 8439 | /* |
| 8440 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8441 | * Register class Channel |
| 8442 | * |
| 8443 | */ |
| 8444 | |
| 8445 | /* Create and fill the metatable. */ |
| 8446 | lua_newtable(gL.T); |
| 8447 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8448 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8449 | lua_pushstring(gL.T, "__index"); |
| 8450 | lua_newtable(gL.T); |
| 8451 | |
| 8452 | /* Register . */ |
| 8453 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8454 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8455 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8456 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8457 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8458 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8459 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8460 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8461 | 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] | 8462 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8463 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8464 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8465 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8466 | |
| 8467 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8468 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8469 | |
| 8470 | /* |
| 8471 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8472 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8473 | * |
| 8474 | */ |
| 8475 | |
| 8476 | /* Create and fill the metatable. */ |
| 8477 | lua_newtable(gL.T); |
| 8478 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8479 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8480 | lua_pushstring(gL.T, "__index"); |
| 8481 | lua_newtable(gL.T); |
| 8482 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8483 | /* Browse existing fetches and create the associated |
| 8484 | * object method. |
| 8485 | */ |
| 8486 | sf = NULL; |
| 8487 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8488 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8489 | * by an underscore. |
| 8490 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8491 | strncpy(trash.area, sf->kw, trash.size); |
| 8492 | trash.area[trash.size - 1] = '\0'; |
| 8493 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8494 | if (*p == '.' || *p == '-' || *p == '+') |
| 8495 | *p = '_'; |
| 8496 | |
| 8497 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8498 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8499 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8500 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8501 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8502 | } |
| 8503 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8504 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8505 | |
| 8506 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8507 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8508 | |
| 8509 | /* |
| 8510 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8511 | * Register class Converters |
| 8512 | * |
| 8513 | */ |
| 8514 | |
| 8515 | /* Create and fill the metatable. */ |
| 8516 | lua_newtable(gL.T); |
| 8517 | |
| 8518 | /* Create and fill the __index entry. */ |
| 8519 | lua_pushstring(gL.T, "__index"); |
| 8520 | lua_newtable(gL.T); |
| 8521 | |
| 8522 | /* Browse existing converters and create the associated |
| 8523 | * object method. |
| 8524 | */ |
| 8525 | sc = NULL; |
| 8526 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8527 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8528 | * by an underscore. |
| 8529 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8530 | strncpy(trash.area, sc->kw, trash.size); |
| 8531 | trash.area[trash.size - 1] = '\0'; |
| 8532 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8533 | if (*p == '.' || *p == '-' || *p == '+') |
| 8534 | *p = '_'; |
| 8535 | |
| 8536 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8537 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8538 | lua_pushlightuserdata(gL.T, sc); |
| 8539 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8540 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8541 | } |
| 8542 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8543 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8544 | |
| 8545 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8546 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8547 | |
| 8548 | /* |
| 8549 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8550 | * Register class HTTP |
| 8551 | * |
| 8552 | */ |
| 8553 | |
| 8554 | /* Create and fill the metatable. */ |
| 8555 | lua_newtable(gL.T); |
| 8556 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8557 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8558 | lua_pushstring(gL.T, "__index"); |
| 8559 | lua_newtable(gL.T); |
| 8560 | |
| 8561 | /* Register Lua functions. */ |
| 8562 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8563 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8564 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8565 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8566 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8567 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8568 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8569 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8570 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8571 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8572 | |
| 8573 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8574 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8575 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8576 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8577 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8578 | 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] | 8579 | 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] | 8580 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8581 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8582 | |
| 8583 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8584 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8585 | |
| 8586 | /* |
| 8587 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8588 | * Register class AppletTCP |
| 8589 | * |
| 8590 | */ |
| 8591 | |
| 8592 | /* Create and fill the metatable. */ |
| 8593 | lua_newtable(gL.T); |
| 8594 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8595 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8596 | lua_pushstring(gL.T, "__index"); |
| 8597 | lua_newtable(gL.T); |
| 8598 | |
| 8599 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8600 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8601 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8602 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8603 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8604 | 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] | 8605 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8606 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8607 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8608 | |
| 8609 | lua_settable(gL.T, -3); |
| 8610 | |
| 8611 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8612 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8613 | |
| 8614 | /* |
| 8615 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8616 | * Register class AppletHTTP |
| 8617 | * |
| 8618 | */ |
| 8619 | |
| 8620 | /* Create and fill the metatable. */ |
| 8621 | lua_newtable(gL.T); |
| 8622 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8623 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8624 | lua_pushstring(gL.T, "__index"); |
| 8625 | lua_newtable(gL.T); |
| 8626 | |
| 8627 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8628 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8629 | 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] | 8630 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8631 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8632 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8633 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8634 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8635 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8636 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8637 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8638 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8639 | |
| 8640 | lua_settable(gL.T, -3); |
| 8641 | |
| 8642 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8643 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8644 | |
| 8645 | /* |
| 8646 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8647 | * Register class TXN |
| 8648 | * |
| 8649 | */ |
| 8650 | |
| 8651 | /* Create and fill the metatable. */ |
| 8652 | lua_newtable(gL.T); |
| 8653 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8654 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8655 | lua_pushstring(gL.T, "__index"); |
| 8656 | lua_newtable(gL.T); |
| 8657 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8658 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8659 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8660 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8661 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8662 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8663 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8664 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8665 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8666 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8667 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8668 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8669 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8670 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8671 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8672 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8673 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8674 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8675 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8676 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8677 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8678 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8679 | |
| 8680 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8681 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8682 | |
| 8683 | /* |
| 8684 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8685 | * Register class reply |
| 8686 | * |
| 8687 | */ |
| 8688 | lua_newtable(gL.T); |
| 8689 | lua_pushstring(gL.T, "__index"); |
| 8690 | lua_newtable(gL.T); |
| 8691 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8692 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8693 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8694 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8695 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8696 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8697 | |
| 8698 | |
| 8699 | /* |
| 8700 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8701 | * Register class Socket |
| 8702 | * |
| 8703 | */ |
| 8704 | |
| 8705 | /* Create and fill the metatable. */ |
| 8706 | lua_newtable(gL.T); |
| 8707 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8708 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8709 | lua_pushstring(gL.T, "__index"); |
| 8710 | lua_newtable(gL.T); |
| 8711 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8712 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8713 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8714 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8715 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8716 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8717 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8718 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8719 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8720 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8721 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8722 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8723 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8724 | 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] | 8725 | |
| 8726 | /* Register the garbage collector entry. */ |
| 8727 | lua_pushstring(gL.T, "__gc"); |
| 8728 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8729 | 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] | 8730 | |
| 8731 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8732 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8733 | |
| 8734 | /* Proxy and server configuration initialisation. */ |
| 8735 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8736 | init_new_proxy(&socket_proxy); |
| 8737 | socket_proxy.parent = NULL; |
| 8738 | socket_proxy.last_change = now.tv_sec; |
| 8739 | socket_proxy.id = "LUA-SOCKET"; |
| 8740 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8741 | socket_proxy.maxconn = 0; |
| 8742 | socket_proxy.accept = NULL; |
| 8743 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8744 | socket_proxy.srv = NULL; |
| 8745 | socket_proxy.conn_retries = 0; |
| 8746 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8747 | |
| 8748 | /* Init TCP server: unchanged parameters */ |
| 8749 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8750 | socket_tcp.next = NULL; |
| 8751 | socket_tcp.proxy = &socket_proxy; |
| 8752 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8753 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8754 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8755 | socket_tcp.idle_conns = NULL; |
| 8756 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8757 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8758 | socket_tcp.last_change = 0; |
| 8759 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8760 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8761 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8762 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8763 | |
| 8764 | /* XXX: Copy default parameter from default server, |
| 8765 | * but the default server is not initialized. |
| 8766 | */ |
| 8767 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8768 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8769 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8770 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8771 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8772 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8773 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8774 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8775 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8776 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8777 | |
| 8778 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8779 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8780 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8781 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8782 | socket_tcp.check.server = &socket_tcp; |
| 8783 | |
| 8784 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8785 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8786 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8787 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8788 | socket_tcp.agent.server = &socket_tcp; |
| 8789 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8790 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8791 | |
| 8792 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8793 | /* Init TCP server: unchanged parameters */ |
| 8794 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8795 | socket_ssl.next = NULL; |
| 8796 | socket_ssl.proxy = &socket_proxy; |
| 8797 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8798 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8799 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8800 | socket_ssl.idle_conns = NULL; |
| 8801 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8802 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8803 | socket_ssl.last_change = 0; |
| 8804 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8805 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8806 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8807 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8808 | |
| 8809 | /* XXX: Copy default parameter from default server, |
| 8810 | * but the default server is not initialized. |
| 8811 | */ |
| 8812 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8813 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8814 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8815 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8816 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8817 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8818 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8819 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8820 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8821 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8822 | |
| 8823 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8824 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8825 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8826 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8827 | socket_ssl.check.server = &socket_ssl; |
| 8828 | |
| 8829 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8830 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8831 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8832 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8833 | socket_ssl.agent.server = &socket_ssl; |
| 8834 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8835 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8836 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8837 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8838 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8839 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8840 | /* |
| 8841 | * |
| 8842 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8843 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8844 | * features like client certificates and ssl_verify. |
| 8845 | * |
| 8846 | */ |
| 8847 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8848 | if (tmp_error != 0) { |
| 8849 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8850 | abort(); /* This must be never arrives because the command line |
| 8851 | not editable by the user. */ |
| 8852 | } |
| 8853 | idx += kw->skip; |
| 8854 | } |
| 8855 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8856 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8857 | |
| 8858 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8859 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8860 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8861 | static void hlua_deinit() |
| 8862 | { |
| 8863 | lua_close(gL.T); |
| 8864 | } |
| 8865 | |
| 8866 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8867 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8868 | static void hlua_register_build_options(void) |
| 8869 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8870 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8871 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8872 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8873 | hap_register_build_opts(ptr, 1); |
| 8874 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8875 | |
| 8876 | INITCALL0(STG_REGISTER, hlua_register_build_options); |