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 | |
Christopher Faulet | 5326d6a | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 286 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 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 | |
| 292 | while (lua_getstack(L, level++, &ar)) { |
| 293 | |
| 294 | /* Add separator */ |
Christopher Faulet | 5326d6a | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 295 | if (b_data(msg)) |
| 296 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 297 | |
| 298 | /* Fill fields: |
| 299 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 300 | * 'l': fills in the field currentline; |
| 301 | * 'n': fills in the field name and namewhat; |
| 302 | * 't': fills in the field istailcall; |
| 303 | */ |
| 304 | lua_getinfo(L, "Slnt", &ar); |
| 305 | |
| 306 | /* Append code localisation */ |
| 307 | if (ar.currentline > 0) |
Christopher Faulet | 5326d6a | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 308 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 309 | else |
Christopher Faulet | 5326d6a | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 310 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * Get function name |
| 314 | * |
| 315 | * if namewhat is no empty, name is defined. |
| 316 | * what contains "Lua" for Lua function, "C" for C function, |
| 317 | * or "main" for main code. |
| 318 | */ |
| 319 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
Christopher Faulet | 5326d6a | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 320 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 321 | |
| 322 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
Christopher Faulet | 5326d6a | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 323 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 324 | |
| 325 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | 5326d6a | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 326 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 327 | |
| 328 | else /* nothing left... */ |
| 329 | chunk_appendf(msg, "?"); |
| 330 | |
| 331 | |
| 332 | /* Display tailed call */ |
| 333 | if (ar.istailcall) |
| 334 | chunk_appendf(msg, " ..."); |
| 335 | } |
| 336 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 337 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 341 | /* This function check the number of arguments available in the |
| 342 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 343 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 344 | */ |
| 345 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 346 | { |
| 347 | if (lua_gettop(L) == nb) |
| 348 | return; |
| 349 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 350 | } |
| 351 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 352 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 353 | * and the line number where the error is encountered. |
| 354 | */ |
| 355 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 356 | { |
| 357 | va_list argp; |
| 358 | va_start(argp, fmt); |
| 359 | luaL_where(L, 1); |
| 360 | lua_pushvfstring(L, fmt, argp); |
| 361 | va_end(argp); |
| 362 | lua_concat(L, 2); |
| 363 | return 1; |
| 364 | } |
| 365 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 366 | /* This functions is used with sample fetch and converters. It |
| 367 | * converts the HAProxy configuration argument in a lua stack |
| 368 | * values. |
| 369 | * |
| 370 | * It takes an array of "arg", and each entry of the array is |
| 371 | * converted and pushed in the LUA stack. |
| 372 | */ |
| 373 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 374 | { |
| 375 | switch (arg->type) { |
| 376 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 377 | case ARGT_TIME: |
| 378 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 379 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 380 | break; |
| 381 | |
| 382 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 383 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 384 | break; |
| 385 | |
| 386 | case ARGT_IPV4: |
| 387 | case ARGT_IPV6: |
| 388 | case ARGT_MSK4: |
| 389 | case ARGT_MSK6: |
| 390 | case ARGT_FE: |
| 391 | case ARGT_BE: |
| 392 | case ARGT_TAB: |
| 393 | case ARGT_SRV: |
| 394 | case ARGT_USR: |
| 395 | case ARGT_MAP: |
| 396 | default: |
| 397 | lua_pushnil(L); |
| 398 | break; |
| 399 | } |
| 400 | return 1; |
| 401 | } |
| 402 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 403 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 404 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 405 | * with sample fetch wrappers. The input arguments are given to the |
| 406 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 407 | */ |
| 408 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 409 | { |
| 410 | switch (lua_type(L, ud)) { |
| 411 | |
| 412 | case LUA_TNUMBER: |
| 413 | case LUA_TBOOLEAN: |
| 414 | arg->type = ARGT_SINT; |
| 415 | arg->data.sint = lua_tointeger(L, ud); |
| 416 | break; |
| 417 | |
| 418 | case LUA_TSTRING: |
| 419 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 420 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 421 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 422 | arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 423 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | case LUA_TUSERDATA: |
| 427 | case LUA_TNIL: |
| 428 | case LUA_TTABLE: |
| 429 | case LUA_TFUNCTION: |
| 430 | case LUA_TTHREAD: |
| 431 | case LUA_TLIGHTUSERDATA: |
| 432 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 433 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 434 | break; |
| 435 | } |
| 436 | return 1; |
| 437 | } |
| 438 | |
| 439 | /* the following functions are used to convert a struct sample |
| 440 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 441 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 442 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 443 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 444 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 445 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 447 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 448 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | break; |
| 450 | |
| 451 | case SMP_T_BIN: |
| 452 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 453 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 454 | break; |
| 455 | |
| 456 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 457 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 459 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 460 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 461 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 462 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 463 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 464 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 465 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 466 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 467 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 468 | break; |
| 469 | default: |
| 470 | lua_pushnil(L); |
| 471 | break; |
| 472 | } |
| 473 | break; |
| 474 | |
| 475 | case SMP_T_IPV4: |
| 476 | case SMP_T_IPV6: |
| 477 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 478 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 479 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 480 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 481 | else |
| 482 | lua_pushnil(L); |
| 483 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 484 | default: |
| 485 | lua_pushnil(L); |
| 486 | break; |
| 487 | } |
| 488 | return 1; |
| 489 | } |
| 490 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 491 | /* the following functions are used to convert a struct sample |
| 492 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 493 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 494 | */ |
| 495 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 496 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 497 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 498 | |
| 499 | case SMP_T_BIN: |
| 500 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 501 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 502 | break; |
| 503 | |
| 504 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 505 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 506 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 507 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 508 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 509 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 510 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 511 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 512 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 513 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 514 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 515 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 516 | break; |
| 517 | default: |
| 518 | lua_pushstring(L, ""); |
| 519 | break; |
| 520 | } |
| 521 | break; |
| 522 | |
| 523 | case SMP_T_SINT: |
| 524 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 525 | case SMP_T_IPV4: |
| 526 | case SMP_T_IPV6: |
| 527 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 528 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 529 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 530 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 531 | else |
| 532 | lua_pushstring(L, ""); |
| 533 | break; |
| 534 | default: |
| 535 | lua_pushstring(L, ""); |
| 536 | break; |
| 537 | } |
| 538 | return 1; |
| 539 | } |
| 540 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 541 | /* the following functions are used to convert an Lua type in a |
| 542 | * struct sample. This is useful to provide data from a converter |
| 543 | * to the LUA code. |
| 544 | */ |
| 545 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 546 | { |
| 547 | switch (lua_type(L, ud)) { |
| 548 | |
| 549 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 550 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 551 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 552 | break; |
| 553 | |
| 554 | |
| 555 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 556 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 557 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 558 | break; |
| 559 | |
| 560 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 561 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 562 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 563 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 564 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 565 | smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 566 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 567 | break; |
| 568 | |
| 569 | case LUA_TUSERDATA: |
| 570 | case LUA_TNIL: |
| 571 | case LUA_TTABLE: |
| 572 | case LUA_TFUNCTION: |
| 573 | case LUA_TTHREAD: |
| 574 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 575 | case LUA_TNONE: |
| 576 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 577 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 578 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | break; |
| 580 | } |
| 581 | return 1; |
| 582 | } |
| 583 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 584 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 585 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 586 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 587 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 588 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 589 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 590 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 591 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 592 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 593 | { |
| 594 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 595 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 596 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 597 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 598 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 599 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 600 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 601 | |
| 602 | idx = 0; |
| 603 | min_arg = ARGM(mask); |
| 604 | mask >>= ARGM_BITS; |
| 605 | |
| 606 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 607 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 608 | |
| 609 | /* Check oversize. */ |
| 610 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 611 | msg = "Malformed argument mask"; |
| 612 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* Check for mandatory arguments. */ |
| 616 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 617 | if (idx < min_arg) { |
| 618 | |
| 619 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 620 | if (idx > 0) { |
| 621 | msg = "Mandatory argument expected"; |
| 622 | goto error; |
| 623 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 624 | |
| 625 | /* If first argument have a certain type, some default values |
| 626 | * may be used. See the function smp_resolve_args(). |
| 627 | */ |
| 628 | switch (mask & ARGT_MASK) { |
| 629 | |
| 630 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 631 | if (!(p->cap & PR_CAP_FE)) { |
| 632 | msg = "Mandatory argument expected"; |
| 633 | goto error; |
| 634 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 635 | argp[idx].data.prx = p; |
| 636 | argp[idx].type = ARGT_FE; |
| 637 | argp[idx+1].type = ARGT_STOP; |
| 638 | break; |
| 639 | |
| 640 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 641 | if (!(p->cap & PR_CAP_BE)) { |
| 642 | msg = "Mandatory argument expected"; |
| 643 | goto error; |
| 644 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 645 | argp[idx].data.prx = p; |
| 646 | argp[idx].type = ARGT_BE; |
| 647 | argp[idx+1].type = ARGT_STOP; |
| 648 | break; |
| 649 | |
| 650 | case ARGT_TAB: |
| 651 | argp[idx].data.prx = p; |
| 652 | argp[idx].type = ARGT_TAB; |
| 653 | argp[idx+1].type = ARGT_STOP; |
| 654 | break; |
| 655 | |
| 656 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 657 | msg = "Mandatory argument expected"; |
| 658 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 662 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 663 | } |
| 664 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 665 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 666 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 667 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 668 | msg = "Last argument expected"; |
| 669 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 673 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 674 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 677 | /* Convert some argument types. All string in argp[] are for not |
| 678 | * duplicated yet. |
| 679 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 680 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 681 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 682 | if (argp[idx].type != ARGT_SINT) { |
| 683 | msg = "integer expected"; |
| 684 | goto error; |
| 685 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 686 | argp[idx].type = ARGT_SINT; |
| 687 | break; |
| 688 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 689 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 690 | if (argp[idx].type != ARGT_SINT) { |
| 691 | msg = "integer expected"; |
| 692 | goto error; |
| 693 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 694 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 695 | break; |
| 696 | |
| 697 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 698 | if (argp[idx].type != ARGT_SINT) { |
| 699 | msg = "integer expected"; |
| 700 | goto error; |
| 701 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 702 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 703 | break; |
| 704 | |
| 705 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 706 | if (argp[idx].type != ARGT_STR) { |
| 707 | msg = "string expected"; |
| 708 | goto error; |
| 709 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 710 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 711 | if (!argp[idx].data.prx) { |
| 712 | msg = "frontend doesn't exist"; |
| 713 | goto error; |
| 714 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | argp[idx].type = ARGT_FE; |
| 716 | break; |
| 717 | |
| 718 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 719 | if (argp[idx].type != ARGT_STR) { |
| 720 | msg = "string expected"; |
| 721 | goto error; |
| 722 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 723 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 724 | if (!argp[idx].data.prx) { |
| 725 | msg = "backend doesn't exist"; |
| 726 | goto error; |
| 727 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 728 | argp[idx].type = ARGT_BE; |
| 729 | break; |
| 730 | |
| 731 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 732 | if (argp[idx].type != ARGT_STR) { |
| 733 | msg = "string expected"; |
| 734 | goto error; |
| 735 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 736 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 737 | if (!argp[idx].data.t) { |
| 738 | msg = "table doesn't exist"; |
| 739 | goto error; |
| 740 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 741 | argp[idx].type = ARGT_TAB; |
| 742 | break; |
| 743 | |
| 744 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 745 | if (argp[idx].type != ARGT_STR) { |
| 746 | msg = "string expected"; |
| 747 | goto error; |
| 748 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 749 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | if (sname) { |
| 751 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 752 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 753 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 754 | if (!px) { |
| 755 | msg = "backend doesn't exist"; |
| 756 | goto error; |
| 757 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 758 | } |
| 759 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 760 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 761 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 762 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 764 | if (!argp[idx].data.srv) { |
| 765 | msg = "server doesn't exist"; |
| 766 | goto error; |
| 767 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 768 | argp[idx].type = ARGT_SRV; |
| 769 | break; |
| 770 | |
| 771 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 772 | if (argp[idx].type != ARGT_STR) { |
| 773 | msg = "string expected"; |
| 774 | goto error; |
| 775 | } |
| 776 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 777 | msg = "invalid IPv4 address"; |
| 778 | goto error; |
| 779 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 780 | argp[idx].type = ARGT_IPV4; |
| 781 | break; |
| 782 | |
| 783 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 784 | if (argp[idx].type == ARGT_SINT) |
| 785 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 786 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 787 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 788 | msg = "invalid IPv4 mask"; |
| 789 | goto error; |
| 790 | } |
| 791 | } |
| 792 | else { |
| 793 | msg = "integer or string expected"; |
| 794 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 795 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 796 | argp[idx].type = ARGT_MSK4; |
| 797 | break; |
| 798 | |
| 799 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 800 | if (argp[idx].type != ARGT_STR) { |
| 801 | msg = "string expected"; |
| 802 | goto error; |
| 803 | } |
| 804 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 805 | msg = "invalid IPv6 address"; |
| 806 | goto error; |
| 807 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 808 | argp[idx].type = ARGT_IPV6; |
| 809 | break; |
| 810 | |
| 811 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 812 | if (argp[idx].type == ARGT_SINT) |
| 813 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 814 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 815 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 816 | msg = "invalid IPv6 mask"; |
| 817 | goto error; |
| 818 | } |
| 819 | } |
| 820 | else { |
| 821 | msg = "integer or string expected"; |
| 822 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 823 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 824 | argp[idx].type = ARGT_MSK6; |
| 825 | break; |
| 826 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 827 | case ARGT_REG: |
| 828 | if (argp[idx].type != ARGT_STR) { |
| 829 | msg = "string expected"; |
| 830 | goto error; |
| 831 | } |
| 832 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 833 | if (!reg) { |
| 834 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 835 | argp[idx].data.str.area, err); |
| 836 | free(err); |
| 837 | goto error; |
| 838 | } |
| 839 | argp[idx].type = ARGT_REG; |
| 840 | argp[idx].data.reg = reg; |
| 841 | break; |
| 842 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 843 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 844 | if (argp[idx].type != ARGT_STR) { |
| 845 | msg = "string expected"; |
| 846 | goto error; |
| 847 | } |
| 848 | if (p->uri_auth && p->uri_auth->userlist && |
| 849 | !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area)) |
| 850 | ul = p->uri_auth->userlist; |
| 851 | else |
| 852 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 853 | |
| 854 | if (!ul) { |
| 855 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 856 | goto error; |
| 857 | } |
| 858 | argp[idx].type = ARGT_USR; |
| 859 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 860 | break; |
| 861 | |
| 862 | case ARGT_STR: |
| 863 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 864 | msg = "unable to duplicate string arg"; |
| 865 | goto error; |
| 866 | } |
| 867 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 868 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 869 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 870 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 871 | msg = "type not yet supported"; |
| 872 | goto error; |
| 873 | break; |
| 874 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* Check for type of argument. */ |
| 878 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 879 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 880 | arg_type_names[(mask & ARGT_MASK)], |
| 881 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 882 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* Next argument. */ |
| 886 | mask >>= ARGT_BITS; |
| 887 | idx++; |
| 888 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 889 | return 0; |
| 890 | |
| 891 | error: |
| 892 | for (i = 0; i < idx; i++) { |
| 893 | if (argp[i].type == ARGT_STR) |
| 894 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 895 | else if (argp[i].type == ARGT_REG) |
| 896 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 897 | } |
| 898 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 899 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 902 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 903 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 904 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 905 | * |
| 906 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 908 | */ |
| 909 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 910 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 911 | struct hlua **hlua = lua_getextraspace(L); |
| 912 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 913 | } |
| 914 | static inline void hlua_sethlua(struct hlua *hlua) |
| 915 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 916 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 917 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 918 | } |
| 919 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 920 | /* This function is used to send logs. It try to send on screen (stderr) |
| 921 | * and on the default syslog server. |
| 922 | */ |
| 923 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 924 | { |
| 925 | struct tm tm; |
| 926 | char *p; |
| 927 | |
| 928 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 929 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 930 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 931 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 932 | /* Break the message if exceed the buffer size. */ |
| 933 | *(p-4) = ' '; |
| 934 | *(p-3) = '.'; |
| 935 | *(p-2) = '.'; |
| 936 | *(p-1) = '.'; |
| 937 | break; |
| 938 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 939 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 940 | *p = *msg; |
| 941 | else |
| 942 | *p = '.'; |
| 943 | } |
| 944 | *p = '\0'; |
| 945 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 946 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 947 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 948 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 949 | return; |
| 950 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 951 | get_localtime(date.tv_sec, &tm); |
| 952 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 953 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 954 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 955 | fflush(stderr); |
| 956 | } |
| 957 | } |
| 958 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 959 | /* This function just ensure that the yield will be always |
| 960 | * returned with a timeout and permit to set some flags |
| 961 | */ |
| 962 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 963 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 964 | { |
| 965 | struct hlua *hlua = hlua_gethlua(L); |
| 966 | |
| 967 | /* Set the wake timeout. If timeout is required, we set |
| 968 | * the expiration time. |
| 969 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 970 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 971 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 972 | hlua->flags |= flags; |
| 973 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 974 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 975 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 978 | /* This function initialises the Lua environment stored in the stream. |
| 979 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 980 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 981 | * |
| 982 | * This function is particular. it initialises a new Lua thread. If the |
| 983 | * initialisation fails (example: out of memory error), the lua function |
| 984 | * throws an error (longjmp). |
| 985 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 986 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 987 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 988 | * threads appear, the safe environment set a lock to ensure only one |
| 989 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 990 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 991 | * |
| 992 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 993 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 994 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 995 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 996 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 997 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 998 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 999 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1000 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1001 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1002 | if (!already_safe) { |
| 1003 | if (!SET_SAFE_LJMP(gL.T)) { |
| 1004 | lua->Tref = LUA_REFNIL; |
| 1005 | return 0; |
| 1006 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1007 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1008 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1009 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1010 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1011 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1012 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1013 | lua->T = lua_newthread(gL.T); |
| 1014 | if (!lua->T) { |
| 1015 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1016 | if (!already_safe) |
| 1017 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | hlua_sethlua(lua); |
| 1021 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1022 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1023 | if (!already_safe) |
| 1024 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1025 | return 1; |
| 1026 | } |
| 1027 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1028 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1029 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1030 | * is not freed. |
| 1031 | */ |
| 1032 | void hlua_ctx_destroy(struct hlua *lua) |
| 1033 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1034 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1035 | return; |
| 1036 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1037 | if (!lua->T) |
| 1038 | goto end; |
| 1039 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1040 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1041 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1042 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1043 | if (!SET_SAFE_LJMP(lua->T)) |
| 1044 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1045 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1046 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1047 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1048 | if (!SET_SAFE_LJMP(gL.T)) |
| 1049 | return; |
| 1050 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1051 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1052 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1053 | * without error, we run the GC on the thread pointer. Its freed all |
| 1054 | * the unused memory. |
| 1055 | * If the thread is finnish with an error or is currently yielded, |
| 1056 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1057 | * so e run the GC on the main thread. |
| 1058 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1059 | * the garbage collection. |
| 1060 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1061 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1062 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1063 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 1064 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 1065 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1066 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1067 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1068 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1069 | |
| 1070 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1071 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /* This function is used to restore the Lua context when a coroutine |
| 1075 | * fails. This function copy the common memory between old coroutine |
| 1076 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1077 | * replaced by the new coroutine. |
| 1078 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1079 | * as string error message and it is copied in the new stack. |
| 1080 | */ |
| 1081 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1082 | { |
| 1083 | lua_State *T; |
| 1084 | int new_ref; |
| 1085 | |
| 1086 | /* Renew the main LUA stack doesn't have sense. */ |
| 1087 | if (lua == &gL) |
| 1088 | return 0; |
| 1089 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1090 | /* New Lua coroutine. */ |
| 1091 | T = lua_newthread(gL.T); |
| 1092 | if (!T) |
| 1093 | return 0; |
| 1094 | |
| 1095 | /* Copy last error message. */ |
| 1096 | if (keep_msg) |
| 1097 | lua_xmove(lua->T, T, 1); |
| 1098 | |
| 1099 | /* Copy data between the coroutines. */ |
| 1100 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1101 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1102 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1103 | |
| 1104 | /* Destroy old data. */ |
| 1105 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1106 | |
| 1107 | /* The thread is garbage collected by Lua. */ |
| 1108 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1109 | |
| 1110 | /* Fill the struct with the new coroutine values. */ |
| 1111 | lua->Mref = new_ref; |
| 1112 | lua->T = T; |
| 1113 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1114 | |
| 1115 | /* Set context. */ |
| 1116 | hlua_sethlua(lua); |
| 1117 | |
| 1118 | return 1; |
| 1119 | } |
| 1120 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1121 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1122 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1123 | struct hlua *hlua = hlua_gethlua(L); |
| 1124 | |
| 1125 | /* Lua cannot yield when its returning from a function, |
| 1126 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1127 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1128 | */ |
| 1129 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1130 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | /* restore the interrupt condition. */ |
| 1135 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1136 | |
| 1137 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1138 | * If the state is not yieldable, trying yield causes an error. |
| 1139 | */ |
| 1140 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1141 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1142 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1143 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1144 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1145 | hlua->run_time += now_ms - hlua->start_time; |
| 1146 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1147 | lua_pushfstring(L, "execution timeout"); |
| 1148 | WILL_LJMP(lua_error(L)); |
| 1149 | } |
| 1150 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1151 | /* Update the start time. */ |
| 1152 | hlua->start_time = now_ms; |
| 1153 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1154 | /* Try to interrupt the process at the end of the current |
| 1155 | * unyieldable function. |
| 1156 | */ |
| 1157 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1158 | } |
| 1159 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | /* This function start or resumes the Lua stack execution. If the flag |
| 1161 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1162 | * The function return an error. |
| 1163 | * |
| 1164 | * The function can returns 4 values: |
| 1165 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1166 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1167 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1168 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1169 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1170 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1171 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1172 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | * LUA code. |
| 1174 | */ |
| 1175 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1176 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1177 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1178 | int nres; |
| 1179 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | int ret; |
| 1181 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1182 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1184 | /* Initialise run time counter. */ |
| 1185 | if (!HLUA_IS_RUNNING(lua)) |
| 1186 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1187 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1188 | /* Lock the whole Lua execution. This lock must be before the |
| 1189 | * label "resume_execution". |
| 1190 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1191 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1192 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1193 | resume_execution: |
| 1194 | |
| 1195 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1196 | * instructions. it is used for preventing infinite loops. |
| 1197 | */ |
| 1198 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1199 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1200 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1201 | HLUA_SET_RUN(lua); |
| 1202 | HLUA_CLR_CTRLYIELD(lua); |
| 1203 | HLUA_CLR_WAKERESWR(lua); |
| 1204 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1205 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1206 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1207 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1208 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1209 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1211 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1212 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1213 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1215 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | switch (ret) { |
| 1217 | |
| 1218 | case LUA_OK: |
| 1219 | ret = HLUA_E_OK; |
| 1220 | break; |
| 1221 | |
| 1222 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1223 | /* Check if the execution timeout is expired. It it is the case, we |
| 1224 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1225 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1226 | tv_update_date(0, 1); |
| 1227 | lua->run_time += now_ms - lua->start_time; |
| 1228 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1229 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1230 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1231 | break; |
| 1232 | } |
| 1233 | /* Process the forced yield. if the general yield is not allowed or |
| 1234 | * if no task were associated this the current Lua execution |
| 1235 | * coroutine, we resume the execution. Else we want to return in the |
| 1236 | * scheduler and we want to be waked up again, to continue the |
| 1237 | * current Lua execution. So we schedule our own task. |
| 1238 | */ |
| 1239 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1240 | if (!yield_allowed || !lua->task) |
| 1241 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1242 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1243 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1244 | if (!yield_allowed) { |
| 1245 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1246 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1247 | break; |
| 1248 | } |
| 1249 | ret = HLUA_E_AGAIN; |
| 1250 | break; |
| 1251 | |
| 1252 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1253 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1254 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1255 | * because the errors ares the only one mean to return immediately |
| 1256 | * from and lua execution. |
| 1257 | */ |
| 1258 | if (lua->flags & HLUA_EXIT) { |
| 1259 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1260 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1261 | break; |
| 1262 | } |
| 1263 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1264 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1265 | if (!lua_checkstack(lua->T, 1)) { |
| 1266 | ret = HLUA_E_ERR; |
| 1267 | break; |
| 1268 | } |
| 1269 | msg = lua_tostring(lua->T, -1); |
| 1270 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1271 | lua_pop(lua->T, 1); |
Christopher Faulet | 5326d6a | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1272 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1273 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1274 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1275 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1276 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1277 | ret = HLUA_E_ERRMSG; |
| 1278 | break; |
| 1279 | |
| 1280 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1281 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1282 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1283 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1284 | break; |
| 1285 | |
| 1286 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1287 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1288 | if (!lua_checkstack(lua->T, 1)) { |
| 1289 | ret = HLUA_E_ERR; |
| 1290 | break; |
| 1291 | } |
| 1292 | msg = lua_tostring(lua->T, -1); |
| 1293 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1294 | lua_pop(lua->T, 1); |
| 1295 | if (msg) |
| 1296 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1297 | else |
| 1298 | lua_pushfstring(lua->T, "message handler error"); |
| 1299 | ret = HLUA_E_ERRMSG; |
| 1300 | break; |
| 1301 | |
| 1302 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1303 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1304 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1305 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1306 | break; |
| 1307 | } |
| 1308 | |
| 1309 | switch (ret) { |
| 1310 | case HLUA_E_AGAIN: |
| 1311 | break; |
| 1312 | |
| 1313 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1314 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1315 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1316 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1317 | break; |
| 1318 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1319 | case HLUA_E_ETMOUT: |
| 1320 | case HLUA_E_NOMEM: |
| 1321 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1322 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1323 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1324 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1325 | hlua_ctx_renew(lua, 0); |
| 1326 | break; |
| 1327 | |
| 1328 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1329 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1330 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1331 | break; |
| 1332 | } |
| 1333 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1334 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1335 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1336 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1337 | return ret; |
| 1338 | } |
| 1339 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1340 | /* This function exit the current code. */ |
| 1341 | __LJMP static int hlua_done(lua_State *L) |
| 1342 | { |
| 1343 | struct hlua *hlua = hlua_gethlua(L); |
| 1344 | |
| 1345 | hlua->flags |= HLUA_EXIT; |
| 1346 | WILL_LJMP(lua_error(L)); |
| 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1351 | /* This function is an LUA binding. It provides a function |
| 1352 | * for deleting ACL from a referenced ACL file. |
| 1353 | */ |
| 1354 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1355 | { |
| 1356 | const char *name; |
| 1357 | const char *key; |
| 1358 | struct pat_ref *ref; |
| 1359 | |
| 1360 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1361 | |
| 1362 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1363 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1364 | |
| 1365 | ref = pat_ref_lookup(name); |
| 1366 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1367 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1368 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1369 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1370 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1371 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | /* This function is an LUA binding. It provides a function |
| 1376 | * for deleting map entry from a referenced map file. |
| 1377 | */ |
| 1378 | static int hlua_del_map(lua_State *L) |
| 1379 | { |
| 1380 | const char *name; |
| 1381 | const char *key; |
| 1382 | struct pat_ref *ref; |
| 1383 | |
| 1384 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1385 | |
| 1386 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1387 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1388 | |
| 1389 | ref = pat_ref_lookup(name); |
| 1390 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1391 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1392 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1393 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1394 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1395 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | /* This function is an LUA binding. It provides a function |
| 1400 | * for adding ACL pattern from a referenced ACL file. |
| 1401 | */ |
| 1402 | static int hlua_add_acl(lua_State *L) |
| 1403 | { |
| 1404 | const char *name; |
| 1405 | const char *key; |
| 1406 | struct pat_ref *ref; |
| 1407 | |
| 1408 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1409 | |
| 1410 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1411 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1412 | |
| 1413 | ref = pat_ref_lookup(name); |
| 1414 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1415 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1416 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1417 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1418 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1419 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1420 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | /* This function is an LUA binding. It provides a function |
| 1425 | * for setting map pattern and sample from a referenced map |
| 1426 | * file. |
| 1427 | */ |
| 1428 | static int hlua_set_map(lua_State *L) |
| 1429 | { |
| 1430 | const char *name; |
| 1431 | const char *key; |
| 1432 | const char *value; |
| 1433 | struct pat_ref *ref; |
| 1434 | |
| 1435 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1436 | |
| 1437 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1438 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1439 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1440 | |
| 1441 | ref = pat_ref_lookup(name); |
| 1442 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1443 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1444 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1445 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1446 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1447 | pat_ref_set(ref, key, value, NULL); |
| 1448 | else |
| 1449 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1450 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1451 | return 0; |
| 1452 | } |
| 1453 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1454 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1455 | * an integer or user data. This data is associated with a metatable. This |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1456 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1457 | * the name of the object (_G[<name>] = <metable> ). |
| 1458 | * |
| 1459 | * A metable is a table that modify the standard behavior of a standard |
| 1460 | * access to the associated data. The entries of this new metatable are |
| 1461 | * defined as is: |
| 1462 | * |
| 1463 | * http://lua-users.org/wiki/MetatableEvents |
| 1464 | * |
| 1465 | * __index |
| 1466 | * |
| 1467 | * we access an absent field in a table, the result is nil. This is |
| 1468 | * true, but it is not the whole truth. Actually, such access triggers |
| 1469 | * the interpreter to look for an __index metamethod: If there is no |
| 1470 | * such method, as usually happens, then the access results in nil; |
| 1471 | * otherwise, the metamethod will provide the result. |
| 1472 | * |
| 1473 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1474 | * the key does not appear in the table, but the metatable has an __index |
| 1475 | * property: |
| 1476 | * |
| 1477 | * - if the value is a function, the function is called, passing in the |
| 1478 | * table and the key; the return value of that function is returned as |
| 1479 | * the result. |
| 1480 | * |
| 1481 | * - if the value is another table, the value of the key in that table is |
| 1482 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1483 | * table's metatable has an __index property, then it continues on up) |
| 1484 | * |
| 1485 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1486 | * |
| 1487 | * http://www.lua.org/pil/13.4.1.html |
| 1488 | * |
| 1489 | * __newindex |
| 1490 | * |
| 1491 | * Like __index, but control property assignment. |
| 1492 | * |
| 1493 | * __mode - Control weak references. A string value with one or both |
| 1494 | * of the characters 'k' and 'v' which specifies that the the |
| 1495 | * keys and/or values in the table are weak references. |
| 1496 | * |
| 1497 | * __call - Treat a table like a function. When a table is followed by |
| 1498 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1499 | * a __call key pointing to a function, that function is invoked |
| 1500 | * (passing any specified arguments) and the return value is |
| 1501 | * returned. |
| 1502 | * |
| 1503 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1504 | * called, if the metatable for myTable has a __metatable |
| 1505 | * key, the value of that key is returned instead of the |
| 1506 | * actual metatable. |
| 1507 | * |
| 1508 | * __tostring - Control string representation. When the builtin |
| 1509 | * "tostring( myTable )" function is called, if the metatable |
| 1510 | * for myTable has a __tostring property set to a function, |
| 1511 | * that function is invoked (passing myTable to it) and the |
| 1512 | * return value is used as the string representation. |
| 1513 | * |
| 1514 | * __len - Control table length. When the table length is requested using |
| 1515 | * the length operator ( '#' ), if the metatable for myTable has |
| 1516 | * a __len key pointing to a function, that function is invoked |
| 1517 | * (passing myTable to it) and the return value used as the value |
| 1518 | * of "#myTable". |
| 1519 | * |
| 1520 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1521 | * collected, if the metatable has a __gc field pointing to a |
| 1522 | * function, that function is first invoked, passing the userdata |
| 1523 | * to it. The __gc metamethod is not called for tables. |
| 1524 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1525 | * |
| 1526 | * Special metamethods for redefining standard operators: |
| 1527 | * http://www.lua.org/pil/13.1.html |
| 1528 | * |
| 1529 | * __add "+" |
| 1530 | * __sub "-" |
| 1531 | * __mul "*" |
| 1532 | * __div "/" |
| 1533 | * __unm "!" |
| 1534 | * __pow "^" |
| 1535 | * __concat ".." |
| 1536 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1537 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1538 | * http://www.lua.org/pil/13.2.html |
| 1539 | * |
| 1540 | * __eq "==" |
| 1541 | * __lt "<" |
| 1542 | * __le "<=" |
| 1543 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1544 | |
| 1545 | /* |
| 1546 | * |
| 1547 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1548 | * Class Map |
| 1549 | * |
| 1550 | * |
| 1551 | */ |
| 1552 | |
| 1553 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1554 | * a class session, otherwise it throws an error. |
| 1555 | */ |
| 1556 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1557 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1558 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | /* This function is the map constructor. It don't need |
| 1562 | * the class Map object. It creates and return a new Map |
| 1563 | * object. It must be called only during "body" or "init" |
| 1564 | * context because it process some filesystem accesses. |
| 1565 | */ |
| 1566 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1567 | { |
| 1568 | const char *fn; |
| 1569 | int match = PAT_MATCH_STR; |
| 1570 | struct sample_conv conv; |
| 1571 | const char *file = ""; |
| 1572 | int line = 0; |
| 1573 | lua_Debug ar; |
| 1574 | char *err = NULL; |
| 1575 | struct arg args[2]; |
| 1576 | |
| 1577 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1578 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1579 | |
| 1580 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1581 | |
| 1582 | if (lua_gettop(L) >= 2) { |
| 1583 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1584 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1585 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1586 | } |
| 1587 | |
| 1588 | /* Get Lua filename and line number. */ |
| 1589 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1590 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1591 | if (ar.currentline > 0) { /* is there info? */ |
| 1592 | file = ar.short_src; |
| 1593 | line = ar.currentline; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | /* fill fake sample_conv struct. */ |
| 1598 | conv.kw = ""; /* unused. */ |
| 1599 | conv.process = NULL; /* unused. */ |
| 1600 | conv.arg_mask = 0; /* unused. */ |
| 1601 | conv.val_args = NULL; /* unused. */ |
| 1602 | conv.out_type = SMP_T_STR; |
| 1603 | conv.private = (void *)(long)match; |
| 1604 | switch (match) { |
| 1605 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1606 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1607 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1608 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1609 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1610 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1611 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1612 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1613 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1614 | default: |
| 1615 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1616 | } |
| 1617 | |
| 1618 | /* fill fake args. */ |
| 1619 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1620 | args[0].data.str.area = strdup(fn); |
| 1621 | args[0].data.str.data = strlen(fn); |
| 1622 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1623 | args[1].type = ARGT_STOP; |
| 1624 | |
| 1625 | /* load the map. */ |
| 1626 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1627 | /* error case: we cant use luaL_error because we must |
| 1628 | * free the err variable. |
| 1629 | */ |
| 1630 | luaL_where(L, 1); |
| 1631 | lua_pushfstring(L, "'new': %s.", err); |
| 1632 | lua_concat(L, 2); |
| 1633 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1634 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1635 | WILL_LJMP(lua_error(L)); |
| 1636 | } |
| 1637 | |
| 1638 | /* create the lua object. */ |
| 1639 | lua_newtable(L); |
| 1640 | lua_pushlightuserdata(L, args[0].data.map); |
| 1641 | lua_rawseti(L, -2, 0); |
| 1642 | |
| 1643 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1644 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1645 | lua_setmetatable(L, -2); |
| 1646 | |
| 1647 | |
| 1648 | return 1; |
| 1649 | } |
| 1650 | |
| 1651 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1652 | { |
| 1653 | struct map_descriptor *desc; |
| 1654 | struct pattern *pat; |
| 1655 | struct sample smp; |
| 1656 | |
| 1657 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1658 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1659 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1660 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1661 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1662 | } |
| 1663 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1664 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1665 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1666 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry Fournier | 66e9ff5 | 2020-11-10 20:38:20 +0100 | [diff] [blame] | 1667 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1671 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1672 | if (str) |
| 1673 | lua_pushstring(L, ""); |
| 1674 | else |
| 1675 | lua_pushnil(L); |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
| 1679 | /* The Lua pattern must return a string, so we can't check the returned type */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1680 | lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1681 | return 1; |
| 1682 | } |
| 1683 | |
| 1684 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1685 | { |
| 1686 | return _hlua_map_lookup(L, 0); |
| 1687 | } |
| 1688 | |
| 1689 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1690 | { |
| 1691 | return _hlua_map_lookup(L, 1); |
| 1692 | } |
| 1693 | |
| 1694 | /* |
| 1695 | * |
| 1696 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | * Class Socket |
| 1698 | * |
| 1699 | * |
| 1700 | */ |
| 1701 | |
| 1702 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1703 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1704 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /* This function is the handler called for each I/O on the established |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1708 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1709 | * received. |
| 1710 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1711 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1713 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1715 | if (appctx->ctx.hlua_cosocket.die) { |
| 1716 | si_shutw(si); |
| 1717 | si_shutr(si); |
| 1718 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1719 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1720 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1721 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1722 | } |
| 1723 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1724 | /* If we cant write, wakeup the pending write signals. */ |
| 1725 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1726 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1727 | |
| 1728 | /* If we cant read, wakeup the pending read signals. */ |
| 1729 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1730 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1731 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1732 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1733 | * to be notified whenever the connection completes. |
| 1734 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1735 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1736 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1737 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1738 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1739 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1740 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1741 | |
| 1742 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1743 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1744 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1745 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1746 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1747 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1748 | |
| 1749 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1750 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1751 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1752 | |
| 1753 | /* Some data were injected in the buffer, notify the stream |
| 1754 | * interface. |
| 1755 | */ |
| 1756 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1757 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1758 | |
| 1759 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1760 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1761 | */ |
| 1762 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1763 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1764 | } |
| 1765 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1766 | /* This function is called when the "struct stream" is destroyed. |
| 1767 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1768 | * Wake all the pending signals. |
| 1769 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1770 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1771 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1772 | struct xref *peer; |
| 1773 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1774 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1775 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1776 | if (peer) |
| 1777 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1780 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1781 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1785 | * uses this object. If the stream does not exists, just quit. |
| 1786 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1787 | * pending signal can rest in the read and write lists. destroy |
| 1788 | * it. |
| 1789 | */ |
| 1790 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1791 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1792 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1794 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1796 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1797 | |
| 1798 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1799 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1800 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1801 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1802 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1803 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1804 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1805 | appctx->ctx.hlua_cosocket.die = 1; |
| 1806 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1807 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1808 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1809 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1814 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1816 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1818 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1820 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1821 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1823 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1824 | |
| 1825 | /* Check if we run on the same thread than the xreator thread. |
| 1826 | * We cannot access to the socket if the thread is different. |
| 1827 | */ |
| 1828 | if (socket->tid != tid) |
| 1829 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1830 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1831 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1832 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1834 | |
| 1835 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1836 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1838 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1839 | appctx->ctx.hlua_cosocket.die = 1; |
| 1840 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1842 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1843 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1844 | return 0; |
| 1845 | } |
| 1846 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1847 | /* The close function calls close_helper. |
| 1848 | */ |
| 1849 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1850 | { |
| 1851 | MAY_LJMP(check_args(L, 1, "close")); |
| 1852 | return hlua_socket_close_helper(L); |
| 1853 | } |
| 1854 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | /* This Lua function assumes that the stack contain three parameters. |
| 1856 | * 1 - USERDATA containing a struct socket |
| 1857 | * 2 - INTEGER with values of the macro defined below |
| 1858 | * If the integer is -1, we must read at most one line. |
| 1859 | * If the integer is -2, we ust read all the data until the |
| 1860 | * end of the stream. |
| 1861 | * If the integer is positive value, we must read a number of |
| 1862 | * bytes corresponding to this value. |
| 1863 | */ |
| 1864 | #define HLSR_READ_LINE (-1) |
| 1865 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1866 | __LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1867 | { |
| 1868 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1869 | int wanted = lua_tointeger(L, 2); |
| 1870 | struct hlua *hlua = hlua_gethlua(L); |
| 1871 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1872 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1873 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1874 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1875 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1876 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1877 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1878 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1879 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1880 | struct stream_interface *si; |
| 1881 | struct stream *s; |
| 1882 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1883 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | |
| 1885 | /* Check if this lua stack is schedulable. */ |
| 1886 | if (!hlua || !hlua->task) |
| 1887 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1888 | "'frontend', 'backend' or 'task'")); |
| 1889 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1890 | /* Check if we run on the same thread than the xreator thread. |
| 1891 | * We cannot access to the socket if the thread is different. |
| 1892 | */ |
| 1893 | if (socket->tid != tid) |
| 1894 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1895 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1896 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1897 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1898 | if (!peer) |
| 1899 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1900 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1901 | si = appctx->owner; |
| 1902 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1904 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1907 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | if (nblk < 0) /* Connection close. */ |
| 1909 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1910 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1912 | |
| 1913 | /* remove final \r\n. */ |
| 1914 | if (nblk == 1) { |
| 1915 | if (blk1[len1-1] == '\n') { |
| 1916 | len1--; |
| 1917 | skip_at_end++; |
| 1918 | if (blk1[len1-1] == '\r') { |
| 1919 | len1--; |
| 1920 | skip_at_end++; |
| 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | else { |
| 1925 | if (blk2[len2-1] == '\n') { |
| 1926 | len2--; |
| 1927 | skip_at_end++; |
| 1928 | if (blk2[len2-1] == '\r') { |
| 1929 | len2--; |
| 1930 | skip_at_end++; |
| 1931 | } |
| 1932 | } |
| 1933 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1938 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1939 | if (nblk < 0) /* Connection close. */ |
| 1940 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1941 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1942 | goto connection_empty; |
| 1943 | } |
| 1944 | |
| 1945 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1946 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1947 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1948 | if (nblk < 0) /* Connection close. */ |
| 1949 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1950 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1951 | goto connection_empty; |
| 1952 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1953 | missing_bytes = wanted - socket->b.n; |
| 1954 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1956 | len1 = missing_bytes; |
| 1957 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1958 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | len = len1; |
| 1962 | |
| 1963 | luaL_addlstring(&socket->b, blk1, len1); |
| 1964 | if (nblk == 2) { |
| 1965 | len += len2; |
| 1966 | luaL_addlstring(&socket->b, blk2, len2); |
| 1967 | } |
| 1968 | |
| 1969 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1970 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | |
| 1972 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1973 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* If the pattern reclaim to read all the data |
| 1976 | * in the connection, got out. |
| 1977 | */ |
| 1978 | if (wanted == HLSR_READ_ALL) |
| 1979 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1980 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | goto connection_empty; |
| 1982 | |
| 1983 | /* Return result. */ |
| 1984 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1985 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | return 1; |
| 1987 | |
| 1988 | connection_closed: |
| 1989 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1990 | xref_unlock(&socket->xref, peer); |
| 1991 | |
| 1992 | no_peer: |
| 1993 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1994 | /* If the buffer containds data. */ |
| 1995 | if (socket->b.n > 0) { |
| 1996 | luaL_pushresult(&socket->b); |
| 1997 | return 1; |
| 1998 | } |
| 1999 | lua_pushnil(L); |
| 2000 | lua_pushstring(L, "connection closed."); |
| 2001 | return 2; |
| 2002 | |
| 2003 | connection_empty: |
| 2004 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2005 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2006 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2007 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2008 | } |
| 2009 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2010 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | return 0; |
| 2012 | } |
| 2013 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2014 | /* This Lua function gets two parameters. The first one can be string |
| 2015 | * or a number. If the string is "*l", the user requires one line. If |
| 2016 | * the string is "*a", the user requires all the contents of the stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2017 | * If the value is a number, the user require a number of bytes equal |
| 2018 | * to the value. The default value is "*l" (a line). |
| 2019 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2020 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | * integer takes this values: |
| 2022 | * -1 : read a line |
| 2023 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2024 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2025 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2026 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | * concatenated with the read data. |
| 2028 | */ |
| 2029 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2030 | { |
| 2031 | int wanted = HLSR_READ_LINE; |
| 2032 | const char *pattern; |
Christopher Faulet | af3d8c1 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2033 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2034 | char *error; |
| 2035 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2036 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | |
| 2038 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2039 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2040 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2041 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2043 | /* Check if we run on the same thread than the xreator thread. |
| 2044 | * We cannot access to the socket if the thread is different. |
| 2045 | */ |
| 2046 | if (socket->tid != tid) |
| 2047 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2048 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | /* check for pattern. */ |
| 2050 | if (lua_gettop(L) >= 2) { |
| 2051 | type = lua_type(L, 2); |
| 2052 | if (type == LUA_TSTRING) { |
| 2053 | pattern = lua_tostring(L, 2); |
| 2054 | if (strcmp(pattern, "*a") == 0) |
| 2055 | wanted = HLSR_READ_ALL; |
| 2056 | else if (strcmp(pattern, "*l") == 0) |
| 2057 | wanted = HLSR_READ_LINE; |
| 2058 | else { |
| 2059 | wanted = strtoll(pattern, &error, 10); |
| 2060 | if (*error != '\0') |
| 2061 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2062 | } |
| 2063 | } |
| 2064 | else if (type == LUA_TNUMBER) { |
| 2065 | wanted = lua_tointeger(L, 2); |
| 2066 | if (wanted < 0) |
| 2067 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | /* Set pattern. */ |
| 2072 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* Check if we would replace the top by itself. */ |
| 2075 | if (lua_gettop(L) != 2) |
| 2076 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | |
Christopher Faulet | af3d8c1 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2078 | /* Save index of the top of the stack because since buffers are used, it |
| 2079 | * may change |
| 2080 | */ |
| 2081 | lastarg = lua_gettop(L); |
| 2082 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2083 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | luaL_buffinit(L, &socket->b); |
| 2085 | |
| 2086 | /* Check prefix. */ |
Christopher Faulet | af3d8c1 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2087 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2089 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2090 | pattern = lua_tolstring(L, 3, &len); |
| 2091 | luaL_addlstring(&socket->b, pattern, len); |
| 2092 | } |
| 2093 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2094 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2098 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2099 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2100 | 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] | 2101 | { |
| 2102 | struct hlua_socket *socket; |
| 2103 | struct hlua *hlua = hlua_gethlua(L); |
| 2104 | struct appctx *appctx; |
| 2105 | size_t buf_len; |
| 2106 | const char *buf; |
| 2107 | int len; |
| 2108 | int send_len; |
| 2109 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2110 | struct xref *peer; |
| 2111 | struct stream_interface *si; |
| 2112 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | |
| 2114 | /* Check if this lua stack is schedulable. */ |
| 2115 | if (!hlua || !hlua->task) |
| 2116 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2117 | "'frontend', 'backend' or 'task'")); |
| 2118 | |
| 2119 | /* Get object */ |
| 2120 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2121 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2122 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2124 | /* Check if we run on the same thread than the xreator thread. |
| 2125 | * We cannot access to the socket if the thread is different. |
| 2126 | */ |
| 2127 | if (socket->tid != tid) |
| 2128 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2129 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2130 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2131 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2132 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2133 | lua_pushinteger(L, -1); |
| 2134 | return 1; |
| 2135 | } |
| 2136 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2137 | si = appctx->owner; |
| 2138 | s = si_strm(si); |
| 2139 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2141 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2142 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2143 | lua_pushinteger(L, -1); |
| 2144 | return 1; |
| 2145 | } |
| 2146 | |
| 2147 | /* Update the input buffer data. */ |
| 2148 | buf += sent; |
| 2149 | send_len = buf_len - sent; |
| 2150 | |
| 2151 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2152 | if (sent >= buf_len) { |
| 2153 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2154 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2155 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2156 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2157 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2158 | * the request buffer if its not required. |
| 2159 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2160 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2161 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2162 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2163 | } |
| 2164 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2165 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2166 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2167 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2169 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | |
| 2171 | /* send data */ |
| 2172 | if (len < send_len) |
| 2173 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2174 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | |
| 2176 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2177 | * the data" (-2) are not expected because the available length |
| 2178 | * is tested. |
| 2179 | * Other unknown error are also not expected. |
| 2180 | */ |
| 2181 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2182 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2183 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2184 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2185 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2187 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2188 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2189 | return 1; |
| 2190 | } |
| 2191 | |
| 2192 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2193 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2194 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2195 | s->req.rex = TICK_ETERNITY; |
| 2196 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | |
| 2198 | /* Update length sent. */ |
| 2199 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2200 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2201 | |
| 2202 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2203 | if (sent + len >= buf_len) { |
| 2204 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2205 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2206 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2207 | |
| 2208 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2209 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2210 | xref_unlock(&socket->xref, peer); |
| 2211 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2212 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2213 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2214 | 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] | 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | /* This function initiate the send of data. It just check the input |
| 2219 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2220 | * 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] | 2221 | * "hlua_socket_write_yield" that can yield. |
| 2222 | * |
| 2223 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2224 | * the associated object. The second is a string buffer. The third is |
| 2225 | * a facultative integer that represents where is the buffer position |
| 2226 | * of the start of the data that can send. The first byte is the |
| 2227 | * position "1". The default value is "1". The fourth argument is a |
| 2228 | * facultative integer that represents where is the buffer position |
| 2229 | * of the end of the data that can send. The default is the last byte. |
| 2230 | */ |
| 2231 | static int hlua_socket_send(struct lua_State *L) |
| 2232 | { |
| 2233 | int i; |
| 2234 | int j; |
| 2235 | const char *buf; |
| 2236 | size_t buf_len; |
| 2237 | |
| 2238 | /* Check number of arguments. */ |
| 2239 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2240 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2241 | |
| 2242 | /* Get the string. */ |
| 2243 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2244 | |
| 2245 | /* Get and check j. */ |
| 2246 | if (lua_gettop(L) == 4) { |
| 2247 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2248 | if (j < 0) |
| 2249 | j = buf_len + j + 1; |
| 2250 | if (j > buf_len) |
| 2251 | j = buf_len + 1; |
| 2252 | lua_pop(L, 1); |
| 2253 | } |
| 2254 | else |
| 2255 | j = buf_len; |
| 2256 | |
| 2257 | /* Get and check i. */ |
| 2258 | if (lua_gettop(L) == 3) { |
| 2259 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2260 | if (i < 0) |
| 2261 | i = buf_len + i + 1; |
| 2262 | if (i > buf_len) |
| 2263 | i = buf_len + 1; |
| 2264 | lua_pop(L, 1); |
| 2265 | } else |
| 2266 | i = 1; |
| 2267 | |
| 2268 | /* Check bth i and j. */ |
| 2269 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2270 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | return 1; |
| 2272 | } |
| 2273 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2274 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | return 1; |
| 2276 | } |
| 2277 | if (i == 0) |
| 2278 | i = 1; |
| 2279 | if (j == 0) |
| 2280 | j = 1; |
| 2281 | |
| 2282 | /* Pop the string. */ |
| 2283 | lua_pop(L, 1); |
| 2284 | |
| 2285 | /* Update the buffer length. */ |
| 2286 | buf += i - 1; |
| 2287 | buf_len = j - i + 1; |
| 2288 | lua_pushlstring(L, buf, buf_len); |
| 2289 | |
| 2290 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2291 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2293 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | } |
| 2295 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2296 | #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] | 2297 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2298 | { |
| 2299 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2300 | int ret; |
| 2301 | int len; |
| 2302 | char *p; |
| 2303 | |
| 2304 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2305 | if (ret <= 0) { |
| 2306 | lua_pushnil(L); |
| 2307 | return 1; |
| 2308 | } |
| 2309 | |
| 2310 | if (ret == AF_UNIX) { |
| 2311 | lua_pushstring(L, buffer+1); |
| 2312 | return 1; |
| 2313 | } |
| 2314 | else if (ret == AF_INET6) { |
| 2315 | buffer[0] = '['; |
| 2316 | len = strlen(buffer); |
| 2317 | buffer[len] = ']'; |
| 2318 | len++; |
| 2319 | buffer[len] = ':'; |
| 2320 | len++; |
| 2321 | p = buffer; |
| 2322 | } |
| 2323 | else if (ret == AF_INET) { |
| 2324 | p = buffer + 1; |
| 2325 | len = strlen(p); |
| 2326 | p[len] = ':'; |
| 2327 | len++; |
| 2328 | } |
| 2329 | else { |
| 2330 | lua_pushnil(L); |
| 2331 | return 1; |
| 2332 | } |
| 2333 | |
| 2334 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2335 | lua_pushnil(L); |
| 2336 | return 1; |
| 2337 | } |
| 2338 | |
| 2339 | lua_pushstring(L, p); |
| 2340 | return 1; |
| 2341 | } |
| 2342 | |
| 2343 | /* Returns information about the peer of the connection. */ |
| 2344 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2345 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2346 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | struct xref *peer; |
| 2348 | struct appctx *appctx; |
| 2349 | struct stream_interface *si; |
| 2350 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2351 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2352 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2353 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2354 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2355 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2357 | /* Check if we run on the same thread than the xreator thread. |
| 2358 | * We cannot access to the socket if the thread is different. |
| 2359 | */ |
| 2360 | if (socket->tid != tid) |
| 2361 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2362 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2363 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2364 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2365 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | lua_pushnil(L); |
| 2367 | return 1; |
| 2368 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2369 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2370 | si = appctx->owner; |
| 2371 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2373 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2374 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2375 | lua_pushnil(L); |
| 2376 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2379 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2380 | xref_unlock(&socket->xref, peer); |
| 2381 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | /* Returns information about my connection side. */ |
| 2385 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2386 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2387 | struct hlua_socket *socket; |
| 2388 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2389 | struct appctx *appctx; |
| 2390 | struct xref *peer; |
| 2391 | struct stream_interface *si; |
| 2392 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2393 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2394 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2396 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2397 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2398 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2399 | /* Check if we run on the same thread than the xreator thread. |
| 2400 | * We cannot access to the socket if the thread is different. |
| 2401 | */ |
| 2402 | if (socket->tid != tid) |
| 2403 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2404 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2405 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2406 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2407 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2408 | lua_pushnil(L); |
| 2409 | return 1; |
| 2410 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2411 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2412 | si = appctx->owner; |
| 2413 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2415 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2416 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2417 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | lua_pushnil(L); |
| 2419 | return 1; |
| 2420 | } |
| 2421 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2422 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2423 | xref_unlock(&socket->xref, peer); |
| 2424 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2428 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2429 | .obj_type = OBJ_TYPE_APPLET, |
| 2430 | .name = "<LUA_TCP>", |
| 2431 | .fct = hlua_socket_handler, |
| 2432 | .release = hlua_socket_release, |
| 2433 | }; |
| 2434 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2435 | __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] | 2436 | { |
| 2437 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2438 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2439 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2440 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2441 | struct stream_interface *si; |
| 2442 | struct stream *s; |
| 2443 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2444 | /* Check if we run on the same thread than the xreator thread. |
| 2445 | * We cannot access to the socket if the thread is different. |
| 2446 | */ |
| 2447 | if (socket->tid != tid) |
| 2448 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2449 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2450 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2451 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2452 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2453 | lua_pushnil(L); |
| 2454 | lua_pushstring(L, "Can't connect"); |
| 2455 | return 2; |
| 2456 | } |
| 2457 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2458 | si = appctx->owner; |
| 2459 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2461 | /* Check if we run on the same thread than the xreator thread. |
| 2462 | * We cannot access to the socket if the thread is different. |
| 2463 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2464 | if (socket->tid != tid) { |
| 2465 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2466 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2467 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2468 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2470 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2471 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | lua_pushnil(L); |
| 2473 | lua_pushstring(L, "Can't connect"); |
| 2474 | return 2; |
| 2475 | } |
| 2476 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2477 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2478 | |
| 2479 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2480 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2481 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | lua_pushinteger(L, 1); |
| 2483 | return 1; |
| 2484 | } |
| 2485 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2486 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2487 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2489 | } |
| 2490 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2491 | 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] | 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | /* This function fail or initite the connection. */ |
| 2496 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2497 | { |
| 2498 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2499 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2501 | struct hlua *hlua; |
| 2502 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2503 | int low, high; |
| 2504 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2505 | struct xref *peer; |
| 2506 | struct stream_interface *si; |
| 2507 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2509 | if (lua_gettop(L) < 2) |
| 2510 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2511 | |
| 2512 | /* Get args. */ |
| 2513 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2514 | |
| 2515 | /* Check if we run on the same thread than the xreator thread. |
| 2516 | * We cannot access to the socket if the thread is different. |
| 2517 | */ |
| 2518 | if (socket->tid != tid) |
| 2519 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2520 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2521 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2522 | if (lua_gettop(L) >= 3) { |
| 2523 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2524 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2526 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2527 | * that are not enclosed within square brackets. |
| 2528 | */ |
| 2529 | if (port > 0) { |
| 2530 | luaL_buffinit(L, &b); |
| 2531 | luaL_addstring(&b, ip); |
| 2532 | luaL_addchar(&b, ':'); |
| 2533 | luaL_pushresult(&b); |
| 2534 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2535 | } |
| 2536 | } |
| 2537 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2538 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2539 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2540 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2541 | lua_pushnil(L); |
| 2542 | return 1; |
| 2543 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2544 | |
| 2545 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2546 | 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] | 2547 | if (!addr) { |
| 2548 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2549 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2550 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2551 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2552 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2553 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2554 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2555 | if (port == -1) { |
| 2556 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2557 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2558 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2559 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2560 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2561 | if (port == -1) { |
| 2562 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2563 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2564 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2565 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2566 | } |
| 2567 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2568 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2569 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2570 | si = appctx->owner; |
| 2571 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2572 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2573 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2574 | xref_unlock(&socket->xref, peer); |
| 2575 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2576 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2577 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2578 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2579 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2580 | |
| 2581 | /* inform the stream that we want to be notified whenever the |
| 2582 | * connection completes. |
| 2583 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2584 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2585 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2586 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2587 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2588 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2589 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2590 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2591 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2592 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2593 | } |
| 2594 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2595 | |
| 2596 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2597 | /* Return yield waiting for connection. */ |
| 2598 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2599 | 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] | 2600 | |
| 2601 | return 0; |
| 2602 | } |
| 2603 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2604 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2605 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2606 | { |
| 2607 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2608 | struct xref *peer; |
| 2609 | struct appctx *appctx; |
| 2610 | struct stream_interface *si; |
| 2611 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | |
| 2613 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2614 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2615 | |
| 2616 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2617 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2618 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2619 | lua_pushnil(L); |
| 2620 | return 1; |
| 2621 | } |
| 2622 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2623 | si = appctx->owner; |
| 2624 | s = si_strm(si); |
| 2625 | |
| 2626 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2627 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2628 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2629 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2630 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | |
| 2632 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2633 | { |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
| 2637 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2638 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2639 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2640 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2641 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2642 | struct xref *peer; |
| 2643 | struct appctx *appctx; |
| 2644 | struct stream_interface *si; |
| 2645 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2646 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2648 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2649 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2650 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2651 | /* convert the timeout to millis */ |
| 2652 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2654 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2655 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2656 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2657 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2658 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2659 | 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] | 2660 | |
| 2661 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2662 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2663 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2664 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2665 | /* Check if we run on the same thread than the xreator thread. |
| 2666 | * We cannot access to the socket if the thread is different. |
| 2667 | */ |
| 2668 | if (socket->tid != tid) |
| 2669 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2670 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2671 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2672 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2673 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2674 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2675 | WILL_LJMP(lua_error(L)); |
| 2676 | return 0; |
| 2677 | } |
| 2678 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2679 | si = appctx->owner; |
| 2680 | s = si_strm(si); |
| 2681 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2682 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2683 | s->req.rto = tmout; |
| 2684 | s->req.wto = tmout; |
| 2685 | s->res.rto = tmout; |
| 2686 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2687 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2688 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2689 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2690 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2691 | |
| 2692 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2693 | task_queue(s->task); |
| 2694 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2695 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2697 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2698 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2702 | { |
| 2703 | struct hlua_socket *socket; |
| 2704 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2705 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2706 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | |
| 2708 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2709 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | hlua_pusherror(L, "socket: full stack"); |
| 2711 | goto out_fail_conf; |
| 2712 | } |
| 2713 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2714 | /* Create the object: obj[0] = userdata. */ |
| 2715 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2716 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2717 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2718 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2719 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2720 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2721 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2722 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2723 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2724 | goto out_fail_conf; |
| 2725 | } |
| 2726 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2727 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2728 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2729 | lua_setmetatable(L, -2); |
| 2730 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2731 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2732 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2733 | if (!appctx) { |
| 2734 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2735 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2736 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2737 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2738 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2739 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2740 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2741 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2742 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2743 | /* Now create a session, task and stream for this applet */ |
| 2744 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2745 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2746 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2747 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2750 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2751 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2752 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2753 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2754 | } |
| 2755 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2756 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2757 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2758 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2759 | /* Configure "right" stream interface. this "si" is used to connect |
| 2760 | * and retrieve data from the server. The connection is initialized |
| 2761 | * with the "struct server". |
| 2762 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2763 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2764 | |
| 2765 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2766 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2767 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2768 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2769 | return 1; |
| 2770 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2771 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2772 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2773 | out_fail_sess: |
| 2774 | appctx_free(appctx); |
| 2775 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2776 | WILL_LJMP(lua_error(L)); |
| 2777 | return 0; |
| 2778 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2779 | |
| 2780 | /* |
| 2781 | * |
| 2782 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2783 | * Class Channel |
| 2784 | * |
| 2785 | * |
| 2786 | */ |
| 2787 | |
| 2788 | /* Returns the struct hlua_channel join to the class channel in the |
| 2789 | * stack entry "ud" or throws an argument error. |
| 2790 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2791 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2793 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2794 | } |
| 2795 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2796 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2797 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2798 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2799 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2801 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2802 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2803 | return 0; |
| 2804 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2805 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2806 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2807 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2808 | |
| 2809 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2810 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2811 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2812 | return 1; |
| 2813 | } |
| 2814 | |
| 2815 | /* Duplicate all the data present in the input channel and put it |
| 2816 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2817 | * the stack if the channel is closed and all the data are consumed, |
| 2818 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2819 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2820 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2821 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2822 | { |
| 2823 | char *blk1; |
| 2824 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2825 | size_t len1; |
| 2826 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2827 | int ret; |
| 2828 | luaL_Buffer b; |
| 2829 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2830 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2831 | if (unlikely(ret == 0)) |
| 2832 | return 0; |
| 2833 | |
| 2834 | if (unlikely(ret < 0)) { |
| 2835 | lua_pushnil(L); |
| 2836 | return -1; |
| 2837 | } |
| 2838 | |
| 2839 | luaL_buffinit(L, &b); |
| 2840 | luaL_addlstring(&b, blk1, len1); |
| 2841 | if (unlikely(ret == 2)) |
| 2842 | luaL_addlstring(&b, blk2, len2); |
| 2843 | luaL_pushresult(&b); |
| 2844 | |
| 2845 | if (unlikely(ret == 2)) |
| 2846 | return len1 + len2; |
| 2847 | return len1; |
| 2848 | } |
| 2849 | |
| 2850 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2851 | * a yield. This function keep the data in the buffer. |
| 2852 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2853 | __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] | 2854 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2855 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2856 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2857 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2858 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2859 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2860 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2861 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2862 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2863 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2864 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2865 | 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] | 2866 | return 1; |
| 2867 | } |
| 2868 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2869 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2870 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2871 | { |
| 2872 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2873 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2874 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2875 | } |
| 2876 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2877 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2878 | * a yield. This function consumes the data in the buffer. It returns |
| 2879 | * a string containing the data or a nil pointer if no data are available |
| 2880 | * and the channel is closed. |
| 2881 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2882 | __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] | 2883 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2884 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2885 | int ret; |
| 2886 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2887 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2888 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2889 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2890 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2891 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2892 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2893 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2894 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2895 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2896 | 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] | 2897 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | if (unlikely(ret == -1)) |
| 2899 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2901 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2902 | return 1; |
| 2903 | } |
| 2904 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2905 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2906 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2907 | { |
| 2908 | MAY_LJMP(check_args(L, 1, "get")); |
| 2909 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2910 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2911 | } |
| 2912 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2913 | /* This functions consumes and returns one line. If the channel is closed, |
| 2914 | * 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] | 2915 | * 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] | 2916 | * value. |
| 2917 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2918 | __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] | 2919 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | char *blk1; |
| 2921 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2922 | size_t len1; |
| 2923 | size_t len2; |
| 2924 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2925 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2926 | int ret; |
| 2927 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2928 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2929 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2930 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2931 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2932 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2933 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2934 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2935 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2936 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2938 | 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] | 2939 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2940 | if (ret == -1) { |
| 2941 | lua_pushnil(L); |
| 2942 | return 1; |
| 2943 | } |
| 2944 | |
| 2945 | luaL_buffinit(L, &b); |
| 2946 | luaL_addlstring(&b, blk1, len1); |
| 2947 | len = len1; |
| 2948 | if (unlikely(ret == 2)) { |
| 2949 | luaL_addlstring(&b, blk2, len2); |
| 2950 | len += len2; |
| 2951 | } |
| 2952 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2953 | 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] | 2954 | return 1; |
| 2955 | } |
| 2956 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2957 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2958 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2959 | { |
| 2960 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2961 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2962 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2963 | } |
| 2964 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2965 | /* This function takes a string as input, and append it at the |
| 2966 | * input side of channel. If the data is too big, but a space |
| 2967 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2968 | * yields. If the data is bigger than the buffer, or if the |
| 2969 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2970 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2971 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2972 | __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] | 2973 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2974 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | size_t len; |
| 2976 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2977 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2978 | int ret; |
| 2979 | int max; |
| 2980 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2981 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2982 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2983 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2984 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2985 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2986 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2987 | * the request buffer if its not required. |
| 2988 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2989 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2990 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2991 | 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] | 2992 | } |
| 2993 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2994 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | if (max > len - l) |
| 2996 | max = len - l; |
| 2997 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2998 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | if (ret == -2 || ret == -3) { |
| 3000 | lua_pushinteger(L, -1); |
| 3001 | return 1; |
| 3002 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3003 | if (ret == -1) { |
| 3004 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3005 | 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] | 3006 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | l += ret; |
| 3008 | lua_pop(L, 1); |
| 3009 | lua_pushinteger(L, l); |
| 3010 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3011 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3012 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3013 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3015 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | */ |
| 3017 | return 1; |
| 3018 | } |
| 3019 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3020 | 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] | 3021 | return 1; |
| 3022 | } |
| 3023 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3024 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3025 | * 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] | 3026 | * buffer size is too little for the data. |
| 3027 | */ |
| 3028 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3029 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3030 | size_t len; |
| 3031 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3032 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3033 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3034 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3035 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3036 | lua_pushinteger(L, 0); |
| 3037 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3038 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3039 | } |
| 3040 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3041 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3043 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3044 | * or -1 if the channel is closed or if the buffer size is too |
| 3045 | * little for the data. |
| 3046 | */ |
| 3047 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3048 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3049 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3050 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3051 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3052 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | lua_pushinteger(L, 0); |
| 3054 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3055 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3056 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3057 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3058 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3059 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3060 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3062 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | } |
| 3064 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3065 | /* Append data in the output side of the buffer. This data is immediately |
| 3066 | * sent. The function returns the amount of data written. If the buffer |
| 3067 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | * if the channel is closed. |
| 3069 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3070 | __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] | 3071 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3072 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | size_t len; |
| 3074 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3075 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3076 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3077 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3078 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3079 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3080 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3081 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3082 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3083 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3084 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | lua_pushinteger(L, -1); |
| 3086 | return 1; |
| 3087 | } |
| 3088 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3089 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3090 | * the request buffer if its not required. |
| 3091 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3092 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3093 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3094 | 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] | 3095 | } |
| 3096 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3097 | /* The written data will be immediately sent, so we can check |
| 3098 | * the available space without taking in account the reserve. |
| 3099 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3100 | * data, because the buffer will be flushed. |
| 3101 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3102 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3103 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3104 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3105 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3106 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3107 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3108 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3109 | return 1; |
| 3110 | |
| 3111 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | if (max > len - l) |
| 3113 | max = len - l; |
| 3114 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3115 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3116 | * detects a non contiguous buffer and realign it. |
| 3117 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3118 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3119 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3120 | |
| 3121 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3122 | 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] | 3123 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3124 | /* buffer replace considers that the input part is filled. |
| 3125 | * so, I must forward these new data in the output part. |
| 3126 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3127 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3128 | |
| 3129 | l += max; |
| 3130 | lua_pop(L, 1); |
| 3131 | lua_pushinteger(L, l); |
| 3132 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3133 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3134 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3135 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3136 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3137 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3138 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3139 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3140 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3141 | if (l < len) { |
| 3142 | /* If we are waiting for space in the response buffer, we |
| 3143 | * must set the flag WAKERESWR. This flag required the task |
| 3144 | * wake up if any activity is detected on the response buffer. |
| 3145 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3146 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3147 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3148 | else |
| 3149 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3150 | 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] | 3151 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3152 | |
| 3153 | return 1; |
| 3154 | } |
| 3155 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3156 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3157 | * yield the LUA process, and resume it without checking the |
| 3158 | * input arguments. |
| 3159 | */ |
| 3160 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3161 | { |
| 3162 | MAY_LJMP(check_args(L, 2, "send")); |
| 3163 | lua_pushinteger(L, 0); |
| 3164 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3165 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | /* This function forward and amount of butes. The data pass from |
| 3169 | * the input side of the buffer to the output side, and can be |
| 3170 | * forwarded. This function never fails. |
| 3171 | * |
| 3172 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3173 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3175 | __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] | 3176 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3177 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3178 | int len; |
| 3179 | int l; |
| 3180 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3181 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3182 | |
| 3183 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3184 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3185 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3186 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3187 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3188 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3189 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3190 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3191 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3192 | |
| 3193 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3194 | if (max > ci_data(chn)) |
| 3195 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3196 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3197 | l += max; |
| 3198 | |
| 3199 | lua_pop(L, 1); |
| 3200 | lua_pushinteger(L, l); |
| 3201 | |
| 3202 | /* Check if it miss bytes to forward. */ |
| 3203 | if (l < len) { |
| 3204 | /* The the input channel or the output channel are closed, we |
| 3205 | * must return the amount of data forwarded. |
| 3206 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3207 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3208 | return 1; |
| 3209 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3210 | /* If we are waiting for space data in the response buffer, we |
| 3211 | * must set the flag WAKERESWR. This flag required the task |
| 3212 | * wake up if any activity is detected on the response buffer. |
| 3213 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3214 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3215 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3216 | else |
| 3217 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3218 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3219 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3220 | 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] | 3221 | } |
| 3222 | |
| 3223 | return 1; |
| 3224 | } |
| 3225 | |
| 3226 | /* Just check the input and prepare the stack for the previous |
| 3227 | * function "hlua_channel_forward_yield" |
| 3228 | */ |
| 3229 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3230 | { |
| 3231 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3232 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3233 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3234 | |
| 3235 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3236 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | /* Just returns the number of bytes available in the input |
| 3240 | * side of the buffer. This function never fails. |
| 3241 | */ |
| 3242 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3243 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3244 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3245 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3246 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3247 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3248 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3249 | struct htx *htx = htxbuf(&chn->buf); |
| 3250 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3251 | } |
| 3252 | else |
| 3253 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3254 | return 1; |
| 3255 | } |
| 3256 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3257 | /* Returns true if the channel is full. */ |
| 3258 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3259 | { |
| 3260 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3261 | |
| 3262 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3263 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3264 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3265 | * applet). |
| 3266 | */ |
| 3267 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3268 | return 1; |
| 3269 | } |
| 3270 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3271 | /* Returns true if the channel is the response channel. */ |
| 3272 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3273 | { |
| 3274 | struct channel *chn; |
| 3275 | |
| 3276 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3277 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3278 | |
| 3279 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3280 | return 1; |
| 3281 | } |
| 3282 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3283 | /* Just returns the number of bytes available in the output |
| 3284 | * side of the buffer. This function never fails. |
| 3285 | */ |
| 3286 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3287 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3288 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3289 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3290 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3291 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3292 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3293 | return 1; |
| 3294 | } |
| 3295 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3296 | /* |
| 3297 | * |
| 3298 | * |
| 3299 | * Class Fetches |
| 3300 | * |
| 3301 | * |
| 3302 | */ |
| 3303 | |
| 3304 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3305 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3306 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3307 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3308 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3309 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | /* This function creates and push in the stack a fetch object according |
| 3313 | * with a current TXN. |
| 3314 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3315 | 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] | 3316 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3317 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3318 | |
| 3319 | /* Check stack size. */ |
| 3320 | if (!lua_checkstack(L, 3)) |
| 3321 | return 0; |
| 3322 | |
| 3323 | /* Create the object: obj[0] = userdata. |
| 3324 | * Note that the base of the Fetches object is the |
| 3325 | * transaction object. |
| 3326 | */ |
| 3327 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3328 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3329 | lua_rawseti(L, -2, 0); |
| 3330 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3331 | hsmp->s = txn->s; |
| 3332 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3333 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3334 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3335 | |
| 3336 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3337 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3338 | lua_setmetatable(L, -2); |
| 3339 | |
| 3340 | return 1; |
| 3341 | } |
| 3342 | |
| 3343 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3344 | * It uses closure argument to store the associated sample-fetch. It |
| 3345 | * returns only one argument or throws an error. An error is thrown |
| 3346 | * only if an error is encountered during the argument parsing. If |
| 3347 | * the "sample-fetch" function fails, nil is returned. |
| 3348 | */ |
| 3349 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3350 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3351 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3352 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3353 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3354 | int i; |
| 3355 | struct sample smp; |
| 3356 | |
| 3357 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3358 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3359 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3360 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3361 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3362 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3363 | /* Check execution authorization. */ |
| 3364 | if (f->use & SMP_USE_HTTP_ANY && |
| 3365 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3366 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3367 | "is not available in Lua services", f->kw); |
| 3368 | WILL_LJMP(lua_error(L)); |
| 3369 | } |
| 3370 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3371 | /* Get extra arguments. */ |
| 3372 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3373 | if (i >= ARGM_NBARGS) |
| 3374 | break; |
| 3375 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3376 | } |
| 3377 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3378 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3379 | |
| 3380 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3381 | 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] | 3382 | |
| 3383 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3384 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3385 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3386 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | /* Initialise the sample. */ |
| 3390 | memset(&smp, 0, sizeof(smp)); |
| 3391 | |
| 3392 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3393 | 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] | 3394 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3395 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3396 | lua_pushstring(L, ""); |
| 3397 | else |
| 3398 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3399 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3403 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3404 | hlua_smp2lua_str(L, &smp); |
| 3405 | else |
| 3406 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3407 | |
| 3408 | end: |
| 3409 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3410 | if (args[i].type == ARGT_STR) |
| 3411 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3412 | else if (args[i].type == ARGT_REG) |
| 3413 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3414 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3415 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3416 | |
| 3417 | error: |
| 3418 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3419 | if (args[i].type == ARGT_STR) |
| 3420 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3421 | else if (args[i].type == ARGT_REG) |
| 3422 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3423 | } |
| 3424 | WILL_LJMP(lua_error(L)); |
| 3425 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3426 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3427 | |
| 3428 | /* |
| 3429 | * |
| 3430 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3431 | * Class Converters |
| 3432 | * |
| 3433 | * |
| 3434 | */ |
| 3435 | |
| 3436 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3437 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3438 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3439 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3440 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3441 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | /* This function creates and push in the stack a Converters object |
| 3445 | * according with a current TXN. |
| 3446 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3447 | 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] | 3448 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3449 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3450 | |
| 3451 | /* Check stack size. */ |
| 3452 | if (!lua_checkstack(L, 3)) |
| 3453 | return 0; |
| 3454 | |
| 3455 | /* Create the object: obj[0] = userdata. |
| 3456 | * Note that the base of the Converters object is the |
| 3457 | * same than the TXN object. |
| 3458 | */ |
| 3459 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3460 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3461 | lua_rawseti(L, -2, 0); |
| 3462 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3463 | hsmp->s = txn->s; |
| 3464 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3465 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3466 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3467 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3468 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3469 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3470 | lua_setmetatable(L, -2); |
| 3471 | |
| 3472 | return 1; |
| 3473 | } |
| 3474 | |
| 3475 | /* This function is an LUA binding. It is called with each converter. |
| 3476 | * It uses closure argument to store the associated converter. It |
| 3477 | * returns only one argument or throws an error. An error is thrown |
| 3478 | * only if an error is encountered during the argument parsing. If |
| 3479 | * the converter function function fails, nil is returned. |
| 3480 | */ |
| 3481 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3482 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3483 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3484 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3485 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3486 | int i; |
| 3487 | struct sample smp; |
| 3488 | |
| 3489 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3490 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3491 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3492 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3493 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3494 | |
| 3495 | /* Get extra arguments. */ |
| 3496 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3497 | if (i >= ARGM_NBARGS) |
| 3498 | break; |
| 3499 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3500 | } |
| 3501 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3502 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3503 | |
| 3504 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3505 | 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] | 3506 | |
| 3507 | /* Run the special args checker. */ |
| 3508 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3509 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3510 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3514 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3515 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3516 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3517 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3518 | } |
| 3519 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3520 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3521 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3522 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3523 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3524 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3525 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3526 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3527 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3528 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3529 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3530 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3531 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3535 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3536 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3537 | lua_pushstring(L, ""); |
| 3538 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3539 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3540 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3544 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3545 | hlua_smp2lua_str(L, &smp); |
| 3546 | else |
| 3547 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3548 | end: |
| 3549 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3550 | if (args[i].type == ARGT_STR) |
| 3551 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3552 | else if (args[i].type == ARGT_REG) |
| 3553 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3554 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3555 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3556 | |
| 3557 | error: |
| 3558 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3559 | if (args[i].type == ARGT_STR) |
| 3560 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3561 | else if (args[i].type == ARGT_REG) |
| 3562 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3563 | } |
| 3564 | WILL_LJMP(lua_error(L)); |
| 3565 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3566 | } |
| 3567 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3568 | /* |
| 3569 | * |
| 3570 | * |
| 3571 | * Class AppletTCP |
| 3572 | * |
| 3573 | * |
| 3574 | */ |
| 3575 | |
| 3576 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3577 | * a class stream, otherwise it throws an error. |
| 3578 | */ |
| 3579 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3580 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3581 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | /* This function creates and push in the stack an Applet object |
| 3585 | * according with a current TXN. |
| 3586 | */ |
| 3587 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3588 | { |
| 3589 | struct hlua_appctx *appctx; |
| 3590 | struct stream_interface *si = ctx->owner; |
| 3591 | struct stream *s = si_strm(si); |
| 3592 | struct proxy *p = s->be; |
| 3593 | |
| 3594 | /* Check stack size. */ |
| 3595 | if (!lua_checkstack(L, 3)) |
| 3596 | return 0; |
| 3597 | |
| 3598 | /* Create the object: obj[0] = userdata. |
| 3599 | * Note that the base of the Converters object is the |
| 3600 | * same than the TXN object. |
| 3601 | */ |
| 3602 | lua_newtable(L); |
| 3603 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3604 | lua_rawseti(L, -2, 0); |
| 3605 | appctx->appctx = ctx; |
| 3606 | appctx->htxn.s = s; |
| 3607 | appctx->htxn.p = p; |
| 3608 | |
| 3609 | /* Create the "f" field that contains a list of fetches. */ |
| 3610 | lua_pushstring(L, "f"); |
| 3611 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3612 | return 0; |
| 3613 | lua_settable(L, -3); |
| 3614 | |
| 3615 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3616 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3617 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3618 | return 0; |
| 3619 | lua_settable(L, -3); |
| 3620 | |
| 3621 | /* Create the "c" field that contains a list of converters. */ |
| 3622 | lua_pushstring(L, "c"); |
| 3623 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3624 | return 0; |
| 3625 | lua_settable(L, -3); |
| 3626 | |
| 3627 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3628 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3629 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3630 | return 0; |
| 3631 | lua_settable(L, -3); |
| 3632 | |
| 3633 | /* Pop a class stream metatable and affect it to the table. */ |
| 3634 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3635 | lua_setmetatable(L, -2); |
| 3636 | |
| 3637 | return 1; |
| 3638 | } |
| 3639 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3640 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3641 | { |
| 3642 | struct hlua_appctx *appctx; |
| 3643 | struct stream *s; |
| 3644 | const char *name; |
| 3645 | size_t len; |
| 3646 | struct sample smp; |
| 3647 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3648 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3649 | 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] | 3650 | |
| 3651 | /* It is useles to retrieve the stream, but this function |
| 3652 | * runs only in a stream context. |
| 3653 | */ |
| 3654 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3655 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3656 | s = appctx->htxn.s; |
| 3657 | |
| 3658 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3659 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3660 | hlua_lua2smp(L, 3, &smp); |
| 3661 | |
| 3662 | /* Store the sample in a variable. */ |
| 3663 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3664 | |
| 3665 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3666 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3667 | else |
| 3668 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3669 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3670 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3674 | { |
| 3675 | struct hlua_appctx *appctx; |
| 3676 | struct stream *s; |
| 3677 | const char *name; |
| 3678 | size_t len; |
| 3679 | struct sample smp; |
| 3680 | |
| 3681 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3682 | |
| 3683 | /* It is useles to retrieve the stream, but this function |
| 3684 | * runs only in a stream context. |
| 3685 | */ |
| 3686 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3687 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3688 | s = appctx->htxn.s; |
| 3689 | |
| 3690 | /* Unset the variable. */ |
| 3691 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3692 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3693 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3697 | { |
| 3698 | struct hlua_appctx *appctx; |
| 3699 | struct stream *s; |
| 3700 | const char *name; |
| 3701 | size_t len; |
| 3702 | struct sample smp; |
| 3703 | |
| 3704 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3705 | |
| 3706 | /* It is useles to retrieve the stream, but this function |
| 3707 | * runs only in a stream context. |
| 3708 | */ |
| 3709 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3710 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3711 | s = appctx->htxn.s; |
| 3712 | |
| 3713 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3714 | if (!vars_get_by_name(name, len, &smp)) { |
| 3715 | lua_pushnil(L); |
| 3716 | return 1; |
| 3717 | } |
| 3718 | |
| 3719 | return hlua_smp2lua(L, &smp); |
| 3720 | } |
| 3721 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3722 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3723 | { |
| 3724 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3725 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3726 | struct hlua *hlua; |
| 3727 | |
| 3728 | /* 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] | 3729 | if (!s->hlua) |
| 3730 | return 0; |
| 3731 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3732 | |
| 3733 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3734 | |
| 3735 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3736 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3737 | |
| 3738 | /* Get and store new value. */ |
| 3739 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3740 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3741 | |
| 3742 | return 0; |
| 3743 | } |
| 3744 | |
| 3745 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3746 | { |
| 3747 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3748 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3749 | struct hlua *hlua; |
| 3750 | |
| 3751 | /* 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] | 3752 | if (!s->hlua) { |
| 3753 | lua_pushnil(L); |
| 3754 | return 1; |
| 3755 | } |
| 3756 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3757 | |
| 3758 | /* Push configuration index in the stack. */ |
| 3759 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3760 | |
| 3761 | return 1; |
| 3762 | } |
| 3763 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3764 | /* If expected data not yet available, it returns a yield. This function |
| 3765 | * consumes the data in the buffer. It returns a string containing the |
| 3766 | * data. This string can be empty. |
| 3767 | */ |
| 3768 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3769 | { |
| 3770 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3771 | struct stream_interface *si = appctx->appctx->owner; |
| 3772 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3773 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3774 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3775 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3776 | size_t 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 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3779 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3780 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3781 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3782 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3783 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3784 | 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] | 3785 | } |
| 3786 | |
| 3787 | /* End of data: commit the total strings and return. */ |
| 3788 | if (ret < 0) { |
| 3789 | luaL_pushresult(&appctx->b); |
| 3790 | return 1; |
| 3791 | } |
| 3792 | |
| 3793 | /* Ensure that the block 2 length is usable. */ |
| 3794 | if (ret == 1) |
| 3795 | len2 = 0; |
| 3796 | |
| 3797 | /* dont check the max length read and dont check. */ |
| 3798 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3799 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3800 | |
| 3801 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3802 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3803 | luaL_pushresult(&appctx->b); |
| 3804 | return 1; |
| 3805 | } |
| 3806 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3807 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3808 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3809 | { |
| 3810 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3811 | |
| 3812 | /* Initialise the string catenation. */ |
| 3813 | luaL_buffinit(L, &appctx->b); |
| 3814 | |
| 3815 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3816 | } |
| 3817 | |
| 3818 | /* If expected data not yet available, it returns a yield. This function |
| 3819 | * consumes the data in the buffer. It returns a string containing the |
| 3820 | * data. This string can be empty. |
| 3821 | */ |
| 3822 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3823 | { |
| 3824 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3825 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3826 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3827 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3828 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3829 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3830 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3831 | size_t 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 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3834 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3835 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3836 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3837 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3838 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3839 | 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] | 3840 | } |
| 3841 | |
| 3842 | /* End of data: commit the total strings and return. */ |
| 3843 | if (ret < 0) { |
| 3844 | luaL_pushresult(&appctx->b); |
| 3845 | return 1; |
| 3846 | } |
| 3847 | |
| 3848 | /* Ensure that the block 2 length is usable. */ |
| 3849 | if (ret == 1) |
| 3850 | len2 = 0; |
| 3851 | |
| 3852 | if (len == -1) { |
| 3853 | |
| 3854 | /* If len == -1, catenate all the data avalaile and |
| 3855 | * yield because we want to get all the data until |
| 3856 | * the end of data stream. |
| 3857 | */ |
| 3858 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3859 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3860 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3861 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3862 | 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] | 3863 | |
| 3864 | } else { |
| 3865 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3866 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3867 | if (len1 > len) |
| 3868 | len1 = len; |
| 3869 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3870 | len -= len1; |
| 3871 | |
| 3872 | /* Copy the second block. */ |
| 3873 | if (len2 > len) |
| 3874 | len2 = len; |
| 3875 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3876 | len -= len2; |
| 3877 | |
| 3878 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3879 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3880 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3881 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3882 | if (len > 0) { |
| 3883 | lua_pushinteger(L, len); |
| 3884 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3885 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3886 | 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] | 3887 | } |
| 3888 | |
| 3889 | /* return the result. */ |
| 3890 | luaL_pushresult(&appctx->b); |
| 3891 | return 1; |
| 3892 | } |
| 3893 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3894 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3895 | hlua_pusherror(L, "Lua: internal error"); |
| 3896 | WILL_LJMP(lua_error(L)); |
| 3897 | return 0; |
| 3898 | } |
| 3899 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3900 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3901 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3902 | { |
| 3903 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3904 | int len = -1; |
| 3905 | |
| 3906 | if (lua_gettop(L) > 2) |
| 3907 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3908 | if (lua_gettop(L) >= 2) { |
| 3909 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3910 | lua_pop(L, 1); |
| 3911 | } |
| 3912 | |
| 3913 | /* Confirm or set the required length */ |
| 3914 | lua_pushinteger(L, len); |
| 3915 | |
| 3916 | /* Initialise the string catenation. */ |
| 3917 | luaL_buffinit(L, &appctx->b); |
| 3918 | |
| 3919 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3920 | } |
| 3921 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3922 | /* Append data in the output side of the buffer. This data is immediately |
| 3923 | * sent. The function returns the amount of data written. If the buffer |
| 3924 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3925 | * if the channel is closed. |
| 3926 | */ |
| 3927 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3928 | { |
| 3929 | size_t len; |
| 3930 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3931 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3932 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3933 | struct stream_interface *si = appctx->appctx->owner; |
| 3934 | struct channel *chn = si_ic(si); |
| 3935 | int max; |
| 3936 | |
| 3937 | /* Get the max amount of data which can write as input in the channel. */ |
| 3938 | max = channel_recv_max(chn); |
| 3939 | if (max > (len - l)) |
| 3940 | max = len - l; |
| 3941 | |
| 3942 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3943 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3944 | |
| 3945 | /* update counters. */ |
| 3946 | l += max; |
| 3947 | lua_pop(L, 1); |
| 3948 | lua_pushinteger(L, l); |
| 3949 | |
| 3950 | /* If some data is not send, declares the situation to the |
| 3951 | * applet, and returns a yield. |
| 3952 | */ |
| 3953 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3954 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3955 | 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] | 3956 | } |
| 3957 | |
| 3958 | return 1; |
| 3959 | } |
| 3960 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3961 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3962 | * yield the LUA process, and resume it without checking the |
| 3963 | * input arguments. |
| 3964 | */ |
| 3965 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3966 | { |
| 3967 | MAY_LJMP(check_args(L, 2, "send")); |
| 3968 | lua_pushinteger(L, 0); |
| 3969 | |
| 3970 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3971 | } |
| 3972 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3973 | /* |
| 3974 | * |
| 3975 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3976 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3977 | * |
| 3978 | * |
| 3979 | */ |
| 3980 | |
| 3981 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3982 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3983 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3984 | __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] | 3985 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3986 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3987 | } |
| 3988 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3989 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3990 | * according with a current TXN. |
| 3991 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3992 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3993 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3994 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3995 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3996 | struct stream_interface *si = ctx->owner; |
| 3997 | struct stream *s = si_strm(si); |
| 3998 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3999 | struct htx *htx; |
| 4000 | struct htx_blk *blk; |
| 4001 | struct htx_sl *sl; |
| 4002 | struct ist path; |
| 4003 | unsigned long long len = 0; |
| 4004 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4005 | |
| 4006 | /* Check stack size. */ |
| 4007 | if (!lua_checkstack(L, 3)) |
| 4008 | return 0; |
| 4009 | |
| 4010 | /* Create the object: obj[0] = userdata. |
| 4011 | * Note that the base of the Converters object is the |
| 4012 | * same than the TXN object. |
| 4013 | */ |
| 4014 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4015 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4016 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4017 | appctx->appctx = ctx; |
| 4018 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4019 | 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] | 4020 | appctx->htxn.s = s; |
| 4021 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4022 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4023 | /* Create the "f" field that contains a list of fetches. */ |
| 4024 | lua_pushstring(L, "f"); |
| 4025 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4026 | return 0; |
| 4027 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4028 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4029 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4030 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4031 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4032 | return 0; |
| 4033 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4034 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4035 | /* Create the "c" field that contains a list of converters. */ |
| 4036 | lua_pushstring(L, "c"); |
| 4037 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4038 | return 0; |
| 4039 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4040 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4041 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4042 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4043 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4044 | return 0; |
| 4045 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4046 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4047 | htx = htxbuf(&s->req.buf); |
| 4048 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4049 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4050 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4051 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4052 | /* Stores the request method. */ |
| 4053 | lua_pushstring(L, "method"); |
| 4054 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4055 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4056 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4057 | /* Stores the http version. */ |
| 4058 | lua_pushstring(L, "version"); |
| 4059 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4060 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4061 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4062 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4063 | * the array on the top of the stack. |
| 4064 | */ |
| 4065 | lua_pushstring(L, "headers"); |
| 4066 | htxn.s = s; |
| 4067 | htxn.p = px; |
| 4068 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4069 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4070 | return 0; |
| 4071 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4072 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4073 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4074 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4075 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4076 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4077 | p = path.ptr; |
| 4078 | end = path.ptr + path.len; |
| 4079 | q = p; |
| 4080 | while (q < end && *q != '?') |
| 4081 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4082 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4083 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4084 | lua_pushstring(L, "path"); |
| 4085 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4086 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4087 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4088 | /* Stores the query string. */ |
| 4089 | lua_pushstring(L, "qs"); |
| 4090 | if (*q == '?') |
| 4091 | q++; |
| 4092 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4093 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4094 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4095 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4096 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4097 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4098 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4099 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4100 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 4101 | break; |
| 4102 | if (type == HTX_BLK_DATA) |
| 4103 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4104 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4105 | if (htx->extra != ULLONG_MAX) |
| 4106 | len += htx->extra; |
| 4107 | |
| 4108 | /* Stores the request path. */ |
| 4109 | lua_pushstring(L, "length"); |
| 4110 | lua_pushinteger(L, len); |
| 4111 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4112 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4113 | /* Create an empty array of HTTP request headers. */ |
| 4114 | lua_pushstring(L, "response"); |
| 4115 | lua_newtable(L); |
| 4116 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4117 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4118 | /* Pop a class stream metatable and affect it to the table. */ |
| 4119 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4120 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4121 | |
| 4122 | return 1; |
| 4123 | } |
| 4124 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4125 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4126 | { |
| 4127 | struct hlua_appctx *appctx; |
| 4128 | struct stream *s; |
| 4129 | const char *name; |
| 4130 | size_t len; |
| 4131 | struct sample smp; |
| 4132 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4133 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4134 | 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] | 4135 | |
| 4136 | /* It is useles to retrieve the stream, but this function |
| 4137 | * runs only in a stream context. |
| 4138 | */ |
| 4139 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4140 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4141 | s = appctx->htxn.s; |
| 4142 | |
| 4143 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4144 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4145 | hlua_lua2smp(L, 3, &smp); |
| 4146 | |
| 4147 | /* Store the sample in a variable. */ |
| 4148 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4149 | |
| 4150 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4151 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4152 | else |
| 4153 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4154 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4155 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4159 | { |
| 4160 | struct hlua_appctx *appctx; |
| 4161 | struct stream *s; |
| 4162 | const char *name; |
| 4163 | size_t len; |
| 4164 | struct sample smp; |
| 4165 | |
| 4166 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4167 | |
| 4168 | /* It is useles to retrieve the stream, but this function |
| 4169 | * runs only in a stream context. |
| 4170 | */ |
| 4171 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4172 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4173 | s = appctx->htxn.s; |
| 4174 | |
| 4175 | /* Unset the variable. */ |
| 4176 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4177 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4178 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4182 | { |
| 4183 | struct hlua_appctx *appctx; |
| 4184 | struct stream *s; |
| 4185 | const char *name; |
| 4186 | size_t len; |
| 4187 | struct sample smp; |
| 4188 | |
| 4189 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4190 | |
| 4191 | /* It is useles to retrieve the stream, but this function |
| 4192 | * runs only in a stream context. |
| 4193 | */ |
| 4194 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4195 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4196 | s = appctx->htxn.s; |
| 4197 | |
| 4198 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4199 | if (!vars_get_by_name(name, len, &smp)) { |
| 4200 | lua_pushnil(L); |
| 4201 | return 1; |
| 4202 | } |
| 4203 | |
| 4204 | return hlua_smp2lua(L, &smp); |
| 4205 | } |
| 4206 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4207 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4208 | { |
| 4209 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4210 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4211 | struct hlua *hlua; |
| 4212 | |
| 4213 | /* 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] | 4214 | if (!s->hlua) |
| 4215 | return 0; |
| 4216 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4217 | |
| 4218 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4219 | |
| 4220 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4221 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4222 | |
| 4223 | /* Get and store new value. */ |
| 4224 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4225 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4226 | |
| 4227 | return 0; |
| 4228 | } |
| 4229 | |
| 4230 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4231 | { |
| 4232 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4233 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4234 | struct hlua *hlua; |
| 4235 | |
| 4236 | /* 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] | 4237 | if (!s->hlua) { |
| 4238 | lua_pushnil(L); |
| 4239 | return 1; |
| 4240 | } |
| 4241 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4242 | |
| 4243 | /* Push configuration index in the stack. */ |
| 4244 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4245 | |
| 4246 | return 1; |
| 4247 | } |
| 4248 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4249 | /* If expected data not yet available, it returns a yield. This function |
| 4250 | * consumes the data in the buffer. It returns a string containing the |
| 4251 | * data. This string can be empty. |
| 4252 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4253 | __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] | 4254 | { |
| 4255 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4256 | struct stream_interface *si = appctx->appctx->owner; |
| 4257 | struct channel *req = si_oc(si); |
| 4258 | struct htx *htx; |
| 4259 | struct htx_blk *blk; |
| 4260 | size_t count; |
| 4261 | int stop = 0; |
| 4262 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4263 | htx = htx_from_buf(&req->buf); |
| 4264 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4265 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4266 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4267 | while (count && !stop && blk) { |
| 4268 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4269 | uint32_t sz = htx_get_blksz(blk); |
| 4270 | struct ist v; |
| 4271 | uint32_t vlen; |
| 4272 | char *nl; |
| 4273 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4274 | if (type == HTX_BLK_EOM) { |
| 4275 | stop = 1; |
| 4276 | break; |
| 4277 | } |
| 4278 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4279 | vlen = sz; |
| 4280 | if (vlen > count) { |
| 4281 | if (type != HTX_BLK_DATA) |
| 4282 | break; |
| 4283 | vlen = count; |
| 4284 | } |
| 4285 | |
| 4286 | switch (type) { |
| 4287 | case HTX_BLK_UNUSED: |
| 4288 | break; |
| 4289 | |
| 4290 | case HTX_BLK_DATA: |
| 4291 | v = htx_get_blk_value(htx, blk); |
| 4292 | v.len = vlen; |
| 4293 | nl = istchr(v, '\n'); |
| 4294 | if (nl != NULL) { |
| 4295 | stop = 1; |
| 4296 | vlen = nl - v.ptr + 1; |
| 4297 | } |
| 4298 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4299 | break; |
| 4300 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4301 | case HTX_BLK_TLR: |
| 4302 | case HTX_BLK_EOM: |
| 4303 | stop = 1; |
| 4304 | break; |
| 4305 | |
| 4306 | default: |
| 4307 | break; |
| 4308 | } |
| 4309 | |
| 4310 | co_set_data(req, co_data(req) - vlen); |
| 4311 | count -= vlen; |
| 4312 | if (sz == vlen) |
| 4313 | blk = htx_remove_blk(htx, blk); |
| 4314 | else { |
| 4315 | htx_cut_data_blk(htx, blk, vlen); |
| 4316 | break; |
| 4317 | } |
| 4318 | } |
| 4319 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4320 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4321 | if (!stop) { |
| 4322 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4323 | 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] | 4324 | } |
| 4325 | |
| 4326 | /* return the result. */ |
| 4327 | luaL_pushresult(&appctx->b); |
| 4328 | return 1; |
| 4329 | } |
| 4330 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4331 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4332 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4333 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4334 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4335 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4336 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4337 | /* Initialise the string catenation. */ |
| 4338 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4339 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4340 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4341 | } |
| 4342 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4343 | /* If expected data not yet available, it returns a yield. This function |
| 4344 | * consumes the data in the buffer. It returns a string containing the |
| 4345 | * data. This string can be empty. |
| 4346 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4347 | __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] | 4348 | { |
| 4349 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4350 | struct stream_interface *si = appctx->appctx->owner; |
| 4351 | struct channel *req = si_oc(si); |
| 4352 | struct htx *htx; |
| 4353 | struct htx_blk *blk; |
| 4354 | size_t count; |
| 4355 | int len; |
| 4356 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4357 | htx = htx_from_buf(&req->buf); |
| 4358 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4359 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4360 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4361 | while (count && len && blk) { |
| 4362 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4363 | uint32_t sz = htx_get_blksz(blk); |
| 4364 | struct ist v; |
| 4365 | uint32_t vlen; |
| 4366 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4367 | if (type == HTX_BLK_EOM) { |
| 4368 | len = 0; |
| 4369 | break; |
| 4370 | } |
| 4371 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4372 | vlen = sz; |
| 4373 | if (len > 0 && vlen > len) |
| 4374 | vlen = len; |
| 4375 | if (vlen > count) { |
| 4376 | if (type != HTX_BLK_DATA) |
| 4377 | break; |
| 4378 | vlen = count; |
| 4379 | } |
| 4380 | |
| 4381 | switch (type) { |
| 4382 | case HTX_BLK_UNUSED: |
| 4383 | break; |
| 4384 | |
| 4385 | case HTX_BLK_DATA: |
| 4386 | v = htx_get_blk_value(htx, blk); |
| 4387 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4388 | break; |
| 4389 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4390 | case HTX_BLK_TLR: |
| 4391 | case HTX_BLK_EOM: |
| 4392 | len = 0; |
| 4393 | break; |
| 4394 | |
| 4395 | default: |
| 4396 | break; |
| 4397 | } |
| 4398 | |
| 4399 | co_set_data(req, co_data(req) - vlen); |
| 4400 | count -= vlen; |
| 4401 | if (len > 0) |
| 4402 | len -= vlen; |
| 4403 | if (sz == vlen) |
| 4404 | blk = htx_remove_blk(htx, blk); |
| 4405 | else { |
| 4406 | htx_cut_data_blk(htx, blk, vlen); |
| 4407 | break; |
| 4408 | } |
| 4409 | } |
| 4410 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4411 | htx_to_buf(htx, &req->buf); |
| 4412 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4413 | /* If we are no other data available, yield waiting for new data. */ |
| 4414 | if (len) { |
| 4415 | if (len > 0) { |
| 4416 | lua_pushinteger(L, len); |
| 4417 | lua_replace(L, 2); |
| 4418 | } |
| 4419 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4420 | 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] | 4421 | } |
| 4422 | |
| 4423 | /* return the result. */ |
| 4424 | luaL_pushresult(&appctx->b); |
| 4425 | return 1; |
| 4426 | } |
| 4427 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4428 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4429 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4430 | { |
| 4431 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4432 | int len = -1; |
| 4433 | |
| 4434 | /* Check arguments. */ |
| 4435 | if (lua_gettop(L) > 2) |
| 4436 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4437 | if (lua_gettop(L) >= 2) { |
| 4438 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4439 | lua_pop(L, 1); |
| 4440 | } |
| 4441 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4442 | lua_pushinteger(L, len); |
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 | /* Initialise the string catenation. */ |
| 4445 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4446 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4447 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | /* Append data in the output side of the buffer. This data is immediately |
| 4451 | * sent. The function returns the amount of data written. If the buffer |
| 4452 | * cannot contain the data, the function yields. The function returns -1 |
| 4453 | * if the channel is closed. |
| 4454 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4455 | __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] | 4456 | { |
| 4457 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4458 | struct stream_interface *si = appctx->appctx->owner; |
| 4459 | struct channel *res = si_ic(si); |
| 4460 | struct htx *htx = htx_from_buf(&res->buf); |
| 4461 | const char *data; |
| 4462 | size_t len; |
| 4463 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4464 | int max; |
| 4465 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4466 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4467 | if (!max) |
| 4468 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4469 | |
| 4470 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4471 | |
| 4472 | /* Get the max amount of data which can write as input in the channel. */ |
| 4473 | if (max > (len - l)) |
| 4474 | max = len - l; |
| 4475 | |
| 4476 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4477 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4478 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4479 | |
| 4480 | /* update counters. */ |
| 4481 | l += max; |
| 4482 | lua_pop(L, 1); |
| 4483 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4484 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4485 | /* If some data is not send, declares the situation to the |
| 4486 | * applet, and returns a yield. |
| 4487 | */ |
| 4488 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4489 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4490 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4491 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4492 | 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] | 4493 | } |
| 4494 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4495 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4496 | return 1; |
| 4497 | } |
| 4498 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4499 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4500 | * yield the LUA process, and resume it without checking the |
| 4501 | * input arguments. |
| 4502 | */ |
| 4503 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4504 | { |
| 4505 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4506 | |
| 4507 | /* We want to send some data. Headers must be sent. */ |
| 4508 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4509 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4510 | WILL_LJMP(lua_error(L)); |
| 4511 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4512 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4513 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4514 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4515 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4516 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4520 | { |
| 4521 | const char *name; |
| 4522 | int ret; |
| 4523 | |
| 4524 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4525 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4526 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4527 | |
| 4528 | /* Push in the stack the "response" entry. */ |
| 4529 | ret = lua_getfield(L, 1, "response"); |
| 4530 | if (ret != LUA_TTABLE) { |
| 4531 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4532 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4533 | WILL_LJMP(lua_error(L)); |
| 4534 | } |
| 4535 | |
| 4536 | /* check if the header is already registered if it is not |
| 4537 | * the case, register it. |
| 4538 | */ |
| 4539 | ret = lua_getfield(L, -1, name); |
| 4540 | if (ret == LUA_TNIL) { |
| 4541 | |
| 4542 | /* Entry not found. */ |
| 4543 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4544 | |
| 4545 | /* Insert the new header name in the array in the top of the stack. |
| 4546 | * It left the new array in the top of the stack. |
| 4547 | */ |
| 4548 | lua_newtable(L); |
| 4549 | lua_pushvalue(L, 2); |
| 4550 | lua_pushvalue(L, -2); |
| 4551 | lua_settable(L, -4); |
| 4552 | |
| 4553 | } else if (ret != LUA_TTABLE) { |
| 4554 | |
| 4555 | /* corruption error. */ |
| 4556 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4557 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4558 | WILL_LJMP(lua_error(L)); |
| 4559 | } |
| 4560 | |
| 4561 | /* Now the top od thestack is an array of values. We push |
| 4562 | * the header value as new entry. |
| 4563 | */ |
| 4564 | lua_pushvalue(L, 3); |
| 4565 | ret = lua_rawlen(L, -2); |
| 4566 | lua_rawseti(L, -2, ret + 1); |
| 4567 | lua_pushboolean(L, 1); |
| 4568 | return 1; |
| 4569 | } |
| 4570 | |
| 4571 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4572 | { |
| 4573 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4574 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4575 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4576 | |
| 4577 | if (status < 100 || status > 599) { |
| 4578 | lua_pushboolean(L, 0); |
| 4579 | return 1; |
| 4580 | } |
| 4581 | |
| 4582 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4583 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4584 | lua_pushboolean(L, 1); |
| 4585 | return 1; |
| 4586 | } |
| 4587 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4588 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4589 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4590 | { |
| 4591 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4592 | struct stream_interface *si = appctx->appctx->owner; |
| 4593 | struct channel *res = si_ic(si); |
| 4594 | struct htx *htx; |
| 4595 | struct htx_sl *sl; |
| 4596 | struct h1m h1m; |
| 4597 | const char *status, *reason; |
| 4598 | const char *name, *value; |
| 4599 | size_t nlen, vlen; |
| 4600 | unsigned int flags; |
| 4601 | |
| 4602 | /* Send the message at once. */ |
| 4603 | htx = htx_from_buf(&res->buf); |
| 4604 | h1m_init_res(&h1m); |
| 4605 | |
| 4606 | /* Use the same http version than the request. */ |
| 4607 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4608 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4609 | if (reason == NULL) |
| 4610 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4611 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4612 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4613 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4614 | } |
| 4615 | else { |
| 4616 | flags = HTX_SL_F_IS_RESP; |
| 4617 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4618 | } |
| 4619 | if (!sl) { |
| 4620 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4621 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4622 | WILL_LJMP(lua_error(L)); |
| 4623 | } |
| 4624 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4625 | |
| 4626 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4627 | lua_pushvalue(L, 0); |
| 4628 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4629 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4630 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4631 | WILL_LJMP(lua_error(L)); |
| 4632 | } |
| 4633 | |
| 4634 | /* Browse the list of headers. */ |
| 4635 | lua_pushnil(L); |
| 4636 | while(lua_next(L, -2) != 0) { |
| 4637 | /* We expect a string as -2. */ |
| 4638 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4639 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4640 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4641 | lua_typename(L, lua_type(L, -2))); |
| 4642 | WILL_LJMP(lua_error(L)); |
| 4643 | } |
| 4644 | name = lua_tolstring(L, -2, &nlen); |
| 4645 | |
| 4646 | /* We expect an array as -1. */ |
| 4647 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4648 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4649 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4650 | name, |
| 4651 | lua_typename(L, lua_type(L, -1))); |
| 4652 | WILL_LJMP(lua_error(L)); |
| 4653 | } |
| 4654 | |
| 4655 | /* Browse the table who is on the top of the stack. */ |
| 4656 | lua_pushnil(L); |
| 4657 | while(lua_next(L, -2) != 0) { |
| 4658 | int id; |
| 4659 | |
| 4660 | /* We expect a number as -2. */ |
| 4661 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4662 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4663 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4664 | name, |
| 4665 | lua_typename(L, lua_type(L, -2))); |
| 4666 | WILL_LJMP(lua_error(L)); |
| 4667 | } |
| 4668 | id = lua_tointeger(L, -2); |
| 4669 | |
| 4670 | /* We expect a string as -2. */ |
| 4671 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4672 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4673 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4674 | name, id, |
| 4675 | lua_typename(L, lua_type(L, -1))); |
| 4676 | WILL_LJMP(lua_error(L)); |
| 4677 | } |
| 4678 | value = lua_tolstring(L, -1, &vlen); |
| 4679 | |
| 4680 | /* Simple Protocol checks. */ |
| 4681 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4682 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4683 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4684 | struct ist v = ist2(value, vlen); |
| 4685 | int ret; |
| 4686 | |
| 4687 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4688 | if (ret < 0) { |
| 4689 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4690 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4691 | name); |
| 4692 | WILL_LJMP(lua_error(L)); |
| 4693 | } |
| 4694 | else if (ret == 0) |
| 4695 | goto next; /* Skip it */ |
| 4696 | } |
| 4697 | |
| 4698 | /* Add a new header */ |
| 4699 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4700 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4701 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4702 | name); |
| 4703 | WILL_LJMP(lua_error(L)); |
| 4704 | } |
| 4705 | next: |
| 4706 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4707 | lua_pop(L, 1); |
| 4708 | } |
| 4709 | |
| 4710 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4711 | lua_pop(L, 1); |
| 4712 | } |
| 4713 | |
| 4714 | if (h1m.flags & H1_MF_CHNK) |
| 4715 | h1m.flags &= ~H1_MF_CLEN; |
| 4716 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4717 | h1m.flags |= H1_MF_XFER_LEN; |
| 4718 | |
| 4719 | /* Uset HTX start-line flags */ |
| 4720 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4721 | flags |= HTX_SL_F_XFER_ENC; |
| 4722 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4723 | flags |= HTX_SL_F_XFER_LEN; |
| 4724 | if (h1m.flags & H1_MF_CHNK) |
| 4725 | flags |= HTX_SL_F_CHNK; |
| 4726 | else if (h1m.flags & H1_MF_CLEN) |
| 4727 | flags |= HTX_SL_F_CLEN; |
| 4728 | if (h1m.body_len == 0) |
| 4729 | flags |= HTX_SL_F_BODYLESS; |
| 4730 | } |
| 4731 | sl->flags |= flags; |
| 4732 | |
| 4733 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4734 | * and the status code implies the presence of a message body, we must |
| 4735 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4736 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4737 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4738 | */ |
| 4739 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4740 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4741 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4742 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4743 | /* Add a new header */ |
| 4744 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4745 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4746 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4747 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4748 | WILL_LJMP(lua_error(L)); |
| 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | /* Finalize headers. */ |
| 4753 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4754 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4755 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4756 | WILL_LJMP(lua_error(L)); |
| 4757 | } |
| 4758 | |
| 4759 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4760 | b_reset(&res->buf); |
| 4761 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4762 | WILL_LJMP(lua_error(L)); |
| 4763 | } |
| 4764 | |
| 4765 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4766 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4767 | |
| 4768 | /* Headers sent, set the flag. */ |
| 4769 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4770 | return 0; |
| 4771 | |
| 4772 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4773 | /* We will build the status line and the headers of the HTTP response. |
| 4774 | * We will try send at once if its not possible, we give back the hand |
| 4775 | * waiting for more room. |
| 4776 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4777 | __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] | 4778 | { |
| 4779 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4780 | struct stream_interface *si = appctx->appctx->owner; |
| 4781 | struct channel *res = si_ic(si); |
| 4782 | |
| 4783 | if (co_data(res)) { |
| 4784 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4785 | 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] | 4786 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4787 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4791 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4792 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4793 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4794 | } |
| 4795 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4796 | /* |
| 4797 | * |
| 4798 | * |
| 4799 | * Class HTTP |
| 4800 | * |
| 4801 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4802 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4803 | |
| 4804 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4805 | * a class stream, otherwise it throws an error. |
| 4806 | */ |
| 4807 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
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 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4810 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4811 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4812 | /* This function creates and push in the stack a HTTP object |
| 4813 | * according with a current TXN. |
| 4814 | */ |
| 4815 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4816 | { |
| 4817 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4818 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4819 | /* Check stack size. */ |
| 4820 | if (!lua_checkstack(L, 3)) |
| 4821 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4822 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4823 | /* Create the object: obj[0] = userdata. |
| 4824 | * Note that the base of the Converters object is the |
| 4825 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4826 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4827 | lua_newtable(L); |
| 4828 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4829 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4830 | |
| 4831 | htxn->s = txn->s; |
| 4832 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4833 | htxn->dir = txn->dir; |
| 4834 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4835 | |
| 4836 | /* Pop a class stream metatable and affect it to the table. */ |
| 4837 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4838 | lua_setmetatable(L, -2); |
| 4839 | |
| 4840 | return 1; |
| 4841 | } |
| 4842 | |
| 4843 | /* This function creates ans returns an array of HTTP headers. |
| 4844 | * This function does not fails. It is used as wrapper with the |
| 4845 | * 2 following functions. |
| 4846 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4847 | __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] | 4848 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4849 | struct htx *htx; |
| 4850 | int32_t pos; |
| 4851 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4852 | /* Create the table. */ |
| 4853 | lua_newtable(L); |
| 4854 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4855 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4856 | htx = htxbuf(&msg->chn->buf); |
| 4857 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4858 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4859 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4860 | struct ist n, v; |
| 4861 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4862 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4863 | if (type == HTX_BLK_HDR) { |
| 4864 | n = htx_get_blk_name(htx,blk); |
| 4865 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4866 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4867 | else if (type == HTX_BLK_EOH) |
| 4868 | break; |
| 4869 | else |
| 4870 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4871 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4872 | /* Check for existing entry: |
| 4873 | * assume that the table is on the top of the stack, and |
| 4874 | * push the key in the stack, the function lua_gettable() |
| 4875 | * perform the lookup. |
| 4876 | */ |
| 4877 | lua_pushlstring(L, n.ptr, n.len); |
| 4878 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4879 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4880 | switch (lua_type(L, -1)) { |
| 4881 | case LUA_TNIL: |
| 4882 | /* Table not found, create it. */ |
| 4883 | lua_pop(L, 1); /* remove the nil value. */ |
| 4884 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4885 | lua_newtable(L); /* create and push empty table. */ |
| 4886 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4887 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4888 | 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] | 4889 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4890 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4891 | case LUA_TTABLE: |
| 4892 | /* Entry found: push the value in the table. */ |
| 4893 | len = lua_rawlen(L, -1); |
| 4894 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4895 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4896 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4897 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4898 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4899 | default: |
| 4900 | /* Other cases are errors. */ |
| 4901 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4902 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4903 | } |
| 4904 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4905 | return 1; |
| 4906 | } |
| 4907 | |
| 4908 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4909 | { |
| 4910 | struct hlua_txn *htxn; |
| 4911 | |
| 4912 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4913 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4914 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4915 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4916 | WILL_LJMP(lua_error(L)); |
| 4917 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4918 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4922 | { |
| 4923 | struct hlua_txn *htxn; |
| 4924 | |
| 4925 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4926 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4927 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4928 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4929 | WILL_LJMP(lua_error(L)); |
| 4930 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4931 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | /* This function replace full header, or just a value in |
| 4935 | * the request or in the response. It is a wrapper fir the |
| 4936 | * 4 following functions. |
| 4937 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4938 | __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] | 4939 | { |
| 4940 | size_t name_len; |
| 4941 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4942 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4943 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4944 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4945 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4946 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4947 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4948 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4949 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4950 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4951 | 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] | 4952 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4953 | return 0; |
| 4954 | } |
| 4955 | |
| 4956 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4957 | { |
| 4958 | struct hlua_txn *htxn; |
| 4959 | |
| 4960 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4961 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4962 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4963 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4964 | WILL_LJMP(lua_error(L)); |
| 4965 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4966 | 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] | 4967 | } |
| 4968 | |
| 4969 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4970 | { |
| 4971 | struct hlua_txn *htxn; |
| 4972 | |
| 4973 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4974 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4975 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4976 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4977 | WILL_LJMP(lua_error(L)); |
| 4978 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4979 | 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] | 4980 | } |
| 4981 | |
| 4982 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4983 | { |
| 4984 | struct hlua_txn *htxn; |
| 4985 | |
| 4986 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4987 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4988 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4989 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4990 | WILL_LJMP(lua_error(L)); |
| 4991 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4992 | 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] | 4993 | } |
| 4994 | |
| 4995 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4996 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4997 | struct hlua_txn *htxn; |
| 4998 | |
| 4999 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5000 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5001 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5002 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5003 | WILL_LJMP(lua_error(L)); |
| 5004 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5005 | 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] | 5006 | } |
| 5007 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5008 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5009 | * It is a wrapper for the 2 following functions. |
| 5010 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5011 | __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] | 5012 | { |
| 5013 | size_t len; |
| 5014 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5015 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5016 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5017 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5018 | ctx.blk = NULL; |
| 5019 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5020 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5021 | return 0; |
| 5022 | } |
| 5023 | |
| 5024 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5025 | { |
| 5026 | struct hlua_txn *htxn; |
| 5027 | |
| 5028 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5029 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5030 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5031 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5032 | WILL_LJMP(lua_error(L)); |
| 5033 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5034 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5038 | { |
| 5039 | struct hlua_txn *htxn; |
| 5040 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5041 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5042 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5043 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5044 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5045 | WILL_LJMP(lua_error(L)); |
| 5046 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5047 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | /* This function adds an header. It is a wrapper used by |
| 5051 | * the 2 following functions. |
| 5052 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5053 | __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] | 5054 | { |
| 5055 | size_t name_len; |
| 5056 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5057 | size_t value_len; |
| 5058 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5059 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5060 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5061 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5062 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5063 | return 0; |
| 5064 | } |
| 5065 | |
| 5066 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5067 | { |
| 5068 | struct hlua_txn *htxn; |
| 5069 | |
| 5070 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5071 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5072 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5073 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5074 | WILL_LJMP(lua_error(L)); |
| 5075 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5076 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5080 | { |
| 5081 | struct hlua_txn *htxn; |
| 5082 | |
| 5083 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5084 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5085 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5086 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5087 | WILL_LJMP(lua_error(L)); |
| 5088 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5089 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5093 | { |
| 5094 | struct hlua_txn *htxn; |
| 5095 | |
| 5096 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5097 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5098 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5099 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5100 | WILL_LJMP(lua_error(L)); |
| 5101 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5102 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5103 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5107 | { |
| 5108 | struct hlua_txn *htxn; |
| 5109 | |
| 5110 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5111 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5112 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5113 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5114 | WILL_LJMP(lua_error(L)); |
| 5115 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5116 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5117 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | /* This function set the method. */ |
| 5121 | static int hlua_http_req_set_meth(lua_State *L) |
| 5122 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5123 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5124 | size_t name_len; |
| 5125 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5126 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5127 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5128 | WILL_LJMP(lua_error(L)); |
| 5129 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5130 | 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] | 5131 | return 1; |
| 5132 | } |
| 5133 | |
| 5134 | /* This function set the method. */ |
| 5135 | static int hlua_http_req_set_path(lua_State *L) |
| 5136 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5137 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5138 | size_t name_len; |
| 5139 | 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] | 5140 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5141 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5142 | WILL_LJMP(lua_error(L)); |
| 5143 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5144 | 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] | 5145 | return 1; |
| 5146 | } |
| 5147 | |
| 5148 | /* This function set the query-string. */ |
| 5149 | static int hlua_http_req_set_query(lua_State *L) |
| 5150 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5151 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5152 | size_t name_len; |
| 5153 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5154 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5155 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5156 | WILL_LJMP(lua_error(L)); |
| 5157 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5158 | /* Check length. */ |
| 5159 | if (name_len > trash.size - 1) { |
| 5160 | lua_pushboolean(L, 0); |
| 5161 | return 1; |
| 5162 | } |
| 5163 | |
| 5164 | /* Add the mark question as prefix. */ |
| 5165 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5166 | trash.area[trash.data++] = '?'; |
| 5167 | memcpy(trash.area + trash.data, name, name_len); |
| 5168 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5169 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5170 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5171 | 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] | 5172 | return 1; |
| 5173 | } |
| 5174 | |
| 5175 | /* This function set the uri. */ |
| 5176 | static int hlua_http_req_set_uri(lua_State *L) |
| 5177 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5178 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5179 | size_t name_len; |
| 5180 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5181 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5182 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5183 | WILL_LJMP(lua_error(L)); |
| 5184 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5185 | 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] | 5186 | return 1; |
| 5187 | } |
| 5188 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5189 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5190 | static int hlua_http_res_set_status(lua_State *L) |
| 5191 | { |
| 5192 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5193 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5194 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5195 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5196 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5197 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5198 | WILL_LJMP(lua_error(L)); |
| 5199 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5200 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5201 | return 0; |
| 5202 | } |
| 5203 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5204 | /* |
| 5205 | * |
| 5206 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5207 | * Class TXN |
| 5208 | * |
| 5209 | * |
| 5210 | */ |
| 5211 | |
| 5212 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5213 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5214 | */ |
| 5215 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5216 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5217 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5218 | } |
| 5219 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5220 | __LJMP static int hlua_set_var(lua_State *L) |
| 5221 | { |
| 5222 | struct hlua_txn *htxn; |
| 5223 | const char *name; |
| 5224 | size_t len; |
| 5225 | struct sample smp; |
| 5226 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5227 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5228 | 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] | 5229 | |
| 5230 | /* It is useles to retrieve the stream, but this function |
| 5231 | * runs only in a stream context. |
| 5232 | */ |
| 5233 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5234 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5235 | |
| 5236 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5237 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5238 | hlua_lua2smp(L, 3, &smp); |
| 5239 | |
| 5240 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5241 | 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] | 5242 | |
| 5243 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5244 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5245 | else |
| 5246 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5247 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5248 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5249 | } |
| 5250 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5251 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5252 | { |
| 5253 | struct hlua_txn *htxn; |
| 5254 | const char *name; |
| 5255 | size_t len; |
| 5256 | struct sample smp; |
| 5257 | |
| 5258 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5259 | |
| 5260 | /* It is useles to retrieve the stream, but this function |
| 5261 | * runs only in a stream context. |
| 5262 | */ |
| 5263 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5264 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5265 | |
| 5266 | /* Unset the variable. */ |
| 5267 | 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] | 5268 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5269 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5270 | } |
| 5271 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5272 | __LJMP static int hlua_get_var(lua_State *L) |
| 5273 | { |
| 5274 | struct hlua_txn *htxn; |
| 5275 | const char *name; |
| 5276 | size_t len; |
| 5277 | struct sample smp; |
| 5278 | |
| 5279 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5280 | |
| 5281 | /* It is useles to retrieve the stream, but this function |
| 5282 | * runs only in a stream context. |
| 5283 | */ |
| 5284 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5285 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5286 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5287 | 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] | 5288 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5289 | lua_pushnil(L); |
| 5290 | return 1; |
| 5291 | } |
| 5292 | |
| 5293 | return hlua_smp2lua(L, &smp); |
| 5294 | } |
| 5295 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5296 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5297 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5298 | struct hlua *hlua; |
| 5299 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5300 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5301 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5302 | /* It is useles to retrieve the stream, but this function |
| 5303 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5304 | */ |
| 5305 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5306 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5307 | |
| 5308 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5309 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5310 | |
| 5311 | /* Get and store new value. */ |
| 5312 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5313 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5314 | |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5318 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5319 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5320 | struct hlua *hlua; |
| 5321 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5322 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5323 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5324 | /* It is useles to retrieve the stream, but this function |
| 5325 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5326 | */ |
| 5327 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5328 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5329 | |
| 5330 | /* Push configuration index in the stack. */ |
| 5331 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5332 | |
| 5333 | return 1; |
| 5334 | } |
| 5335 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5336 | /* Create stack entry containing a class TXN. This function |
| 5337 | * return 0 if the stack does not contains free slots, |
| 5338 | * otherwise it returns 1. |
| 5339 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5340 | 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] | 5341 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5342 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5343 | |
| 5344 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5345 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5346 | return 0; |
| 5347 | |
| 5348 | /* NOTE: The allocation never fails. The failure |
| 5349 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5350 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5351 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5352 | /* Create the object: obj[0] = userdata. */ |
| 5353 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5354 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5355 | lua_rawseti(L, -2, 0); |
| 5356 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5357 | htxn->s = s; |
| 5358 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5359 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5360 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5361 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5362 | /* Create the "f" field that contains a list of fetches. */ |
| 5363 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5364 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5365 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5366 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5367 | |
| 5368 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5369 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5370 | 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] | 5371 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5372 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5373 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5374 | /* Create the "c" field that contains a list of converters. */ |
| 5375 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5376 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5377 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5378 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5379 | |
| 5380 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5381 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5382 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5383 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5384 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5385 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5386 | /* Create the "req" field that contains the request channel object. */ |
| 5387 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5388 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5389 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5390 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5391 | |
| 5392 | /* Create the "res" field that contains the response channel object. */ |
| 5393 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5394 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5395 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5396 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5397 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5398 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5399 | lua_pushstring(L, "http"); |
| 5400 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5401 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5402 | return 0; |
| 5403 | } |
| 5404 | else |
| 5405 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5406 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5407 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5408 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5409 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5410 | lua_setmetatable(L, -2); |
| 5411 | |
| 5412 | return 1; |
| 5413 | } |
| 5414 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5415 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5416 | { |
| 5417 | const char *msg; |
| 5418 | struct hlua_txn *htxn; |
| 5419 | |
| 5420 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5421 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5422 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5423 | |
| 5424 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5425 | return 0; |
| 5426 | } |
| 5427 | |
| 5428 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5429 | { |
| 5430 | int level; |
| 5431 | const char *msg; |
| 5432 | struct hlua_txn *htxn; |
| 5433 | |
| 5434 | MAY_LJMP(check_args(L, 3, "log")); |
| 5435 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5436 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5437 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5438 | |
| 5439 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5440 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5441 | |
| 5442 | hlua_sendlog(htxn->s->be, level, msg); |
| 5443 | return 0; |
| 5444 | } |
| 5445 | |
| 5446 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5447 | { |
| 5448 | const char *msg; |
| 5449 | struct hlua_txn *htxn; |
| 5450 | |
| 5451 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5452 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5453 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5454 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5455 | return 0; |
| 5456 | } |
| 5457 | |
| 5458 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5459 | { |
| 5460 | const char *msg; |
| 5461 | struct hlua_txn *htxn; |
| 5462 | |
| 5463 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5464 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5465 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5466 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5467 | return 0; |
| 5468 | } |
| 5469 | |
| 5470 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5471 | { |
| 5472 | const char *msg; |
| 5473 | struct hlua_txn *htxn; |
| 5474 | |
| 5475 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5476 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5477 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5478 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5479 | return 0; |
| 5480 | } |
| 5481 | |
| 5482 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5483 | { |
| 5484 | const char *msg; |
| 5485 | struct hlua_txn *htxn; |
| 5486 | |
| 5487 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5488 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5489 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5490 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5491 | return 0; |
| 5492 | } |
| 5493 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5494 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5495 | { |
| 5496 | struct hlua_txn *htxn; |
| 5497 | int ll; |
| 5498 | |
| 5499 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5500 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5501 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5502 | |
| 5503 | if (ll < 0 || ll > 7) |
| 5504 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5505 | |
| 5506 | htxn->s->logs.level = ll; |
| 5507 | return 0; |
| 5508 | } |
| 5509 | |
| 5510 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5511 | { |
| 5512 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5513 | int tos; |
| 5514 | |
| 5515 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5516 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5517 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5518 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5519 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5520 | return 0; |
| 5521 | } |
| 5522 | |
| 5523 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5524 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5525 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5526 | int mark; |
| 5527 | |
| 5528 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5529 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5530 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5531 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5532 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5533 | return 0; |
| 5534 | } |
| 5535 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5536 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5537 | { |
| 5538 | struct hlua_txn *htxn; |
| 5539 | |
| 5540 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5541 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5542 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5543 | return 0; |
| 5544 | } |
| 5545 | |
| 5546 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5547 | { |
| 5548 | struct hlua_txn *htxn; |
| 5549 | |
| 5550 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5551 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5552 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5553 | return 0; |
| 5554 | } |
| 5555 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5556 | /* 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] | 5557 | * 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] | 5558 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5559 | * error. The Reply must be on top of the stack. |
| 5560 | */ |
| 5561 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5562 | { |
| 5563 | struct htx *htx; |
| 5564 | struct htx_sl *sl; |
| 5565 | struct h1m h1m; |
| 5566 | const char *status, *reason, *body; |
| 5567 | size_t status_len, reason_len, body_len; |
| 5568 | int ret, code, flags; |
| 5569 | |
| 5570 | code = 200; |
| 5571 | status = "200"; |
| 5572 | status_len = 3; |
| 5573 | ret = lua_getfield(L, -1, "status"); |
| 5574 | if (ret == LUA_TNUMBER) { |
| 5575 | code = lua_tointeger(L, -1); |
| 5576 | status = lua_tolstring(L, -1, &status_len); |
| 5577 | } |
| 5578 | lua_pop(L, 1); |
| 5579 | |
| 5580 | reason = http_get_reason(code); |
| 5581 | reason_len = strlen(reason); |
| 5582 | ret = lua_getfield(L, -1, "reason"); |
| 5583 | if (ret == LUA_TSTRING) |
| 5584 | reason = lua_tolstring(L, -1, &reason_len); |
| 5585 | lua_pop(L, 1); |
| 5586 | |
| 5587 | body = NULL; |
| 5588 | body_len = 0; |
| 5589 | ret = lua_getfield(L, -1, "body"); |
| 5590 | if (ret == LUA_TSTRING) |
| 5591 | body = lua_tolstring(L, -1, &body_len); |
| 5592 | lua_pop(L, 1); |
| 5593 | |
| 5594 | /* Prepare the response before inserting the headers */ |
| 5595 | h1m_init_res(&h1m); |
| 5596 | htx = htx_from_buf(&s->res.buf); |
| 5597 | channel_htx_truncate(&s->res, htx); |
| 5598 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5599 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5600 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5601 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5602 | } |
| 5603 | else { |
| 5604 | flags = HTX_SL_F_IS_RESP; |
| 5605 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5606 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5607 | } |
| 5608 | if (!sl) |
| 5609 | goto fail; |
| 5610 | sl->info.res.status = code; |
| 5611 | |
| 5612 | /* Push in the stack the "headers" entry. */ |
| 5613 | ret = lua_getfield(L, -1, "headers"); |
| 5614 | if (ret != LUA_TTABLE) |
| 5615 | goto skip_headers; |
| 5616 | |
| 5617 | lua_pushnil(L); |
| 5618 | while (lua_next(L, -2) != 0) { |
| 5619 | struct ist name, value; |
| 5620 | const char *n, *v; |
| 5621 | size_t nlen, vlen; |
| 5622 | |
| 5623 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5624 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5625 | goto next_hdr; |
| 5626 | } |
| 5627 | |
| 5628 | n = lua_tolstring(L, -2, &nlen); |
| 5629 | name = ist2(n, nlen); |
| 5630 | if (isteqi(name, ist("content-length"))) { |
| 5631 | /* Always skip content-length header. It will be added |
| 5632 | * later with the correct len |
| 5633 | */ |
| 5634 | goto next_hdr; |
| 5635 | } |
| 5636 | |
| 5637 | /* Loop on header's values */ |
| 5638 | lua_pushnil(L); |
| 5639 | while (lua_next(L, -2)) { |
| 5640 | if (!lua_isstring(L, -1)) { |
| 5641 | /* Skip the value if it is not a string */ |
| 5642 | goto next_value; |
| 5643 | } |
| 5644 | |
| 5645 | v = lua_tolstring(L, -1, &vlen); |
| 5646 | value = ist2(v, vlen); |
| 5647 | |
| 5648 | if (isteqi(name, ist("transfer-encoding"))) |
| 5649 | h1_parse_xfer_enc_header(&h1m, value); |
| 5650 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5651 | goto fail; |
| 5652 | |
| 5653 | next_value: |
| 5654 | lua_pop(L, 1); |
| 5655 | } |
| 5656 | |
| 5657 | next_hdr: |
| 5658 | lua_pop(L, 1); |
| 5659 | } |
| 5660 | skip_headers: |
| 5661 | lua_pop(L, 1); |
| 5662 | |
| 5663 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5664 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5665 | const char *clen = ultoa(body_len); |
| 5666 | |
| 5667 | h1m.flags |= H1_MF_CLEN; |
| 5668 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5669 | goto fail; |
| 5670 | } |
| 5671 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5672 | h1m.flags |= H1_MF_XFER_LEN; |
| 5673 | |
| 5674 | /* Update HTX start-line flags */ |
| 5675 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5676 | flags |= HTX_SL_F_XFER_ENC; |
| 5677 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5678 | flags |= HTX_SL_F_XFER_LEN; |
| 5679 | if (h1m.flags & H1_MF_CHNK) |
| 5680 | flags |= HTX_SL_F_CHNK; |
| 5681 | else if (h1m.flags & H1_MF_CLEN) |
| 5682 | flags |= HTX_SL_F_CLEN; |
| 5683 | if (h1m.body_len == 0) |
| 5684 | flags |= HTX_SL_F_BODYLESS; |
| 5685 | } |
| 5686 | sl->flags |= flags; |
| 5687 | |
| 5688 | |
| 5689 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5690 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5691 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5692 | goto fail; |
| 5693 | |
| 5694 | /* Now, forward the response and terminate the transaction */ |
| 5695 | s->txn->status = code; |
| 5696 | htx_to_buf(htx, &s->res.buf); |
| 5697 | if (!http_forward_proxy_resp(s, 1)) |
| 5698 | goto fail; |
| 5699 | |
| 5700 | return 1; |
| 5701 | |
| 5702 | fail: |
| 5703 | channel_htx_truncate(&s->res, htx); |
| 5704 | return 0; |
| 5705 | } |
| 5706 | |
| 5707 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5708 | * processing is just aborted. Nothing is returned to the client and all |
| 5709 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5710 | * is forwarded to the client before terminating the transaction. On success, |
| 5711 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5712 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5713 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5714 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5715 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5716 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5717 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5718 | struct stream *s; |
| 5719 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5720 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5721 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5722 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5723 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5724 | * just end the execution of the current lua code. */ |
| 5725 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5726 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5727 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5728 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5729 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5730 | struct channel *req = &s->req; |
| 5731 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5732 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5733 | channel_auto_read(req); |
| 5734 | channel_abort(req); |
| 5735 | channel_auto_close(req); |
| 5736 | channel_erase(req); |
| 5737 | |
| 5738 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5739 | channel_auto_read(res); |
| 5740 | channel_auto_close(res); |
| 5741 | channel_shutr_now(res); |
| 5742 | |
| 5743 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5744 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5745 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5746 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5747 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5748 | /* No reply or invalid reply */ |
| 5749 | s->txn->status = 0; |
| 5750 | http_reply_and_close(s, 0, NULL); |
| 5751 | } |
| 5752 | else { |
| 5753 | /* Remove extra args to have the reply on top of the stack */ |
| 5754 | if (lua_gettop(L) > 2) |
| 5755 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5756 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5757 | if (!hlua_txn_forward_reply(L, s)) { |
| 5758 | if (!(s->flags & SF_ERR_MASK)) |
| 5759 | s->flags |= SF_ERR_PRXCOND; |
| 5760 | lua_pushinteger(L, ACT_RET_ERR); |
| 5761 | WILL_LJMP(hlua_done(L)); |
| 5762 | return 0; /* Never reached */ |
| 5763 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5764 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5765 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5766 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5767 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5768 | /* let's log the request time */ |
| 5769 | s->logs.tv_request = now; |
| 5770 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5771 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5772 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5773 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5774 | done: |
| 5775 | if (!(s->flags & SF_ERR_MASK)) |
| 5776 | s->flags |= SF_ERR_LOCAL; |
| 5777 | if (!(s->flags & SF_FINST_MASK)) |
| 5778 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5779 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5780 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5781 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5782 | return 0; |
| 5783 | } |
| 5784 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5785 | /* |
| 5786 | * |
| 5787 | * |
| 5788 | * Class REPLY |
| 5789 | * |
| 5790 | * |
| 5791 | */ |
| 5792 | |
| 5793 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5794 | * free slots, the function fails and returns 0; |
| 5795 | */ |
| 5796 | static int hlua_txn_reply_new(lua_State *L) |
| 5797 | { |
| 5798 | struct hlua_txn *htxn; |
| 5799 | const char *reason, *body = NULL; |
| 5800 | int ret, status; |
| 5801 | |
| 5802 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5803 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5804 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5805 | WILL_LJMP(lua_error(L)); |
| 5806 | } |
| 5807 | |
| 5808 | /* Default value */ |
| 5809 | status = 200; |
| 5810 | reason = http_get_reason(status); |
| 5811 | |
| 5812 | if (lua_istable(L, 2)) { |
| 5813 | /* load status and reason from the table argument at index 2 */ |
| 5814 | ret = lua_getfield(L, 2, "status"); |
| 5815 | if (ret == LUA_TNIL) |
| 5816 | goto reason; |
| 5817 | else if (ret != LUA_TNUMBER) { |
| 5818 | /* invalid status: ignore the reason */ |
| 5819 | goto body; |
| 5820 | } |
| 5821 | status = lua_tointeger(L, -1); |
| 5822 | |
| 5823 | reason: |
| 5824 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5825 | ret = lua_getfield(L, 2, "reason"); |
| 5826 | if (ret == LUA_TSTRING) |
| 5827 | reason = lua_tostring(L, -1); |
| 5828 | |
| 5829 | body: |
| 5830 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5831 | ret = lua_getfield(L, 2, "body"); |
| 5832 | if (ret == LUA_TSTRING) |
| 5833 | body = lua_tostring(L, -1); |
| 5834 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5835 | } |
| 5836 | |
| 5837 | /* Create the Reply table */ |
| 5838 | lua_newtable(L); |
| 5839 | |
| 5840 | /* Add status element */ |
| 5841 | lua_pushstring(L, "status"); |
| 5842 | lua_pushinteger(L, status); |
| 5843 | lua_settable(L, -3); |
| 5844 | |
| 5845 | /* Add reason element */ |
| 5846 | reason = http_get_reason(status); |
| 5847 | lua_pushstring(L, "reason"); |
| 5848 | lua_pushstring(L, reason); |
| 5849 | lua_settable(L, -3); |
| 5850 | |
| 5851 | /* Add body element, nil if undefined */ |
| 5852 | lua_pushstring(L, "body"); |
| 5853 | if (body) |
| 5854 | lua_pushstring(L, body); |
| 5855 | else |
| 5856 | lua_pushnil(L); |
| 5857 | lua_settable(L, -3); |
| 5858 | |
| 5859 | /* Add headers element */ |
| 5860 | lua_pushstring(L, "headers"); |
| 5861 | lua_newtable(L); |
| 5862 | |
| 5863 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5864 | if (lua_istable(L, 2)) { |
| 5865 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5866 | ret = lua_getfield(L, 2, "headers"); |
| 5867 | if (ret == LUA_TTABLE) { |
| 5868 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5869 | lua_pushnil(L); |
| 5870 | while (lua_next(L, -2) != 0) { |
| 5871 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5872 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5873 | /* invalid value type, skip it */ |
| 5874 | lua_pop(L, 1); |
| 5875 | continue; |
| 5876 | } |
| 5877 | |
| 5878 | |
| 5879 | /* Duplicate the key and swap it with the value. */ |
| 5880 | lua_pushvalue(L, -2); |
| 5881 | lua_insert(L, -2); |
| 5882 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5883 | |
| 5884 | lua_newtable(L); |
| 5885 | lua_insert(L, -2); |
| 5886 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5887 | |
| 5888 | if (lua_isstring(L, -1)) { |
| 5889 | /* push the value in the inner table */ |
| 5890 | lua_rawseti(L, -2, 1); |
| 5891 | } |
| 5892 | else { /* table */ |
| 5893 | lua_pushnil(L); |
| 5894 | while (lua_next(L, -2) != 0) { |
| 5895 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5896 | if (!lua_isstring(L, -1)) { |
| 5897 | /* invalid value type, skip it*/ |
| 5898 | lua_pop(L, 1); |
| 5899 | continue; |
| 5900 | } |
| 5901 | /* push the value in the inner table */ |
| 5902 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5903 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5904 | } |
| 5905 | lua_pop(L, 1); |
| 5906 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5907 | } |
| 5908 | |
| 5909 | /* push (k,v) on the stack in the headers table: |
| 5910 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5911 | */ |
| 5912 | lua_settable(L, -5); |
| 5913 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5914 | } |
| 5915 | } |
| 5916 | lua_pop(L, 1); |
| 5917 | } |
| 5918 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5919 | lua_settable(L, -3); |
| 5920 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5921 | |
| 5922 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5923 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5924 | lua_setmetatable(L, -2); |
| 5925 | return 1; |
| 5926 | } |
| 5927 | |
| 5928 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5929 | * provided, the default one corresponding to the status code is used. |
| 5930 | */ |
| 5931 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5932 | { |
| 5933 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5934 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5935 | |
| 5936 | /* First argument (self) must be a table */ |
| 5937 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5938 | |
| 5939 | if (status < 100 || status > 599) { |
| 5940 | lua_pushboolean(L, 0); |
| 5941 | return 1; |
| 5942 | } |
| 5943 | if (!reason) |
| 5944 | reason = http_get_reason(status); |
| 5945 | |
| 5946 | lua_pushinteger(L, status); |
| 5947 | lua_setfield(L, 1, "status"); |
| 5948 | |
| 5949 | lua_pushstring(L, reason); |
| 5950 | lua_setfield(L, 1, "reason"); |
| 5951 | |
| 5952 | lua_pushboolean(L, 1); |
| 5953 | return 1; |
| 5954 | } |
| 5955 | |
| 5956 | /* Add a header into the reply object. Each header name is associated to an |
| 5957 | * array of values in the "headers" table. If the header name is not found, a |
| 5958 | * new entry is created. |
| 5959 | */ |
| 5960 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5961 | { |
| 5962 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5963 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5964 | int ret; |
| 5965 | |
| 5966 | /* First argument (self) must be a table */ |
| 5967 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5968 | |
| 5969 | /* Push in the stack the "headers" entry. */ |
| 5970 | ret = lua_getfield(L, 1, "headers"); |
| 5971 | if (ret != LUA_TTABLE) { |
| 5972 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5973 | WILL_LJMP(lua_error(L)); |
| 5974 | } |
| 5975 | |
| 5976 | /* check if the header is already registered. If not, register it. */ |
| 5977 | ret = lua_getfield(L, -1, name); |
| 5978 | if (ret == LUA_TNIL) { |
| 5979 | /* Entry not found. */ |
| 5980 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5981 | |
| 5982 | /* Insert the new header name in the array in the top of the stack. |
| 5983 | * It left the new array in the top of the stack. |
| 5984 | */ |
| 5985 | lua_newtable(L); |
| 5986 | lua_pushstring(L, name); |
| 5987 | lua_pushvalue(L, -2); |
| 5988 | lua_settable(L, -4); |
| 5989 | } |
| 5990 | else if (ret != LUA_TTABLE) { |
| 5991 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5992 | WILL_LJMP(lua_error(L)); |
| 5993 | } |
| 5994 | |
| 5995 | /* Now the top od thestack is an array of values. We push |
| 5996 | * the header value as new entry. |
| 5997 | */ |
| 5998 | lua_pushstring(L, value); |
| 5999 | ret = lua_rawlen(L, -2); |
| 6000 | lua_rawseti(L, -2, ret + 1); |
| 6001 | |
| 6002 | lua_pushboolean(L, 1); |
| 6003 | return 1; |
| 6004 | } |
| 6005 | |
| 6006 | /* Remove all occurrences of a given header name. */ |
| 6007 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6008 | { |
| 6009 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6010 | int ret; |
| 6011 | |
| 6012 | /* First argument (self) must be a table */ |
| 6013 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6014 | |
| 6015 | /* Push in the stack the "headers" entry. */ |
| 6016 | ret = lua_getfield(L, 1, "headers"); |
| 6017 | if (ret != LUA_TTABLE) { |
| 6018 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6019 | WILL_LJMP(lua_error(L)); |
| 6020 | } |
| 6021 | |
| 6022 | lua_pushstring(L, name); |
| 6023 | lua_pushnil(L); |
| 6024 | lua_settable(L, -3); |
| 6025 | |
| 6026 | lua_pushboolean(L, 1); |
| 6027 | return 1; |
| 6028 | } |
| 6029 | |
| 6030 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6031 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6032 | { |
| 6033 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6034 | |
| 6035 | /* First argument (self) must be a table */ |
| 6036 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6037 | |
| 6038 | lua_pushstring(L, payload); |
| 6039 | lua_setfield(L, 1, "body"); |
| 6040 | |
| 6041 | lua_pushboolean(L, 1); |
| 6042 | return 1; |
| 6043 | } |
| 6044 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6045 | __LJMP static int hlua_log(lua_State *L) |
| 6046 | { |
| 6047 | int level; |
| 6048 | const char *msg; |
| 6049 | |
| 6050 | MAY_LJMP(check_args(L, 2, "log")); |
| 6051 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6052 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6053 | |
| 6054 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6055 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6056 | |
| 6057 | hlua_sendlog(NULL, level, msg); |
| 6058 | return 0; |
| 6059 | } |
| 6060 | |
| 6061 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6062 | { |
| 6063 | const char *msg; |
| 6064 | |
| 6065 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6066 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6067 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6068 | return 0; |
| 6069 | } |
| 6070 | |
| 6071 | __LJMP static int hlua_log_info(lua_State *L) |
| 6072 | { |
| 6073 | const char *msg; |
| 6074 | |
| 6075 | MAY_LJMP(check_args(L, 1, "info")); |
| 6076 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6077 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6078 | return 0; |
| 6079 | } |
| 6080 | |
| 6081 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6082 | { |
| 6083 | const char *msg; |
| 6084 | |
| 6085 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6086 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6087 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6088 | return 0; |
| 6089 | } |
| 6090 | |
| 6091 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6092 | { |
| 6093 | const char *msg; |
| 6094 | |
| 6095 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6096 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6097 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6098 | return 0; |
| 6099 | } |
| 6100 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6101 | __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] | 6102 | { |
| 6103 | int wakeup_ms = lua_tointeger(L, -1); |
| 6104 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6105 | 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] | 6106 | return 0; |
| 6107 | } |
| 6108 | |
| 6109 | __LJMP static int hlua_sleep(lua_State *L) |
| 6110 | { |
| 6111 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6112 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6113 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6114 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6115 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6116 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6117 | wakeup_ms = tick_add(now_ms, delay); |
| 6118 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6119 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6120 | 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] | 6121 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6122 | } |
| 6123 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6124 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6125 | { |
| 6126 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6127 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6128 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6129 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6130 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6131 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6132 | wakeup_ms = tick_add(now_ms, delay); |
| 6133 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6134 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6135 | 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] | 6136 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6137 | } |
| 6138 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6139 | /* This functionis an LUA binding. it permits to give back |
| 6140 | * the hand at the HAProxy scheduler. It is used when the |
| 6141 | * LUA processing consumes a lot of time. |
| 6142 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6143 | __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] | 6144 | { |
| 6145 | return 0; |
| 6146 | } |
| 6147 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6148 | __LJMP static int hlua_yield(lua_State *L) |
| 6149 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6150 | 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] | 6151 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6152 | } |
| 6153 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6154 | /* This function change the nice of the currently executed |
| 6155 | * task. It is used set low or high priority at the current |
| 6156 | * task. |
| 6157 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6158 | __LJMP static int hlua_set_nice(lua_State *L) |
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 | struct hlua *hlua; |
| 6161 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6162 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6163 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6164 | hlua = hlua_gethlua(L); |
| 6165 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6166 | |
| 6167 | /* If he task is not set, I'm in a start mode. */ |
| 6168 | if (!hlua || !hlua->task) |
| 6169 | return 0; |
| 6170 | |
| 6171 | if (nice < -1024) |
| 6172 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6173 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6174 | nice = 1024; |
| 6175 | |
| 6176 | hlua->task->nice = nice; |
| 6177 | return 0; |
| 6178 | } |
| 6179 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6180 | /* 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] | 6181 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6182 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6183 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6184 | * |
| 6185 | * Task wrapper are longjmp safe because the only one Lua code |
| 6186 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6187 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6188 | 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] | 6189 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6190 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6191 | enum hlua_exec status; |
| 6192 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6193 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6194 | task_set_affinity(task, tid_bit); |
| 6195 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6196 | /* If it is the first call to the task, we must initialize the |
| 6197 | * execution timeouts. |
| 6198 | */ |
| 6199 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6200 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6201 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6202 | /* Execute the Lua code. */ |
| 6203 | status = hlua_ctx_resume(hlua, 1); |
| 6204 | |
| 6205 | switch (status) { |
| 6206 | /* finished or yield */ |
| 6207 | case HLUA_E_OK: |
| 6208 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6209 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6210 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6211 | break; |
| 6212 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6213 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6214 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6215 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6216 | break; |
| 6217 | |
| 6218 | /* finished with error. */ |
| 6219 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6220 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6221 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6222 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6223 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6224 | break; |
| 6225 | |
| 6226 | case HLUA_E_ERR: |
| 6227 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6228 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6229 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6230 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6231 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6232 | break; |
| 6233 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6234 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6235 | } |
| 6236 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6237 | /* This function is an LUA binding that register LUA function to be |
| 6238 | * executed after the HAProxy configuration parsing and before the |
| 6239 | * HAProxy scheduler starts. This function expect only one LUA |
| 6240 | * argument that is a function. This function returns nothing, but |
| 6241 | * throws if an error is encountered. |
| 6242 | */ |
| 6243 | __LJMP static int hlua_register_init(lua_State *L) |
| 6244 | { |
| 6245 | struct hlua_init_function *init; |
| 6246 | int ref; |
| 6247 | |
| 6248 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6249 | |
| 6250 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6251 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6252 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6253 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6254 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6255 | |
| 6256 | init->function_ref = ref; |
| 6257 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6258 | return 0; |
| 6259 | } |
| 6260 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6261 | /* This functio is an LUA binding. It permits to register a task |
| 6262 | * executed in parallel of the main HAroxy activity. The task is |
| 6263 | * created and it is set in the HAProxy scheduler. It can be called |
| 6264 | * from the "init" section, "post init" or during the runtime. |
| 6265 | * |
| 6266 | * Lua prototype: |
| 6267 | * |
| 6268 | * <none> core.register_task(<function>) |
| 6269 | */ |
| 6270 | static int hlua_register_task(lua_State *L) |
| 6271 | { |
| 6272 | struct hlua *hlua; |
| 6273 | struct task *task; |
| 6274 | int ref; |
| 6275 | |
| 6276 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6277 | |
| 6278 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6279 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6280 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6281 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6282 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Christopher Faulet | f2e7362 | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6283 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6284 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6285 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6286 | if (!task) |
| 6287 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6288 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6289 | task->context = hlua; |
| 6290 | task->process = hlua_process_task; |
| 6291 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6292 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6293 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6294 | |
| 6295 | /* Restore the function in the stack. */ |
| 6296 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6297 | hlua->nargs = 0; |
| 6298 | |
| 6299 | /* Schedule task. */ |
| 6300 | task_schedule(task, now_ms); |
| 6301 | |
| 6302 | return 0; |
| 6303 | } |
| 6304 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6305 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6306 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6307 | * resume converters. |
| 6308 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6309 | 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] | 6310 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6311 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6312 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6313 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6314 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6315 | if (!stream) |
| 6316 | return 0; |
| 6317 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6318 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6319 | * Lua context can be not initialized. This behavior |
| 6320 | * permits to save performances because a systematic |
| 6321 | * Lua initialization cause 5% performances loss. |
| 6322 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6323 | if (!stream->hlua) { |
Christopher Faulet | f2e7362 | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6324 | struct hlua *hlua; |
| 6325 | |
| 6326 | hlua = pool_alloc(pool_head_hlua); |
| 6327 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6328 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6329 | return 0; |
| 6330 | } |
Christopher Faulet | f2e7362 | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6331 | HLUA_INIT(hlua); |
| 6332 | stream->hlua = hlua; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6333 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6334 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6335 | return 0; |
| 6336 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6337 | } |
| 6338 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6339 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6340 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6341 | |
| 6342 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6343 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6344 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6345 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6346 | else |
| 6347 | error = "critical error"; |
| 6348 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6349 | return 0; |
| 6350 | } |
| 6351 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6352 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6353 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6354 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6355 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6356 | return 0; |
| 6357 | } |
| 6358 | |
| 6359 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6360 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6361 | |
| 6362 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6363 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6364 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6365 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6366 | return 0; |
| 6367 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6368 | hlua_smp2lua(stream->hlua->T, smp); |
| 6369 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6370 | |
| 6371 | /* push keywords in the stack. */ |
| 6372 | if (arg_p) { |
| 6373 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6374 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6375 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6376 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6377 | return 0; |
| 6378 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6379 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6380 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6381 | } |
| 6382 | } |
| 6383 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6384 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6385 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6386 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6387 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6388 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6389 | } |
| 6390 | |
| 6391 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6392 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6393 | /* finished. */ |
| 6394 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6395 | /* If the stack is empty, the function fails. */ |
| 6396 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6397 | return 0; |
| 6398 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6399 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6400 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6401 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6402 | return 1; |
| 6403 | |
| 6404 | /* yield. */ |
| 6405 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6406 | 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] | 6407 | return 0; |
| 6408 | |
| 6409 | /* finished with error. */ |
| 6410 | case HLUA_E_ERRMSG: |
| 6411 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6412 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6413 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6414 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6415 | return 0; |
| 6416 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6417 | case HLUA_E_ETMOUT: |
| 6418 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6419 | return 0; |
| 6420 | |
| 6421 | case HLUA_E_NOMEM: |
| 6422 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6423 | return 0; |
| 6424 | |
| 6425 | case HLUA_E_YIELD: |
| 6426 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6427 | return 0; |
| 6428 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6429 | case HLUA_E_ERR: |
| 6430 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6431 | 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] | 6432 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6433 | |
| 6434 | default: |
| 6435 | return 0; |
| 6436 | } |
| 6437 | } |
| 6438 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6439 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6440 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6441 | * resume sample-fetches. This function will be called by the sample |
| 6442 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6443 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6444 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6445 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6446 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6447 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6448 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6449 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6450 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6451 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6452 | if (!stream) |
| 6453 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6454 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6455 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6456 | * Lua context can be not initialized. This behavior |
| 6457 | * permits to save performances because a systematic |
| 6458 | * Lua initialization cause 5% performances loss. |
| 6459 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6460 | if (!stream->hlua) { |
Christopher Faulet | f2e7362 | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6461 | struct hlua *hlua; |
| 6462 | |
| 6463 | hlua = pool_alloc(pool_head_hlua); |
| 6464 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6465 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6466 | return 0; |
| 6467 | } |
Christopher Faulet | f2e7362 | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6468 | hlua->T = NULL; |
| 6469 | stream->hlua = hlua; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6470 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6471 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6472 | return 0; |
| 6473 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6474 | } |
| 6475 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6476 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6477 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6478 | |
| 6479 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6480 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6481 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6482 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6483 | else |
| 6484 | error = "critical error"; |
| 6485 | 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] | 6486 | return 0; |
| 6487 | } |
| 6488 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6489 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6490 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6491 | 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] | 6492 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6493 | return 0; |
| 6494 | } |
| 6495 | |
| 6496 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6497 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6498 | |
| 6499 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6500 | 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] | 6501 | 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] | 6502 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6503 | return 0; |
| 6504 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6505 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6506 | |
| 6507 | /* push keywords in the stack. */ |
| 6508 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6509 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6510 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6511 | 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] | 6512 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6513 | return 0; |
| 6514 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6515 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6516 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6517 | } |
| 6518 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6519 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6520 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6521 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6522 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6523 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6527 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6528 | /* finished. */ |
| 6529 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6530 | /* If the stack is empty, the function fails. */ |
| 6531 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6532 | return 0; |
| 6533 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6534 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6535 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6536 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6537 | |
| 6538 | /* Set the end of execution flag. */ |
| 6539 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6540 | return 1; |
| 6541 | |
| 6542 | /* yield. */ |
| 6543 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6544 | 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] | 6545 | return 0; |
| 6546 | |
| 6547 | /* finished with error. */ |
| 6548 | case HLUA_E_ERRMSG: |
| 6549 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6550 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6551 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6552 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6553 | return 0; |
| 6554 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6555 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6556 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6557 | return 0; |
| 6558 | |
| 6559 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6560 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6561 | return 0; |
| 6562 | |
| 6563 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6564 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6565 | return 0; |
| 6566 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6567 | case HLUA_E_ERR: |
| 6568 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6569 | 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] | 6570 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6571 | |
| 6572 | default: |
| 6573 | return 0; |
| 6574 | } |
| 6575 | } |
| 6576 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6577 | /* This function is an LUA binding used for registering |
| 6578 | * "sample-conv" functions. It expects a converter name used |
| 6579 | * in the haproxy configuration file, and an LUA function. |
| 6580 | */ |
| 6581 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6582 | { |
| 6583 | struct sample_conv_kw_list *sck; |
| 6584 | const char *name; |
| 6585 | int ref; |
| 6586 | int len; |
| 6587 | struct hlua_function *fcn; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6588 | struct sample_conv *sc; |
| 6589 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6590 | |
| 6591 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6592 | |
| 6593 | /* First argument : converter name. */ |
| 6594 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6595 | |
| 6596 | /* Second argument : lua function. */ |
| 6597 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6598 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6599 | /* Check if the converter is already registered */ |
| 6600 | trash = get_trash_chunk(); |
| 6601 | chunk_printf(trash, "lua.%s", name); |
| 6602 | sc = find_sample_conv(trash->area, trash->data); |
| 6603 | if (sc != NULL) { |
| 6604 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6605 | "This will become a hard error in version 2.5.\n", name); |
| 6606 | } |
| 6607 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6608 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6609 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6610 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6611 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6612 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6613 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6614 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6615 | |
| 6616 | /* Fill fcn. */ |
| 6617 | fcn->name = strdup(name); |
| 6618 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6619 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6620 | fcn->function_ref = ref; |
| 6621 | |
| 6622 | /* List head */ |
| 6623 | sck->list.n = sck->list.p = NULL; |
| 6624 | |
| 6625 | /* converter keyword. */ |
| 6626 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6627 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6628 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6629 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6630 | |
| 6631 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6632 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6633 | 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] | 6634 | sck->kw[0].val_args = NULL; |
| 6635 | sck->kw[0].in_type = SMP_T_STR; |
| 6636 | sck->kw[0].out_type = SMP_T_STR; |
| 6637 | sck->kw[0].private = fcn; |
| 6638 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6639 | /* Register this new converter */ |
| 6640 | sample_register_convs(sck); |
| 6641 | |
| 6642 | return 0; |
| 6643 | } |
| 6644 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6645 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6646 | * "sample-fetch" functions. It expects a converter name used |
| 6647 | * in the haproxy configuration file, and an LUA function. |
| 6648 | */ |
| 6649 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6650 | { |
| 6651 | const char *name; |
| 6652 | int ref; |
| 6653 | int len; |
| 6654 | struct sample_fetch_kw_list *sfk; |
| 6655 | struct hlua_function *fcn; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6656 | struct sample_fetch *sf; |
| 6657 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6658 | |
| 6659 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6660 | |
| 6661 | /* First argument : sample-fetch name. */ |
| 6662 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6663 | |
| 6664 | /* Second argument : lua function. */ |
| 6665 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6666 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6667 | /* Check if the sample-fetch is already registered */ |
| 6668 | trash = get_trash_chunk(); |
| 6669 | chunk_printf(trash, "lua.%s", name); |
| 6670 | sf = find_sample_fetch(trash->area, trash->data); |
| 6671 | if (sf != NULL) { |
| 6672 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6673 | "This will become a hard error in version 2.5.\n", name); |
| 6674 | } |
| 6675 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6676 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6677 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6678 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6679 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6680 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6681 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6682 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6683 | |
| 6684 | /* Fill fcn. */ |
| 6685 | fcn->name = strdup(name); |
| 6686 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6687 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6688 | fcn->function_ref = ref; |
| 6689 | |
| 6690 | /* List head */ |
| 6691 | sfk->list.n = sfk->list.p = NULL; |
| 6692 | |
| 6693 | /* sample-fetch keyword. */ |
| 6694 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6695 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6696 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6697 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6698 | |
| 6699 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6700 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6701 | 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] | 6702 | sfk->kw[0].val_args = NULL; |
| 6703 | sfk->kw[0].out_type = SMP_T_STR; |
| 6704 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6705 | sfk->kw[0].val = 0; |
| 6706 | sfk->kw[0].private = fcn; |
| 6707 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6708 | /* Register this new fetch. */ |
| 6709 | sample_register_fetches(sfk); |
| 6710 | |
| 6711 | return 0; |
| 6712 | } |
| 6713 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6714 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6715 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6716 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6717 | { |
| 6718 | struct hlua *hlua = hlua_gethlua(L); |
| 6719 | unsigned int delay; |
| 6720 | unsigned int wakeup_ms; |
| 6721 | |
| 6722 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6723 | |
| 6724 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6725 | wakeup_ms = tick_add(now_ms, delay); |
| 6726 | hlua->wake_time = wakeup_ms; |
| 6727 | return 0; |
| 6728 | } |
| 6729 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6730 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6731 | * wrapper during the initialisation period. This function may return any |
| 6732 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6733 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6734 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6735 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6736 | 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] | 6737 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6738 | { |
| 6739 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6740 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6741 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6742 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6743 | |
| 6744 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6745 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6746 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6747 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6748 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6749 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6750 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6751 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6752 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6753 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6754 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6755 | * Lua context can be not initialized. This behavior |
| 6756 | * permits to save performances because a systematic |
| 6757 | * Lua initialization cause 5% performances loss. |
| 6758 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6759 | if (!s->hlua) { |
Christopher Faulet | f2e7362 | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6760 | struct hlua *hlua; |
| 6761 | |
| 6762 | hlua = pool_alloc(pool_head_hlua); |
| 6763 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6764 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6765 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6766 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6767 | } |
Christopher Faulet | f2e7362 | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6768 | HLUA_INIT(hlua); |
| 6769 | s->hlua = hlua; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6770 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6771 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6772 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6773 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6774 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6775 | } |
| 6776 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6777 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6778 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6779 | |
| 6780 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6781 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6782 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6783 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6784 | else |
| 6785 | error = "critical error"; |
| 6786 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6787 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6788 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6789 | } |
| 6790 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6791 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6792 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6793 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6794 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6795 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6796 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6797 | } |
| 6798 | |
| 6799 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6800 | 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] | 6801 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6802 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6803 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6804 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6805 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6806 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6807 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6808 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6809 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6810 | |
| 6811 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6812 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6813 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6814 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6815 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6816 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6817 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6818 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6819 | lua_pushstring(s->hlua->T, *arg); |
| 6820 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6821 | } |
| 6822 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6823 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6824 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6825 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6826 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6827 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6831 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6832 | /* finished. */ |
| 6833 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6834 | /* Catch the return value */ |
| 6835 | if (lua_gettop(s->hlua->T) > 0) |
| 6836 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6837 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6838 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6839 | if (act_ret == ACT_RET_YIELD) { |
| 6840 | if (flags & ACT_OPT_FINAL) |
| 6841 | goto err_yield; |
| 6842 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6843 | if (dir == SMP_OPT_DIR_REQ) |
| 6844 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6845 | s->hlua->wake_time); |
| 6846 | else |
| 6847 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6848 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6849 | } |
| 6850 | goto end; |
| 6851 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6852 | /* yield. */ |
| 6853 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6854 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6855 | if (dir == SMP_OPT_DIR_REQ) |
| 6856 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6857 | s->hlua->wake_time); |
| 6858 | else |
| 6859 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6860 | s->hlua->wake_time); |
| 6861 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6862 | /* Some actions can be wake up when a "write" event |
| 6863 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6864 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6865 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6866 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6867 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6868 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6869 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6870 | act_ret = ACT_RET_YIELD; |
| 6871 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6872 | |
| 6873 | /* finished with error. */ |
| 6874 | case HLUA_E_ERRMSG: |
| 6875 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6876 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6877 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6878 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6879 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6880 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6881 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6882 | 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] | 6883 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6884 | |
| 6885 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6886 | 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] | 6887 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6888 | |
| 6889 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6890 | err_yield: |
| 6891 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6892 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6893 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6894 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6895 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6896 | case HLUA_E_ERR: |
| 6897 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6898 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6899 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6900 | |
| 6901 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6902 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6903 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6904 | |
| 6905 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6906 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6907 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6908 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6909 | } |
| 6910 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6911 | 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] | 6912 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6913 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6914 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6915 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6916 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6917 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6921 | { |
| 6922 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6923 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6924 | struct task *task; |
| 6925 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6926 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6927 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6928 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6929 | if (!hlua) { |
| 6930 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6931 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6932 | return 0; |
| 6933 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6934 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6935 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6936 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6937 | |
| 6938 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6939 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6940 | if (!task) { |
| 6941 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6942 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6943 | return 0; |
| 6944 | } |
| 6945 | task->nice = 0; |
| 6946 | task->context = ctx; |
| 6947 | task->process = hlua_applet_wakeup; |
| 6948 | ctx->ctx.hlua_apptcp.task = task; |
| 6949 | |
| 6950 | /* In the execution wrappers linked with a stream, the |
| 6951 | * Lua context can be not initialized. This behavior |
| 6952 | * permits to save performances because a systematic |
| 6953 | * Lua initialization cause 5% performances loss. |
| 6954 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6955 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6956 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6957 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6958 | return 0; |
| 6959 | } |
| 6960 | |
| 6961 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6962 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6963 | |
| 6964 | /* The following Lua calls can fail. */ |
| 6965 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6966 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6967 | error = lua_tostring(hlua->T, -1); |
| 6968 | else |
| 6969 | error = "critical error"; |
| 6970 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6971 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6972 | return 0; |
| 6973 | } |
| 6974 | |
| 6975 | /* Check stack available size. */ |
| 6976 | if (!lua_checkstack(hlua->T, 1)) { |
| 6977 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6978 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6979 | RESET_SAFE_LJMP(hlua->T); |
| 6980 | return 0; |
| 6981 | } |
| 6982 | |
| 6983 | /* Restore the function in the stack. */ |
| 6984 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6985 | |
| 6986 | /* Create and and push object stream in the stack. */ |
| 6987 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6988 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6989 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6990 | RESET_SAFE_LJMP(hlua->T); |
| 6991 | return 0; |
| 6992 | } |
| 6993 | hlua->nargs = 1; |
| 6994 | |
| 6995 | /* push keywords in the stack. */ |
| 6996 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6997 | if (!lua_checkstack(hlua->T, 1)) { |
| 6998 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6999 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7000 | RESET_SAFE_LJMP(hlua->T); |
| 7001 | return 0; |
| 7002 | } |
| 7003 | lua_pushstring(hlua->T, *arg); |
| 7004 | hlua->nargs++; |
| 7005 | } |
| 7006 | |
| 7007 | RESET_SAFE_LJMP(hlua->T); |
| 7008 | |
| 7009 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7010 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7011 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7012 | |
| 7013 | return 1; |
| 7014 | } |
| 7015 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7016 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7017 | { |
| 7018 | struct stream_interface *si = ctx->owner; |
| 7019 | struct stream *strm = si_strm(si); |
| 7020 | struct channel *res = si_ic(si); |
| 7021 | struct act_rule *rule = ctx->rule; |
| 7022 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7023 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7024 | |
| 7025 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7026 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7027 | /* eat the whole request */ |
| 7028 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7029 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7030 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7031 | |
| 7032 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7033 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7034 | return; |
| 7035 | |
| 7036 | /* Execute the function. */ |
| 7037 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7038 | /* finished. */ |
| 7039 | case HLUA_E_OK: |
| 7040 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7041 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7042 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7043 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7044 | res->flags |= CF_READ_NULL; |
| 7045 | si_shutr(si); |
| 7046 | return; |
| 7047 | |
| 7048 | /* yield. */ |
| 7049 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7050 | if (hlua->wake_time != TICK_ETERNITY) |
| 7051 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7052 | return; |
| 7053 | |
| 7054 | /* finished with error. */ |
| 7055 | case HLUA_E_ERRMSG: |
| 7056 | /* Display log. */ |
| 7057 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7058 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7059 | lua_pop(hlua->T, 1); |
| 7060 | goto error; |
| 7061 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7062 | case HLUA_E_ETMOUT: |
| 7063 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7064 | rule->arg.hlua_rule->fcn.name); |
| 7065 | goto error; |
| 7066 | |
| 7067 | case HLUA_E_NOMEM: |
| 7068 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7069 | rule->arg.hlua_rule->fcn.name); |
| 7070 | goto error; |
| 7071 | |
| 7072 | case HLUA_E_YIELD: /* unexpected */ |
| 7073 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7074 | rule->arg.hlua_rule->fcn.name); |
| 7075 | goto error; |
| 7076 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7077 | case HLUA_E_ERR: |
| 7078 | /* Display log. */ |
| 7079 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7080 | rule->arg.hlua_rule->fcn.name); |
| 7081 | goto error; |
| 7082 | |
| 7083 | default: |
| 7084 | goto error; |
| 7085 | } |
| 7086 | |
| 7087 | error: |
| 7088 | |
| 7089 | /* For all other cases, just close the stream. */ |
| 7090 | si_shutw(si); |
| 7091 | si_shutr(si); |
| 7092 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7093 | } |
| 7094 | |
| 7095 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7096 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7097 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7098 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7099 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7100 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7101 | } |
| 7102 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7103 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7104 | * an errors occurs and -1 if more data are required for initializing |
| 7105 | * the applet. |
| 7106 | */ |
| 7107 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7108 | { |
| 7109 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7110 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7111 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7112 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7113 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7114 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7115 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7116 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7117 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7118 | if (!hlua) { |
| 7119 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7120 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7121 | return 0; |
| 7122 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7123 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7124 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7125 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7126 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7127 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7128 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7129 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7130 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7131 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7132 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7133 | if (!task) { |
| 7134 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7135 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7136 | return 0; |
| 7137 | } |
| 7138 | task->nice = 0; |
| 7139 | task->context = ctx; |
| 7140 | task->process = hlua_applet_wakeup; |
| 7141 | ctx->ctx.hlua_apphttp.task = task; |
| 7142 | |
| 7143 | /* In the execution wrappers linked with a stream, the |
| 7144 | * Lua context can be not initialized. This behavior |
| 7145 | * permits to save performances because a systematic |
| 7146 | * Lua initialization cause 5% performances loss. |
| 7147 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7148 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7149 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7150 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7151 | return 0; |
| 7152 | } |
| 7153 | |
| 7154 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7155 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7156 | |
| 7157 | /* The following Lua calls can fail. */ |
| 7158 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7159 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7160 | error = lua_tostring(hlua->T, -1); |
| 7161 | else |
| 7162 | error = "critical error"; |
| 7163 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7164 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7165 | return 0; |
| 7166 | } |
| 7167 | |
| 7168 | /* Check stack available size. */ |
| 7169 | if (!lua_checkstack(hlua->T, 1)) { |
| 7170 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7171 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7172 | RESET_SAFE_LJMP(hlua->T); |
| 7173 | return 0; |
| 7174 | } |
| 7175 | |
| 7176 | /* Restore the function in the stack. */ |
| 7177 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7178 | |
| 7179 | /* Create and and push object stream in the stack. */ |
| 7180 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7181 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7182 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7183 | RESET_SAFE_LJMP(hlua->T); |
| 7184 | return 0; |
| 7185 | } |
| 7186 | hlua->nargs = 1; |
| 7187 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7188 | /* push keywords in the stack. */ |
| 7189 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7190 | if (!lua_checkstack(hlua->T, 1)) { |
| 7191 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7192 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7193 | RESET_SAFE_LJMP(hlua->T); |
| 7194 | return 0; |
| 7195 | } |
| 7196 | lua_pushstring(hlua->T, *arg); |
| 7197 | hlua->nargs++; |
| 7198 | } |
| 7199 | |
| 7200 | RESET_SAFE_LJMP(hlua->T); |
| 7201 | |
| 7202 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7203 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7204 | |
| 7205 | return 1; |
| 7206 | } |
| 7207 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7208 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7209 | { |
| 7210 | struct stream_interface *si = ctx->owner; |
| 7211 | struct stream *strm = si_strm(si); |
| 7212 | struct channel *req = si_oc(si); |
| 7213 | struct channel *res = si_ic(si); |
| 7214 | struct act_rule *rule = ctx->rule; |
| 7215 | struct proxy *px = strm->be; |
| 7216 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7217 | struct htx *req_htx, *res_htx; |
| 7218 | |
| 7219 | res_htx = htx_from_buf(&res->buf); |
| 7220 | |
| 7221 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7222 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7223 | goto out; |
| 7224 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7225 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7226 | if (!b_size(&res->buf)) { |
| 7227 | si_rx_room_blk(si); |
| 7228 | goto out; |
| 7229 | } |
| 7230 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7231 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7232 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7233 | |
| 7234 | /* Set the currently running flag. */ |
| 7235 | if (!HLUA_IS_RUNNING(hlua) && |
| 7236 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7237 | struct htx_blk *blk; |
| 7238 | size_t count = co_data(req); |
| 7239 | |
| 7240 | if (!count) { |
| 7241 | si_cant_get(si); |
| 7242 | goto out; |
| 7243 | } |
| 7244 | |
| 7245 | /* We need to flush the request header. This left the body for |
| 7246 | * the Lua. |
| 7247 | */ |
| 7248 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7249 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7250 | while (count && blk) { |
| 7251 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7252 | uint32_t sz = htx_get_blksz(blk); |
| 7253 | |
| 7254 | if (sz > count) { |
| 7255 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7256 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7257 | goto out; |
| 7258 | } |
| 7259 | |
| 7260 | count -= sz; |
| 7261 | co_set_data(req, co_data(req) - sz); |
| 7262 | blk = htx_remove_blk(req_htx, blk); |
| 7263 | |
| 7264 | if (type == HTX_BLK_EOH) |
| 7265 | break; |
| 7266 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7267 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7268 | } |
| 7269 | |
| 7270 | /* Executes The applet if it is not done. */ |
| 7271 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7272 | |
| 7273 | /* Execute the function. */ |
| 7274 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7275 | /* finished. */ |
| 7276 | case HLUA_E_OK: |
| 7277 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7278 | break; |
| 7279 | |
| 7280 | /* yield. */ |
| 7281 | case HLUA_E_AGAIN: |
| 7282 | if (hlua->wake_time != TICK_ETERNITY) |
| 7283 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7284 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7285 | |
| 7286 | /* finished with error. */ |
| 7287 | case HLUA_E_ERRMSG: |
| 7288 | /* Display log. */ |
| 7289 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7290 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7291 | lua_pop(hlua->T, 1); |
| 7292 | goto error; |
| 7293 | |
| 7294 | case HLUA_E_ETMOUT: |
| 7295 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7296 | rule->arg.hlua_rule->fcn.name); |
| 7297 | goto error; |
| 7298 | |
| 7299 | case HLUA_E_NOMEM: |
| 7300 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7301 | rule->arg.hlua_rule->fcn.name); |
| 7302 | goto error; |
| 7303 | |
| 7304 | case HLUA_E_YIELD: /* unexpected */ |
| 7305 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7306 | rule->arg.hlua_rule->fcn.name); |
| 7307 | goto error; |
| 7308 | |
| 7309 | case HLUA_E_ERR: |
| 7310 | /* Display log. */ |
| 7311 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7312 | rule->arg.hlua_rule->fcn.name); |
| 7313 | goto error; |
| 7314 | |
| 7315 | default: |
| 7316 | goto error; |
| 7317 | } |
| 7318 | } |
| 7319 | |
| 7320 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7321 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7322 | goto done; |
| 7323 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7324 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7325 | goto error; |
| 7326 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7327 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7328 | 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] | 7329 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7330 | si_rx_room_blk(si); |
| 7331 | goto out; |
| 7332 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7333 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7334 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7335 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | done: |
| 7339 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7340 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7341 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7342 | si_shutr(si); |
| 7343 | } |
| 7344 | |
| 7345 | /* eat the whole request */ |
| 7346 | if (co_data(req)) { |
| 7347 | req_htx = htx_from_buf(&req->buf); |
| 7348 | co_htx_skip(req, req_htx, co_data(req)); |
| 7349 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7350 | } |
| 7351 | } |
| 7352 | |
| 7353 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7354 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7355 | return; |
| 7356 | |
| 7357 | error: |
| 7358 | |
| 7359 | /* If we are in HTTP mode, and we are not send any |
| 7360 | * data, return a 500 server error in best effort: |
| 7361 | * if there is no room available in the buffer, |
| 7362 | * just close the connection. |
| 7363 | */ |
| 7364 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7365 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7366 | |
| 7367 | channel_erase(res); |
| 7368 | res->buf.data = b_data(err); |
| 7369 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7370 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7371 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7372 | } |
| 7373 | if (!(strm->flags & SF_ERR_MASK)) |
| 7374 | strm->flags |= SF_ERR_RESOURCE; |
| 7375 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7376 | goto done; |
| 7377 | } |
| 7378 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7379 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7380 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7381 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7382 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7383 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7384 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7385 | } |
| 7386 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7387 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7388 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7389 | * |
| 7390 | * This function can fail with an abort() due to an Lua critical error. |
| 7391 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7392 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7393 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7394 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7395 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7396 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7397 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7398 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7399 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7400 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7401 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7402 | if (!rule->arg.hlua_rule) { |
| 7403 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7404 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7405 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7406 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7407 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7408 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7409 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7410 | if (!rule->arg.hlua_rule->args) { |
| 7411 | memprintf(err, "out of memory error"); |
| 7412 | return ACT_RET_PRS_ERR; |
| 7413 | } |
| 7414 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7415 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7416 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7417 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7418 | /* Expect some arguments */ |
| 7419 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7420 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7421 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7422 | return ACT_RET_PRS_ERR; |
| 7423 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7424 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7425 | if (!rule->arg.hlua_rule->args[i]) { |
| 7426 | memprintf(err, "out of memory error"); |
| 7427 | return ACT_RET_PRS_ERR; |
| 7428 | } |
| 7429 | (*cur_arg)++; |
| 7430 | } |
| 7431 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7432 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7433 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7434 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7435 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7436 | } |
| 7437 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7438 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7439 | struct act_rule *rule, char **err) |
| 7440 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7441 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7442 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7443 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7444 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7445 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7446 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7447 | * the call of this analyzer. |
| 7448 | */ |
| 7449 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7450 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7451 | return ACT_RET_PRS_ERR; |
| 7452 | } |
| 7453 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7454 | /* Memory for the rule. */ |
| 7455 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7456 | if (!rule->arg.hlua_rule) { |
| 7457 | memprintf(err, "out of memory error"); |
| 7458 | return ACT_RET_PRS_ERR; |
| 7459 | } |
| 7460 | |
| 7461 | /* Reference the Lua function and store the reference. */ |
| 7462 | rule->arg.hlua_rule->fcn = *fcn; |
| 7463 | |
| 7464 | /* TODO: later accept arguments. */ |
| 7465 | rule->arg.hlua_rule->args = NULL; |
| 7466 | |
| 7467 | /* Add applet pointer in the rule. */ |
| 7468 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7469 | rule->applet.name = fcn->name; |
| 7470 | rule->applet.init = hlua_applet_http_init; |
| 7471 | rule->applet.fct = hlua_applet_http_fct; |
| 7472 | rule->applet.release = hlua_applet_http_release; |
| 7473 | rule->applet.timeout = hlua_timeout_applet; |
| 7474 | |
| 7475 | return ACT_RET_PRS_OK; |
| 7476 | } |
| 7477 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7478 | /* This function is an LUA binding used for registering |
| 7479 | * "sample-conv" functions. It expects a converter name used |
| 7480 | * in the haproxy configuration file, and an LUA function. |
| 7481 | */ |
| 7482 | __LJMP static int hlua_register_action(lua_State *L) |
| 7483 | { |
| 7484 | struct action_kw_list *akl; |
| 7485 | const char *name; |
| 7486 | int ref; |
| 7487 | int len; |
| 7488 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7489 | int nargs; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7490 | struct buffer *trash; |
| 7491 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7492 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7493 | /* Initialise the number of expected arguments at 0. */ |
| 7494 | nargs = 0; |
| 7495 | |
| 7496 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7497 | 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] | 7498 | |
| 7499 | /* First argument : converter name. */ |
| 7500 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7501 | |
| 7502 | /* Second argument : environment. */ |
| 7503 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7504 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7505 | |
| 7506 | /* Third argument : lua function. */ |
| 7507 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7508 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7509 | /* 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] | 7510 | if (lua_gettop(L) >= 4) |
| 7511 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7512 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7513 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7514 | lua_pushnil(L); |
| 7515 | while (lua_next(L, 2) != 0) { |
| 7516 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7517 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7518 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7519 | /* Check if action exists */ |
| 7520 | trash = get_trash_chunk(); |
| 7521 | chunk_printf(trash, "lua.%s", name); |
| 7522 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7523 | akw = tcp_req_cont_action(trash->area); |
| 7524 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7525 | akw = tcp_res_cont_action(trash->area); |
| 7526 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7527 | akw = action_http_req_custom(trash->area); |
| 7528 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7529 | akw = action_http_res_custom(trash->area); |
| 7530 | } else { |
| 7531 | akw = NULL; |
| 7532 | } |
| 7533 | if (akw != NULL) { |
| 7534 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7535 | "This will become a hard error in version 2.5.\n", name); |
| 7536 | } |
| 7537 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7538 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7539 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7540 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7541 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7542 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7543 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7544 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7545 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7546 | |
| 7547 | /* Fill fcn. */ |
| 7548 | fcn->name = strdup(name); |
| 7549 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7550 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7551 | fcn->function_ref = ref; |
| 7552 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7553 | /* Set the expected number od arguments. */ |
| 7554 | fcn->nargs = nargs; |
| 7555 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7556 | /* List head */ |
| 7557 | akl->list.n = akl->list.p = NULL; |
| 7558 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7559 | /* action keyword. */ |
| 7560 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7561 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7562 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7563 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7564 | |
| 7565 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7566 | |
| 7567 | akl->kw[0].match_pfx = 0; |
| 7568 | akl->kw[0].private = fcn; |
| 7569 | akl->kw[0].parse = action_register_lua; |
| 7570 | |
| 7571 | /* select the action registering point. */ |
| 7572 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7573 | tcp_req_cont_keywords_register(akl); |
| 7574 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7575 | tcp_res_cont_keywords_register(akl); |
| 7576 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7577 | http_req_keywords_register(akl); |
| 7578 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7579 | http_res_keywords_register(akl); |
| 7580 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7581 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7582 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7583 | "are expected.", lua_tostring(L, -1))); |
| 7584 | |
| 7585 | /* pop the environment string. */ |
| 7586 | lua_pop(L, 1); |
| 7587 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7588 | return ACT_RET_PRS_OK; |
| 7589 | } |
| 7590 | |
| 7591 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7592 | struct act_rule *rule, char **err) |
| 7593 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7594 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7595 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7596 | if (px->mode == PR_MODE_HTTP) { |
| 7597 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7598 | return ACT_RET_PRS_ERR; |
| 7599 | } |
| 7600 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7601 | /* Memory for the rule. */ |
| 7602 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7603 | if (!rule->arg.hlua_rule) { |
| 7604 | memprintf(err, "out of memory error"); |
| 7605 | return ACT_RET_PRS_ERR; |
| 7606 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7607 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7608 | /* Reference the Lua function and store the reference. */ |
| 7609 | rule->arg.hlua_rule->fcn = *fcn; |
| 7610 | |
| 7611 | /* TODO: later accept arguments. */ |
| 7612 | rule->arg.hlua_rule->args = NULL; |
| 7613 | |
| 7614 | /* Add applet pointer in the rule. */ |
| 7615 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7616 | rule->applet.name = fcn->name; |
| 7617 | rule->applet.init = hlua_applet_tcp_init; |
| 7618 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7619 | rule->applet.release = hlua_applet_tcp_release; |
| 7620 | rule->applet.timeout = hlua_timeout_applet; |
| 7621 | |
| 7622 | return 0; |
| 7623 | } |
| 7624 | |
| 7625 | /* This function is an LUA binding used for registering |
| 7626 | * "sample-conv" functions. It expects a converter name used |
| 7627 | * in the haproxy configuration file, and an LUA function. |
| 7628 | */ |
| 7629 | __LJMP static int hlua_register_service(lua_State *L) |
| 7630 | { |
| 7631 | struct action_kw_list *akl; |
| 7632 | const char *name; |
| 7633 | const char *env; |
| 7634 | int ref; |
| 7635 | int len; |
| 7636 | struct hlua_function *fcn; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7637 | struct buffer *trash; |
| 7638 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7639 | |
| 7640 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7641 | |
| 7642 | /* First argument : converter name. */ |
| 7643 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7644 | |
| 7645 | /* Second argument : environment. */ |
| 7646 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7647 | |
| 7648 | /* Third argument : lua function. */ |
| 7649 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7650 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7651 | /* Check for service already registered */ |
| 7652 | trash = get_trash_chunk(); |
| 7653 | chunk_printf(trash, "lua.%s", name); |
| 7654 | akw = service_find(trash->area); |
| 7655 | if (akw != NULL) { |
| 7656 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7657 | "This will become a hard error in version 2.5.\n", name); |
| 7658 | } |
| 7659 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7660 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7661 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7662 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7663 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7664 | fcn = calloc(1, sizeof(*fcn)); |
| 7665 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7666 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7667 | |
| 7668 | /* Fill fcn. */ |
| 7669 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7670 | fcn->name = calloc(1, len); |
| 7671 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7672 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7673 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7674 | fcn->function_ref = ref; |
| 7675 | |
| 7676 | /* List head */ |
| 7677 | akl->list.n = akl->list.p = NULL; |
| 7678 | |
| 7679 | /* converter keyword. */ |
| 7680 | len = strlen("lua.") + strlen(name) + 1; |
| 7681 | akl->kw[0].kw = calloc(1, len); |
| 7682 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7683 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7684 | |
| 7685 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7686 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7687 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7688 | if (strcmp(env, "tcp") == 0) |
| 7689 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7690 | else if (strcmp(env, "http") == 0) |
| 7691 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7692 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7693 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7694 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7695 | |
| 7696 | akl->kw[0].match_pfx = 0; |
| 7697 | akl->kw[0].private = fcn; |
| 7698 | |
| 7699 | /* End of array. */ |
| 7700 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7701 | |
| 7702 | /* Register this new converter */ |
| 7703 | service_keywords_register(akl); |
| 7704 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7705 | return 0; |
| 7706 | } |
| 7707 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7708 | /* This function initialises Lua cli handler. It copies the |
| 7709 | * arguments in the Lua stack and create channel IO objects. |
| 7710 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7711 | 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] | 7712 | { |
| 7713 | struct hlua *hlua; |
| 7714 | struct hlua_function *fcn; |
| 7715 | int i; |
| 7716 | const char *error; |
| 7717 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7718 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7719 | appctx->ctx.hlua_cli.fcn = private; |
| 7720 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7721 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7722 | if (!hlua) { |
| 7723 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7724 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7725 | } |
| 7726 | HLUA_INIT(hlua); |
| 7727 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7728 | |
| 7729 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7730 | * 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] | 7731 | * applet_http. It is absolutely compatible. |
| 7732 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7733 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7734 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7735 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7736 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7737 | } |
| 7738 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7739 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7740 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7741 | |
| 7742 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7743 | 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] | 7744 | 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] | 7745 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7746 | } |
| 7747 | |
| 7748 | /* The following Lua calls can fail. */ |
| 7749 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7750 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7751 | error = lua_tostring(hlua->T, -1); |
| 7752 | else |
| 7753 | error = "critical error"; |
| 7754 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7755 | goto error; |
| 7756 | } |
| 7757 | |
| 7758 | /* Check stack available size. */ |
| 7759 | if (!lua_checkstack(hlua->T, 2)) { |
| 7760 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7761 | goto error; |
| 7762 | } |
| 7763 | |
| 7764 | /* Restore the function in the stack. */ |
| 7765 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7766 | |
| 7767 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7768 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7769 | */ |
| 7770 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7771 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7772 | goto error; |
| 7773 | } |
| 7774 | hlua->nargs = 1; |
| 7775 | |
| 7776 | /* push keywords in the stack. */ |
| 7777 | for (i = 0; *args[i]; i++) { |
| 7778 | /* Check stack available size. */ |
| 7779 | if (!lua_checkstack(hlua->T, 1)) { |
| 7780 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7781 | goto error; |
| 7782 | } |
| 7783 | lua_pushstring(hlua->T, args[i]); |
| 7784 | hlua->nargs++; |
| 7785 | } |
| 7786 | |
| 7787 | /* We must initialize the execution timeouts. */ |
| 7788 | hlua->max_time = hlua_timeout_session; |
| 7789 | |
| 7790 | /* At this point the execution is safe. */ |
| 7791 | RESET_SAFE_LJMP(hlua->T); |
| 7792 | |
| 7793 | /* It's ok */ |
| 7794 | return 0; |
| 7795 | |
| 7796 | /* It's not ok. */ |
| 7797 | error: |
| 7798 | RESET_SAFE_LJMP(hlua->T); |
| 7799 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7800 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7801 | return 1; |
| 7802 | } |
| 7803 | |
| 7804 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7805 | { |
| 7806 | struct hlua *hlua; |
| 7807 | struct stream_interface *si; |
| 7808 | struct hlua_function *fcn; |
| 7809 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7810 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7811 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7812 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7813 | |
| 7814 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7815 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7816 | return 1; |
| 7817 | |
| 7818 | /* Execute the function. */ |
| 7819 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7820 | |
| 7821 | /* finished. */ |
| 7822 | case HLUA_E_OK: |
| 7823 | return 1; |
| 7824 | |
| 7825 | /* yield. */ |
| 7826 | case HLUA_E_AGAIN: |
| 7827 | /* We want write. */ |
| 7828 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7829 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7830 | /* Set the timeout. */ |
| 7831 | if (hlua->wake_time != TICK_ETERNITY) |
| 7832 | task_schedule(hlua->task, hlua->wake_time); |
| 7833 | return 0; |
| 7834 | |
| 7835 | /* finished with error. */ |
| 7836 | case HLUA_E_ERRMSG: |
| 7837 | /* Display log. */ |
| 7838 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7839 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7840 | lua_pop(hlua->T, 1); |
| 7841 | return 1; |
| 7842 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7843 | case HLUA_E_ETMOUT: |
| 7844 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7845 | fcn->name); |
| 7846 | return 1; |
| 7847 | |
| 7848 | case HLUA_E_NOMEM: |
| 7849 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7850 | fcn->name); |
| 7851 | return 1; |
| 7852 | |
| 7853 | case HLUA_E_YIELD: /* unexpected */ |
| 7854 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7855 | fcn->name); |
| 7856 | return 1; |
| 7857 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7858 | case HLUA_E_ERR: |
| 7859 | /* Display log. */ |
| 7860 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7861 | fcn->name); |
| 7862 | return 1; |
| 7863 | |
| 7864 | default: |
| 7865 | return 1; |
| 7866 | } |
| 7867 | |
| 7868 | return 1; |
| 7869 | } |
| 7870 | |
| 7871 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7872 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7873 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7874 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7875 | } |
| 7876 | |
| 7877 | /* This function is an LUA binding used for registering |
| 7878 | * new keywords in the cli. It expects a list of keywords |
| 7879 | * which are the "path". It is limited to 5 keywords. A |
| 7880 | * description of the command, a function to be executed |
| 7881 | * for the parsing and a function for io handlers. |
| 7882 | */ |
| 7883 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7884 | { |
| 7885 | struct cli_kw_list *cli_kws; |
| 7886 | const char *message; |
| 7887 | int ref_io; |
| 7888 | int len; |
| 7889 | struct hlua_function *fcn; |
| 7890 | int index; |
| 7891 | int i; |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7892 | struct buffer *trash; |
| 7893 | const char *kw[5]; |
| 7894 | struct cli_kw *cli_kw; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7895 | |
| 7896 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7897 | |
| 7898 | /* First argument : an array of maximum 5 keywords. */ |
| 7899 | if (!lua_istable(L, 1)) |
| 7900 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7901 | |
| 7902 | /* Second argument : string with contextual message. */ |
| 7903 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7904 | |
| 7905 | /* Third and fourth argument : lua function. */ |
| 7906 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7907 | |
Thierry Fournier | 04cbfd7 | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7908 | /* Check for CLI service already registered */ |
| 7909 | trash = get_trash_chunk(); |
| 7910 | index = 0; |
| 7911 | lua_pushnil(L); |
| 7912 | memset(kw, 0, sizeof(kw)); |
| 7913 | while (lua_next(L, 1) != 0) { |
| 7914 | if (index >= CLI_PREFIX_KW_NB) |
| 7915 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7916 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7917 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7918 | kw[index] = lua_tostring(L, -1); |
| 7919 | if (index == 0) |
| 7920 | chunk_printf(trash, "%s", kw[index]); |
| 7921 | else |
| 7922 | chunk_appendf(trash, " %s", kw[index]); |
| 7923 | index++; |
| 7924 | lua_pop(L, 1); |
| 7925 | } |
| 7926 | cli_kw = cli_find_kw_exact((char **)kw); |
| 7927 | if (cli_kw != NULL) { |
| 7928 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 7929 | "This will become a hard error in version 2.5.\n", trash->area); |
| 7930 | } |
| 7931 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7932 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7933 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7934 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7935 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7936 | fcn = calloc(1, sizeof(*fcn)); |
| 7937 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7938 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7939 | |
| 7940 | /* Fill path. */ |
| 7941 | index = 0; |
| 7942 | lua_pushnil(L); |
| 7943 | while(lua_next(L, 1) != 0) { |
| 7944 | if (index >= 5) |
| 7945 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7946 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7947 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7948 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7949 | if (!cli_kws->kw[0].str_kw[index]) |
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 | index++; |
| 7952 | lua_pop(L, 1); |
| 7953 | } |
| 7954 | |
| 7955 | /* Copy help message. */ |
| 7956 | cli_kws->kw[0].usage = strdup(message); |
| 7957 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7958 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7959 | |
| 7960 | /* Fill fcn io handler. */ |
| 7961 | len = strlen("<lua.cli>") + 1; |
| 7962 | for (i = 0; i < index; i++) |
| 7963 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7964 | fcn->name = calloc(1, len); |
| 7965 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7966 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7967 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7968 | for (i = 0; i < index; i++) { |
| 7969 | strncat((char *)fcn->name, ".", len); |
| 7970 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7971 | } |
| 7972 | strncat((char *)fcn->name, ">", len); |
| 7973 | fcn->function_ref = ref_io; |
| 7974 | |
| 7975 | /* Fill last entries. */ |
| 7976 | cli_kws->kw[0].private = fcn; |
| 7977 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7978 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7979 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7980 | |
| 7981 | /* Register this new converter */ |
| 7982 | cli_register_kw(cli_kws); |
| 7983 | |
| 7984 | return 0; |
| 7985 | } |
| 7986 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7987 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7988 | struct proxy *defpx, const char *file, int line, |
| 7989 | char **err, unsigned int *timeout) |
| 7990 | { |
| 7991 | const char *error; |
| 7992 | |
| 7993 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7994 | if (error == PARSE_TIME_OVER) { |
| 7995 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7996 | args[1], args[0]); |
| 7997 | return -1; |
| 7998 | } |
| 7999 | else if (error == PARSE_TIME_UNDER) { |
| 8000 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8001 | args[1], args[0]); |
| 8002 | return -1; |
| 8003 | } |
| 8004 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8005 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8006 | return -1; |
| 8007 | } |
| 8008 | return 0; |
| 8009 | } |
| 8010 | |
| 8011 | static int hlua_session_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_session); |
| 8017 | } |
| 8018 | |
| 8019 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8020 | struct proxy *defpx, const char *file, int line, |
| 8021 | char **err) |
| 8022 | { |
| 8023 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8024 | file, line, err, &hlua_timeout_task); |
| 8025 | } |
| 8026 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8027 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8028 | struct proxy *defpx, const char *file, int line, |
| 8029 | char **err) |
| 8030 | { |
| 8031 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8032 | file, line, err, &hlua_timeout_applet); |
| 8033 | } |
| 8034 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8035 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8036 | struct proxy *defpx, const char *file, int line, |
| 8037 | char **err) |
| 8038 | { |
| 8039 | char *error; |
| 8040 | |
| 8041 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8042 | if (*error != '\0') { |
| 8043 | memprintf(err, "%s: invalid number", args[0]); |
| 8044 | return -1; |
| 8045 | } |
| 8046 | return 0; |
| 8047 | } |
| 8048 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8049 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8050 | struct proxy *defpx, const char *file, int line, |
| 8051 | char **err) |
| 8052 | { |
| 8053 | char *error; |
| 8054 | |
| 8055 | if (*(args[1]) == 0) { |
| 8056 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8057 | return -1; |
| 8058 | } |
| 8059 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8060 | if (*error != '\0') { |
| 8061 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8062 | return -1; |
| 8063 | } |
| 8064 | return 0; |
| 8065 | } |
| 8066 | |
| 8067 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8068 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8069 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8070 | * the main lua entry point. |
| 8071 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8072 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8073 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8074 | * |
| 8075 | * In some error case, LUA set an error message in top of the stack. This function |
| 8076 | * 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] | 8077 | * |
| 8078 | * This function can fail with an abort() due to an Lua critical error. |
| 8079 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8080 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8081 | */ |
| 8082 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8083 | struct proxy *defpx, const char *file, int line, |
| 8084 | char **err) |
| 8085 | { |
| 8086 | int error; |
| 8087 | |
Thierry Fournier | ad6e9c5 | 2020-11-29 01:06:24 +0100 | [diff] [blame] | 8088 | if (*(args[1]) == 0) { |
| 8089 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8090 | return -1; |
| 8091 | } |
| 8092 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8093 | /* Just load and compile the file. */ |
| 8094 | error = luaL_loadfile(gL.T, args[1]); |
| 8095 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8096 | 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] | 8097 | lua_pop(gL.T, 1); |
| 8098 | return -1; |
| 8099 | } |
| 8100 | |
| 8101 | /* If no syntax error where detected, execute the code. */ |
| 8102 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8103 | switch (error) { |
| 8104 | case LUA_OK: |
| 8105 | break; |
| 8106 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8107 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8108 | lua_pop(gL.T, 1); |
| 8109 | return -1; |
| 8110 | case LUA_ERRMEM: |
Thierry Fournier | 518017f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8111 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8112 | return -1; |
| 8113 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8114 | 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] | 8115 | lua_pop(gL.T, 1); |
| 8116 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8117 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8118 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8119 | 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] | 8120 | lua_pop(gL.T, 1); |
| 8121 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8122 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8123 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8124 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8125 | lua_pop(gL.T, 1); |
| 8126 | return -1; |
| 8127 | } |
| 8128 | |
| 8129 | return 0; |
| 8130 | } |
| 8131 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8132 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8133 | * in the given <ctx>. |
| 8134 | */ |
| 8135 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 8136 | { |
| 8137 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 8138 | lua_pushstring(ctx.T, path); /* push given path */ |
| 8139 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 8140 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 8141 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 8142 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 8143 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 8144 | |
| 8145 | return 0; |
| 8146 | } |
| 8147 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8148 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 8149 | struct proxy *defpx, const char *file, int line, |
| 8150 | char **err) |
| 8151 | { |
| 8152 | char *path; |
| 8153 | char *type = "path"; |
| 8154 | if (too_many_args(2, args, err, NULL)) { |
| 8155 | return -1; |
| 8156 | } |
| 8157 | |
| 8158 | if (!(*args[1])) { |
| 8159 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 8160 | return -1; |
| 8161 | } |
| 8162 | path = args[1]; |
| 8163 | |
| 8164 | if (*args[2]) { |
| 8165 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8166 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 8167 | return -1; |
| 8168 | } |
| 8169 | type = args[2]; |
| 8170 | } |
| 8171 | |
| 8172 | return hlua_prepend_path(gL, type, path); |
| 8173 | } |
| 8174 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8175 | /* configuration keywords declaration */ |
| 8176 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8177 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8178 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8179 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8180 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8181 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8182 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8183 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8184 | { 0, NULL, NULL }, |
| 8185 | }}; |
| 8186 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8187 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8188 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8189 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8190 | /* This function can fail with an abort() due to an Lua critical error. |
| 8191 | * We are in the initialisation process of HAProxy, this abort() is |
| 8192 | * tolerated. |
| 8193 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8194 | int hlua_post_init() |
| 8195 | { |
| 8196 | struct hlua_init_function *init; |
| 8197 | const char *msg; |
| 8198 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8199 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8200 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8201 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8202 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8203 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8204 | error = lua_tostring(gL.T, -1); |
| 8205 | else |
| 8206 | error = "critical error"; |
| 8207 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8208 | exit(1); |
| 8209 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8210 | |
| 8211 | #if USE_OPENSSL |
| 8212 | /* Initialize SSL server. */ |
| 8213 | if (socket_ssl.xprt->prepare_srv) { |
| 8214 | int saved_used_backed = global.ssl_used_backend; |
| 8215 | // don't affect maxconn automatic computation |
| 8216 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8217 | global.ssl_used_backend = saved_used_backed; |
| 8218 | } |
| 8219 | #endif |
| 8220 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8221 | hlua_fcn_post_init(gL.T); |
| 8222 | RESET_SAFE_LJMP(gL.T); |
| 8223 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8224 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8225 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8226 | ret = hlua_ctx_resume(&gL, 0); |
| 8227 | switch (ret) { |
| 8228 | case HLUA_E_OK: |
| 8229 | lua_pop(gL.T, -1); |
Thierry Fournier | 5a96467 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8230 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8231 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8232 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8233 | return 0; |
| 8234 | case HLUA_E_ERRMSG: |
| 8235 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8236 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8237 | return 0; |
| 8238 | case HLUA_E_ERR: |
| 8239 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8240 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8241 | return 0; |
| 8242 | } |
| 8243 | } |
| 8244 | return 1; |
| 8245 | } |
| 8246 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8247 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8248 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8249 | * is the previously allocated size or the kind of object in case of a new |
| 8250 | * allocation. <nsize> is the requested new size. |
| 8251 | */ |
| 8252 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8253 | { |
| 8254 | struct hlua_mem_allocator *zone = ud; |
| 8255 | |
| 8256 | if (nsize == 0) { |
| 8257 | /* it's a free */ |
| 8258 | if (ptr) |
| 8259 | zone->allocated -= osize; |
| 8260 | free(ptr); |
| 8261 | return NULL; |
| 8262 | } |
| 8263 | |
| 8264 | if (!ptr) { |
| 8265 | /* it's a new allocation */ |
| 8266 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8267 | return NULL; |
| 8268 | |
| 8269 | ptr = malloc(nsize); |
| 8270 | if (ptr) |
| 8271 | zone->allocated += nsize; |
| 8272 | return ptr; |
| 8273 | } |
| 8274 | |
| 8275 | /* it's a realloc */ |
| 8276 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8277 | return NULL; |
| 8278 | |
| 8279 | ptr = realloc(ptr, nsize); |
| 8280 | if (ptr) |
| 8281 | zone->allocated += nsize - osize; |
| 8282 | return ptr; |
| 8283 | } |
| 8284 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8285 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8286 | * We are in the initialisation process of HAProxy, this abort() is |
| 8287 | * tolerated. |
| 8288 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8289 | void hlua_init(void) |
| 8290 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8291 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8292 | int idx; |
| 8293 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8294 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8295 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8296 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8297 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8298 | struct srv_kw *kw; |
| 8299 | int tmp_error; |
| 8300 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8301 | char *args[] = { /* SSL client configuration. */ |
| 8302 | "ssl", |
| 8303 | "verify", |
| 8304 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8305 | NULL |
| 8306 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8307 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8308 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8309 | /* Init main lua stack. */ |
| 8310 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8311 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8312 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8313 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8314 | hlua_sethlua(&gL); |
| 8315 | gL.Tref = LUA_REFNIL; |
| 8316 | gL.task = NULL; |
| 8317 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8318 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8319 | * the Lua function can fail with an abort. We are in the initialisation |
| 8320 | * process of HAProxy, this abort() is tolerated. |
| 8321 | */ |
| 8322 | |
Thierry Fournier | 2b7983c | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8323 | /* Set safe environment for the initialisation. */ |
| 8324 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8325 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8326 | error_msg = lua_tostring(gL.T, -1); |
| 8327 | else |
| 8328 | error_msg = "critical error"; |
| 8329 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8330 | exit(1); |
| 8331 | } |
| 8332 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8333 | /* Initialise lua. */ |
| 8334 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8335 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8336 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8337 | #ifdef HLUA_PREPEND_PATH |
| 8338 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8339 | #endif |
| 8340 | #ifdef HLUA_PREPEND_CPATH |
| 8341 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8342 | #endif |
| 8343 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8344 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8345 | |
| 8346 | /* |
| 8347 | * |
| 8348 | * Create "core" object. |
| 8349 | * |
| 8350 | */ |
| 8351 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8352 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8353 | lua_newtable(gL.T); |
| 8354 | |
| 8355 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8356 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8357 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8358 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8359 | /* Register special functions. */ |
| 8360 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8361 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8362 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8363 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8364 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8365 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8366 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8367 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8368 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8369 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8370 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8371 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8372 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8373 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8374 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8375 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8376 | hlua_class_function(gL.T, "log", hlua_log); |
| 8377 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8378 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8379 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8380 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8381 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8382 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8383 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8384 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8385 | |
| 8386 | /* |
| 8387 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8388 | * Create "act" object. |
| 8389 | * |
| 8390 | */ |
| 8391 | |
| 8392 | /* This table entry is the object "act" base. */ |
| 8393 | lua_newtable(gL.T); |
| 8394 | |
| 8395 | /* push action return constants */ |
| 8396 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8397 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8398 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8399 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8400 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8401 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8402 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8403 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8404 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8405 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8406 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8407 | lua_setglobal(gL.T, "act"); |
| 8408 | |
| 8409 | /* |
| 8410 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8411 | * Register class Map |
| 8412 | * |
| 8413 | */ |
| 8414 | |
| 8415 | /* This table entry is the object "Map" base. */ |
| 8416 | lua_newtable(gL.T); |
| 8417 | |
| 8418 | /* register pattern types. */ |
| 8419 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8420 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8421 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8422 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8423 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8424 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8425 | |
| 8426 | /* register constructor. */ |
| 8427 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8428 | |
| 8429 | /* Create and fill the metatable. */ |
| 8430 | lua_newtable(gL.T); |
| 8431 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8432 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8433 | lua_pushstring(gL.T, "__index"); |
| 8434 | lua_newtable(gL.T); |
| 8435 | |
| 8436 | /* Register . */ |
| 8437 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8438 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8439 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8440 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8441 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8442 | /* Register previous table in the registry with reference and named entry. |
| 8443 | * The function hlua_register_metatable() pops the stack, so we |
| 8444 | * previously create a copy of the table. |
| 8445 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8446 | 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] | 8447 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8448 | |
| 8449 | /* Assign the metatable to the mai Map object. */ |
| 8450 | lua_setmetatable(gL.T, -2); |
| 8451 | |
| 8452 | /* Set a name to the table. */ |
| 8453 | lua_setglobal(gL.T, "Map"); |
| 8454 | |
| 8455 | /* |
| 8456 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8457 | * Register class Channel |
| 8458 | * |
| 8459 | */ |
| 8460 | |
| 8461 | /* Create and fill the metatable. */ |
| 8462 | lua_newtable(gL.T); |
| 8463 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8464 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8465 | lua_pushstring(gL.T, "__index"); |
| 8466 | lua_newtable(gL.T); |
| 8467 | |
| 8468 | /* Register . */ |
| 8469 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8470 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8471 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8472 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8473 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8474 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8475 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8476 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8477 | 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] | 8478 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8479 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8480 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8481 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8482 | |
| 8483 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8484 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8485 | |
| 8486 | /* |
| 8487 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8488 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8489 | * |
| 8490 | */ |
| 8491 | |
| 8492 | /* Create and fill the metatable. */ |
| 8493 | lua_newtable(gL.T); |
| 8494 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8495 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8496 | lua_pushstring(gL.T, "__index"); |
| 8497 | lua_newtable(gL.T); |
| 8498 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8499 | /* Browse existing fetches and create the associated |
| 8500 | * object method. |
| 8501 | */ |
| 8502 | sf = NULL; |
| 8503 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8504 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8505 | * by an underscore. |
| 8506 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8507 | strncpy(trash.area, sf->kw, trash.size); |
| 8508 | trash.area[trash.size - 1] = '\0'; |
| 8509 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8510 | if (*p == '.' || *p == '-' || *p == '+') |
| 8511 | *p = '_'; |
| 8512 | |
| 8513 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8514 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8515 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8516 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8517 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8518 | } |
| 8519 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8520 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8521 | |
| 8522 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8523 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8524 | |
| 8525 | /* |
| 8526 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8527 | * Register class Converters |
| 8528 | * |
| 8529 | */ |
| 8530 | |
| 8531 | /* Create and fill the metatable. */ |
| 8532 | lua_newtable(gL.T); |
| 8533 | |
| 8534 | /* Create and fill the __index entry. */ |
| 8535 | lua_pushstring(gL.T, "__index"); |
| 8536 | lua_newtable(gL.T); |
| 8537 | |
| 8538 | /* Browse existing converters and create the associated |
| 8539 | * object method. |
| 8540 | */ |
| 8541 | sc = NULL; |
| 8542 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8543 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8544 | * by an underscore. |
| 8545 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8546 | strncpy(trash.area, sc->kw, trash.size); |
| 8547 | trash.area[trash.size - 1] = '\0'; |
| 8548 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8549 | if (*p == '.' || *p == '-' || *p == '+') |
| 8550 | *p = '_'; |
| 8551 | |
| 8552 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8553 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8554 | lua_pushlightuserdata(gL.T, sc); |
| 8555 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8556 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8557 | } |
| 8558 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8559 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8560 | |
| 8561 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8562 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8563 | |
| 8564 | /* |
| 8565 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8566 | * Register class HTTP |
| 8567 | * |
| 8568 | */ |
| 8569 | |
| 8570 | /* Create and fill the metatable. */ |
| 8571 | lua_newtable(gL.T); |
| 8572 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8573 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8574 | lua_pushstring(gL.T, "__index"); |
| 8575 | lua_newtable(gL.T); |
| 8576 | |
| 8577 | /* Register Lua functions. */ |
| 8578 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8579 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8580 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8581 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8582 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8583 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8584 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8585 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8586 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8587 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8588 | |
| 8589 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8590 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8591 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8592 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8593 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8594 | 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] | 8595 | 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] | 8596 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8597 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8598 | |
| 8599 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8600 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8601 | |
| 8602 | /* |
| 8603 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8604 | * Register class AppletTCP |
| 8605 | * |
| 8606 | */ |
| 8607 | |
| 8608 | /* Create and fill the metatable. */ |
| 8609 | lua_newtable(gL.T); |
| 8610 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8611 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8612 | lua_pushstring(gL.T, "__index"); |
| 8613 | lua_newtable(gL.T); |
| 8614 | |
| 8615 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8616 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8617 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8618 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8619 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8620 | 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] | 8621 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8622 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8623 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8624 | |
| 8625 | lua_settable(gL.T, -3); |
| 8626 | |
| 8627 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8628 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8629 | |
| 8630 | /* |
| 8631 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8632 | * Register class AppletHTTP |
| 8633 | * |
| 8634 | */ |
| 8635 | |
| 8636 | /* Create and fill the metatable. */ |
| 8637 | lua_newtable(gL.T); |
| 8638 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8639 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8640 | lua_pushstring(gL.T, "__index"); |
| 8641 | lua_newtable(gL.T); |
| 8642 | |
| 8643 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8644 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8645 | 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] | 8646 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8647 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8648 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8649 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8650 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8651 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8652 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8653 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8654 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8655 | |
| 8656 | lua_settable(gL.T, -3); |
| 8657 | |
| 8658 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8659 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8660 | |
| 8661 | /* |
| 8662 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8663 | * Register class TXN |
| 8664 | * |
| 8665 | */ |
| 8666 | |
| 8667 | /* Create and fill the metatable. */ |
| 8668 | lua_newtable(gL.T); |
| 8669 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8670 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8671 | lua_pushstring(gL.T, "__index"); |
| 8672 | lua_newtable(gL.T); |
| 8673 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8674 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8675 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8676 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8677 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8678 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8679 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8680 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8681 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8682 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8683 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8684 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8685 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8686 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8687 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8688 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8689 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8690 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8691 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8692 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8693 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8694 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8695 | |
| 8696 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8697 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8698 | |
| 8699 | /* |
| 8700 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8701 | * Register class reply |
| 8702 | * |
| 8703 | */ |
| 8704 | lua_newtable(gL.T); |
| 8705 | lua_pushstring(gL.T, "__index"); |
| 8706 | lua_newtable(gL.T); |
| 8707 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8708 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8709 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8710 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8711 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8712 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8713 | |
| 8714 | |
| 8715 | /* |
| 8716 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8717 | * Register class Socket |
| 8718 | * |
| 8719 | */ |
| 8720 | |
| 8721 | /* Create and fill the metatable. */ |
| 8722 | lua_newtable(gL.T); |
| 8723 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8724 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8725 | lua_pushstring(gL.T, "__index"); |
| 8726 | lua_newtable(gL.T); |
| 8727 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8728 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8729 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8730 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8731 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8732 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8733 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8734 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8735 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8736 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8737 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8738 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8739 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8740 | 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] | 8741 | |
| 8742 | /* Register the garbage collector entry. */ |
| 8743 | lua_pushstring(gL.T, "__gc"); |
| 8744 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8745 | 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] | 8746 | |
| 8747 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8748 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8749 | |
| 8750 | /* Proxy and server configuration initialisation. */ |
| 8751 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8752 | init_new_proxy(&socket_proxy); |
| 8753 | socket_proxy.parent = NULL; |
| 8754 | socket_proxy.last_change = now.tv_sec; |
| 8755 | socket_proxy.id = "LUA-SOCKET"; |
| 8756 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8757 | socket_proxy.maxconn = 0; |
| 8758 | socket_proxy.accept = NULL; |
| 8759 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8760 | socket_proxy.srv = NULL; |
| 8761 | socket_proxy.conn_retries = 0; |
| 8762 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8763 | |
| 8764 | /* Init TCP server: unchanged parameters */ |
| 8765 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8766 | socket_tcp.next = NULL; |
| 8767 | socket_tcp.proxy = &socket_proxy; |
| 8768 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
Willy Tarreau | 72653c5 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 8769 | for (i = 0; i < MAX_THREADS; i++) |
| 8770 | MT_LIST_INIT(&socket_tcp.actconns[i]); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8771 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8772 | socket_tcp.idle_conns = NULL; |
| 8773 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8774 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8775 | socket_tcp.last_change = 0; |
| 8776 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8777 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8778 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8779 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8780 | |
| 8781 | /* XXX: Copy default parameter from default server, |
| 8782 | * but the default server is not initialized. |
| 8783 | */ |
| 8784 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8785 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8786 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8787 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8788 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8789 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8790 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8791 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8792 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8793 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8794 | |
| 8795 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8796 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8797 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8798 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8799 | socket_tcp.check.server = &socket_tcp; |
| 8800 | |
| 8801 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8802 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8803 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8804 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8805 | socket_tcp.agent.server = &socket_tcp; |
| 8806 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8807 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8808 | |
| 8809 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8810 | /* Init TCP server: unchanged parameters */ |
| 8811 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8812 | socket_ssl.next = NULL; |
| 8813 | socket_ssl.proxy = &socket_proxy; |
| 8814 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
Willy Tarreau | 72653c5 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 8815 | for (i = 0; i < MAX_THREADS; i++) |
| 8816 | MT_LIST_INIT(&socket_ssl.actconns[i]); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8817 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8818 | socket_ssl.idle_conns = NULL; |
| 8819 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8820 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8821 | socket_ssl.last_change = 0; |
| 8822 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8823 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8824 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8825 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8826 | |
| 8827 | /* XXX: Copy default parameter from default server, |
| 8828 | * but the default server is not initialized. |
| 8829 | */ |
| 8830 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8831 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8832 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8833 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8834 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8835 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8836 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8837 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8838 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8839 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8840 | |
| 8841 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8842 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8843 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8844 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8845 | socket_ssl.check.server = &socket_ssl; |
| 8846 | |
| 8847 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8848 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8849 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8850 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8851 | socket_ssl.agent.server = &socket_ssl; |
| 8852 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8853 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8854 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8855 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8856 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8857 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8858 | /* |
| 8859 | * |
| 8860 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8861 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8862 | * features like client certificates and ssl_verify. |
| 8863 | * |
| 8864 | */ |
| 8865 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8866 | if (tmp_error != 0) { |
| 8867 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8868 | abort(); /* This must be never arrives because the command line |
| 8869 | not editable by the user. */ |
| 8870 | } |
| 8871 | idx += kw->skip; |
| 8872 | } |
| 8873 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8874 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8875 | |
| 8876 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8877 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8878 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8879 | static void hlua_deinit() |
| 8880 | { |
| 8881 | lua_close(gL.T); |
| 8882 | } |
| 8883 | |
| 8884 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8885 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8886 | static void hlua_register_build_options(void) |
| 8887 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8888 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8889 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8890 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8891 | hap_register_build_opts(ptr, 1); |
| 8892 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8893 | |
| 8894 | INITCALL0(STG_REGISTER, hlua_register_build_options); |