Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 14 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 16 | #include <lauxlib.h> |
| 17 | #include <lua.h> |
| 18 | #include <lualib.h> |
| 19 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 20 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 21 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 24 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 26 | #include <haproxy/api.h> |
| 27 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 28 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 29 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 30 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 31 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 32 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 33 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 34 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 35 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 36 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 37 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 38 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 39 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 40 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 41 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 42 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 43 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 44 | #include <haproxy/payload.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 45 | #include <haproxy/proxy-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 46 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 47 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 48 | #include <haproxy/server-t.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 49 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 50 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 51 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 52 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 53 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 54 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 55 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 56 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 57 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/xref.h> |
| 59 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 60 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 61 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 62 | * macro is used only for identifying the function that can |
| 63 | * not return because a longjmp is executed. |
| 64 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 65 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 66 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 67 | */ |
| 68 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 69 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | #define MAY_LJMP(func) func |
| 71 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 72 | /* This couple of function executes securely some Lua calls outside of |
| 73 | * the lua runtime environment. Each Lua call can return a longjmp |
| 74 | * if it encounter a memory error. |
| 75 | * |
| 76 | * Lua documentation extract: |
| 77 | * |
| 78 | * If an error happens outside any protected environment, Lua calls |
| 79 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 80 | * exiting the host application. Your panic function can avoid this |
| 81 | * exit by never returning (e.g., doing a long jump to your own |
| 82 | * recovery point outside Lua). |
| 83 | * |
| 84 | * The panic function runs as if it were a message handler (see |
| 85 | * §2.3); in particular, the error message is at the top of the |
| 86 | * stack. However, there is no guarantee about stack space. To push |
| 87 | * anything on the stack, the panic function must first check the |
| 88 | * available space (see §4.2). |
| 89 | * |
| 90 | * We must check all the Lua entry point. This includes: |
| 91 | * - The include/proto/hlua.h exported functions |
| 92 | * - the task wrapper function |
| 93 | * - The action wrapper function |
| 94 | * - The converters wrapper function |
| 95 | * - The sample-fetch wrapper functions |
| 96 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 97 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 98 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 99 | * |
| 100 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 101 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 102 | * because they must be exists in the program stack when the longjmp |
| 103 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 104 | * |
| 105 | * Note that the Lua processing is not really thread safe. It provides |
| 106 | * heavy system which consists to add our own lock function in the Lua |
| 107 | * code and recompile the library. This system will probably not accepted |
| 108 | * by maintainers of various distribs. |
| 109 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 110 | * 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] | 111 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 112 | * of function and a lua_unlock() at the end of the function. So I |
| 113 | * conclude that the Lua thread safe mode just perform a mutex around |
| 114 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 115 | * easier for distro maintainers. |
| 116 | * |
| 117 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 118 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 119 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 120 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 121 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 122 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 124 | 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] | 125 | |
| 126 | #define SET_SAFE_LJMP(__L) \ |
| 127 | ({ \ |
| 128 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 129 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 130 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 131 | lua_atpanic(__L, hlua_panic_safe); \ |
| 132 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 133 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | } else { \ |
| 135 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 136 | ret = 1; \ |
| 137 | } \ |
| 138 | ret; \ |
| 139 | }) |
| 140 | |
| 141 | /* If we are the last function catching Lua errors, we |
| 142 | * must reset the panic function. |
| 143 | */ |
| 144 | #define RESET_SAFE_LJMP(__L) \ |
| 145 | do { \ |
| 146 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 147 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 148 | } while(0) |
| 149 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 150 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 151 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 152 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 153 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 154 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 155 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 156 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 157 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 158 | /* The main Lua execution context. */ |
| 159 | struct hlua gL; |
| 160 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 161 | /* This is the memory pool containing struct lua for applets |
| 162 | * (including cli). |
| 163 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 164 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 165 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 166 | /* Used for Socket connection. */ |
| 167 | static struct proxy socket_proxy; |
| 168 | static struct server socket_tcp; |
| 169 | #ifdef USE_OPENSSL |
| 170 | static struct server socket_ssl; |
| 171 | #endif |
| 172 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 173 | /* List head of the function called at the initialisation time. */ |
| 174 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 175 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 176 | /* The following variables contains the reference of the different |
| 177 | * Lua classes. These references are useful for identify metadata |
| 178 | * associated with an object. |
| 179 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 180 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 181 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 182 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 183 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 184 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 185 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 186 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 187 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 188 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 189 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 190 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 191 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 192 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 193 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 194 | * because a task may remain alive during all the haproxy execution. |
| 195 | */ |
| 196 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 197 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 198 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 199 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 200 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 201 | * it is used for preventing infinite loops. |
| 202 | * |
| 203 | * I test the scheer with an infinite loop containing one incrementation |
| 204 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 205 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 206 | * to one interrupt each 10 000 instructions. |
| 207 | * |
| 208 | * configured | Number of |
| 209 | * instructions | loops executed |
| 210 | * between two | in milions |
| 211 | * forced yields | |
| 212 | * ---------------+--------------- |
| 213 | * 10 | 160 |
| 214 | * 500 | 670 |
| 215 | * 1000 | 680 |
| 216 | * 5000 | 700 |
| 217 | * 7000 | 700 |
| 218 | * 8000 | 700 |
| 219 | * 9000 | 710 <- ceil |
| 220 | * 10000 | 710 |
| 221 | * 100000 | 710 |
| 222 | * 1000000 | 710 |
| 223 | * |
| 224 | */ |
| 225 | static unsigned int hlua_nb_instruction = 10000; |
| 226 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 227 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 228 | * be enforced on any memory allocation. |
| 229 | */ |
| 230 | struct hlua_mem_allocator { |
| 231 | size_t allocated; |
| 232 | size_t limit; |
| 233 | }; |
| 234 | |
| 235 | static struct hlua_mem_allocator hlua_global_allocator; |
| 236 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 237 | /* These functions converts types between HAProxy internal args or |
| 238 | * sample and LUA types. Another function permits to check if the |
| 239 | * LUA stack contains arguments according with an required ARG_T |
| 240 | * format. |
| 241 | */ |
| 242 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 243 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 244 | __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] | 245 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 246 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 247 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 248 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 249 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 250 | __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] | 251 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 252 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 253 | do { \ |
| 254 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 255 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 256 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 257 | } while (0) |
| 258 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 259 | /* Used to check an Lua function type in the stack. It creates and |
| 260 | * returns a reference of the function. This function throws an |
| 261 | * error if the rgument is not a "function". |
| 262 | */ |
| 263 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 264 | { |
| 265 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 266 | 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] | 267 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 268 | } |
| 269 | lua_pushvalue(L, argno); |
| 270 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 271 | } |
| 272 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 273 | /* Return the string that is of the top of the stack. */ |
| 274 | const char *hlua_get_top_error_string(lua_State *L) |
| 275 | { |
| 276 | if (lua_gettop(L) < 1) |
| 277 | return "unknown error"; |
| 278 | if (lua_type(L, -1) != LUA_TSTRING) |
| 279 | return "unknown error"; |
| 280 | return lua_tostring(L, -1); |
| 281 | } |
| 282 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 283 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 284 | { |
| 285 | lua_Debug ar; |
| 286 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 287 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 288 | int filled = 0; |
| 289 | |
| 290 | while (lua_getstack(L, level++, &ar)) { |
| 291 | |
| 292 | /* Add separator */ |
| 293 | if (filled) |
| 294 | chunk_appendf(msg, ", "); |
| 295 | filled = 1; |
| 296 | |
| 297 | /* Fill fields: |
| 298 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 299 | * 'l': fills in the field currentline; |
| 300 | * 'n': fills in the field name and namewhat; |
| 301 | * 't': fills in the field istailcall; |
| 302 | */ |
| 303 | lua_getinfo(L, "Slnt", &ar); |
| 304 | |
| 305 | /* Append code localisation */ |
| 306 | if (ar.currentline > 0) |
| 307 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 308 | else |
| 309 | chunk_appendf(msg, "%s ", ar.short_src); |
| 310 | |
| 311 | /* |
| 312 | * Get function name |
| 313 | * |
| 314 | * if namewhat is no empty, name is defined. |
| 315 | * what contains "Lua" for Lua function, "C" for C function, |
| 316 | * or "main" for main code. |
| 317 | */ |
| 318 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 319 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 320 | |
| 321 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 322 | chunk_appendf(msg, "main chunk"); |
| 323 | |
| 324 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 325 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 326 | |
| 327 | else /* nothing left... */ |
| 328 | chunk_appendf(msg, "?"); |
| 329 | |
| 330 | |
| 331 | /* Display tailed call */ |
| 332 | if (ar.istailcall) |
| 333 | chunk_appendf(msg, " ..."); |
| 334 | } |
| 335 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 336 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 340 | /* This function check the number of arguments available in the |
| 341 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 342 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 343 | */ |
| 344 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 345 | { |
| 346 | if (lua_gettop(L) == nb) |
| 347 | return; |
| 348 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 349 | } |
| 350 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 351 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 352 | * and the line number where the error is encountered. |
| 353 | */ |
| 354 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 355 | { |
| 356 | va_list argp; |
| 357 | va_start(argp, fmt); |
| 358 | luaL_where(L, 1); |
| 359 | lua_pushvfstring(L, fmt, argp); |
| 360 | va_end(argp); |
| 361 | lua_concat(L, 2); |
| 362 | return 1; |
| 363 | } |
| 364 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 365 | /* This functions is used with sample fetch and converters. It |
| 366 | * converts the HAProxy configuration argument in a lua stack |
| 367 | * values. |
| 368 | * |
| 369 | * It takes an array of "arg", and each entry of the array is |
| 370 | * converted and pushed in the LUA stack. |
| 371 | */ |
| 372 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 373 | { |
| 374 | switch (arg->type) { |
| 375 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 376 | case ARGT_TIME: |
| 377 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 378 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | break; |
| 380 | |
| 381 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 382 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 383 | break; |
| 384 | |
| 385 | case ARGT_IPV4: |
| 386 | case ARGT_IPV6: |
| 387 | case ARGT_MSK4: |
| 388 | case ARGT_MSK6: |
| 389 | case ARGT_FE: |
| 390 | case ARGT_BE: |
| 391 | case ARGT_TAB: |
| 392 | case ARGT_SRV: |
| 393 | case ARGT_USR: |
| 394 | case ARGT_MAP: |
| 395 | default: |
| 396 | lua_pushnil(L); |
| 397 | break; |
| 398 | } |
| 399 | return 1; |
| 400 | } |
| 401 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 402 | /* 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] | 403 | * 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] | 404 | * with sample fetch wrappers. The input arguments are given to the |
| 405 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 406 | */ |
| 407 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 408 | { |
| 409 | switch (lua_type(L, ud)) { |
| 410 | |
| 411 | case LUA_TNUMBER: |
| 412 | case LUA_TBOOLEAN: |
| 413 | arg->type = ARGT_SINT; |
| 414 | arg->data.sint = lua_tointeger(L, ud); |
| 415 | break; |
| 416 | |
| 417 | case LUA_TSTRING: |
| 418 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 419 | 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] | 420 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 421 | arg->data.str.size = arg->data.str.data; |
| 422 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case LUA_TUSERDATA: |
| 426 | case LUA_TNIL: |
| 427 | case LUA_TTABLE: |
| 428 | case LUA_TFUNCTION: |
| 429 | case LUA_TTHREAD: |
| 430 | case LUA_TLIGHTUSERDATA: |
| 431 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 432 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | /* the following functions are used to convert a struct sample |
| 439 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 440 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 441 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 442 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 443 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 444 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 445 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 447 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | case SMP_T_BIN: |
| 451 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 452 | 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] | 453 | break; |
| 454 | |
| 455 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 456 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 458 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 459 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 460 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 461 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 462 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 463 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 464 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 465 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 466 | 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] | 467 | break; |
| 468 | default: |
| 469 | lua_pushnil(L); |
| 470 | break; |
| 471 | } |
| 472 | break; |
| 473 | |
| 474 | case SMP_T_IPV4: |
| 475 | case SMP_T_IPV6: |
| 476 | 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] | 477 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 478 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 479 | 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] | 480 | else |
| 481 | lua_pushnil(L); |
| 482 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 483 | default: |
| 484 | lua_pushnil(L); |
| 485 | break; |
| 486 | } |
| 487 | return 1; |
| 488 | } |
| 489 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 490 | /* the following functions are used to convert a struct sample |
| 491 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 492 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 493 | */ |
| 494 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 495 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 496 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 497 | |
| 498 | case SMP_T_BIN: |
| 499 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 500 | 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] | 501 | break; |
| 502 | |
| 503 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 504 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 505 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 506 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 507 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 508 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 509 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 510 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 511 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 512 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 513 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 514 | 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] | 515 | break; |
| 516 | default: |
| 517 | lua_pushstring(L, ""); |
| 518 | break; |
| 519 | } |
| 520 | break; |
| 521 | |
| 522 | case SMP_T_SINT: |
| 523 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 524 | case SMP_T_IPV4: |
| 525 | case SMP_T_IPV6: |
| 526 | 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] | 527 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 528 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 529 | 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] | 530 | else |
| 531 | lua_pushstring(L, ""); |
| 532 | break; |
| 533 | default: |
| 534 | lua_pushstring(L, ""); |
| 535 | break; |
| 536 | } |
| 537 | return 1; |
| 538 | } |
| 539 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 540 | /* the following functions are used to convert an Lua type in a |
| 541 | * struct sample. This is useful to provide data from a converter |
| 542 | * to the LUA code. |
| 543 | */ |
| 544 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 545 | { |
| 546 | switch (lua_type(L, ud)) { |
| 547 | |
| 548 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 549 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 550 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 551 | break; |
| 552 | |
| 553 | |
| 554 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 555 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 556 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 557 | break; |
| 558 | |
| 559 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 560 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 562 | 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] | 563 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 564 | smp->data.u.str.size = smp->data.u.str.data; |
| 565 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | break; |
| 567 | |
| 568 | case LUA_TUSERDATA: |
| 569 | case LUA_TNIL: |
| 570 | case LUA_TTABLE: |
| 571 | case LUA_TFUNCTION: |
| 572 | case LUA_TTHREAD: |
| 573 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 574 | case LUA_TNONE: |
| 575 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 576 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 577 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | return 1; |
| 581 | } |
| 582 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 583 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 584 | * 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] | 585 | * 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] | 586 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 587 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 588 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 589 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 590 | __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] | 591 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | { |
| 593 | int min_arg; |
| 594 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 595 | struct proxy *px; |
| 596 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 597 | |
| 598 | idx = 0; |
| 599 | min_arg = ARGM(mask); |
| 600 | mask >>= ARGM_BITS; |
| 601 | |
| 602 | while (1) { |
| 603 | |
| 604 | /* Check oversize. */ |
| 605 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 606 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* Check for mandatory arguments. */ |
| 610 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 611 | if (idx < min_arg) { |
| 612 | |
| 613 | /* If miss other argument than the first one, we return an error. */ |
| 614 | if (idx > 0) |
| 615 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 616 | |
| 617 | /* If first argument have a certain type, some default values |
| 618 | * may be used. See the function smp_resolve_args(). |
| 619 | */ |
| 620 | switch (mask & ARGT_MASK) { |
| 621 | |
| 622 | case ARGT_FE: |
| 623 | if (!(p->cap & PR_CAP_FE)) |
| 624 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 625 | argp[idx].data.prx = p; |
| 626 | argp[idx].type = ARGT_FE; |
| 627 | argp[idx+1].type = ARGT_STOP; |
| 628 | break; |
| 629 | |
| 630 | case ARGT_BE: |
| 631 | if (!(p->cap & PR_CAP_BE)) |
| 632 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 633 | argp[idx].data.prx = p; |
| 634 | argp[idx].type = ARGT_BE; |
| 635 | argp[idx+1].type = ARGT_STOP; |
| 636 | break; |
| 637 | |
| 638 | case ARGT_TAB: |
| 639 | argp[idx].data.prx = p; |
| 640 | argp[idx].type = ARGT_TAB; |
| 641 | argp[idx+1].type = ARGT_STOP; |
| 642 | break; |
| 643 | |
| 644 | default: |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 646 | break; |
| 647 | } |
| 648 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 652 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 653 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 654 | argp[idx].type != ARGT_STOP) { |
| 655 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 656 | } |
| 657 | |
| 658 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 659 | argp[idx].type == ARGT_STOP) { |
| 660 | return 0; |
| 661 | } |
| 662 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 663 | /* Convert some argument types. */ |
| 664 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 665 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 666 | if (argp[idx].type != ARGT_SINT) |
| 667 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 668 | argp[idx].type = ARGT_SINT; |
| 669 | break; |
| 670 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 671 | case ARGT_TIME: |
| 672 | if (argp[idx].type != ARGT_SINT) |
| 673 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 674 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 675 | break; |
| 676 | |
| 677 | case ARGT_SIZE: |
| 678 | if (argp[idx].type != ARGT_SINT) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 680 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 681 | break; |
| 682 | |
| 683 | case ARGT_FE: |
| 684 | if (argp[idx].type != ARGT_STR) |
| 685 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 686 | memcpy(trash.area, argp[idx].data.str.area, |
| 687 | argp[idx].data.str.data); |
| 688 | trash.area[argp[idx].data.str.data] = 0; |
| 689 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 690 | if (!argp[idx].data.prx) |
| 691 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 692 | argp[idx].type = ARGT_FE; |
| 693 | break; |
| 694 | |
| 695 | case ARGT_BE: |
| 696 | if (argp[idx].type != ARGT_STR) |
| 697 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 698 | memcpy(trash.area, argp[idx].data.str.area, |
| 699 | argp[idx].data.str.data); |
| 700 | trash.area[argp[idx].data.str.data] = 0; |
| 701 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 702 | if (!argp[idx].data.prx) |
| 703 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 704 | argp[idx].type = ARGT_BE; |
| 705 | break; |
| 706 | |
| 707 | case ARGT_TAB: |
| 708 | if (argp[idx].type != ARGT_STR) |
| 709 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 710 | memcpy(trash.area, argp[idx].data.str.area, |
| 711 | argp[idx].data.str.data); |
| 712 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 713 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 714 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 716 | argp[idx].type = ARGT_TAB; |
| 717 | break; |
| 718 | |
| 719 | case ARGT_SRV: |
| 720 | if (argp[idx].type != ARGT_STR) |
| 721 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 722 | memcpy(trash.area, argp[idx].data.str.area, |
| 723 | argp[idx].data.str.data); |
| 724 | trash.area[argp[idx].data.str.data] = 0; |
| 725 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 726 | if (sname) { |
| 727 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 728 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 729 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 730 | if (!px) |
| 731 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 732 | } |
| 733 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 734 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 735 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 736 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 737 | argp[idx].data.srv = findserver(px, sname); |
| 738 | if (!argp[idx].data.srv) |
| 739 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 740 | argp[idx].type = ARGT_SRV; |
| 741 | break; |
| 742 | |
| 743 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 744 | memcpy(trash.area, argp[idx].data.str.area, |
| 745 | argp[idx].data.str.data); |
| 746 | trash.area[argp[idx].data.str.data] = 0; |
| 747 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 748 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 749 | argp[idx].type = ARGT_IPV4; |
| 750 | break; |
| 751 | |
| 752 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 753 | memcpy(trash.area, argp[idx].data.str.area, |
| 754 | argp[idx].data.str.data); |
| 755 | trash.area[argp[idx].data.str.data] = 0; |
| 756 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 757 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 758 | argp[idx].type = ARGT_MSK4; |
| 759 | break; |
| 760 | |
| 761 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 762 | memcpy(trash.area, argp[idx].data.str.area, |
| 763 | argp[idx].data.str.data); |
| 764 | trash.area[argp[idx].data.str.data] = 0; |
| 765 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 766 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 767 | argp[idx].type = ARGT_IPV6; |
| 768 | break; |
| 769 | |
| 770 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 771 | memcpy(trash.area, argp[idx].data.str.area, |
| 772 | argp[idx].data.str.data); |
| 773 | trash.area[argp[idx].data.str.data] = 0; |
| 774 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 775 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 776 | argp[idx].type = ARGT_MSK6; |
| 777 | break; |
| 778 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | case ARGT_MAP: |
| 780 | case ARGT_REG: |
| 781 | case ARGT_USR: |
| 782 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 783 | break; |
| 784 | } |
| 785 | |
| 786 | /* Check for type of argument. */ |
| 787 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 788 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 789 | arg_type_names[(mask & ARGT_MASK)], |
| 790 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 791 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 792 | } |
| 793 | |
| 794 | /* Next argument. */ |
| 795 | mask >>= ARGT_BITS; |
| 796 | idx++; |
| 797 | } |
| 798 | } |
| 799 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 800 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 801 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 802 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 803 | * |
| 804 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 805 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 806 | */ |
| 807 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 808 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 809 | struct hlua **hlua = lua_getextraspace(L); |
| 810 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 811 | } |
| 812 | static inline void hlua_sethlua(struct hlua *hlua) |
| 813 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 814 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 815 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 818 | /* This function is used to send logs. It try to send on screen (stderr) |
| 819 | * and on the default syslog server. |
| 820 | */ |
| 821 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 822 | { |
| 823 | struct tm tm; |
| 824 | char *p; |
| 825 | |
| 826 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 827 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 828 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 829 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 830 | /* Break the message if exceed the buffer size. */ |
| 831 | *(p-4) = ' '; |
| 832 | *(p-3) = '.'; |
| 833 | *(p-2) = '.'; |
| 834 | *(p-1) = '.'; |
| 835 | break; |
| 836 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 837 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 838 | *p = *msg; |
| 839 | else |
| 840 | *p = '.'; |
| 841 | } |
| 842 | *p = '\0'; |
| 843 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 844 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 845 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 846 | get_localtime(date.tv_sec, &tm); |
| 847 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 848 | 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] | 849 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 850 | fflush(stderr); |
| 851 | } |
| 852 | } |
| 853 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 854 | /* This function just ensure that the yield will be always |
| 855 | * returned with a timeout and permit to set some flags |
| 856 | */ |
| 857 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 858 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 859 | { |
| 860 | struct hlua *hlua = hlua_gethlua(L); |
| 861 | |
| 862 | /* Set the wake timeout. If timeout is required, we set |
| 863 | * the expiration time. |
| 864 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 865 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 866 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 867 | hlua->flags |= flags; |
| 868 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 869 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 870 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 871 | } |
| 872 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 873 | /* This function initialises the Lua environment stored in the stream. |
| 874 | * 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] | 875 | * 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] | 876 | * |
| 877 | * This function is particular. it initialises a new Lua thread. If the |
| 878 | * initialisation fails (example: out of memory error), the lua function |
| 879 | * throws an error (longjmp). |
| 880 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 881 | * 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] | 882 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 883 | * threads appear, the safe environment set a lock to ensure only one |
| 884 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 885 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 886 | * |
| 887 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 888 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 889 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 890 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 891 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 892 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 893 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 894 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 895 | 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] | 896 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 897 | if (!already_safe) { |
| 898 | if (!SET_SAFE_LJMP(gL.T)) { |
| 899 | lua->Tref = LUA_REFNIL; |
| 900 | return 0; |
| 901 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 902 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 903 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 904 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 905 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 906 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 907 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 908 | lua->T = lua_newthread(gL.T); |
| 909 | if (!lua->T) { |
| 910 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 911 | if (!already_safe) |
| 912 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 913 | return 0; |
| 914 | } |
| 915 | hlua_sethlua(lua); |
| 916 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 917 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 918 | if (!already_safe) |
| 919 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 920 | return 1; |
| 921 | } |
| 922 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 923 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 925 | * is not freed. |
| 926 | */ |
| 927 | void hlua_ctx_destroy(struct hlua *lua) |
| 928 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 929 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 930 | return; |
| 931 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 932 | if (!lua->T) |
| 933 | goto end; |
| 934 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 935 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 936 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 937 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 938 | if (!SET_SAFE_LJMP(lua->T)) |
| 939 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 940 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 941 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 942 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 943 | if (!SET_SAFE_LJMP(gL.T)) |
| 944 | return; |
| 945 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 946 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 947 | /* Forces a garbage collecting process. If the Lua program is finished |
| 948 | * without error, we run the GC on the thread pointer. Its freed all |
| 949 | * the unused memory. |
| 950 | * If the thread is finnish with an error or is currently yielded, |
| 951 | * it seems that the GC applied on the thread doesn't clean anything, |
| 952 | * so e run the GC on the main thread. |
| 953 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 954 | * the garbage collection. |
| 955 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 956 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 957 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 958 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 959 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 960 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 961 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 962 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 963 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 964 | |
| 965 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 966 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* This function is used to restore the Lua context when a coroutine |
| 970 | * fails. This function copy the common memory between old coroutine |
| 971 | * and the new coroutine. The old coroutine is destroyed, and its |
| 972 | * replaced by the new coroutine. |
| 973 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 974 | * as string error message and it is copied in the new stack. |
| 975 | */ |
| 976 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 977 | { |
| 978 | lua_State *T; |
| 979 | int new_ref; |
| 980 | |
| 981 | /* Renew the main LUA stack doesn't have sense. */ |
| 982 | if (lua == &gL) |
| 983 | return 0; |
| 984 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 985 | /* New Lua coroutine. */ |
| 986 | T = lua_newthread(gL.T); |
| 987 | if (!T) |
| 988 | return 0; |
| 989 | |
| 990 | /* Copy last error message. */ |
| 991 | if (keep_msg) |
| 992 | lua_xmove(lua->T, T, 1); |
| 993 | |
| 994 | /* Copy data between the coroutines. */ |
| 995 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 996 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 997 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 998 | |
| 999 | /* Destroy old data. */ |
| 1000 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1001 | |
| 1002 | /* The thread is garbage collected by Lua. */ |
| 1003 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1004 | |
| 1005 | /* Fill the struct with the new coroutine values. */ |
| 1006 | lua->Mref = new_ref; |
| 1007 | lua->T = T; |
| 1008 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1009 | |
| 1010 | /* Set context. */ |
| 1011 | hlua_sethlua(lua); |
| 1012 | |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1016 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1017 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1018 | struct hlua *hlua = hlua_gethlua(L); |
| 1019 | |
| 1020 | /* Lua cannot yield when its returning from a function, |
| 1021 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1022 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1023 | */ |
| 1024 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1025 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | /* restore the interrupt condition. */ |
| 1030 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1031 | |
| 1032 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1033 | * If the state is not yieldable, trying yield causes an error. |
| 1034 | */ |
| 1035 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1036 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1037 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1038 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1039 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1040 | hlua->run_time += now_ms - hlua->start_time; |
| 1041 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1042 | lua_pushfstring(L, "execution timeout"); |
| 1043 | WILL_LJMP(lua_error(L)); |
| 1044 | } |
| 1045 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1046 | /* Update the start time. */ |
| 1047 | hlua->start_time = now_ms; |
| 1048 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1049 | /* Try to interrupt the process at the end of the current |
| 1050 | * unyieldable function. |
| 1051 | */ |
| 1052 | 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] | 1053 | } |
| 1054 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1055 | /* This function start or resumes the Lua stack execution. If the flag |
| 1056 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1057 | * The function return an error. |
| 1058 | * |
| 1059 | * The function can returns 4 values: |
| 1060 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1061 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1062 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1063 | * - 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] | 1064 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1065 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1066 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * 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] | 1068 | * LUA code. |
| 1069 | */ |
| 1070 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1071 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1072 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1073 | int nres; |
| 1074 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1075 | int ret; |
| 1076 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1077 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1078 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1079 | /* Initialise run time counter. */ |
| 1080 | if (!HLUA_IS_RUNNING(lua)) |
| 1081 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1082 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1083 | /* Lock the whole Lua execution. This lock must be before the |
| 1084 | * label "resume_execution". |
| 1085 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1086 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1087 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1088 | resume_execution: |
| 1089 | |
| 1090 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1091 | * instructions. it is used for preventing infinite loops. |
| 1092 | */ |
| 1093 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1094 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1095 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1096 | HLUA_SET_RUN(lua); |
| 1097 | HLUA_CLR_CTRLYIELD(lua); |
| 1098 | HLUA_CLR_WAKERESWR(lua); |
| 1099 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1100 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1101 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1102 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1103 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1104 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1106 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1107 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1108 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1109 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1110 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1111 | switch (ret) { |
| 1112 | |
| 1113 | case LUA_OK: |
| 1114 | ret = HLUA_E_OK; |
| 1115 | break; |
| 1116 | |
| 1117 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1118 | /* Check if the execution timeout is expired. It it is the case, we |
| 1119 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1120 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1121 | tv_update_date(0, 1); |
| 1122 | lua->run_time += now_ms - lua->start_time; |
| 1123 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1124 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1125 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1126 | break; |
| 1127 | } |
| 1128 | /* Process the forced yield. if the general yield is not allowed or |
| 1129 | * if no task were associated this the current Lua execution |
| 1130 | * coroutine, we resume the execution. Else we want to return in the |
| 1131 | * scheduler and we want to be waked up again, to continue the |
| 1132 | * current Lua execution. So we schedule our own task. |
| 1133 | */ |
| 1134 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1135 | if (!yield_allowed || !lua->task) |
| 1136 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1137 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1138 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1139 | if (!yield_allowed) { |
| 1140 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1141 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1142 | break; |
| 1143 | } |
| 1144 | ret = HLUA_E_AGAIN; |
| 1145 | break; |
| 1146 | |
| 1147 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1148 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1149 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1150 | * because the errors ares the only one mean to return immediately |
| 1151 | * from and lua execution. |
| 1152 | */ |
| 1153 | if (lua->flags & HLUA_EXIT) { |
| 1154 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1155 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1156 | break; |
| 1157 | } |
| 1158 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1159 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | if (!lua_checkstack(lua->T, 1)) { |
| 1161 | ret = HLUA_E_ERR; |
| 1162 | break; |
| 1163 | } |
| 1164 | msg = lua_tostring(lua->T, -1); |
| 1165 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1166 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1167 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1168 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1169 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1171 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | ret = HLUA_E_ERRMSG; |
| 1173 | break; |
| 1174 | |
| 1175 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1176 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1177 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1178 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1179 | break; |
| 1180 | |
| 1181 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1182 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | if (!lua_checkstack(lua->T, 1)) { |
| 1184 | ret = HLUA_E_ERR; |
| 1185 | break; |
| 1186 | } |
| 1187 | msg = lua_tostring(lua->T, -1); |
| 1188 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1189 | lua_pop(lua->T, 1); |
| 1190 | if (msg) |
| 1191 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1192 | else |
| 1193 | lua_pushfstring(lua->T, "message handler error"); |
| 1194 | ret = HLUA_E_ERRMSG; |
| 1195 | break; |
| 1196 | |
| 1197 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1198 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1199 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1200 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1201 | break; |
| 1202 | } |
| 1203 | |
| 1204 | switch (ret) { |
| 1205 | case HLUA_E_AGAIN: |
| 1206 | break; |
| 1207 | |
| 1208 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1209 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1211 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | break; |
| 1213 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1214 | case HLUA_E_ETMOUT: |
| 1215 | case HLUA_E_NOMEM: |
| 1216 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1217 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1218 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1219 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1220 | hlua_ctx_renew(lua, 0); |
| 1221 | break; |
| 1222 | |
| 1223 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1224 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1225 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1229 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1230 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1231 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1232 | return ret; |
| 1233 | } |
| 1234 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1235 | /* This function exit the current code. */ |
| 1236 | __LJMP static int hlua_done(lua_State *L) |
| 1237 | { |
| 1238 | struct hlua *hlua = hlua_gethlua(L); |
| 1239 | |
| 1240 | hlua->flags |= HLUA_EXIT; |
| 1241 | WILL_LJMP(lua_error(L)); |
| 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1246 | /* This function is an LUA binding. It provides a function |
| 1247 | * for deleting ACL from a referenced ACL file. |
| 1248 | */ |
| 1249 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1250 | { |
| 1251 | const char *name; |
| 1252 | const char *key; |
| 1253 | struct pat_ref *ref; |
| 1254 | |
| 1255 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1256 | |
| 1257 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1258 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1259 | |
| 1260 | ref = pat_ref_lookup(name); |
| 1261 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1262 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1263 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1264 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1265 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1266 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | /* This function is an LUA binding. It provides a function |
| 1271 | * for deleting map entry from a referenced map file. |
| 1272 | */ |
| 1273 | static int hlua_del_map(lua_State *L) |
| 1274 | { |
| 1275 | const char *name; |
| 1276 | const char *key; |
| 1277 | struct pat_ref *ref; |
| 1278 | |
| 1279 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1280 | |
| 1281 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1282 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1283 | |
| 1284 | ref = pat_ref_lookup(name); |
| 1285 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1286 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1287 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1288 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1289 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1290 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | /* This function is an LUA binding. It provides a function |
| 1295 | * for adding ACL pattern from a referenced ACL file. |
| 1296 | */ |
| 1297 | static int hlua_add_acl(lua_State *L) |
| 1298 | { |
| 1299 | const char *name; |
| 1300 | const char *key; |
| 1301 | struct pat_ref *ref; |
| 1302 | |
| 1303 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1304 | |
| 1305 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1306 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1307 | |
| 1308 | ref = pat_ref_lookup(name); |
| 1309 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1310 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1311 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1312 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1313 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1314 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1315 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | /* This function is an LUA binding. It provides a function |
| 1320 | * for setting map pattern and sample from a referenced map |
| 1321 | * file. |
| 1322 | */ |
| 1323 | static int hlua_set_map(lua_State *L) |
| 1324 | { |
| 1325 | const char *name; |
| 1326 | const char *key; |
| 1327 | const char *value; |
| 1328 | struct pat_ref *ref; |
| 1329 | |
| 1330 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1331 | |
| 1332 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1333 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1334 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1335 | |
| 1336 | ref = pat_ref_lookup(name); |
| 1337 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1338 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1339 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1340 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1341 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1342 | pat_ref_set(ref, key, value, NULL); |
| 1343 | else |
| 1344 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1345 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1346 | return 0; |
| 1347 | } |
| 1348 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1349 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1350 | * 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] | 1351 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1352 | * the name of the object (_G[<name>] = <metable> ). |
| 1353 | * |
| 1354 | * A metable is a table that modify the standard behavior of a standard |
| 1355 | * access to the associated data. The entries of this new metatable are |
| 1356 | * defined as is: |
| 1357 | * |
| 1358 | * http://lua-users.org/wiki/MetatableEvents |
| 1359 | * |
| 1360 | * __index |
| 1361 | * |
| 1362 | * we access an absent field in a table, the result is nil. This is |
| 1363 | * true, but it is not the whole truth. Actually, such access triggers |
| 1364 | * the interpreter to look for an __index metamethod: If there is no |
| 1365 | * such method, as usually happens, then the access results in nil; |
| 1366 | * otherwise, the metamethod will provide the result. |
| 1367 | * |
| 1368 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1369 | * the key does not appear in the table, but the metatable has an __index |
| 1370 | * property: |
| 1371 | * |
| 1372 | * - if the value is a function, the function is called, passing in the |
| 1373 | * table and the key; the return value of that function is returned as |
| 1374 | * the result. |
| 1375 | * |
| 1376 | * - if the value is another table, the value of the key in that table is |
| 1377 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1378 | * table's metatable has an __index property, then it continues on up) |
| 1379 | * |
| 1380 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1381 | * |
| 1382 | * http://www.lua.org/pil/13.4.1.html |
| 1383 | * |
| 1384 | * __newindex |
| 1385 | * |
| 1386 | * Like __index, but control property assignment. |
| 1387 | * |
| 1388 | * __mode - Control weak references. A string value with one or both |
| 1389 | * of the characters 'k' and 'v' which specifies that the the |
| 1390 | * keys and/or values in the table are weak references. |
| 1391 | * |
| 1392 | * __call - Treat a table like a function. When a table is followed by |
| 1393 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1394 | * a __call key pointing to a function, that function is invoked |
| 1395 | * (passing any specified arguments) and the return value is |
| 1396 | * returned. |
| 1397 | * |
| 1398 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1399 | * called, if the metatable for myTable has a __metatable |
| 1400 | * key, the value of that key is returned instead of the |
| 1401 | * actual metatable. |
| 1402 | * |
| 1403 | * __tostring - Control string representation. When the builtin |
| 1404 | * "tostring( myTable )" function is called, if the metatable |
| 1405 | * for myTable has a __tostring property set to a function, |
| 1406 | * that function is invoked (passing myTable to it) and the |
| 1407 | * return value is used as the string representation. |
| 1408 | * |
| 1409 | * __len - Control table length. When the table length is requested using |
| 1410 | * the length operator ( '#' ), if the metatable for myTable has |
| 1411 | * a __len key pointing to a function, that function is invoked |
| 1412 | * (passing myTable to it) and the return value used as the value |
| 1413 | * of "#myTable". |
| 1414 | * |
| 1415 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1416 | * collected, if the metatable has a __gc field pointing to a |
| 1417 | * function, that function is first invoked, passing the userdata |
| 1418 | * to it. The __gc metamethod is not called for tables. |
| 1419 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1420 | * |
| 1421 | * Special metamethods for redefining standard operators: |
| 1422 | * http://www.lua.org/pil/13.1.html |
| 1423 | * |
| 1424 | * __add "+" |
| 1425 | * __sub "-" |
| 1426 | * __mul "*" |
| 1427 | * __div "/" |
| 1428 | * __unm "!" |
| 1429 | * __pow "^" |
| 1430 | * __concat ".." |
| 1431 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1432 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1433 | * http://www.lua.org/pil/13.2.html |
| 1434 | * |
| 1435 | * __eq "==" |
| 1436 | * __lt "<" |
| 1437 | * __le "<=" |
| 1438 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1439 | |
| 1440 | /* |
| 1441 | * |
| 1442 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1443 | * Class Map |
| 1444 | * |
| 1445 | * |
| 1446 | */ |
| 1447 | |
| 1448 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1449 | * a class session, otherwise it throws an error. |
| 1450 | */ |
| 1451 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1452 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1453 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /* This function is the map constructor. It don't need |
| 1457 | * the class Map object. It creates and return a new Map |
| 1458 | * object. It must be called only during "body" or "init" |
| 1459 | * context because it process some filesystem accesses. |
| 1460 | */ |
| 1461 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1462 | { |
| 1463 | const char *fn; |
| 1464 | int match = PAT_MATCH_STR; |
| 1465 | struct sample_conv conv; |
| 1466 | const char *file = ""; |
| 1467 | int line = 0; |
| 1468 | lua_Debug ar; |
| 1469 | char *err = NULL; |
| 1470 | struct arg args[2]; |
| 1471 | |
| 1472 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1473 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1474 | |
| 1475 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1476 | |
| 1477 | if (lua_gettop(L) >= 2) { |
| 1478 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1479 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1480 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1481 | } |
| 1482 | |
| 1483 | /* Get Lua filename and line number. */ |
| 1484 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1485 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1486 | if (ar.currentline > 0) { /* is there info? */ |
| 1487 | file = ar.short_src; |
| 1488 | line = ar.currentline; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | /* fill fake sample_conv struct. */ |
| 1493 | conv.kw = ""; /* unused. */ |
| 1494 | conv.process = NULL; /* unused. */ |
| 1495 | conv.arg_mask = 0; /* unused. */ |
| 1496 | conv.val_args = NULL; /* unused. */ |
| 1497 | conv.out_type = SMP_T_STR; |
| 1498 | conv.private = (void *)(long)match; |
| 1499 | switch (match) { |
| 1500 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1501 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1502 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1503 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1504 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1505 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1506 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1507 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1508 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1509 | default: |
| 1510 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1511 | } |
| 1512 | |
| 1513 | /* fill fake args. */ |
| 1514 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1515 | args[0].data.str.area = strdup(fn); |
| 1516 | args[0].data.str.data = strlen(fn); |
| 1517 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1518 | args[1].type = ARGT_STOP; |
| 1519 | |
| 1520 | /* load the map. */ |
| 1521 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1522 | /* error case: we cant use luaL_error because we must |
| 1523 | * free the err variable. |
| 1524 | */ |
| 1525 | luaL_where(L, 1); |
| 1526 | lua_pushfstring(L, "'new': %s.", err); |
| 1527 | lua_concat(L, 2); |
| 1528 | free(err); |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1529 | free(args[0].data.str.area); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1530 | WILL_LJMP(lua_error(L)); |
| 1531 | } |
| 1532 | |
| 1533 | /* create the lua object. */ |
| 1534 | lua_newtable(L); |
| 1535 | lua_pushlightuserdata(L, args[0].data.map); |
| 1536 | lua_rawseti(L, -2, 0); |
| 1537 | |
| 1538 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1539 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1540 | lua_setmetatable(L, -2); |
| 1541 | |
| 1542 | |
| 1543 | return 1; |
| 1544 | } |
| 1545 | |
| 1546 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1547 | { |
| 1548 | struct map_descriptor *desc; |
| 1549 | struct pattern *pat; |
| 1550 | struct sample smp; |
| 1551 | |
| 1552 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1553 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1554 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1555 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1556 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1557 | } |
| 1558 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1559 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1560 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1561 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1565 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1566 | if (str) |
| 1567 | lua_pushstring(L, ""); |
| 1568 | else |
| 1569 | lua_pushnil(L); |
| 1570 | return 1; |
| 1571 | } |
| 1572 | |
| 1573 | /* 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] | 1574 | 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] | 1575 | return 1; |
| 1576 | } |
| 1577 | |
| 1578 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1579 | { |
| 1580 | return _hlua_map_lookup(L, 0); |
| 1581 | } |
| 1582 | |
| 1583 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1584 | { |
| 1585 | return _hlua_map_lookup(L, 1); |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * |
| 1590 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1591 | * Class Socket |
| 1592 | * |
| 1593 | * |
| 1594 | */ |
| 1595 | |
| 1596 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1597 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1598 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /* 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] | 1602 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | * received. |
| 1604 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1605 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1606 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1607 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1608 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1609 | if (appctx->ctx.hlua_cosocket.die) { |
| 1610 | si_shutw(si); |
| 1611 | si_shutr(si); |
| 1612 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1613 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1614 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1615 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1616 | } |
| 1617 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1618 | /* If we cant write, wakeup the pending write signals. */ |
| 1619 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1620 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1621 | |
| 1622 | /* If we cant read, wakeup the pending read signals. */ |
| 1623 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1624 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1625 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1626 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1627 | * to be notified whenever the connection completes. |
| 1628 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1629 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1630 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1631 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1632 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1633 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1634 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1635 | |
| 1636 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1637 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1638 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1639 | /* 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] | 1640 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1641 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1642 | |
| 1643 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1644 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1645 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1646 | |
| 1647 | /* Some data were injected in the buffer, notify the stream |
| 1648 | * interface. |
| 1649 | */ |
| 1650 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1651 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1652 | |
| 1653 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1654 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1655 | */ |
| 1656 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1657 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1658 | } |
| 1659 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1660 | /* This function is called when the "struct stream" is destroyed. |
| 1661 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | * Wake all the pending signals. |
| 1663 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1664 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1665 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1666 | struct xref *peer; |
| 1667 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1668 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1669 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1670 | if (peer) |
| 1671 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1672 | |
| 1673 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1674 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1675 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1679 | * uses this object. If the stream does not exists, just quit. |
| 1680 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1681 | * pending signal can rest in the read and write lists. destroy |
| 1682 | * it. |
| 1683 | */ |
| 1684 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1685 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1686 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1688 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1690 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1691 | |
| 1692 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1693 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1694 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1695 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1696 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1698 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1699 | appctx->ctx.hlua_cosocket.die = 1; |
| 1700 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1702 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1703 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1708 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1709 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1710 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1711 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1712 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1714 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1715 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1717 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1718 | |
| 1719 | /* Check if we run on the same thread than the xreator thread. |
| 1720 | * We cannot access to the socket if the thread is different. |
| 1721 | */ |
| 1722 | if (socket->tid != tid) |
| 1723 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1724 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1725 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1726 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1727 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1728 | |
| 1729 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1730 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1731 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1732 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1733 | appctx->ctx.hlua_cosocket.die = 1; |
| 1734 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1735 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1736 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1737 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1738 | return 0; |
| 1739 | } |
| 1740 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1741 | /* The close function calls close_helper. |
| 1742 | */ |
| 1743 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1744 | { |
| 1745 | MAY_LJMP(check_args(L, 1, "close")); |
| 1746 | return hlua_socket_close_helper(L); |
| 1747 | } |
| 1748 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1749 | /* This Lua function assumes that the stack contain three parameters. |
| 1750 | * 1 - USERDATA containing a struct socket |
| 1751 | * 2 - INTEGER with values of the macro defined below |
| 1752 | * If the integer is -1, we must read at most one line. |
| 1753 | * If the integer is -2, we ust read all the data until the |
| 1754 | * end of the stream. |
| 1755 | * If the integer is positive value, we must read a number of |
| 1756 | * bytes corresponding to this value. |
| 1757 | */ |
| 1758 | #define HLSR_READ_LINE (-1) |
| 1759 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1760 | __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] | 1761 | { |
| 1762 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1763 | int wanted = lua_tointeger(L, 2); |
| 1764 | struct hlua *hlua = hlua_gethlua(L); |
| 1765 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1766 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1767 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1768 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1769 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1770 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1771 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1772 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1773 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1774 | struct stream_interface *si; |
| 1775 | struct stream *s; |
| 1776 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1777 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* Check if this lua stack is schedulable. */ |
| 1780 | if (!hlua || !hlua->task) |
| 1781 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1782 | "'frontend', 'backend' or 'task'")); |
| 1783 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1784 | /* Check if we run on the same thread than the xreator thread. |
| 1785 | * We cannot access to the socket if the thread is different. |
| 1786 | */ |
| 1787 | if (socket->tid != tid) |
| 1788 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1789 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1790 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1791 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1792 | if (!peer) |
| 1793 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1794 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1795 | si = appctx->owner; |
| 1796 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1797 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1798 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1800 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1801 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1802 | if (nblk < 0) /* Connection close. */ |
| 1803 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1804 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1805 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1806 | |
| 1807 | /* remove final \r\n. */ |
| 1808 | if (nblk == 1) { |
| 1809 | if (blk1[len1-1] == '\n') { |
| 1810 | len1--; |
| 1811 | skip_at_end++; |
| 1812 | if (blk1[len1-1] == '\r') { |
| 1813 | len1--; |
| 1814 | skip_at_end++; |
| 1815 | } |
| 1816 | } |
| 1817 | } |
| 1818 | else { |
| 1819 | if (blk2[len2-1] == '\n') { |
| 1820 | len2--; |
| 1821 | skip_at_end++; |
| 1822 | if (blk2[len2-1] == '\r') { |
| 1823 | len2--; |
| 1824 | skip_at_end++; |
| 1825 | } |
| 1826 | } |
| 1827 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1832 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | if (nblk < 0) /* Connection close. */ |
| 1834 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1835 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | goto connection_empty; |
| 1837 | } |
| 1838 | |
| 1839 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1841 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1842 | if (nblk < 0) /* Connection close. */ |
| 1843 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1844 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1845 | goto connection_empty; |
| 1846 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1847 | missing_bytes = wanted - socket->b.n; |
| 1848 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1850 | len1 = missing_bytes; |
| 1851 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1852 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | len = len1; |
| 1856 | |
| 1857 | luaL_addlstring(&socket->b, blk1, len1); |
| 1858 | if (nblk == 2) { |
| 1859 | len += len2; |
| 1860 | luaL_addlstring(&socket->b, blk2, len2); |
| 1861 | } |
| 1862 | |
| 1863 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1864 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1865 | |
| 1866 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1867 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* If the pattern reclaim to read all the data |
| 1870 | * in the connection, got out. |
| 1871 | */ |
| 1872 | if (wanted == HLSR_READ_ALL) |
| 1873 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1874 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | goto connection_empty; |
| 1876 | |
| 1877 | /* Return result. */ |
| 1878 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1879 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | return 1; |
| 1881 | |
| 1882 | connection_closed: |
| 1883 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1884 | xref_unlock(&socket->xref, peer); |
| 1885 | |
| 1886 | no_peer: |
| 1887 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | /* If the buffer containds data. */ |
| 1889 | if (socket->b.n > 0) { |
| 1890 | luaL_pushresult(&socket->b); |
| 1891 | return 1; |
| 1892 | } |
| 1893 | lua_pushnil(L); |
| 1894 | lua_pushstring(L, "connection closed."); |
| 1895 | return 2; |
| 1896 | |
| 1897 | connection_empty: |
| 1898 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1899 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1900 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1902 | } |
| 1903 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1904 | 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] | 1905 | return 0; |
| 1906 | } |
| 1907 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1908 | /* This Lua function gets two parameters. The first one can be string |
| 1909 | * or a number. If the string is "*l", the user requires one line. If |
| 1910 | * 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] | 1911 | * If the value is a number, the user require a number of bytes equal |
| 1912 | * to the value. The default value is "*l" (a line). |
| 1913 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1914 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | * integer takes this values: |
| 1916 | * -1 : read a line |
| 1917 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1918 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1920 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1921 | * concatenated with the read data. |
| 1922 | */ |
| 1923 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1924 | { |
| 1925 | int wanted = HLSR_READ_LINE; |
| 1926 | const char *pattern; |
| 1927 | int type; |
| 1928 | char *error; |
| 1929 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1930 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1931 | |
| 1932 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1933 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1934 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1935 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1937 | /* Check if we run on the same thread than the xreator thread. |
| 1938 | * We cannot access to the socket if the thread is different. |
| 1939 | */ |
| 1940 | if (socket->tid != tid) |
| 1941 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1942 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | /* check for pattern. */ |
| 1944 | if (lua_gettop(L) >= 2) { |
| 1945 | type = lua_type(L, 2); |
| 1946 | if (type == LUA_TSTRING) { |
| 1947 | pattern = lua_tostring(L, 2); |
| 1948 | if (strcmp(pattern, "*a") == 0) |
| 1949 | wanted = HLSR_READ_ALL; |
| 1950 | else if (strcmp(pattern, "*l") == 0) |
| 1951 | wanted = HLSR_READ_LINE; |
| 1952 | else { |
| 1953 | wanted = strtoll(pattern, &error, 10); |
| 1954 | if (*error != '\0') |
| 1955 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1956 | } |
| 1957 | } |
| 1958 | else if (type == LUA_TNUMBER) { |
| 1959 | wanted = lua_tointeger(L, 2); |
| 1960 | if (wanted < 0) |
| 1961 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | /* Set pattern. */ |
| 1966 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1967 | |
| 1968 | /* Check if we would replace the top by itself. */ |
| 1969 | if (lua_gettop(L) != 2) |
| 1970 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1972 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | luaL_buffinit(L, &socket->b); |
| 1974 | |
| 1975 | /* Check prefix. */ |
| 1976 | if (lua_gettop(L) >= 3) { |
| 1977 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1978 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1979 | pattern = lua_tolstring(L, 3, &len); |
| 1980 | luaL_addlstring(&socket->b, pattern, len); |
| 1981 | } |
| 1982 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1983 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1987 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1989 | 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] | 1990 | { |
| 1991 | struct hlua_socket *socket; |
| 1992 | struct hlua *hlua = hlua_gethlua(L); |
| 1993 | struct appctx *appctx; |
| 1994 | size_t buf_len; |
| 1995 | const char *buf; |
| 1996 | int len; |
| 1997 | int send_len; |
| 1998 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1999 | struct xref *peer; |
| 2000 | struct stream_interface *si; |
| 2001 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2002 | |
| 2003 | /* Check if this lua stack is schedulable. */ |
| 2004 | if (!hlua || !hlua->task) |
| 2005 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2006 | "'frontend', 'backend' or 'task'")); |
| 2007 | |
| 2008 | /* Get object */ |
| 2009 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2010 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2011 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2013 | /* Check if we run on the same thread than the xreator thread. |
| 2014 | * We cannot access to the socket if the thread is different. |
| 2015 | */ |
| 2016 | if (socket->tid != tid) |
| 2017 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2018 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2019 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2020 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2021 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2022 | lua_pushinteger(L, -1); |
| 2023 | return 1; |
| 2024 | } |
| 2025 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2026 | si = appctx->owner; |
| 2027 | s = si_strm(si); |
| 2028 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2030 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2031 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | lua_pushinteger(L, -1); |
| 2033 | return 1; |
| 2034 | } |
| 2035 | |
| 2036 | /* Update the input buffer data. */ |
| 2037 | buf += sent; |
| 2038 | send_len = buf_len - sent; |
| 2039 | |
| 2040 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2041 | if (sent >= buf_len) { |
| 2042 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2044 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2046 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2047 | * the request buffer if its not required. |
| 2048 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2049 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2050 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2051 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2052 | } |
| 2053 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2054 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2055 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2056 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2058 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2059 | |
| 2060 | /* send data */ |
| 2061 | if (len < send_len) |
| 2062 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2063 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | |
| 2065 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2066 | * the data" (-2) are not expected because the available length |
| 2067 | * is tested. |
| 2068 | * Other unknown error are also not expected. |
| 2069 | */ |
| 2070 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2071 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2072 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2073 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2074 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2076 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2077 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | return 1; |
| 2079 | } |
| 2080 | |
| 2081 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2082 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2083 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2084 | s->req.rex = TICK_ETERNITY; |
| 2085 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | |
| 2087 | /* Update length sent. */ |
| 2088 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2089 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2092 | if (sent + len >= buf_len) { |
| 2093 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2095 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | |
| 2097 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2098 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2099 | xref_unlock(&socket->xref, peer); |
| 2100 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2101 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2102 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2103 | 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] | 2104 | return 0; |
| 2105 | } |
| 2106 | |
| 2107 | /* This function initiate the send of data. It just check the input |
| 2108 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2109 | * 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] | 2110 | * "hlua_socket_write_yield" that can yield. |
| 2111 | * |
| 2112 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2113 | * the associated object. The second is a string buffer. The third is |
| 2114 | * a facultative integer that represents where is the buffer position |
| 2115 | * of the start of the data that can send. The first byte is the |
| 2116 | * position "1". The default value is "1". The fourth argument is a |
| 2117 | * facultative integer that represents where is the buffer position |
| 2118 | * of the end of the data that can send. The default is the last byte. |
| 2119 | */ |
| 2120 | static int hlua_socket_send(struct lua_State *L) |
| 2121 | { |
| 2122 | int i; |
| 2123 | int j; |
| 2124 | const char *buf; |
| 2125 | size_t buf_len; |
| 2126 | |
| 2127 | /* Check number of arguments. */ |
| 2128 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2129 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2130 | |
| 2131 | /* Get the string. */ |
| 2132 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2133 | |
| 2134 | /* Get and check j. */ |
| 2135 | if (lua_gettop(L) == 4) { |
| 2136 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2137 | if (j < 0) |
| 2138 | j = buf_len + j + 1; |
| 2139 | if (j > buf_len) |
| 2140 | j = buf_len + 1; |
| 2141 | lua_pop(L, 1); |
| 2142 | } |
| 2143 | else |
| 2144 | j = buf_len; |
| 2145 | |
| 2146 | /* Get and check i. */ |
| 2147 | if (lua_gettop(L) == 3) { |
| 2148 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2149 | if (i < 0) |
| 2150 | i = buf_len + i + 1; |
| 2151 | if (i > buf_len) |
| 2152 | i = buf_len + 1; |
| 2153 | lua_pop(L, 1); |
| 2154 | } else |
| 2155 | i = 1; |
| 2156 | |
| 2157 | /* Check bth i and j. */ |
| 2158 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2159 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2160 | return 1; |
| 2161 | } |
| 2162 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2163 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2164 | return 1; |
| 2165 | } |
| 2166 | if (i == 0) |
| 2167 | i = 1; |
| 2168 | if (j == 0) |
| 2169 | j = 1; |
| 2170 | |
| 2171 | /* Pop the string. */ |
| 2172 | lua_pop(L, 1); |
| 2173 | |
| 2174 | /* Update the buffer length. */ |
| 2175 | buf += i - 1; |
| 2176 | buf_len = j - i + 1; |
| 2177 | lua_pushlstring(L, buf, buf_len); |
| 2178 | |
| 2179 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2180 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2182 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2185 | #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] | 2186 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2187 | { |
| 2188 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2189 | int ret; |
| 2190 | int len; |
| 2191 | char *p; |
| 2192 | |
| 2193 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2194 | if (ret <= 0) { |
| 2195 | lua_pushnil(L); |
| 2196 | return 1; |
| 2197 | } |
| 2198 | |
| 2199 | if (ret == AF_UNIX) { |
| 2200 | lua_pushstring(L, buffer+1); |
| 2201 | return 1; |
| 2202 | } |
| 2203 | else if (ret == AF_INET6) { |
| 2204 | buffer[0] = '['; |
| 2205 | len = strlen(buffer); |
| 2206 | buffer[len] = ']'; |
| 2207 | len++; |
| 2208 | buffer[len] = ':'; |
| 2209 | len++; |
| 2210 | p = buffer; |
| 2211 | } |
| 2212 | else if (ret == AF_INET) { |
| 2213 | p = buffer + 1; |
| 2214 | len = strlen(p); |
| 2215 | p[len] = ':'; |
| 2216 | len++; |
| 2217 | } |
| 2218 | else { |
| 2219 | lua_pushnil(L); |
| 2220 | return 1; |
| 2221 | } |
| 2222 | |
| 2223 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2224 | lua_pushnil(L); |
| 2225 | return 1; |
| 2226 | } |
| 2227 | |
| 2228 | lua_pushstring(L, p); |
| 2229 | return 1; |
| 2230 | } |
| 2231 | |
| 2232 | /* Returns information about the peer of the connection. */ |
| 2233 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2234 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2235 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2236 | struct xref *peer; |
| 2237 | struct appctx *appctx; |
| 2238 | struct stream_interface *si; |
| 2239 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2240 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2241 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2243 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2244 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2246 | /* Check if we run on the same thread than the xreator thread. |
| 2247 | * We cannot access to the socket if the thread is different. |
| 2248 | */ |
| 2249 | if (socket->tid != tid) |
| 2250 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2251 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2252 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2253 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2254 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2255 | lua_pushnil(L); |
| 2256 | return 1; |
| 2257 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2258 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2259 | si = appctx->owner; |
| 2260 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2261 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2262 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2263 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2264 | lua_pushnil(L); |
| 2265 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2268 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2269 | xref_unlock(&socket->xref, peer); |
| 2270 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | /* Returns information about my connection side. */ |
| 2274 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2275 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2276 | struct hlua_socket *socket; |
| 2277 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2278 | struct appctx *appctx; |
| 2279 | struct xref *peer; |
| 2280 | struct stream_interface *si; |
| 2281 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2282 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2283 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2285 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2286 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2288 | /* Check if we run on the same thread than the xreator thread. |
| 2289 | * We cannot access to the socket if the thread is different. |
| 2290 | */ |
| 2291 | if (socket->tid != tid) |
| 2292 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2293 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2294 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2295 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2296 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | lua_pushnil(L); |
| 2298 | return 1; |
| 2299 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2300 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2301 | si = appctx->owner; |
| 2302 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2304 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2305 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2306 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | lua_pushnil(L); |
| 2308 | return 1; |
| 2309 | } |
| 2310 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2311 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2312 | xref_unlock(&socket->xref, peer); |
| 2313 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2317 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2318 | .obj_type = OBJ_TYPE_APPLET, |
| 2319 | .name = "<LUA_TCP>", |
| 2320 | .fct = hlua_socket_handler, |
| 2321 | .release = hlua_socket_release, |
| 2322 | }; |
| 2323 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2324 | __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] | 2325 | { |
| 2326 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2327 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2328 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2330 | struct stream_interface *si; |
| 2331 | struct stream *s; |
| 2332 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2333 | /* Check if we run on the same thread than the xreator thread. |
| 2334 | * We cannot access to the socket if the thread is different. |
| 2335 | */ |
| 2336 | if (socket->tid != tid) |
| 2337 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2338 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2340 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2341 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2342 | lua_pushnil(L); |
| 2343 | lua_pushstring(L, "Can't connect"); |
| 2344 | return 2; |
| 2345 | } |
| 2346 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2347 | si = appctx->owner; |
| 2348 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2349 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2350 | /* Check if we run on the same thread than the xreator thread. |
| 2351 | * We cannot access to the socket if the thread is different. |
| 2352 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | if (socket->tid != tid) { |
| 2354 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2355 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2356 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2357 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2359 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | lua_pushnil(L); |
| 2362 | lua_pushstring(L, "Can't connect"); |
| 2363 | return 2; |
| 2364 | } |
| 2365 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2366 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | |
| 2368 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2369 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2370 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | lua_pushinteger(L, 1); |
| 2372 | return 1; |
| 2373 | } |
| 2374 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2375 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2376 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2378 | } |
| 2379 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2380 | 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] | 2381 | return 0; |
| 2382 | } |
| 2383 | |
| 2384 | /* This function fail or initite the connection. */ |
| 2385 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2386 | { |
| 2387 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2388 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2390 | struct hlua *hlua; |
| 2391 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2392 | int low, high; |
| 2393 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2394 | struct xref *peer; |
| 2395 | struct stream_interface *si; |
| 2396 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2398 | if (lua_gettop(L) < 2) |
| 2399 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | |
| 2401 | /* Get args. */ |
| 2402 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2403 | |
| 2404 | /* Check if we run on the same thread than the xreator thread. |
| 2405 | * We cannot access to the socket if the thread is different. |
| 2406 | */ |
| 2407 | if (socket->tid != tid) |
| 2408 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2409 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2410 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2411 | if (lua_gettop(L) >= 3) { |
| 2412 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2413 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2415 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2416 | * that are not enclosed within square brackets. |
| 2417 | */ |
| 2418 | if (port > 0) { |
| 2419 | luaL_buffinit(L, &b); |
| 2420 | luaL_addstring(&b, ip); |
| 2421 | luaL_addchar(&b, ':'); |
| 2422 | luaL_pushresult(&b); |
| 2423 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2424 | } |
| 2425 | } |
| 2426 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2427 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2428 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2429 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2430 | lua_pushnil(L); |
| 2431 | return 1; |
| 2432 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2433 | |
| 2434 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2435 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2436 | if (!addr) { |
| 2437 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2438 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2439 | } |
| 2440 | if (low != high) { |
| 2441 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2442 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2444 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2446 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2447 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2448 | if (port == -1) { |
| 2449 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2450 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2451 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2452 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2453 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2454 | if (port == -1) { |
| 2455 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2456 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2457 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2458 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2459 | } |
| 2460 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2461 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2462 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2463 | si = appctx->owner; |
| 2464 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2465 | |
| 2466 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2467 | xref_unlock(&socket->xref, peer); |
| 2468 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2469 | } |
| 2470 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2471 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2473 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2474 | |
| 2475 | /* inform the stream that we want to be notified whenever the |
| 2476 | * connection completes. |
| 2477 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2478 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2479 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2480 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2481 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2482 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2483 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2484 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2485 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2486 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2487 | } |
| 2488 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2489 | |
| 2490 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2491 | /* Return yield waiting for connection. */ |
| 2492 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2493 | 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] | 2494 | |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2498 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2500 | { |
| 2501 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2502 | struct xref *peer; |
| 2503 | struct appctx *appctx; |
| 2504 | struct stream_interface *si; |
| 2505 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | |
| 2507 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2508 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2509 | |
| 2510 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2511 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2512 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2513 | lua_pushnil(L); |
| 2514 | return 1; |
| 2515 | } |
| 2516 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2517 | si = appctx->owner; |
| 2518 | s = si_strm(si); |
| 2519 | |
| 2520 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2521 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2523 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2524 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | |
| 2526 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2527 | { |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
| 2531 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2532 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2533 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2534 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2535 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2536 | struct xref *peer; |
| 2537 | struct appctx *appctx; |
| 2538 | struct stream_interface *si; |
| 2539 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2540 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2541 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2542 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2543 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2544 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2545 | /* convert the timeout to millis */ |
| 2546 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2547 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2548 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2549 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2550 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2551 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2552 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2553 | 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] | 2554 | |
| 2555 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2556 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2557 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2558 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2559 | /* Check if we run on the same thread than the xreator thread. |
| 2560 | * We cannot access to the socket if the thread is different. |
| 2561 | */ |
| 2562 | if (socket->tid != tid) |
| 2563 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2564 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2565 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2566 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2567 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2568 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2569 | WILL_LJMP(lua_error(L)); |
| 2570 | return 0; |
| 2571 | } |
| 2572 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2573 | si = appctx->owner; |
| 2574 | s = si_strm(si); |
| 2575 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2576 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2577 | s->req.rto = tmout; |
| 2578 | s->req.wto = tmout; |
| 2579 | s->res.rto = tmout; |
| 2580 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2581 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2582 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2583 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2584 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2585 | |
| 2586 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2587 | task_queue(s->task); |
| 2588 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2589 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2591 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2592 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2596 | { |
| 2597 | struct hlua_socket *socket; |
| 2598 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2599 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2600 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | |
| 2602 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2603 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | hlua_pusherror(L, "socket: full stack"); |
| 2605 | goto out_fail_conf; |
| 2606 | } |
| 2607 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2608 | /* Create the object: obj[0] = userdata. */ |
| 2609 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2610 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2611 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2613 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2615 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2616 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2617 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2618 | goto out_fail_conf; |
| 2619 | } |
| 2620 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2621 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2623 | lua_setmetatable(L, -2); |
| 2624 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2625 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2626 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2627 | if (!appctx) { |
| 2628 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2629 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2630 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2632 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2633 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2634 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2635 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2636 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2637 | /* Now create a session, task and stream for this applet */ |
| 2638 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2639 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2641 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2644 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2645 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2646 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2647 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | } |
| 2649 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2650 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2651 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | /* Configure "right" stream interface. this "si" is used to connect |
| 2654 | * and retrieve data from the server. The connection is initialized |
| 2655 | * with the "struct server". |
| 2656 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2657 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | |
| 2659 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2660 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2661 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | return 1; |
| 2664 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2665 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2666 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2667 | out_fail_sess: |
| 2668 | appctx_free(appctx); |
| 2669 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | WILL_LJMP(lua_error(L)); |
| 2671 | return 0; |
| 2672 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2673 | |
| 2674 | /* |
| 2675 | * |
| 2676 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2677 | * Class Channel |
| 2678 | * |
| 2679 | * |
| 2680 | */ |
| 2681 | |
| 2682 | /* Returns the struct hlua_channel join to the class channel in the |
| 2683 | * stack entry "ud" or throws an argument error. |
| 2684 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2685 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2686 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2687 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2688 | } |
| 2689 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2690 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2691 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2692 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2693 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2694 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2695 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2696 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2697 | return 0; |
| 2698 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2699 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2700 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2701 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2702 | |
| 2703 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2704 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2705 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2706 | return 1; |
| 2707 | } |
| 2708 | |
| 2709 | /* Duplicate all the data present in the input channel and put it |
| 2710 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2711 | * the stack if the channel is closed and all the data are consumed, |
| 2712 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2713 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2714 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2715 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2716 | { |
| 2717 | char *blk1; |
| 2718 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2719 | size_t len1; |
| 2720 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2721 | int ret; |
| 2722 | luaL_Buffer b; |
| 2723 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2724 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2725 | if (unlikely(ret == 0)) |
| 2726 | return 0; |
| 2727 | |
| 2728 | if (unlikely(ret < 0)) { |
| 2729 | lua_pushnil(L); |
| 2730 | return -1; |
| 2731 | } |
| 2732 | |
| 2733 | luaL_buffinit(L, &b); |
| 2734 | luaL_addlstring(&b, blk1, len1); |
| 2735 | if (unlikely(ret == 2)) |
| 2736 | luaL_addlstring(&b, blk2, len2); |
| 2737 | luaL_pushresult(&b); |
| 2738 | |
| 2739 | if (unlikely(ret == 2)) |
| 2740 | return len1 + len2; |
| 2741 | return len1; |
| 2742 | } |
| 2743 | |
| 2744 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2745 | * a yield. This function keep the data in the buffer. |
| 2746 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2747 | __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] | 2748 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2749 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2750 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2751 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2752 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2753 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2754 | WILL_LJMP(lua_error(L)); |
| 2755 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2756 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2757 | 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] | 2758 | return 1; |
| 2759 | } |
| 2760 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2761 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2762 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2763 | { |
| 2764 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2765 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2766 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2767 | } |
| 2768 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2769 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2770 | * a yield. This function consumes the data in the buffer. It returns |
| 2771 | * a string containing the data or a nil pointer if no data are available |
| 2772 | * and the channel is closed. |
| 2773 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2774 | __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] | 2775 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2776 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2777 | int ret; |
| 2778 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2779 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2780 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2781 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2782 | WILL_LJMP(lua_error(L)); |
| 2783 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2784 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2785 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2786 | 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] | 2787 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2788 | if (unlikely(ret == -1)) |
| 2789 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2790 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2791 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | return 1; |
| 2793 | } |
| 2794 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2795 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2796 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2797 | { |
| 2798 | MAY_LJMP(check_args(L, 1, "get")); |
| 2799 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2800 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2801 | } |
| 2802 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2803 | /* This functions consumes and returns one line. If the channel is closed, |
| 2804 | * 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] | 2805 | * 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] | 2806 | * value. |
| 2807 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2808 | __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] | 2809 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2810 | char *blk1; |
| 2811 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2812 | size_t len1; |
| 2813 | size_t len2; |
| 2814 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2815 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2816 | int ret; |
| 2817 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2818 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2819 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2820 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2821 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2822 | WILL_LJMP(lua_error(L)); |
| 2823 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2824 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2825 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2826 | 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] | 2827 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2828 | if (ret == -1) { |
| 2829 | lua_pushnil(L); |
| 2830 | return 1; |
| 2831 | } |
| 2832 | |
| 2833 | luaL_buffinit(L, &b); |
| 2834 | luaL_addlstring(&b, blk1, len1); |
| 2835 | len = len1; |
| 2836 | if (unlikely(ret == 2)) { |
| 2837 | luaL_addlstring(&b, blk2, len2); |
| 2838 | len += len2; |
| 2839 | } |
| 2840 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2841 | 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] | 2842 | return 1; |
| 2843 | } |
| 2844 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2845 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2846 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2847 | { |
| 2848 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2849 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2850 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2851 | } |
| 2852 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2853 | /* This function takes a string as input, and append it at the |
| 2854 | * input side of channel. If the data is too big, but a space |
| 2855 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2856 | * yields. If the data is bigger than the buffer, or if the |
| 2857 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2858 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2859 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2860 | __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] | 2861 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2862 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | size_t len; |
| 2864 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2865 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2866 | int ret; |
| 2867 | int max; |
| 2868 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2869 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2870 | WILL_LJMP(lua_error(L)); |
| 2871 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2872 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2873 | * the request buffer if its not required. |
| 2874 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2875 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2876 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2877 | 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] | 2878 | } |
| 2879 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2880 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2881 | if (max > len - l) |
| 2882 | max = len - l; |
| 2883 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2884 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2885 | if (ret == -2 || ret == -3) { |
| 2886 | lua_pushinteger(L, -1); |
| 2887 | return 1; |
| 2888 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2889 | if (ret == -1) { |
| 2890 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2891 | 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] | 2892 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2893 | l += ret; |
| 2894 | lua_pop(L, 1); |
| 2895 | lua_pushinteger(L, l); |
| 2896 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2897 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2898 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2899 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2901 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2902 | */ |
| 2903 | return 1; |
| 2904 | } |
| 2905 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2906 | 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] | 2907 | return 1; |
| 2908 | } |
| 2909 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2910 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2911 | * 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] | 2912 | * buffer size is too little for the data. |
| 2913 | */ |
| 2914 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2915 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2916 | size_t len; |
| 2917 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2919 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2920 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2921 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | lua_pushinteger(L, 0); |
| 2923 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2924 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | } |
| 2926 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2927 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2929 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2930 | * or -1 if the channel is closed or if the buffer size is too |
| 2931 | * little for the data. |
| 2932 | */ |
| 2933 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2934 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2935 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2936 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2938 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2939 | lua_pushinteger(L, 0); |
| 2940 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2941 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2942 | WILL_LJMP(lua_error(L)); |
| 2943 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2944 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2945 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2946 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2947 | } |
| 2948 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2949 | /* Append data in the output side of the buffer. This data is immediately |
| 2950 | * sent. The function returns the amount of data written. If the buffer |
| 2951 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2952 | * if the channel is closed. |
| 2953 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2954 | __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] | 2955 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2956 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2957 | size_t len; |
| 2958 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2959 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2960 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2961 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2962 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2963 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2964 | WILL_LJMP(lua_error(L)); |
| 2965 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2966 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2967 | lua_pushinteger(L, -1); |
| 2968 | return 1; |
| 2969 | } |
| 2970 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2971 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2972 | * the request buffer if its not required. |
| 2973 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2974 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2975 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2976 | 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] | 2977 | } |
| 2978 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2979 | /* The written data will be immediately sent, so we can check |
| 2980 | * the available space without taking in account the reserve. |
| 2981 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2982 | * data, because the buffer will be flushed. |
| 2983 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2984 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2985 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2986 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2987 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2988 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2989 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2990 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2991 | return 1; |
| 2992 | |
| 2993 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | if (max > len - l) |
| 2995 | max = len - l; |
| 2996 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2997 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2998 | * detects a non contiguous buffer and realign it. |
| 2999 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3000 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3001 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3002 | |
| 3003 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3004 | 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] | 3005 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | /* buffer replace considers that the input part is filled. |
| 3007 | * so, I must forward these new data in the output part. |
| 3008 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3009 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | |
| 3011 | l += max; |
| 3012 | lua_pop(L, 1); |
| 3013 | lua_pushinteger(L, l); |
| 3014 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3015 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3016 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3017 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3018 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3019 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3020 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3021 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3022 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3023 | if (l < len) { |
| 3024 | /* If we are waiting for space in the response buffer, we |
| 3025 | * must set the flag WAKERESWR. This flag required the task |
| 3026 | * wake up if any activity is detected on the response buffer. |
| 3027 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3028 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3029 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3030 | else |
| 3031 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3032 | 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] | 3033 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | |
| 3035 | return 1; |
| 3036 | } |
| 3037 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3038 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3039 | * yield the LUA process, and resume it without checking the |
| 3040 | * input arguments. |
| 3041 | */ |
| 3042 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3043 | { |
| 3044 | MAY_LJMP(check_args(L, 2, "send")); |
| 3045 | lua_pushinteger(L, 0); |
| 3046 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3047 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | /* This function forward and amount of butes. The data pass from |
| 3051 | * the input side of the buffer to the output side, and can be |
| 3052 | * forwarded. This function never fails. |
| 3053 | * |
| 3054 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3055 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3056 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3057 | __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] | 3058 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3059 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | int len; |
| 3061 | int l; |
| 3062 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3063 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3064 | |
| 3065 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3066 | |
| 3067 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3068 | WILL_LJMP(lua_error(L)); |
| 3069 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3070 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3071 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3072 | |
| 3073 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3074 | if (max > ci_data(chn)) |
| 3075 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3076 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3077 | l += max; |
| 3078 | |
| 3079 | lua_pop(L, 1); |
| 3080 | lua_pushinteger(L, l); |
| 3081 | |
| 3082 | /* Check if it miss bytes to forward. */ |
| 3083 | if (l < len) { |
| 3084 | /* The the input channel or the output channel are closed, we |
| 3085 | * must return the amount of data forwarded. |
| 3086 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3087 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3088 | return 1; |
| 3089 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3090 | /* If we are waiting for space data in the response buffer, we |
| 3091 | * must set the flag WAKERESWR. This flag required the task |
| 3092 | * wake up if any activity is detected on the response buffer. |
| 3093 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3094 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3095 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3096 | else |
| 3097 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3098 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3100 | 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] | 3101 | } |
| 3102 | |
| 3103 | return 1; |
| 3104 | } |
| 3105 | |
| 3106 | /* Just check the input and prepare the stack for the previous |
| 3107 | * function "hlua_channel_forward_yield" |
| 3108 | */ |
| 3109 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3110 | { |
| 3111 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3112 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3113 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3114 | |
| 3115 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3116 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | /* Just returns the number of bytes available in the input |
| 3120 | * side of the buffer. This function never fails. |
| 3121 | */ |
| 3122 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3123 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3124 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3125 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3127 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3128 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3129 | struct htx *htx = htxbuf(&chn->buf); |
| 3130 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3131 | } |
| 3132 | else |
| 3133 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3134 | return 1; |
| 3135 | } |
| 3136 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3137 | /* Returns true if the channel is full. */ |
| 3138 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3139 | { |
| 3140 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3141 | |
| 3142 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3143 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3144 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3145 | * applet). |
| 3146 | */ |
| 3147 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3148 | return 1; |
| 3149 | } |
| 3150 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3151 | /* Returns true if the channel is the response channel. */ |
| 3152 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3153 | { |
| 3154 | struct channel *chn; |
| 3155 | |
| 3156 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3157 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3158 | |
| 3159 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3160 | return 1; |
| 3161 | } |
| 3162 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3163 | /* Just returns the number of bytes available in the output |
| 3164 | * side of the buffer. This function never fails. |
| 3165 | */ |
| 3166 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3167 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3168 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3169 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3171 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3172 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3173 | return 1; |
| 3174 | } |
| 3175 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3176 | /* |
| 3177 | * |
| 3178 | * |
| 3179 | * Class Fetches |
| 3180 | * |
| 3181 | * |
| 3182 | */ |
| 3183 | |
| 3184 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3185 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3186 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3187 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3188 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3189 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | /* This function creates and push in the stack a fetch object according |
| 3193 | * with a current TXN. |
| 3194 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3195 | 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] | 3196 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3197 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3198 | |
| 3199 | /* Check stack size. */ |
| 3200 | if (!lua_checkstack(L, 3)) |
| 3201 | return 0; |
| 3202 | |
| 3203 | /* Create the object: obj[0] = userdata. |
| 3204 | * Note that the base of the Fetches object is the |
| 3205 | * transaction object. |
| 3206 | */ |
| 3207 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3208 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3209 | lua_rawseti(L, -2, 0); |
| 3210 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3211 | hsmp->s = txn->s; |
| 3212 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3213 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3214 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3215 | |
| 3216 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3217 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3218 | lua_setmetatable(L, -2); |
| 3219 | |
| 3220 | return 1; |
| 3221 | } |
| 3222 | |
| 3223 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3224 | * It uses closure argument to store the associated sample-fetch. It |
| 3225 | * returns only one argument or throws an error. An error is thrown |
| 3226 | * only if an error is encountered during the argument parsing. If |
| 3227 | * the "sample-fetch" function fails, nil is returned. |
| 3228 | */ |
| 3229 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3230 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3231 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3232 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3233 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3234 | int i; |
| 3235 | struct sample smp; |
| 3236 | |
| 3237 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3238 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3239 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3240 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3241 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3242 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3243 | /* Check execution authorization. */ |
| 3244 | if (f->use & SMP_USE_HTTP_ANY && |
| 3245 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3246 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3247 | "is not available in Lua services", f->kw); |
| 3248 | WILL_LJMP(lua_error(L)); |
| 3249 | } |
| 3250 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3251 | /* Get extra arguments. */ |
| 3252 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3253 | if (i >= ARGM_NBARGS) |
| 3254 | break; |
| 3255 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3256 | } |
| 3257 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3258 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3259 | |
| 3260 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3261 | 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] | 3262 | |
| 3263 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3264 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3265 | lua_pushfstring(L, "error in arguments"); |
| 3266 | WILL_LJMP(lua_error(L)); |
| 3267 | } |
| 3268 | |
| 3269 | /* Initialise the sample. */ |
| 3270 | memset(&smp, 0, sizeof(smp)); |
| 3271 | |
| 3272 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3273 | 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] | 3274 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3275 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3276 | lua_pushstring(L, ""); |
| 3277 | else |
| 3278 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3279 | return 1; |
| 3280 | } |
| 3281 | |
| 3282 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3283 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3284 | hlua_smp2lua_str(L, &smp); |
| 3285 | else |
| 3286 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3287 | return 1; |
| 3288 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3289 | |
| 3290 | /* |
| 3291 | * |
| 3292 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3293 | * Class Converters |
| 3294 | * |
| 3295 | * |
| 3296 | */ |
| 3297 | |
| 3298 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3299 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3300 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3301 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3302 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3303 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | /* This function creates and push in the stack a Converters object |
| 3307 | * according with a current TXN. |
| 3308 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3309 | 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] | 3310 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3311 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3312 | |
| 3313 | /* Check stack size. */ |
| 3314 | if (!lua_checkstack(L, 3)) |
| 3315 | return 0; |
| 3316 | |
| 3317 | /* Create the object: obj[0] = userdata. |
| 3318 | * Note that the base of the Converters object is the |
| 3319 | * same than the TXN object. |
| 3320 | */ |
| 3321 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3322 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3323 | lua_rawseti(L, -2, 0); |
| 3324 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3325 | hsmp->s = txn->s; |
| 3326 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3327 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3328 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3329 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3330 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3331 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3332 | lua_setmetatable(L, -2); |
| 3333 | |
| 3334 | return 1; |
| 3335 | } |
| 3336 | |
| 3337 | /* This function is an LUA binding. It is called with each converter. |
| 3338 | * It uses closure argument to store the associated converter. It |
| 3339 | * returns only one argument or throws an error. An error is thrown |
| 3340 | * only if an error is encountered during the argument parsing. If |
| 3341 | * the converter function function fails, nil is returned. |
| 3342 | */ |
| 3343 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3344 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3345 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3346 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3347 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3348 | int i; |
| 3349 | struct sample smp; |
| 3350 | |
| 3351 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3352 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3353 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3354 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3355 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3356 | |
| 3357 | /* Get extra arguments. */ |
| 3358 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3359 | if (i >= ARGM_NBARGS) |
| 3360 | break; |
| 3361 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3362 | } |
| 3363 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3364 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3365 | |
| 3366 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3367 | 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] | 3368 | |
| 3369 | /* Run the special args checker. */ |
| 3370 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3371 | hlua_pusherror(L, "error in arguments"); |
| 3372 | WILL_LJMP(lua_error(L)); |
| 3373 | } |
| 3374 | |
| 3375 | /* Initialise the sample. */ |
| 3376 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3377 | hlua_pusherror(L, "error in the input argument"); |
| 3378 | WILL_LJMP(lua_error(L)); |
| 3379 | } |
| 3380 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3381 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3382 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3383 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3384 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3385 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3386 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3387 | WILL_LJMP(lua_error(L)); |
| 3388 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3389 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3390 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3391 | hlua_pusherror(L, "error during the input argument casting"); |
| 3392 | WILL_LJMP(lua_error(L)); |
| 3393 | } |
| 3394 | |
| 3395 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3396 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3397 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3398 | lua_pushstring(L, ""); |
| 3399 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3400 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3401 | return 1; |
| 3402 | } |
| 3403 | |
| 3404 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3405 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3406 | hlua_smp2lua_str(L, &smp); |
| 3407 | else |
| 3408 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3409 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3410 | } |
| 3411 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3412 | /* |
| 3413 | * |
| 3414 | * |
| 3415 | * Class AppletTCP |
| 3416 | * |
| 3417 | * |
| 3418 | */ |
| 3419 | |
| 3420 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3421 | * a class stream, otherwise it throws an error. |
| 3422 | */ |
| 3423 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3424 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3425 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | /* This function creates and push in the stack an Applet object |
| 3429 | * according with a current TXN. |
| 3430 | */ |
| 3431 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3432 | { |
| 3433 | struct hlua_appctx *appctx; |
| 3434 | struct stream_interface *si = ctx->owner; |
| 3435 | struct stream *s = si_strm(si); |
| 3436 | struct proxy *p = s->be; |
| 3437 | |
| 3438 | /* Check stack size. */ |
| 3439 | if (!lua_checkstack(L, 3)) |
| 3440 | return 0; |
| 3441 | |
| 3442 | /* Create the object: obj[0] = userdata. |
| 3443 | * Note that the base of the Converters object is the |
| 3444 | * same than the TXN object. |
| 3445 | */ |
| 3446 | lua_newtable(L); |
| 3447 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3448 | lua_rawseti(L, -2, 0); |
| 3449 | appctx->appctx = ctx; |
| 3450 | appctx->htxn.s = s; |
| 3451 | appctx->htxn.p = p; |
| 3452 | |
| 3453 | /* Create the "f" field that contains a list of fetches. */ |
| 3454 | lua_pushstring(L, "f"); |
| 3455 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3456 | return 0; |
| 3457 | lua_settable(L, -3); |
| 3458 | |
| 3459 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3460 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3461 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3462 | return 0; |
| 3463 | lua_settable(L, -3); |
| 3464 | |
| 3465 | /* Create the "c" field that contains a list of converters. */ |
| 3466 | lua_pushstring(L, "c"); |
| 3467 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3468 | return 0; |
| 3469 | lua_settable(L, -3); |
| 3470 | |
| 3471 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3472 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3473 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3474 | return 0; |
| 3475 | lua_settable(L, -3); |
| 3476 | |
| 3477 | /* Pop a class stream metatable and affect it to the table. */ |
| 3478 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3479 | lua_setmetatable(L, -2); |
| 3480 | |
| 3481 | return 1; |
| 3482 | } |
| 3483 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3484 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3485 | { |
| 3486 | struct hlua_appctx *appctx; |
| 3487 | struct stream *s; |
| 3488 | const char *name; |
| 3489 | size_t len; |
| 3490 | struct sample smp; |
| 3491 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3492 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3493 | 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] | 3494 | |
| 3495 | /* It is useles to retrieve the stream, but this function |
| 3496 | * runs only in a stream context. |
| 3497 | */ |
| 3498 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3499 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3500 | s = appctx->htxn.s; |
| 3501 | |
| 3502 | /* Converts the third argument in a sample. */ |
| 3503 | hlua_lua2smp(L, 3, &smp); |
| 3504 | |
| 3505 | /* Store the sample in a variable. */ |
| 3506 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3507 | |
| 3508 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3509 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3510 | else |
| 3511 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3512 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3513 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3517 | { |
| 3518 | struct hlua_appctx *appctx; |
| 3519 | struct stream *s; |
| 3520 | const char *name; |
| 3521 | size_t len; |
| 3522 | struct sample smp; |
| 3523 | |
| 3524 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3525 | |
| 3526 | /* It is useles to retrieve the stream, but this function |
| 3527 | * runs only in a stream context. |
| 3528 | */ |
| 3529 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3530 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3531 | s = appctx->htxn.s; |
| 3532 | |
| 3533 | /* Unset the variable. */ |
| 3534 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3535 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3536 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3540 | { |
| 3541 | struct hlua_appctx *appctx; |
| 3542 | struct stream *s; |
| 3543 | const char *name; |
| 3544 | size_t len; |
| 3545 | struct sample smp; |
| 3546 | |
| 3547 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3548 | |
| 3549 | /* It is useles to retrieve the stream, but this function |
| 3550 | * runs only in a stream context. |
| 3551 | */ |
| 3552 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3553 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3554 | s = appctx->htxn.s; |
| 3555 | |
| 3556 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3557 | if (!vars_get_by_name(name, len, &smp)) { |
| 3558 | lua_pushnil(L); |
| 3559 | return 1; |
| 3560 | } |
| 3561 | |
| 3562 | return hlua_smp2lua(L, &smp); |
| 3563 | } |
| 3564 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3565 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3566 | { |
| 3567 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3568 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3569 | struct hlua *hlua; |
| 3570 | |
| 3571 | /* 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] | 3572 | if (!s->hlua) |
| 3573 | return 0; |
| 3574 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3575 | |
| 3576 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3577 | |
| 3578 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3579 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3580 | |
| 3581 | /* Get and store new value. */ |
| 3582 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3583 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3584 | |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
| 3588 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3589 | { |
| 3590 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3591 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3592 | struct hlua *hlua; |
| 3593 | |
| 3594 | /* 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] | 3595 | if (!s->hlua) { |
| 3596 | lua_pushnil(L); |
| 3597 | return 1; |
| 3598 | } |
| 3599 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3600 | |
| 3601 | /* Push configuration index in the stack. */ |
| 3602 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3603 | |
| 3604 | return 1; |
| 3605 | } |
| 3606 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3607 | /* If expected data not yet available, it returns a yield. This function |
| 3608 | * consumes the data in the buffer. It returns a string containing the |
| 3609 | * data. This string can be empty. |
| 3610 | */ |
| 3611 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3612 | { |
| 3613 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3614 | struct stream_interface *si = appctx->appctx->owner; |
| 3615 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3616 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3617 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3618 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3619 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3620 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3621 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3622 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3623 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3624 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3625 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3626 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3627 | 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] | 3628 | } |
| 3629 | |
| 3630 | /* End of data: commit the total strings and return. */ |
| 3631 | if (ret < 0) { |
| 3632 | luaL_pushresult(&appctx->b); |
| 3633 | return 1; |
| 3634 | } |
| 3635 | |
| 3636 | /* Ensure that the block 2 length is usable. */ |
| 3637 | if (ret == 1) |
| 3638 | len2 = 0; |
| 3639 | |
| 3640 | /* dont check the max length read and dont check. */ |
| 3641 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3642 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3643 | |
| 3644 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3645 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3646 | luaL_pushresult(&appctx->b); |
| 3647 | return 1; |
| 3648 | } |
| 3649 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3650 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3651 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3652 | { |
| 3653 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3654 | |
| 3655 | /* Initialise the string catenation. */ |
| 3656 | luaL_buffinit(L, &appctx->b); |
| 3657 | |
| 3658 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3659 | } |
| 3660 | |
| 3661 | /* If expected data not yet available, it returns a yield. This function |
| 3662 | * consumes the data in the buffer. It returns a string containing the |
| 3663 | * data. This string can be empty. |
| 3664 | */ |
| 3665 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3666 | { |
| 3667 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3668 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3669 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3670 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3671 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3672 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3673 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3674 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3675 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3676 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3677 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3678 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3679 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3680 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3681 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3682 | 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] | 3683 | } |
| 3684 | |
| 3685 | /* End of data: commit the total strings and return. */ |
| 3686 | if (ret < 0) { |
| 3687 | luaL_pushresult(&appctx->b); |
| 3688 | return 1; |
| 3689 | } |
| 3690 | |
| 3691 | /* Ensure that the block 2 length is usable. */ |
| 3692 | if (ret == 1) |
| 3693 | len2 = 0; |
| 3694 | |
| 3695 | if (len == -1) { |
| 3696 | |
| 3697 | /* If len == -1, catenate all the data avalaile and |
| 3698 | * yield because we want to get all the data until |
| 3699 | * the end of data stream. |
| 3700 | */ |
| 3701 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3702 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3703 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3704 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3705 | 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] | 3706 | |
| 3707 | } else { |
| 3708 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3709 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3710 | if (len1 > len) |
| 3711 | len1 = len; |
| 3712 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3713 | len -= len1; |
| 3714 | |
| 3715 | /* Copy the second block. */ |
| 3716 | if (len2 > len) |
| 3717 | len2 = len; |
| 3718 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3719 | len -= len2; |
| 3720 | |
| 3721 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3722 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3723 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3724 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3725 | if (len > 0) { |
| 3726 | lua_pushinteger(L, len); |
| 3727 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3728 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3729 | 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] | 3730 | } |
| 3731 | |
| 3732 | /* return the result. */ |
| 3733 | luaL_pushresult(&appctx->b); |
| 3734 | return 1; |
| 3735 | } |
| 3736 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3737 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3738 | hlua_pusherror(L, "Lua: internal error"); |
| 3739 | WILL_LJMP(lua_error(L)); |
| 3740 | return 0; |
| 3741 | } |
| 3742 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3743 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3744 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3745 | { |
| 3746 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3747 | int len = -1; |
| 3748 | |
| 3749 | if (lua_gettop(L) > 2) |
| 3750 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3751 | if (lua_gettop(L) >= 2) { |
| 3752 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3753 | lua_pop(L, 1); |
| 3754 | } |
| 3755 | |
| 3756 | /* Confirm or set the required length */ |
| 3757 | lua_pushinteger(L, len); |
| 3758 | |
| 3759 | /* Initialise the string catenation. */ |
| 3760 | luaL_buffinit(L, &appctx->b); |
| 3761 | |
| 3762 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3763 | } |
| 3764 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3765 | /* Append data in the output side of the buffer. This data is immediately |
| 3766 | * sent. The function returns the amount of data written. If the buffer |
| 3767 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3768 | * if the channel is closed. |
| 3769 | */ |
| 3770 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3771 | { |
| 3772 | size_t len; |
| 3773 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3774 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3775 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3776 | struct stream_interface *si = appctx->appctx->owner; |
| 3777 | struct channel *chn = si_ic(si); |
| 3778 | int max; |
| 3779 | |
| 3780 | /* Get the max amount of data which can write as input in the channel. */ |
| 3781 | max = channel_recv_max(chn); |
| 3782 | if (max > (len - l)) |
| 3783 | max = len - l; |
| 3784 | |
| 3785 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3786 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3787 | |
| 3788 | /* update counters. */ |
| 3789 | l += max; |
| 3790 | lua_pop(L, 1); |
| 3791 | lua_pushinteger(L, l); |
| 3792 | |
| 3793 | /* If some data is not send, declares the situation to the |
| 3794 | * applet, and returns a yield. |
| 3795 | */ |
| 3796 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3797 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3798 | 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] | 3799 | } |
| 3800 | |
| 3801 | return 1; |
| 3802 | } |
| 3803 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3804 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3805 | * yield the LUA process, and resume it without checking the |
| 3806 | * input arguments. |
| 3807 | */ |
| 3808 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3809 | { |
| 3810 | MAY_LJMP(check_args(L, 2, "send")); |
| 3811 | lua_pushinteger(L, 0); |
| 3812 | |
| 3813 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3814 | } |
| 3815 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3816 | /* |
| 3817 | * |
| 3818 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3819 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3820 | * |
| 3821 | * |
| 3822 | */ |
| 3823 | |
| 3824 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3825 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3826 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3827 | __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] | 3828 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3829 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3830 | } |
| 3831 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3832 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3833 | * according with a current TXN. |
| 3834 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3835 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3836 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3837 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3838 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3839 | struct stream_interface *si = ctx->owner; |
| 3840 | struct stream *s = si_strm(si); |
| 3841 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3842 | struct htx *htx; |
| 3843 | struct htx_blk *blk; |
| 3844 | struct htx_sl *sl; |
| 3845 | struct ist path; |
| 3846 | unsigned long long len = 0; |
| 3847 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3848 | |
| 3849 | /* Check stack size. */ |
| 3850 | if (!lua_checkstack(L, 3)) |
| 3851 | return 0; |
| 3852 | |
| 3853 | /* Create the object: obj[0] = userdata. |
| 3854 | * Note that the base of the Converters object is the |
| 3855 | * same than the TXN object. |
| 3856 | */ |
| 3857 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3858 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3859 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3860 | appctx->appctx = ctx; |
| 3861 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3862 | 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] | 3863 | appctx->htxn.s = s; |
| 3864 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3865 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3866 | /* Create the "f" field that contains a list of fetches. */ |
| 3867 | lua_pushstring(L, "f"); |
| 3868 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3869 | return 0; |
| 3870 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3871 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3872 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3873 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3874 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3875 | return 0; |
| 3876 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3877 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3878 | /* Create the "c" field that contains a list of converters. */ |
| 3879 | lua_pushstring(L, "c"); |
| 3880 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3881 | return 0; |
| 3882 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3883 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3884 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3885 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3886 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3887 | return 0; |
| 3888 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3889 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3890 | htx = htxbuf(&s->req.buf); |
| 3891 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3892 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3893 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3894 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3895 | /* Stores the request method. */ |
| 3896 | lua_pushstring(L, "method"); |
| 3897 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3898 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3899 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3900 | /* Stores the http version. */ |
| 3901 | lua_pushstring(L, "version"); |
| 3902 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3903 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3904 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3905 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3906 | * the array on the top of the stack. |
| 3907 | */ |
| 3908 | lua_pushstring(L, "headers"); |
| 3909 | htxn.s = s; |
| 3910 | htxn.p = px; |
| 3911 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 3912 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3913 | return 0; |
| 3914 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3915 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3916 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3917 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3918 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3919 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3920 | p = path.ptr; |
| 3921 | end = path.ptr + path.len; |
| 3922 | q = p; |
| 3923 | while (q < end && *q != '?') |
| 3924 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3925 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3926 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3927 | lua_pushstring(L, "path"); |
| 3928 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3929 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3930 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3931 | /* Stores the query string. */ |
| 3932 | lua_pushstring(L, "qs"); |
| 3933 | if (*q == '?') |
| 3934 | q++; |
| 3935 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3936 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3937 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3938 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3939 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3940 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3941 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3942 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3943 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3944 | break; |
| 3945 | if (type == HTX_BLK_DATA) |
| 3946 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3947 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3948 | if (htx->extra != ULLONG_MAX) |
| 3949 | len += htx->extra; |
| 3950 | |
| 3951 | /* Stores the request path. */ |
| 3952 | lua_pushstring(L, "length"); |
| 3953 | lua_pushinteger(L, len); |
| 3954 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3955 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3956 | /* Create an empty array of HTTP request headers. */ |
| 3957 | lua_pushstring(L, "response"); |
| 3958 | lua_newtable(L); |
| 3959 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3960 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3961 | /* Pop a class stream metatable and affect it to the table. */ |
| 3962 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3963 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3964 | |
| 3965 | return 1; |
| 3966 | } |
| 3967 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3968 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3969 | { |
| 3970 | struct hlua_appctx *appctx; |
| 3971 | struct stream *s; |
| 3972 | const char *name; |
| 3973 | size_t len; |
| 3974 | struct sample smp; |
| 3975 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3976 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3977 | 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] | 3978 | |
| 3979 | /* It is useles to retrieve the stream, but this function |
| 3980 | * runs only in a stream context. |
| 3981 | */ |
| 3982 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3983 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3984 | s = appctx->htxn.s; |
| 3985 | |
| 3986 | /* Converts the third argument in a sample. */ |
| 3987 | hlua_lua2smp(L, 3, &smp); |
| 3988 | |
| 3989 | /* Store the sample in a variable. */ |
| 3990 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3991 | |
| 3992 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3993 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3994 | else |
| 3995 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3996 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3997 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4001 | { |
| 4002 | struct hlua_appctx *appctx; |
| 4003 | struct stream *s; |
| 4004 | const char *name; |
| 4005 | size_t len; |
| 4006 | struct sample smp; |
| 4007 | |
| 4008 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4009 | |
| 4010 | /* It is useles to retrieve the stream, but this function |
| 4011 | * runs only in a stream context. |
| 4012 | */ |
| 4013 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4014 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4015 | s = appctx->htxn.s; |
| 4016 | |
| 4017 | /* Unset the variable. */ |
| 4018 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4019 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4020 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4024 | { |
| 4025 | struct hlua_appctx *appctx; |
| 4026 | struct stream *s; |
| 4027 | const char *name; |
| 4028 | size_t len; |
| 4029 | struct sample smp; |
| 4030 | |
| 4031 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4032 | |
| 4033 | /* It is useles to retrieve the stream, but this function |
| 4034 | * runs only in a stream context. |
| 4035 | */ |
| 4036 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4037 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4038 | s = appctx->htxn.s; |
| 4039 | |
| 4040 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4041 | if (!vars_get_by_name(name, len, &smp)) { |
| 4042 | lua_pushnil(L); |
| 4043 | return 1; |
| 4044 | } |
| 4045 | |
| 4046 | return hlua_smp2lua(L, &smp); |
| 4047 | } |
| 4048 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4049 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4050 | { |
| 4051 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4052 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4053 | struct hlua *hlua; |
| 4054 | |
| 4055 | /* 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] | 4056 | if (!s->hlua) |
| 4057 | return 0; |
| 4058 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4059 | |
| 4060 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4061 | |
| 4062 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4063 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4064 | |
| 4065 | /* Get and store new value. */ |
| 4066 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4067 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4068 | |
| 4069 | return 0; |
| 4070 | } |
| 4071 | |
| 4072 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4073 | { |
| 4074 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4075 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4076 | struct hlua *hlua; |
| 4077 | |
| 4078 | /* 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] | 4079 | if (!s->hlua) { |
| 4080 | lua_pushnil(L); |
| 4081 | return 1; |
| 4082 | } |
| 4083 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4084 | |
| 4085 | /* Push configuration index in the stack. */ |
| 4086 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4087 | |
| 4088 | return 1; |
| 4089 | } |
| 4090 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4091 | /* If expected data not yet available, it returns a yield. This function |
| 4092 | * consumes the data in the buffer. It returns a string containing the |
| 4093 | * data. This string can be empty. |
| 4094 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4095 | __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] | 4096 | { |
| 4097 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4098 | struct stream_interface *si = appctx->appctx->owner; |
| 4099 | struct channel *req = si_oc(si); |
| 4100 | struct htx *htx; |
| 4101 | struct htx_blk *blk; |
| 4102 | size_t count; |
| 4103 | int stop = 0; |
| 4104 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4105 | htx = htx_from_buf(&req->buf); |
| 4106 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4107 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4108 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4109 | while (count && !stop && blk) { |
| 4110 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4111 | uint32_t sz = htx_get_blksz(blk); |
| 4112 | struct ist v; |
| 4113 | uint32_t vlen; |
| 4114 | char *nl; |
| 4115 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4116 | if (type == HTX_BLK_EOM) { |
| 4117 | stop = 1; |
| 4118 | break; |
| 4119 | } |
| 4120 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4121 | vlen = sz; |
| 4122 | if (vlen > count) { |
| 4123 | if (type != HTX_BLK_DATA) |
| 4124 | break; |
| 4125 | vlen = count; |
| 4126 | } |
| 4127 | |
| 4128 | switch (type) { |
| 4129 | case HTX_BLK_UNUSED: |
| 4130 | break; |
| 4131 | |
| 4132 | case HTX_BLK_DATA: |
| 4133 | v = htx_get_blk_value(htx, blk); |
| 4134 | v.len = vlen; |
| 4135 | nl = istchr(v, '\n'); |
| 4136 | if (nl != NULL) { |
| 4137 | stop = 1; |
| 4138 | vlen = nl - v.ptr + 1; |
| 4139 | } |
| 4140 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4141 | break; |
| 4142 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4143 | case HTX_BLK_TLR: |
| 4144 | case HTX_BLK_EOM: |
| 4145 | stop = 1; |
| 4146 | break; |
| 4147 | |
| 4148 | default: |
| 4149 | break; |
| 4150 | } |
| 4151 | |
| 4152 | co_set_data(req, co_data(req) - vlen); |
| 4153 | count -= vlen; |
| 4154 | if (sz == vlen) |
| 4155 | blk = htx_remove_blk(htx, blk); |
| 4156 | else { |
| 4157 | htx_cut_data_blk(htx, blk, vlen); |
| 4158 | break; |
| 4159 | } |
| 4160 | } |
| 4161 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4162 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4163 | if (!stop) { |
| 4164 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4165 | 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] | 4166 | } |
| 4167 | |
| 4168 | /* return the result. */ |
| 4169 | luaL_pushresult(&appctx->b); |
| 4170 | return 1; |
| 4171 | } |
| 4172 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4173 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4174 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4175 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4176 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4177 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4178 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4179 | /* Initialise the string catenation. */ |
| 4180 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4181 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4182 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4183 | } |
| 4184 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4185 | /* If expected data not yet available, it returns a yield. This function |
| 4186 | * consumes the data in the buffer. It returns a string containing the |
| 4187 | * data. This string can be empty. |
| 4188 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4189 | __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] | 4190 | { |
| 4191 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4192 | struct stream_interface *si = appctx->appctx->owner; |
| 4193 | struct channel *req = si_oc(si); |
| 4194 | struct htx *htx; |
| 4195 | struct htx_blk *blk; |
| 4196 | size_t count; |
| 4197 | int len; |
| 4198 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4199 | htx = htx_from_buf(&req->buf); |
| 4200 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4201 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4202 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4203 | while (count && len && blk) { |
| 4204 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4205 | uint32_t sz = htx_get_blksz(blk); |
| 4206 | struct ist v; |
| 4207 | uint32_t vlen; |
| 4208 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4209 | if (type == HTX_BLK_EOM) { |
| 4210 | len = 0; |
| 4211 | break; |
| 4212 | } |
| 4213 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4214 | vlen = sz; |
| 4215 | if (len > 0 && vlen > len) |
| 4216 | vlen = len; |
| 4217 | if (vlen > count) { |
| 4218 | if (type != HTX_BLK_DATA) |
| 4219 | break; |
| 4220 | vlen = count; |
| 4221 | } |
| 4222 | |
| 4223 | switch (type) { |
| 4224 | case HTX_BLK_UNUSED: |
| 4225 | break; |
| 4226 | |
| 4227 | case HTX_BLK_DATA: |
| 4228 | v = htx_get_blk_value(htx, blk); |
| 4229 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4230 | break; |
| 4231 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4232 | case HTX_BLK_TLR: |
| 4233 | case HTX_BLK_EOM: |
| 4234 | len = 0; |
| 4235 | break; |
| 4236 | |
| 4237 | default: |
| 4238 | break; |
| 4239 | } |
| 4240 | |
| 4241 | co_set_data(req, co_data(req) - vlen); |
| 4242 | count -= vlen; |
| 4243 | if (len > 0) |
| 4244 | len -= vlen; |
| 4245 | if (sz == vlen) |
| 4246 | blk = htx_remove_blk(htx, blk); |
| 4247 | else { |
| 4248 | htx_cut_data_blk(htx, blk, vlen); |
| 4249 | break; |
| 4250 | } |
| 4251 | } |
| 4252 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4253 | htx_to_buf(htx, &req->buf); |
| 4254 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4255 | /* If we are no other data available, yield waiting for new data. */ |
| 4256 | if (len) { |
| 4257 | if (len > 0) { |
| 4258 | lua_pushinteger(L, len); |
| 4259 | lua_replace(L, 2); |
| 4260 | } |
| 4261 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4262 | 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] | 4263 | } |
| 4264 | |
| 4265 | /* return the result. */ |
| 4266 | luaL_pushresult(&appctx->b); |
| 4267 | return 1; |
| 4268 | } |
| 4269 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4270 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4271 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4272 | { |
| 4273 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4274 | int len = -1; |
| 4275 | |
| 4276 | /* Check arguments. */ |
| 4277 | if (lua_gettop(L) > 2) |
| 4278 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4279 | if (lua_gettop(L) >= 2) { |
| 4280 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4281 | lua_pop(L, 1); |
| 4282 | } |
| 4283 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4284 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4285 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4286 | /* Initialise the string catenation. */ |
| 4287 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4288 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4289 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | /* Append data in the output side of the buffer. This data is immediately |
| 4293 | * sent. The function returns the amount of data written. If the buffer |
| 4294 | * cannot contain the data, the function yields. The function returns -1 |
| 4295 | * if the channel is closed. |
| 4296 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4297 | __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] | 4298 | { |
| 4299 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4300 | struct stream_interface *si = appctx->appctx->owner; |
| 4301 | struct channel *res = si_ic(si); |
| 4302 | struct htx *htx = htx_from_buf(&res->buf); |
| 4303 | const char *data; |
| 4304 | size_t len; |
| 4305 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4306 | int max; |
| 4307 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4308 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4309 | if (!max) |
| 4310 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4311 | |
| 4312 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4313 | |
| 4314 | /* Get the max amount of data which can write as input in the channel. */ |
| 4315 | if (max > (len - l)) |
| 4316 | max = len - l; |
| 4317 | |
| 4318 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4319 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4320 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4321 | |
| 4322 | /* update counters. */ |
| 4323 | l += max; |
| 4324 | lua_pop(L, 1); |
| 4325 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4326 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4327 | /* If some data is not send, declares the situation to the |
| 4328 | * applet, and returns a yield. |
| 4329 | */ |
| 4330 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4331 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4332 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4333 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4334 | 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] | 4335 | } |
| 4336 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4337 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4338 | return 1; |
| 4339 | } |
| 4340 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4341 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4342 | * yield the LUA process, and resume it without checking the |
| 4343 | * input arguments. |
| 4344 | */ |
| 4345 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4346 | { |
| 4347 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4348 | |
| 4349 | /* We want to send some data. Headers must be sent. */ |
| 4350 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4351 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4352 | WILL_LJMP(lua_error(L)); |
| 4353 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4354 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4355 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4356 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4357 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4358 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4362 | { |
| 4363 | const char *name; |
| 4364 | int ret; |
| 4365 | |
| 4366 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4367 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4368 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4369 | |
| 4370 | /* Push in the stack the "response" entry. */ |
| 4371 | ret = lua_getfield(L, 1, "response"); |
| 4372 | if (ret != LUA_TTABLE) { |
| 4373 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4374 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4375 | WILL_LJMP(lua_error(L)); |
| 4376 | } |
| 4377 | |
| 4378 | /* check if the header is already registered if it is not |
| 4379 | * the case, register it. |
| 4380 | */ |
| 4381 | ret = lua_getfield(L, -1, name); |
| 4382 | if (ret == LUA_TNIL) { |
| 4383 | |
| 4384 | /* Entry not found. */ |
| 4385 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4386 | |
| 4387 | /* Insert the new header name in the array in the top of the stack. |
| 4388 | * It left the new array in the top of the stack. |
| 4389 | */ |
| 4390 | lua_newtable(L); |
| 4391 | lua_pushvalue(L, 2); |
| 4392 | lua_pushvalue(L, -2); |
| 4393 | lua_settable(L, -4); |
| 4394 | |
| 4395 | } else if (ret != LUA_TTABLE) { |
| 4396 | |
| 4397 | /* corruption error. */ |
| 4398 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4399 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4400 | WILL_LJMP(lua_error(L)); |
| 4401 | } |
| 4402 | |
| 4403 | /* Now the top od thestack is an array of values. We push |
| 4404 | * the header value as new entry. |
| 4405 | */ |
| 4406 | lua_pushvalue(L, 3); |
| 4407 | ret = lua_rawlen(L, -2); |
| 4408 | lua_rawseti(L, -2, ret + 1); |
| 4409 | lua_pushboolean(L, 1); |
| 4410 | return 1; |
| 4411 | } |
| 4412 | |
| 4413 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4414 | { |
| 4415 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4416 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4417 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4418 | |
| 4419 | if (status < 100 || status > 599) { |
| 4420 | lua_pushboolean(L, 0); |
| 4421 | return 1; |
| 4422 | } |
| 4423 | |
| 4424 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4425 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4426 | lua_pushboolean(L, 1); |
| 4427 | return 1; |
| 4428 | } |
| 4429 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4430 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4431 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4432 | { |
| 4433 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4434 | struct stream_interface *si = appctx->appctx->owner; |
| 4435 | struct channel *res = si_ic(si); |
| 4436 | struct htx *htx; |
| 4437 | struct htx_sl *sl; |
| 4438 | struct h1m h1m; |
| 4439 | const char *status, *reason; |
| 4440 | const char *name, *value; |
| 4441 | size_t nlen, vlen; |
| 4442 | unsigned int flags; |
| 4443 | |
| 4444 | /* Send the message at once. */ |
| 4445 | htx = htx_from_buf(&res->buf); |
| 4446 | h1m_init_res(&h1m); |
| 4447 | |
| 4448 | /* Use the same http version than the request. */ |
| 4449 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4450 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4451 | if (reason == NULL) |
| 4452 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4453 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4454 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4455 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4456 | } |
| 4457 | else { |
| 4458 | flags = HTX_SL_F_IS_RESP; |
| 4459 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4460 | } |
| 4461 | if (!sl) { |
| 4462 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4463 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4464 | WILL_LJMP(lua_error(L)); |
| 4465 | } |
| 4466 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4467 | |
| 4468 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4469 | lua_pushvalue(L, 0); |
| 4470 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4471 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4472 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4473 | WILL_LJMP(lua_error(L)); |
| 4474 | } |
| 4475 | |
| 4476 | /* Browse the list of headers. */ |
| 4477 | lua_pushnil(L); |
| 4478 | while(lua_next(L, -2) != 0) { |
| 4479 | /* We expect a string as -2. */ |
| 4480 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4481 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4482 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4483 | lua_typename(L, lua_type(L, -2))); |
| 4484 | WILL_LJMP(lua_error(L)); |
| 4485 | } |
| 4486 | name = lua_tolstring(L, -2, &nlen); |
| 4487 | |
| 4488 | /* We expect an array as -1. */ |
| 4489 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4490 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4491 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4492 | name, |
| 4493 | lua_typename(L, lua_type(L, -1))); |
| 4494 | WILL_LJMP(lua_error(L)); |
| 4495 | } |
| 4496 | |
| 4497 | /* Browse the table who is on the top of the stack. */ |
| 4498 | lua_pushnil(L); |
| 4499 | while(lua_next(L, -2) != 0) { |
| 4500 | int id; |
| 4501 | |
| 4502 | /* We expect a number as -2. */ |
| 4503 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4504 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4505 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4506 | name, |
| 4507 | lua_typename(L, lua_type(L, -2))); |
| 4508 | WILL_LJMP(lua_error(L)); |
| 4509 | } |
| 4510 | id = lua_tointeger(L, -2); |
| 4511 | |
| 4512 | /* We expect a string as -2. */ |
| 4513 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4514 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4515 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4516 | name, id, |
| 4517 | lua_typename(L, lua_type(L, -1))); |
| 4518 | WILL_LJMP(lua_error(L)); |
| 4519 | } |
| 4520 | value = lua_tolstring(L, -1, &vlen); |
| 4521 | |
| 4522 | /* Simple Protocol checks. */ |
| 4523 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4524 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4525 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4526 | struct ist v = ist2(value, vlen); |
| 4527 | int ret; |
| 4528 | |
| 4529 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4530 | if (ret < 0) { |
| 4531 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4532 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4533 | name); |
| 4534 | WILL_LJMP(lua_error(L)); |
| 4535 | } |
| 4536 | else if (ret == 0) |
| 4537 | goto next; /* Skip it */ |
| 4538 | } |
| 4539 | |
| 4540 | /* Add a new header */ |
| 4541 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4542 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4543 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4544 | name); |
| 4545 | WILL_LJMP(lua_error(L)); |
| 4546 | } |
| 4547 | next: |
| 4548 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4549 | lua_pop(L, 1); |
| 4550 | } |
| 4551 | |
| 4552 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4553 | lua_pop(L, 1); |
| 4554 | } |
| 4555 | |
| 4556 | if (h1m.flags & H1_MF_CHNK) |
| 4557 | h1m.flags &= ~H1_MF_CLEN; |
| 4558 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4559 | h1m.flags |= H1_MF_XFER_LEN; |
| 4560 | |
| 4561 | /* Uset HTX start-line flags */ |
| 4562 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4563 | flags |= HTX_SL_F_XFER_ENC; |
| 4564 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4565 | flags |= HTX_SL_F_XFER_LEN; |
| 4566 | if (h1m.flags & H1_MF_CHNK) |
| 4567 | flags |= HTX_SL_F_CHNK; |
| 4568 | else if (h1m.flags & H1_MF_CLEN) |
| 4569 | flags |= HTX_SL_F_CLEN; |
| 4570 | if (h1m.body_len == 0) |
| 4571 | flags |= HTX_SL_F_BODYLESS; |
| 4572 | } |
| 4573 | sl->flags |= flags; |
| 4574 | |
| 4575 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4576 | * and the status code implies the presence of a message body, we must |
| 4577 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4578 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4579 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4580 | */ |
| 4581 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4582 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4583 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4584 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4585 | /* Add a new header */ |
| 4586 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4587 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4588 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4589 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4590 | WILL_LJMP(lua_error(L)); |
| 4591 | } |
| 4592 | } |
| 4593 | |
| 4594 | /* Finalize headers. */ |
| 4595 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4596 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4597 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4598 | WILL_LJMP(lua_error(L)); |
| 4599 | } |
| 4600 | |
| 4601 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4602 | b_reset(&res->buf); |
| 4603 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4604 | WILL_LJMP(lua_error(L)); |
| 4605 | } |
| 4606 | |
| 4607 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4608 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4609 | |
| 4610 | /* Headers sent, set the flag. */ |
| 4611 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4612 | return 0; |
| 4613 | |
| 4614 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4615 | /* We will build the status line and the headers of the HTTP response. |
| 4616 | * We will try send at once if its not possible, we give back the hand |
| 4617 | * waiting for more room. |
| 4618 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4619 | __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] | 4620 | { |
| 4621 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4622 | struct stream_interface *si = appctx->appctx->owner; |
| 4623 | struct channel *res = si_ic(si); |
| 4624 | |
| 4625 | if (co_data(res)) { |
| 4626 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4627 | 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] | 4628 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4629 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4633 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4634 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4635 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4636 | } |
| 4637 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4638 | /* |
| 4639 | * |
| 4640 | * |
| 4641 | * Class HTTP |
| 4642 | * |
| 4643 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4644 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4645 | |
| 4646 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4647 | * a class stream, otherwise it throws an error. |
| 4648 | */ |
| 4649 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4650 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4651 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4652 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4653 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4654 | /* This function creates and push in the stack a HTTP object |
| 4655 | * according with a current TXN. |
| 4656 | */ |
| 4657 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4658 | { |
| 4659 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4660 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4661 | /* Check stack size. */ |
| 4662 | if (!lua_checkstack(L, 3)) |
| 4663 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4664 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4665 | /* Create the object: obj[0] = userdata. |
| 4666 | * Note that the base of the Converters object is the |
| 4667 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4668 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4669 | lua_newtable(L); |
| 4670 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4671 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | |
| 4673 | htxn->s = txn->s; |
| 4674 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4675 | htxn->dir = txn->dir; |
| 4676 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4677 | |
| 4678 | /* Pop a class stream metatable and affect it to the table. */ |
| 4679 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4680 | lua_setmetatable(L, -2); |
| 4681 | |
| 4682 | return 1; |
| 4683 | } |
| 4684 | |
| 4685 | /* This function creates ans returns an array of HTTP headers. |
| 4686 | * This function does not fails. It is used as wrapper with the |
| 4687 | * 2 following functions. |
| 4688 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4689 | __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] | 4690 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4691 | struct htx *htx; |
| 4692 | int32_t pos; |
| 4693 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4694 | /* Create the table. */ |
| 4695 | lua_newtable(L); |
| 4696 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4698 | htx = htxbuf(&msg->chn->buf); |
| 4699 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4700 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4701 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4702 | struct ist n, v; |
| 4703 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4704 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4705 | if (type == HTX_BLK_HDR) { |
| 4706 | n = htx_get_blk_name(htx,blk); |
| 4707 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4708 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4709 | else if (type == HTX_BLK_EOH) |
| 4710 | break; |
| 4711 | else |
| 4712 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4713 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4714 | /* Check for existing entry: |
| 4715 | * assume that the table is on the top of the stack, and |
| 4716 | * push the key in the stack, the function lua_gettable() |
| 4717 | * perform the lookup. |
| 4718 | */ |
| 4719 | lua_pushlstring(L, n.ptr, n.len); |
| 4720 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4721 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4722 | switch (lua_type(L, -1)) { |
| 4723 | case LUA_TNIL: |
| 4724 | /* Table not found, create it. */ |
| 4725 | lua_pop(L, 1); /* remove the nil value. */ |
| 4726 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4727 | lua_newtable(L); /* create and push empty table. */ |
| 4728 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4729 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4730 | 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] | 4731 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4732 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4733 | case LUA_TTABLE: |
| 4734 | /* Entry found: push the value in the table. */ |
| 4735 | len = lua_rawlen(L, -1); |
| 4736 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4737 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4738 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4739 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4740 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4741 | default: |
| 4742 | /* Other cases are errors. */ |
| 4743 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4744 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4745 | } |
| 4746 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4747 | return 1; |
| 4748 | } |
| 4749 | |
| 4750 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4751 | { |
| 4752 | struct hlua_txn *htxn; |
| 4753 | |
| 4754 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4755 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4756 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4757 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4758 | WILL_LJMP(lua_error(L)); |
| 4759 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4760 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4764 | { |
| 4765 | struct hlua_txn *htxn; |
| 4766 | |
| 4767 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4768 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4769 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4770 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4771 | WILL_LJMP(lua_error(L)); |
| 4772 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4773 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | /* This function replace full header, or just a value in |
| 4777 | * the request or in the response. It is a wrapper fir the |
| 4778 | * 4 following functions. |
| 4779 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4780 | __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] | 4781 | { |
| 4782 | size_t name_len; |
| 4783 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4784 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4785 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4786 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4787 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4788 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4789 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4790 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4791 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4792 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4793 | 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] | 4794 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4795 | return 0; |
| 4796 | } |
| 4797 | |
| 4798 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4799 | { |
| 4800 | struct hlua_txn *htxn; |
| 4801 | |
| 4802 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4803 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4804 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4805 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4806 | WILL_LJMP(lua_error(L)); |
| 4807 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4808 | 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] | 4809 | } |
| 4810 | |
| 4811 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4812 | { |
| 4813 | struct hlua_txn *htxn; |
| 4814 | |
| 4815 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4816 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4817 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4818 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4819 | WILL_LJMP(lua_error(L)); |
| 4820 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4821 | 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] | 4822 | } |
| 4823 | |
| 4824 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4825 | { |
| 4826 | struct hlua_txn *htxn; |
| 4827 | |
| 4828 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4829 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4830 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4831 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4832 | WILL_LJMP(lua_error(L)); |
| 4833 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4834 | 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] | 4835 | } |
| 4836 | |
| 4837 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4838 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4839 | struct hlua_txn *htxn; |
| 4840 | |
| 4841 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4842 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4843 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4844 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4845 | WILL_LJMP(lua_error(L)); |
| 4846 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4847 | 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] | 4848 | } |
| 4849 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4850 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4851 | * It is a wrapper for the 2 following functions. |
| 4852 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4853 | __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] | 4854 | { |
| 4855 | size_t len; |
| 4856 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4857 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4858 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4859 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4860 | ctx.blk = NULL; |
| 4861 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4862 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4863 | return 0; |
| 4864 | } |
| 4865 | |
| 4866 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4867 | { |
| 4868 | struct hlua_txn *htxn; |
| 4869 | |
| 4870 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4871 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4872 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4873 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4874 | WILL_LJMP(lua_error(L)); |
| 4875 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4876 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4880 | { |
| 4881 | struct hlua_txn *htxn; |
| 4882 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4883 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4884 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4885 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4886 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4887 | WILL_LJMP(lua_error(L)); |
| 4888 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4889 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | /* This function adds an header. It is a wrapper used by |
| 4893 | * the 2 following functions. |
| 4894 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4895 | __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] | 4896 | { |
| 4897 | size_t name_len; |
| 4898 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4899 | size_t value_len; |
| 4900 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4901 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4902 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4903 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4904 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4905 | return 0; |
| 4906 | } |
| 4907 | |
| 4908 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4909 | { |
| 4910 | struct hlua_txn *htxn; |
| 4911 | |
| 4912 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 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 | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4918 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4922 | { |
| 4923 | struct hlua_txn *htxn; |
| 4924 | |
| 4925 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 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 | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4931 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4935 | { |
| 4936 | struct hlua_txn *htxn; |
| 4937 | |
| 4938 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4939 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4940 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4941 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4942 | WILL_LJMP(lua_error(L)); |
| 4943 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4944 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 4945 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4949 | { |
| 4950 | struct hlua_txn *htxn; |
| 4951 | |
| 4952 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4953 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4954 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4955 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4956 | WILL_LJMP(lua_error(L)); |
| 4957 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4958 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 4959 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4960 | } |
| 4961 | |
| 4962 | /* This function set the method. */ |
| 4963 | static int hlua_http_req_set_meth(lua_State *L) |
| 4964 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4965 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4966 | size_t name_len; |
| 4967 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4968 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4969 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4970 | WILL_LJMP(lua_error(L)); |
| 4971 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4972 | 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] | 4973 | return 1; |
| 4974 | } |
| 4975 | |
| 4976 | /* This function set the method. */ |
| 4977 | static int hlua_http_req_set_path(lua_State *L) |
| 4978 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4979 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4980 | size_t name_len; |
| 4981 | 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] | 4982 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4983 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4984 | WILL_LJMP(lua_error(L)); |
| 4985 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4986 | 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] | 4987 | return 1; |
| 4988 | } |
| 4989 | |
| 4990 | /* This function set the query-string. */ |
| 4991 | static int hlua_http_req_set_query(lua_State *L) |
| 4992 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4993 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4994 | size_t name_len; |
| 4995 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4996 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4997 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4998 | WILL_LJMP(lua_error(L)); |
| 4999 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5000 | /* Check length. */ |
| 5001 | if (name_len > trash.size - 1) { |
| 5002 | lua_pushboolean(L, 0); |
| 5003 | return 1; |
| 5004 | } |
| 5005 | |
| 5006 | /* Add the mark question as prefix. */ |
| 5007 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5008 | trash.area[trash.data++] = '?'; |
| 5009 | memcpy(trash.area + trash.data, name, name_len); |
| 5010 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5011 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5012 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5013 | 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] | 5014 | return 1; |
| 5015 | } |
| 5016 | |
| 5017 | /* This function set the uri. */ |
| 5018 | static int hlua_http_req_set_uri(lua_State *L) |
| 5019 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5020 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5021 | size_t name_len; |
| 5022 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5023 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5024 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5025 | WILL_LJMP(lua_error(L)); |
| 5026 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5027 | 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] | 5028 | return 1; |
| 5029 | } |
| 5030 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5031 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5032 | static int hlua_http_res_set_status(lua_State *L) |
| 5033 | { |
| 5034 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5035 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5036 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5037 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5038 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5039 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5040 | WILL_LJMP(lua_error(L)); |
| 5041 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5042 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5043 | return 0; |
| 5044 | } |
| 5045 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5046 | /* |
| 5047 | * |
| 5048 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5049 | * Class TXN |
| 5050 | * |
| 5051 | * |
| 5052 | */ |
| 5053 | |
| 5054 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5055 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5056 | */ |
| 5057 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5058 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5059 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5060 | } |
| 5061 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5062 | __LJMP static int hlua_set_var(lua_State *L) |
| 5063 | { |
| 5064 | struct hlua_txn *htxn; |
| 5065 | const char *name; |
| 5066 | size_t len; |
| 5067 | struct sample smp; |
| 5068 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5069 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5070 | 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] | 5071 | |
| 5072 | /* It is useles to retrieve the stream, but this function |
| 5073 | * runs only in a stream context. |
| 5074 | */ |
| 5075 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5076 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5077 | |
| 5078 | /* Converts the third argument in a sample. */ |
| 5079 | hlua_lua2smp(L, 3, &smp); |
| 5080 | |
| 5081 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5082 | 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] | 5083 | |
| 5084 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5085 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5086 | else |
| 5087 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5088 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5089 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5090 | } |
| 5091 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5092 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5093 | { |
| 5094 | struct hlua_txn *htxn; |
| 5095 | const char *name; |
| 5096 | size_t len; |
| 5097 | struct sample smp; |
| 5098 | |
| 5099 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5100 | |
| 5101 | /* It is useles to retrieve the stream, but this function |
| 5102 | * runs only in a stream context. |
| 5103 | */ |
| 5104 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5105 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5106 | |
| 5107 | /* Unset the variable. */ |
| 5108 | 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] | 5109 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5110 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5111 | } |
| 5112 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5113 | __LJMP static int hlua_get_var(lua_State *L) |
| 5114 | { |
| 5115 | struct hlua_txn *htxn; |
| 5116 | const char *name; |
| 5117 | size_t len; |
| 5118 | struct sample smp; |
| 5119 | |
| 5120 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5121 | |
| 5122 | /* It is useles to retrieve the stream, but this function |
| 5123 | * runs only in a stream context. |
| 5124 | */ |
| 5125 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5126 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5127 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5128 | 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] | 5129 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5130 | lua_pushnil(L); |
| 5131 | return 1; |
| 5132 | } |
| 5133 | |
| 5134 | return hlua_smp2lua(L, &smp); |
| 5135 | } |
| 5136 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5137 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5138 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5139 | struct hlua *hlua; |
| 5140 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5141 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5142 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5143 | /* It is useles to retrieve the stream, but this function |
| 5144 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5145 | */ |
| 5146 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5147 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5148 | |
| 5149 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5150 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5151 | |
| 5152 | /* Get and store new value. */ |
| 5153 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5154 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5155 | |
| 5156 | return 0; |
| 5157 | } |
| 5158 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5159 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5160 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5161 | struct hlua *hlua; |
| 5162 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5163 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5164 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5165 | /* It is useles to retrieve the stream, but this function |
| 5166 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5167 | */ |
| 5168 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5169 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5170 | |
| 5171 | /* Push configuration index in the stack. */ |
| 5172 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5173 | |
| 5174 | return 1; |
| 5175 | } |
| 5176 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5177 | /* Create stack entry containing a class TXN. This function |
| 5178 | * return 0 if the stack does not contains free slots, |
| 5179 | * otherwise it returns 1. |
| 5180 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5181 | 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] | 5182 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5183 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5184 | |
| 5185 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5186 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5187 | return 0; |
| 5188 | |
| 5189 | /* NOTE: The allocation never fails. The failure |
| 5190 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5191 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5192 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5193 | /* Create the object: obj[0] = userdata. */ |
| 5194 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5195 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5196 | lua_rawseti(L, -2, 0); |
| 5197 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5198 | htxn->s = s; |
| 5199 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5200 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5201 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5202 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5203 | /* Create the "f" field that contains a list of fetches. */ |
| 5204 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5205 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5206 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5207 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5208 | |
| 5209 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5210 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5211 | 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] | 5212 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5213 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5214 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5215 | /* Create the "c" field that contains a list of converters. */ |
| 5216 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5217 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5218 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5219 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5220 | |
| 5221 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5222 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5223 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5224 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5225 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5226 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5227 | /* Create the "req" field that contains the request channel object. */ |
| 5228 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5229 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5230 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5231 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5232 | |
| 5233 | /* Create the "res" field that contains the response channel object. */ |
| 5234 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5235 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5236 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5237 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5238 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5239 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5240 | lua_pushstring(L, "http"); |
| 5241 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5242 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5243 | return 0; |
| 5244 | } |
| 5245 | else |
| 5246 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5247 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5248 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5249 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5250 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5251 | lua_setmetatable(L, -2); |
| 5252 | |
| 5253 | return 1; |
| 5254 | } |
| 5255 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5256 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5257 | { |
| 5258 | const char *msg; |
| 5259 | struct hlua_txn *htxn; |
| 5260 | |
| 5261 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5262 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5263 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5264 | |
| 5265 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5266 | return 0; |
| 5267 | } |
| 5268 | |
| 5269 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5270 | { |
| 5271 | int level; |
| 5272 | const char *msg; |
| 5273 | struct hlua_txn *htxn; |
| 5274 | |
| 5275 | MAY_LJMP(check_args(L, 3, "log")); |
| 5276 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5277 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5278 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5279 | |
| 5280 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5281 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5282 | |
| 5283 | hlua_sendlog(htxn->s->be, level, msg); |
| 5284 | return 0; |
| 5285 | } |
| 5286 | |
| 5287 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5288 | { |
| 5289 | const char *msg; |
| 5290 | struct hlua_txn *htxn; |
| 5291 | |
| 5292 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5293 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5294 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5295 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5296 | return 0; |
| 5297 | } |
| 5298 | |
| 5299 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5300 | { |
| 5301 | const char *msg; |
| 5302 | struct hlua_txn *htxn; |
| 5303 | |
| 5304 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5305 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5306 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5307 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5308 | return 0; |
| 5309 | } |
| 5310 | |
| 5311 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5312 | { |
| 5313 | const char *msg; |
| 5314 | struct hlua_txn *htxn; |
| 5315 | |
| 5316 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5317 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5318 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5319 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5320 | return 0; |
| 5321 | } |
| 5322 | |
| 5323 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5324 | { |
| 5325 | const char *msg; |
| 5326 | struct hlua_txn *htxn; |
| 5327 | |
| 5328 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5329 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5330 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5331 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5332 | return 0; |
| 5333 | } |
| 5334 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5335 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5336 | { |
| 5337 | struct hlua_txn *htxn; |
| 5338 | int ll; |
| 5339 | |
| 5340 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5341 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5342 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5343 | |
| 5344 | if (ll < 0 || ll > 7) |
| 5345 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5346 | |
| 5347 | htxn->s->logs.level = ll; |
| 5348 | return 0; |
| 5349 | } |
| 5350 | |
| 5351 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5352 | { |
| 5353 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5354 | int tos; |
| 5355 | |
| 5356 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5357 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5358 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5359 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5360 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5361 | return 0; |
| 5362 | } |
| 5363 | |
| 5364 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5365 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5366 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5367 | int mark; |
| 5368 | |
| 5369 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5370 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5371 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5372 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5373 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5374 | return 0; |
| 5375 | } |
| 5376 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5377 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5378 | { |
| 5379 | struct hlua_txn *htxn; |
| 5380 | |
| 5381 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5382 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5383 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5384 | return 0; |
| 5385 | } |
| 5386 | |
| 5387 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5388 | { |
| 5389 | struct hlua_txn *htxn; |
| 5390 | |
| 5391 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5392 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5393 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5394 | return 0; |
| 5395 | } |
| 5396 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5397 | /* 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] | 5398 | * 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] | 5399 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5400 | * error. The Reply must be on top of the stack. |
| 5401 | */ |
| 5402 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5403 | { |
| 5404 | struct htx *htx; |
| 5405 | struct htx_sl *sl; |
| 5406 | struct h1m h1m; |
| 5407 | const char *status, *reason, *body; |
| 5408 | size_t status_len, reason_len, body_len; |
| 5409 | int ret, code, flags; |
| 5410 | |
| 5411 | code = 200; |
| 5412 | status = "200"; |
| 5413 | status_len = 3; |
| 5414 | ret = lua_getfield(L, -1, "status"); |
| 5415 | if (ret == LUA_TNUMBER) { |
| 5416 | code = lua_tointeger(L, -1); |
| 5417 | status = lua_tolstring(L, -1, &status_len); |
| 5418 | } |
| 5419 | lua_pop(L, 1); |
| 5420 | |
| 5421 | reason = http_get_reason(code); |
| 5422 | reason_len = strlen(reason); |
| 5423 | ret = lua_getfield(L, -1, "reason"); |
| 5424 | if (ret == LUA_TSTRING) |
| 5425 | reason = lua_tolstring(L, -1, &reason_len); |
| 5426 | lua_pop(L, 1); |
| 5427 | |
| 5428 | body = NULL; |
| 5429 | body_len = 0; |
| 5430 | ret = lua_getfield(L, -1, "body"); |
| 5431 | if (ret == LUA_TSTRING) |
| 5432 | body = lua_tolstring(L, -1, &body_len); |
| 5433 | lua_pop(L, 1); |
| 5434 | |
| 5435 | /* Prepare the response before inserting the headers */ |
| 5436 | h1m_init_res(&h1m); |
| 5437 | htx = htx_from_buf(&s->res.buf); |
| 5438 | channel_htx_truncate(&s->res, htx); |
| 5439 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5440 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5441 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5442 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5443 | } |
| 5444 | else { |
| 5445 | flags = HTX_SL_F_IS_RESP; |
| 5446 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5447 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5448 | } |
| 5449 | if (!sl) |
| 5450 | goto fail; |
| 5451 | sl->info.res.status = code; |
| 5452 | |
| 5453 | /* Push in the stack the "headers" entry. */ |
| 5454 | ret = lua_getfield(L, -1, "headers"); |
| 5455 | if (ret != LUA_TTABLE) |
| 5456 | goto skip_headers; |
| 5457 | |
| 5458 | lua_pushnil(L); |
| 5459 | while (lua_next(L, -2) != 0) { |
| 5460 | struct ist name, value; |
| 5461 | const char *n, *v; |
| 5462 | size_t nlen, vlen; |
| 5463 | |
| 5464 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5465 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5466 | goto next_hdr; |
| 5467 | } |
| 5468 | |
| 5469 | n = lua_tolstring(L, -2, &nlen); |
| 5470 | name = ist2(n, nlen); |
| 5471 | if (isteqi(name, ist("content-length"))) { |
| 5472 | /* Always skip content-length header. It will be added |
| 5473 | * later with the correct len |
| 5474 | */ |
| 5475 | goto next_hdr; |
| 5476 | } |
| 5477 | |
| 5478 | /* Loop on header's values */ |
| 5479 | lua_pushnil(L); |
| 5480 | while (lua_next(L, -2)) { |
| 5481 | if (!lua_isstring(L, -1)) { |
| 5482 | /* Skip the value if it is not a string */ |
| 5483 | goto next_value; |
| 5484 | } |
| 5485 | |
| 5486 | v = lua_tolstring(L, -1, &vlen); |
| 5487 | value = ist2(v, vlen); |
| 5488 | |
| 5489 | if (isteqi(name, ist("transfer-encoding"))) |
| 5490 | h1_parse_xfer_enc_header(&h1m, value); |
| 5491 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5492 | goto fail; |
| 5493 | |
| 5494 | next_value: |
| 5495 | lua_pop(L, 1); |
| 5496 | } |
| 5497 | |
| 5498 | next_hdr: |
| 5499 | lua_pop(L, 1); |
| 5500 | } |
| 5501 | skip_headers: |
| 5502 | lua_pop(L, 1); |
| 5503 | |
| 5504 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5505 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5506 | const char *clen = ultoa(body_len); |
| 5507 | |
| 5508 | h1m.flags |= H1_MF_CLEN; |
| 5509 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5510 | goto fail; |
| 5511 | } |
| 5512 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5513 | h1m.flags |= H1_MF_XFER_LEN; |
| 5514 | |
| 5515 | /* Update HTX start-line flags */ |
| 5516 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5517 | flags |= HTX_SL_F_XFER_ENC; |
| 5518 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5519 | flags |= HTX_SL_F_XFER_LEN; |
| 5520 | if (h1m.flags & H1_MF_CHNK) |
| 5521 | flags |= HTX_SL_F_CHNK; |
| 5522 | else if (h1m.flags & H1_MF_CLEN) |
| 5523 | flags |= HTX_SL_F_CLEN; |
| 5524 | if (h1m.body_len == 0) |
| 5525 | flags |= HTX_SL_F_BODYLESS; |
| 5526 | } |
| 5527 | sl->flags |= flags; |
| 5528 | |
| 5529 | |
| 5530 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5531 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5532 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5533 | goto fail; |
| 5534 | |
| 5535 | /* Now, forward the response and terminate the transaction */ |
| 5536 | s->txn->status = code; |
| 5537 | htx_to_buf(htx, &s->res.buf); |
| 5538 | if (!http_forward_proxy_resp(s, 1)) |
| 5539 | goto fail; |
| 5540 | |
| 5541 | return 1; |
| 5542 | |
| 5543 | fail: |
| 5544 | channel_htx_truncate(&s->res, htx); |
| 5545 | return 0; |
| 5546 | } |
| 5547 | |
| 5548 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5549 | * processing is just aborted. Nothing is returned to the client and all |
| 5550 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5551 | * is forwarded to the client before terminating the transaction. On success, |
| 5552 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5553 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5554 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5555 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5556 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5557 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5558 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5559 | struct stream *s; |
| 5560 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5561 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5562 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5563 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5564 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5565 | * just end the execution of the current lua code. */ |
| 5566 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5567 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5568 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5569 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5570 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5571 | struct channel *req = &s->req; |
| 5572 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5573 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5574 | channel_auto_read(req); |
| 5575 | channel_abort(req); |
| 5576 | channel_auto_close(req); |
| 5577 | channel_erase(req); |
| 5578 | |
| 5579 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5580 | channel_auto_read(res); |
| 5581 | channel_auto_close(res); |
| 5582 | channel_shutr_now(res); |
| 5583 | |
| 5584 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5585 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5586 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5587 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5588 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5589 | /* No reply or invalid reply */ |
| 5590 | s->txn->status = 0; |
| 5591 | http_reply_and_close(s, 0, NULL); |
| 5592 | } |
| 5593 | else { |
| 5594 | /* Remove extra args to have the reply on top of the stack */ |
| 5595 | if (lua_gettop(L) > 2) |
| 5596 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5597 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5598 | if (!hlua_txn_forward_reply(L, s)) { |
| 5599 | if (!(s->flags & SF_ERR_MASK)) |
| 5600 | s->flags |= SF_ERR_PRXCOND; |
| 5601 | lua_pushinteger(L, ACT_RET_ERR); |
| 5602 | WILL_LJMP(hlua_done(L)); |
| 5603 | return 0; /* Never reached */ |
| 5604 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5605 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5606 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5607 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5608 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5609 | /* let's log the request time */ |
| 5610 | s->logs.tv_request = now; |
| 5611 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5612 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5613 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5614 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5615 | done: |
| 5616 | if (!(s->flags & SF_ERR_MASK)) |
| 5617 | s->flags |= SF_ERR_LOCAL; |
| 5618 | if (!(s->flags & SF_FINST_MASK)) |
| 5619 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5620 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5621 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5622 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5623 | return 0; |
| 5624 | } |
| 5625 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5626 | /* |
| 5627 | * |
| 5628 | * |
| 5629 | * Class REPLY |
| 5630 | * |
| 5631 | * |
| 5632 | */ |
| 5633 | |
| 5634 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5635 | * free slots, the function fails and returns 0; |
| 5636 | */ |
| 5637 | static int hlua_txn_reply_new(lua_State *L) |
| 5638 | { |
| 5639 | struct hlua_txn *htxn; |
| 5640 | const char *reason, *body = NULL; |
| 5641 | int ret, status; |
| 5642 | |
| 5643 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5644 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5645 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5646 | WILL_LJMP(lua_error(L)); |
| 5647 | } |
| 5648 | |
| 5649 | /* Default value */ |
| 5650 | status = 200; |
| 5651 | reason = http_get_reason(status); |
| 5652 | |
| 5653 | if (lua_istable(L, 2)) { |
| 5654 | /* load status and reason from the table argument at index 2 */ |
| 5655 | ret = lua_getfield(L, 2, "status"); |
| 5656 | if (ret == LUA_TNIL) |
| 5657 | goto reason; |
| 5658 | else if (ret != LUA_TNUMBER) { |
| 5659 | /* invalid status: ignore the reason */ |
| 5660 | goto body; |
| 5661 | } |
| 5662 | status = lua_tointeger(L, -1); |
| 5663 | |
| 5664 | reason: |
| 5665 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5666 | ret = lua_getfield(L, 2, "reason"); |
| 5667 | if (ret == LUA_TSTRING) |
| 5668 | reason = lua_tostring(L, -1); |
| 5669 | |
| 5670 | body: |
| 5671 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5672 | ret = lua_getfield(L, 2, "body"); |
| 5673 | if (ret == LUA_TSTRING) |
| 5674 | body = lua_tostring(L, -1); |
| 5675 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5676 | } |
| 5677 | |
| 5678 | /* Create the Reply table */ |
| 5679 | lua_newtable(L); |
| 5680 | |
| 5681 | /* Add status element */ |
| 5682 | lua_pushstring(L, "status"); |
| 5683 | lua_pushinteger(L, status); |
| 5684 | lua_settable(L, -3); |
| 5685 | |
| 5686 | /* Add reason element */ |
| 5687 | reason = http_get_reason(status); |
| 5688 | lua_pushstring(L, "reason"); |
| 5689 | lua_pushstring(L, reason); |
| 5690 | lua_settable(L, -3); |
| 5691 | |
| 5692 | /* Add body element, nil if undefined */ |
| 5693 | lua_pushstring(L, "body"); |
| 5694 | if (body) |
| 5695 | lua_pushstring(L, body); |
| 5696 | else |
| 5697 | lua_pushnil(L); |
| 5698 | lua_settable(L, -3); |
| 5699 | |
| 5700 | /* Add headers element */ |
| 5701 | lua_pushstring(L, "headers"); |
| 5702 | lua_newtable(L); |
| 5703 | |
| 5704 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5705 | if (lua_istable(L, 2)) { |
| 5706 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5707 | ret = lua_getfield(L, 2, "headers"); |
| 5708 | if (ret == LUA_TTABLE) { |
| 5709 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5710 | lua_pushnil(L); |
| 5711 | while (lua_next(L, -2) != 0) { |
| 5712 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5713 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5714 | /* invalid value type, skip it */ |
| 5715 | lua_pop(L, 1); |
| 5716 | continue; |
| 5717 | } |
| 5718 | |
| 5719 | |
| 5720 | /* Duplicate the key and swap it with the value. */ |
| 5721 | lua_pushvalue(L, -2); |
| 5722 | lua_insert(L, -2); |
| 5723 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5724 | |
| 5725 | lua_newtable(L); |
| 5726 | lua_insert(L, -2); |
| 5727 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5728 | |
| 5729 | if (lua_isstring(L, -1)) { |
| 5730 | /* push the value in the inner table */ |
| 5731 | lua_rawseti(L, -2, 1); |
| 5732 | } |
| 5733 | else { /* table */ |
| 5734 | lua_pushnil(L); |
| 5735 | while (lua_next(L, -2) != 0) { |
| 5736 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5737 | if (!lua_isstring(L, -1)) { |
| 5738 | /* invalid value type, skip it*/ |
| 5739 | lua_pop(L, 1); |
| 5740 | continue; |
| 5741 | } |
| 5742 | /* push the value in the inner table */ |
| 5743 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5744 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5745 | } |
| 5746 | lua_pop(L, 1); |
| 5747 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5748 | } |
| 5749 | |
| 5750 | /* push (k,v) on the stack in the headers table: |
| 5751 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5752 | */ |
| 5753 | lua_settable(L, -5); |
| 5754 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5755 | } |
| 5756 | } |
| 5757 | lua_pop(L, 1); |
| 5758 | } |
| 5759 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5760 | lua_settable(L, -3); |
| 5761 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5762 | |
| 5763 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5764 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5765 | lua_setmetatable(L, -2); |
| 5766 | return 1; |
| 5767 | } |
| 5768 | |
| 5769 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5770 | * provided, the default one corresponding to the status code is used. |
| 5771 | */ |
| 5772 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5773 | { |
| 5774 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5775 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5776 | |
| 5777 | /* First argument (self) must be a table */ |
| 5778 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5779 | |
| 5780 | if (status < 100 || status > 599) { |
| 5781 | lua_pushboolean(L, 0); |
| 5782 | return 1; |
| 5783 | } |
| 5784 | if (!reason) |
| 5785 | reason = http_get_reason(status); |
| 5786 | |
| 5787 | lua_pushinteger(L, status); |
| 5788 | lua_setfield(L, 1, "status"); |
| 5789 | |
| 5790 | lua_pushstring(L, reason); |
| 5791 | lua_setfield(L, 1, "reason"); |
| 5792 | |
| 5793 | lua_pushboolean(L, 1); |
| 5794 | return 1; |
| 5795 | } |
| 5796 | |
| 5797 | /* Add a header into the reply object. Each header name is associated to an |
| 5798 | * array of values in the "headers" table. If the header name is not found, a |
| 5799 | * new entry is created. |
| 5800 | */ |
| 5801 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5802 | { |
| 5803 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5804 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5805 | int ret; |
| 5806 | |
| 5807 | /* First argument (self) must be a table */ |
| 5808 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5809 | |
| 5810 | /* Push in the stack the "headers" entry. */ |
| 5811 | ret = lua_getfield(L, 1, "headers"); |
| 5812 | if (ret != LUA_TTABLE) { |
| 5813 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5814 | WILL_LJMP(lua_error(L)); |
| 5815 | } |
| 5816 | |
| 5817 | /* check if the header is already registered. If not, register it. */ |
| 5818 | ret = lua_getfield(L, -1, name); |
| 5819 | if (ret == LUA_TNIL) { |
| 5820 | /* Entry not found. */ |
| 5821 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5822 | |
| 5823 | /* Insert the new header name in the array in the top of the stack. |
| 5824 | * It left the new array in the top of the stack. |
| 5825 | */ |
| 5826 | lua_newtable(L); |
| 5827 | lua_pushstring(L, name); |
| 5828 | lua_pushvalue(L, -2); |
| 5829 | lua_settable(L, -4); |
| 5830 | } |
| 5831 | else if (ret != LUA_TTABLE) { |
| 5832 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5833 | WILL_LJMP(lua_error(L)); |
| 5834 | } |
| 5835 | |
| 5836 | /* Now the top od thestack is an array of values. We push |
| 5837 | * the header value as new entry. |
| 5838 | */ |
| 5839 | lua_pushstring(L, value); |
| 5840 | ret = lua_rawlen(L, -2); |
| 5841 | lua_rawseti(L, -2, ret + 1); |
| 5842 | |
| 5843 | lua_pushboolean(L, 1); |
| 5844 | return 1; |
| 5845 | } |
| 5846 | |
| 5847 | /* Remove all occurrences of a given header name. */ |
| 5848 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5849 | { |
| 5850 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5851 | int ret; |
| 5852 | |
| 5853 | /* First argument (self) must be a table */ |
| 5854 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5855 | |
| 5856 | /* Push in the stack the "headers" entry. */ |
| 5857 | ret = lua_getfield(L, 1, "headers"); |
| 5858 | if (ret != LUA_TTABLE) { |
| 5859 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 5860 | WILL_LJMP(lua_error(L)); |
| 5861 | } |
| 5862 | |
| 5863 | lua_pushstring(L, name); |
| 5864 | lua_pushnil(L); |
| 5865 | lua_settable(L, -3); |
| 5866 | |
| 5867 | lua_pushboolean(L, 1); |
| 5868 | return 1; |
| 5869 | } |
| 5870 | |
| 5871 | /* Set the reply's body. Overwrite any existing entry. */ |
| 5872 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 5873 | { |
| 5874 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5875 | |
| 5876 | /* First argument (self) must be a table */ |
| 5877 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5878 | |
| 5879 | lua_pushstring(L, payload); |
| 5880 | lua_setfield(L, 1, "body"); |
| 5881 | |
| 5882 | lua_pushboolean(L, 1); |
| 5883 | return 1; |
| 5884 | } |
| 5885 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5886 | __LJMP static int hlua_log(lua_State *L) |
| 5887 | { |
| 5888 | int level; |
| 5889 | const char *msg; |
| 5890 | |
| 5891 | MAY_LJMP(check_args(L, 2, "log")); |
| 5892 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5893 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5894 | |
| 5895 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5896 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5897 | |
| 5898 | hlua_sendlog(NULL, level, msg); |
| 5899 | return 0; |
| 5900 | } |
| 5901 | |
| 5902 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5903 | { |
| 5904 | const char *msg; |
| 5905 | |
| 5906 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5907 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5908 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5909 | return 0; |
| 5910 | } |
| 5911 | |
| 5912 | __LJMP static int hlua_log_info(lua_State *L) |
| 5913 | { |
| 5914 | const char *msg; |
| 5915 | |
| 5916 | MAY_LJMP(check_args(L, 1, "info")); |
| 5917 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5918 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5919 | return 0; |
| 5920 | } |
| 5921 | |
| 5922 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5923 | { |
| 5924 | const char *msg; |
| 5925 | |
| 5926 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5927 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5928 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5929 | return 0; |
| 5930 | } |
| 5931 | |
| 5932 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5933 | { |
| 5934 | const char *msg; |
| 5935 | |
| 5936 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5937 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5938 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5939 | return 0; |
| 5940 | } |
| 5941 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5942 | __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] | 5943 | { |
| 5944 | int wakeup_ms = lua_tointeger(L, -1); |
| 5945 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5946 | 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] | 5947 | return 0; |
| 5948 | } |
| 5949 | |
| 5950 | __LJMP static int hlua_sleep(lua_State *L) |
| 5951 | { |
| 5952 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5953 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5954 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5955 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5956 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5957 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5958 | wakeup_ms = tick_add(now_ms, delay); |
| 5959 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5960 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5961 | 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] | 5962 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5963 | } |
| 5964 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5965 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5966 | { |
| 5967 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5968 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5969 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5970 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5971 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5972 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5973 | wakeup_ms = tick_add(now_ms, delay); |
| 5974 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5975 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5976 | 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] | 5977 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5978 | } |
| 5979 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5980 | /* This functionis an LUA binding. it permits to give back |
| 5981 | * the hand at the HAProxy scheduler. It is used when the |
| 5982 | * LUA processing consumes a lot of time. |
| 5983 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5984 | __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] | 5985 | { |
| 5986 | return 0; |
| 5987 | } |
| 5988 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5989 | __LJMP static int hlua_yield(lua_State *L) |
| 5990 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5991 | 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] | 5992 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5993 | } |
| 5994 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5995 | /* This function change the nice of the currently executed |
| 5996 | * task. It is used set low or high priority at the current |
| 5997 | * task. |
| 5998 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5999 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6000 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6001 | struct hlua *hlua; |
| 6002 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6003 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6004 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6005 | hlua = hlua_gethlua(L); |
| 6006 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6007 | |
| 6008 | /* If he task is not set, I'm in a start mode. */ |
| 6009 | if (!hlua || !hlua->task) |
| 6010 | return 0; |
| 6011 | |
| 6012 | if (nice < -1024) |
| 6013 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6014 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6015 | nice = 1024; |
| 6016 | |
| 6017 | hlua->task->nice = nice; |
| 6018 | return 0; |
| 6019 | } |
| 6020 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6021 | /* 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] | 6022 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6023 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6024 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6025 | * |
| 6026 | * Task wrapper are longjmp safe because the only one Lua code |
| 6027 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6028 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6029 | 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] | 6030 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6031 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6032 | enum hlua_exec status; |
| 6033 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6034 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6035 | task_set_affinity(task, tid_bit); |
| 6036 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6037 | /* If it is the first call to the task, we must initialize the |
| 6038 | * execution timeouts. |
| 6039 | */ |
| 6040 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6041 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6042 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6043 | /* Execute the Lua code. */ |
| 6044 | status = hlua_ctx_resume(hlua, 1); |
| 6045 | |
| 6046 | switch (status) { |
| 6047 | /* finished or yield */ |
| 6048 | case HLUA_E_OK: |
| 6049 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6050 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6051 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6052 | break; |
| 6053 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6054 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6055 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6056 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6057 | break; |
| 6058 | |
| 6059 | /* finished with error. */ |
| 6060 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6061 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6062 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6063 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6064 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6065 | break; |
| 6066 | |
| 6067 | case HLUA_E_ERR: |
| 6068 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6069 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6070 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6071 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6072 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6073 | break; |
| 6074 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6075 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6076 | } |
| 6077 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6078 | /* This function is an LUA binding that register LUA function to be |
| 6079 | * executed after the HAProxy configuration parsing and before the |
| 6080 | * HAProxy scheduler starts. This function expect only one LUA |
| 6081 | * argument that is a function. This function returns nothing, but |
| 6082 | * throws if an error is encountered. |
| 6083 | */ |
| 6084 | __LJMP static int hlua_register_init(lua_State *L) |
| 6085 | { |
| 6086 | struct hlua_init_function *init; |
| 6087 | int ref; |
| 6088 | |
| 6089 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6090 | |
| 6091 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6092 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6093 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6094 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6095 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6096 | |
| 6097 | init->function_ref = ref; |
| 6098 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6099 | return 0; |
| 6100 | } |
| 6101 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6102 | /* This functio is an LUA binding. It permits to register a task |
| 6103 | * executed in parallel of the main HAroxy activity. The task is |
| 6104 | * created and it is set in the HAProxy scheduler. It can be called |
| 6105 | * from the "init" section, "post init" or during the runtime. |
| 6106 | * |
| 6107 | * Lua prototype: |
| 6108 | * |
| 6109 | * <none> core.register_task(<function>) |
| 6110 | */ |
| 6111 | static int hlua_register_task(lua_State *L) |
| 6112 | { |
| 6113 | struct hlua *hlua; |
| 6114 | struct task *task; |
| 6115 | int ref; |
| 6116 | |
| 6117 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6118 | |
| 6119 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6120 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6121 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6122 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6123 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6124 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6125 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6126 | if (!task) |
| 6127 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6128 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6129 | task->context = hlua; |
| 6130 | task->process = hlua_process_task; |
| 6131 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6132 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6133 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6134 | |
| 6135 | /* Restore the function in the stack. */ |
| 6136 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6137 | hlua->nargs = 0; |
| 6138 | |
| 6139 | /* Schedule task. */ |
| 6140 | task_schedule(task, now_ms); |
| 6141 | |
| 6142 | return 0; |
| 6143 | } |
| 6144 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6145 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6146 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6147 | * resume converters. |
| 6148 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6149 | 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] | 6150 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6151 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6152 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6153 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6154 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6155 | if (!stream) |
| 6156 | return 0; |
| 6157 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6158 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6159 | * Lua context can be not initialized. This behavior |
| 6160 | * permits to save performances because a systematic |
| 6161 | * Lua initialization cause 5% performances loss. |
| 6162 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6163 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6164 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6165 | if (!stream->hlua) { |
| 6166 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6167 | return 0; |
| 6168 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6169 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6170 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6171 | return 0; |
| 6172 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6173 | } |
| 6174 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6175 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6176 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6177 | |
| 6178 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6179 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6180 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6181 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6182 | else |
| 6183 | error = "critical error"; |
| 6184 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6185 | return 0; |
| 6186 | } |
| 6187 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6188 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6189 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6190 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6191 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6192 | return 0; |
| 6193 | } |
| 6194 | |
| 6195 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6196 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6197 | |
| 6198 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6199 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6200 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6201 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6202 | return 0; |
| 6203 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6204 | hlua_smp2lua(stream->hlua->T, smp); |
| 6205 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6206 | |
| 6207 | /* push keywords in the stack. */ |
| 6208 | if (arg_p) { |
| 6209 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6210 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6211 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6212 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6213 | return 0; |
| 6214 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6215 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6216 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6217 | } |
| 6218 | } |
| 6219 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6220 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6221 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6222 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6223 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6224 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6225 | } |
| 6226 | |
| 6227 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6228 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6229 | /* finished. */ |
| 6230 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6231 | /* If the stack is empty, the function fails. */ |
| 6232 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6233 | return 0; |
| 6234 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6235 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6236 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6237 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6238 | return 1; |
| 6239 | |
| 6240 | /* yield. */ |
| 6241 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6242 | 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] | 6243 | return 0; |
| 6244 | |
| 6245 | /* finished with error. */ |
| 6246 | case HLUA_E_ERRMSG: |
| 6247 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6248 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6249 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6250 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6251 | return 0; |
| 6252 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6253 | case HLUA_E_ETMOUT: |
| 6254 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6255 | return 0; |
| 6256 | |
| 6257 | case HLUA_E_NOMEM: |
| 6258 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6259 | return 0; |
| 6260 | |
| 6261 | case HLUA_E_YIELD: |
| 6262 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6263 | return 0; |
| 6264 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6265 | case HLUA_E_ERR: |
| 6266 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6267 | 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] | 6268 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6269 | |
| 6270 | default: |
| 6271 | return 0; |
| 6272 | } |
| 6273 | } |
| 6274 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6275 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6276 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6277 | * resume sample-fetches. This function will be called by the sample |
| 6278 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6279 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6280 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6281 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6282 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6283 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6284 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6285 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6286 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6287 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6288 | if (!stream) |
| 6289 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6290 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6291 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6292 | * Lua context can be not initialized. This behavior |
| 6293 | * permits to save performances because a systematic |
| 6294 | * Lua initialization cause 5% performances loss. |
| 6295 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6296 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6297 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6298 | if (!stream->hlua) { |
| 6299 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6300 | return 0; |
| 6301 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6302 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6303 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6304 | return 0; |
| 6305 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6306 | } |
| 6307 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6308 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6309 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6310 | |
| 6311 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6312 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6313 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6314 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6315 | else |
| 6316 | error = "critical error"; |
| 6317 | 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] | 6318 | return 0; |
| 6319 | } |
| 6320 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6321 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6322 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6323 | 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] | 6324 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6325 | return 0; |
| 6326 | } |
| 6327 | |
| 6328 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6329 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6330 | |
| 6331 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6332 | 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] | 6333 | 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] | 6334 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6335 | return 0; |
| 6336 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6337 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6338 | |
| 6339 | /* push keywords in the stack. */ |
| 6340 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6341 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6342 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6343 | 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] | 6344 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6345 | return 0; |
| 6346 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6347 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6348 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6349 | } |
| 6350 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6351 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6352 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6353 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6354 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6355 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6356 | } |
| 6357 | |
| 6358 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6359 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6360 | /* finished. */ |
| 6361 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6362 | /* If the stack is empty, the function fails. */ |
| 6363 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6364 | return 0; |
| 6365 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6366 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6367 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6368 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6369 | |
| 6370 | /* Set the end of execution flag. */ |
| 6371 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6372 | return 1; |
| 6373 | |
| 6374 | /* yield. */ |
| 6375 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6376 | 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] | 6377 | return 0; |
| 6378 | |
| 6379 | /* finished with error. */ |
| 6380 | case HLUA_E_ERRMSG: |
| 6381 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6382 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6383 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6384 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6385 | return 0; |
| 6386 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6387 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6388 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6389 | return 0; |
| 6390 | |
| 6391 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6392 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6393 | return 0; |
| 6394 | |
| 6395 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6396 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6397 | return 0; |
| 6398 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6399 | case HLUA_E_ERR: |
| 6400 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6401 | 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] | 6402 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6403 | |
| 6404 | default: |
| 6405 | return 0; |
| 6406 | } |
| 6407 | } |
| 6408 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6409 | /* This function is an LUA binding used for registering |
| 6410 | * "sample-conv" functions. It expects a converter name used |
| 6411 | * in the haproxy configuration file, and an LUA function. |
| 6412 | */ |
| 6413 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6414 | { |
| 6415 | struct sample_conv_kw_list *sck; |
| 6416 | const char *name; |
| 6417 | int ref; |
| 6418 | int len; |
| 6419 | struct hlua_function *fcn; |
| 6420 | |
| 6421 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6422 | |
| 6423 | /* First argument : converter name. */ |
| 6424 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6425 | |
| 6426 | /* Second argument : lua function. */ |
| 6427 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6428 | |
| 6429 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6430 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6431 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6432 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6433 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6434 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6435 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6436 | |
| 6437 | /* Fill fcn. */ |
| 6438 | fcn->name = strdup(name); |
| 6439 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6440 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6441 | fcn->function_ref = ref; |
| 6442 | |
| 6443 | /* List head */ |
| 6444 | sck->list.n = sck->list.p = NULL; |
| 6445 | |
| 6446 | /* converter keyword. */ |
| 6447 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6448 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6449 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6450 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6451 | |
| 6452 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6453 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6454 | 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] | 6455 | sck->kw[0].val_args = NULL; |
| 6456 | sck->kw[0].in_type = SMP_T_STR; |
| 6457 | sck->kw[0].out_type = SMP_T_STR; |
| 6458 | sck->kw[0].private = fcn; |
| 6459 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6460 | /* Register this new converter */ |
| 6461 | sample_register_convs(sck); |
| 6462 | |
| 6463 | return 0; |
| 6464 | } |
| 6465 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6466 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6467 | * "sample-fetch" functions. It expects a converter name used |
| 6468 | * in the haproxy configuration file, and an LUA function. |
| 6469 | */ |
| 6470 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6471 | { |
| 6472 | const char *name; |
| 6473 | int ref; |
| 6474 | int len; |
| 6475 | struct sample_fetch_kw_list *sfk; |
| 6476 | struct hlua_function *fcn; |
| 6477 | |
| 6478 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6479 | |
| 6480 | /* First argument : sample-fetch name. */ |
| 6481 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6482 | |
| 6483 | /* Second argument : lua function. */ |
| 6484 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6485 | |
| 6486 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6487 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6488 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6489 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6490 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6491 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6492 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6493 | |
| 6494 | /* Fill fcn. */ |
| 6495 | fcn->name = strdup(name); |
| 6496 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6497 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6498 | fcn->function_ref = ref; |
| 6499 | |
| 6500 | /* List head */ |
| 6501 | sfk->list.n = sfk->list.p = NULL; |
| 6502 | |
| 6503 | /* sample-fetch keyword. */ |
| 6504 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6505 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6506 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6507 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6508 | |
| 6509 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6510 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6511 | 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] | 6512 | sfk->kw[0].val_args = NULL; |
| 6513 | sfk->kw[0].out_type = SMP_T_STR; |
| 6514 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6515 | sfk->kw[0].val = 0; |
| 6516 | sfk->kw[0].private = fcn; |
| 6517 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6518 | /* Register this new fetch. */ |
| 6519 | sample_register_fetches(sfk); |
| 6520 | |
| 6521 | return 0; |
| 6522 | } |
| 6523 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6524 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6525 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6526 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6527 | { |
| 6528 | struct hlua *hlua = hlua_gethlua(L); |
| 6529 | unsigned int delay; |
| 6530 | unsigned int wakeup_ms; |
| 6531 | |
| 6532 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6533 | |
| 6534 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6535 | wakeup_ms = tick_add(now_ms, delay); |
| 6536 | hlua->wake_time = wakeup_ms; |
| 6537 | return 0; |
| 6538 | } |
| 6539 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6540 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6541 | * wrapper during the initialisation period. This function may return any |
| 6542 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6543 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6544 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6545 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6546 | 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] | 6547 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6548 | { |
| 6549 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6550 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6551 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6552 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6553 | |
| 6554 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6555 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6556 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6557 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6558 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6559 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6560 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6561 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6562 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6563 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6564 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6565 | * Lua context can be not initialized. This behavior |
| 6566 | * permits to save performances because a systematic |
| 6567 | * Lua initialization cause 5% performances loss. |
| 6568 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6569 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6570 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6571 | if (!s->hlua) { |
| 6572 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6573 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6574 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6575 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6576 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6577 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6578 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6579 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6580 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6581 | } |
| 6582 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6583 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6584 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6585 | |
| 6586 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6587 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6588 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6589 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6590 | else |
| 6591 | error = "critical error"; |
| 6592 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6593 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6594 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6595 | } |
| 6596 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6597 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6598 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6599 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6600 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6601 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6602 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6603 | } |
| 6604 | |
| 6605 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6606 | 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] | 6607 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6608 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6609 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6610 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6611 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6612 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6613 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6614 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6615 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6616 | |
| 6617 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6618 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6619 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6620 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6621 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6622 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6623 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6624 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6625 | lua_pushstring(s->hlua->T, *arg); |
| 6626 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6627 | } |
| 6628 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6629 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6630 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6631 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6632 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6633 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6634 | } |
| 6635 | |
| 6636 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6637 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6638 | /* finished. */ |
| 6639 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6640 | /* Catch the return value */ |
| 6641 | if (lua_gettop(s->hlua->T) > 0) |
| 6642 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6643 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6644 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6645 | if (act_ret == ACT_RET_YIELD) { |
| 6646 | if (flags & ACT_OPT_FINAL) |
| 6647 | goto err_yield; |
| 6648 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6649 | if (dir == SMP_OPT_DIR_REQ) |
| 6650 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6651 | s->hlua->wake_time); |
| 6652 | else |
| 6653 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6654 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6655 | } |
| 6656 | goto end; |
| 6657 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6658 | /* yield. */ |
| 6659 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6660 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6661 | if (dir == SMP_OPT_DIR_REQ) |
| 6662 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6663 | s->hlua->wake_time); |
| 6664 | else |
| 6665 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6666 | s->hlua->wake_time); |
| 6667 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6668 | /* Some actions can be wake up when a "write" event |
| 6669 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6670 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6671 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6672 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6673 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6674 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6675 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6676 | act_ret = ACT_RET_YIELD; |
| 6677 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6678 | |
| 6679 | /* finished with error. */ |
| 6680 | case HLUA_E_ERRMSG: |
| 6681 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6682 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6683 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6684 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6685 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6686 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6687 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6688 | 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] | 6689 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6690 | |
| 6691 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6692 | 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] | 6693 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6694 | |
| 6695 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6696 | err_yield: |
| 6697 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6698 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6699 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6700 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6701 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6702 | case HLUA_E_ERR: |
| 6703 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6704 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6705 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6706 | |
| 6707 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6708 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6709 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6710 | |
| 6711 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6712 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6713 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6714 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6715 | } |
| 6716 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6717 | 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] | 6718 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6719 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6720 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6721 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6722 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6723 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6724 | } |
| 6725 | |
| 6726 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6727 | { |
| 6728 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6729 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6730 | struct task *task; |
| 6731 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6732 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6733 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6734 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6735 | if (!hlua) { |
| 6736 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6737 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6738 | return 0; |
| 6739 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6740 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6741 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6742 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6743 | |
| 6744 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6745 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6746 | if (!task) { |
| 6747 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6748 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6749 | return 0; |
| 6750 | } |
| 6751 | task->nice = 0; |
| 6752 | task->context = ctx; |
| 6753 | task->process = hlua_applet_wakeup; |
| 6754 | ctx->ctx.hlua_apptcp.task = task; |
| 6755 | |
| 6756 | /* In the execution wrappers linked with a stream, the |
| 6757 | * Lua context can be not initialized. This behavior |
| 6758 | * permits to save performances because a systematic |
| 6759 | * Lua initialization cause 5% performances loss. |
| 6760 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6761 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6762 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6763 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6764 | return 0; |
| 6765 | } |
| 6766 | |
| 6767 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6768 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6769 | |
| 6770 | /* The following Lua calls can fail. */ |
| 6771 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6772 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6773 | error = lua_tostring(hlua->T, -1); |
| 6774 | else |
| 6775 | error = "critical error"; |
| 6776 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6777 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6778 | return 0; |
| 6779 | } |
| 6780 | |
| 6781 | /* Check stack available size. */ |
| 6782 | if (!lua_checkstack(hlua->T, 1)) { |
| 6783 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6784 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6785 | RESET_SAFE_LJMP(hlua->T); |
| 6786 | return 0; |
| 6787 | } |
| 6788 | |
| 6789 | /* Restore the function in the stack. */ |
| 6790 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6791 | |
| 6792 | /* Create and and push object stream in the stack. */ |
| 6793 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6794 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6795 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6796 | RESET_SAFE_LJMP(hlua->T); |
| 6797 | return 0; |
| 6798 | } |
| 6799 | hlua->nargs = 1; |
| 6800 | |
| 6801 | /* push keywords in the stack. */ |
| 6802 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6803 | if (!lua_checkstack(hlua->T, 1)) { |
| 6804 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6805 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6806 | RESET_SAFE_LJMP(hlua->T); |
| 6807 | return 0; |
| 6808 | } |
| 6809 | lua_pushstring(hlua->T, *arg); |
| 6810 | hlua->nargs++; |
| 6811 | } |
| 6812 | |
| 6813 | RESET_SAFE_LJMP(hlua->T); |
| 6814 | |
| 6815 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6816 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6817 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6818 | |
| 6819 | return 1; |
| 6820 | } |
| 6821 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6822 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6823 | { |
| 6824 | struct stream_interface *si = ctx->owner; |
| 6825 | struct stream *strm = si_strm(si); |
| 6826 | struct channel *res = si_ic(si); |
| 6827 | struct act_rule *rule = ctx->rule; |
| 6828 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6829 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6830 | |
| 6831 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6832 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6833 | /* eat the whole request */ |
| 6834 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6835 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6836 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6837 | |
| 6838 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6839 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6840 | return; |
| 6841 | |
| 6842 | /* Execute the function. */ |
| 6843 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6844 | /* finished. */ |
| 6845 | case HLUA_E_OK: |
| 6846 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6847 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6848 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6849 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6850 | res->flags |= CF_READ_NULL; |
| 6851 | si_shutr(si); |
| 6852 | return; |
| 6853 | |
| 6854 | /* yield. */ |
| 6855 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6856 | if (hlua->wake_time != TICK_ETERNITY) |
| 6857 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6858 | return; |
| 6859 | |
| 6860 | /* finished with error. */ |
| 6861 | case HLUA_E_ERRMSG: |
| 6862 | /* Display log. */ |
| 6863 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6864 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6865 | lua_pop(hlua->T, 1); |
| 6866 | goto error; |
| 6867 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6868 | case HLUA_E_ETMOUT: |
| 6869 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6870 | rule->arg.hlua_rule->fcn.name); |
| 6871 | goto error; |
| 6872 | |
| 6873 | case HLUA_E_NOMEM: |
| 6874 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6875 | rule->arg.hlua_rule->fcn.name); |
| 6876 | goto error; |
| 6877 | |
| 6878 | case HLUA_E_YIELD: /* unexpected */ |
| 6879 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6880 | rule->arg.hlua_rule->fcn.name); |
| 6881 | goto error; |
| 6882 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6883 | case HLUA_E_ERR: |
| 6884 | /* Display log. */ |
| 6885 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6886 | rule->arg.hlua_rule->fcn.name); |
| 6887 | goto error; |
| 6888 | |
| 6889 | default: |
| 6890 | goto error; |
| 6891 | } |
| 6892 | |
| 6893 | error: |
| 6894 | |
| 6895 | /* For all other cases, just close the stream. */ |
| 6896 | si_shutw(si); |
| 6897 | si_shutr(si); |
| 6898 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6899 | } |
| 6900 | |
| 6901 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6902 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6903 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6904 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6905 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6906 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6907 | } |
| 6908 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6909 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6910 | * an errors occurs and -1 if more data are required for initializing |
| 6911 | * the applet. |
| 6912 | */ |
| 6913 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6914 | { |
| 6915 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6916 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6917 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6918 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6919 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6920 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6921 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6922 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6923 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6924 | if (!hlua) { |
| 6925 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6926 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6927 | return 0; |
| 6928 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6929 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6930 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6931 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6932 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6933 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6934 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6935 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6936 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6937 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6938 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6939 | if (!task) { |
| 6940 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6941 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6942 | return 0; |
| 6943 | } |
| 6944 | task->nice = 0; |
| 6945 | task->context = ctx; |
| 6946 | task->process = hlua_applet_wakeup; |
| 6947 | ctx->ctx.hlua_apphttp.task = task; |
| 6948 | |
| 6949 | /* In the execution wrappers linked with a stream, the |
| 6950 | * Lua context can be not initialized. This behavior |
| 6951 | * permits to save performances because a systematic |
| 6952 | * Lua initialization cause 5% performances loss. |
| 6953 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6954 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6955 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6956 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6957 | return 0; |
| 6958 | } |
| 6959 | |
| 6960 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6961 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6962 | |
| 6963 | /* The following Lua calls can fail. */ |
| 6964 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6965 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6966 | error = lua_tostring(hlua->T, -1); |
| 6967 | else |
| 6968 | error = "critical error"; |
| 6969 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6970 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6971 | return 0; |
| 6972 | } |
| 6973 | |
| 6974 | /* Check stack available size. */ |
| 6975 | if (!lua_checkstack(hlua->T, 1)) { |
| 6976 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6977 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6978 | RESET_SAFE_LJMP(hlua->T); |
| 6979 | return 0; |
| 6980 | } |
| 6981 | |
| 6982 | /* Restore the function in the stack. */ |
| 6983 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6984 | |
| 6985 | /* Create and and push object stream in the stack. */ |
| 6986 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6987 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6988 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6989 | RESET_SAFE_LJMP(hlua->T); |
| 6990 | return 0; |
| 6991 | } |
| 6992 | hlua->nargs = 1; |
| 6993 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6994 | /* push keywords in the stack. */ |
| 6995 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6996 | if (!lua_checkstack(hlua->T, 1)) { |
| 6997 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6998 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6999 | RESET_SAFE_LJMP(hlua->T); |
| 7000 | return 0; |
| 7001 | } |
| 7002 | lua_pushstring(hlua->T, *arg); |
| 7003 | hlua->nargs++; |
| 7004 | } |
| 7005 | |
| 7006 | RESET_SAFE_LJMP(hlua->T); |
| 7007 | |
| 7008 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7009 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7010 | |
| 7011 | return 1; |
| 7012 | } |
| 7013 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7014 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7015 | { |
| 7016 | struct stream_interface *si = ctx->owner; |
| 7017 | struct stream *strm = si_strm(si); |
| 7018 | struct channel *req = si_oc(si); |
| 7019 | struct channel *res = si_ic(si); |
| 7020 | struct act_rule *rule = ctx->rule; |
| 7021 | struct proxy *px = strm->be; |
| 7022 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7023 | struct htx *req_htx, *res_htx; |
| 7024 | |
| 7025 | res_htx = htx_from_buf(&res->buf); |
| 7026 | |
| 7027 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7028 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7029 | goto out; |
| 7030 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7031 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7032 | if (!b_size(&res->buf)) { |
| 7033 | si_rx_room_blk(si); |
| 7034 | goto out; |
| 7035 | } |
| 7036 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7037 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7038 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7039 | |
| 7040 | /* Set the currently running flag. */ |
| 7041 | if (!HLUA_IS_RUNNING(hlua) && |
| 7042 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7043 | struct htx_blk *blk; |
| 7044 | size_t count = co_data(req); |
| 7045 | |
| 7046 | if (!count) { |
| 7047 | si_cant_get(si); |
| 7048 | goto out; |
| 7049 | } |
| 7050 | |
| 7051 | /* We need to flush the request header. This left the body for |
| 7052 | * the Lua. |
| 7053 | */ |
| 7054 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7055 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7056 | while (count && blk) { |
| 7057 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7058 | uint32_t sz = htx_get_blksz(blk); |
| 7059 | |
| 7060 | if (sz > count) { |
| 7061 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7062 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7063 | goto out; |
| 7064 | } |
| 7065 | |
| 7066 | count -= sz; |
| 7067 | co_set_data(req, co_data(req) - sz); |
| 7068 | blk = htx_remove_blk(req_htx, blk); |
| 7069 | |
| 7070 | if (type == HTX_BLK_EOH) |
| 7071 | break; |
| 7072 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7073 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7074 | } |
| 7075 | |
| 7076 | /* Executes The applet if it is not done. */ |
| 7077 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7078 | |
| 7079 | /* Execute the function. */ |
| 7080 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7081 | /* finished. */ |
| 7082 | case HLUA_E_OK: |
| 7083 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7084 | break; |
| 7085 | |
| 7086 | /* yield. */ |
| 7087 | case HLUA_E_AGAIN: |
| 7088 | if (hlua->wake_time != TICK_ETERNITY) |
| 7089 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7090 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7091 | |
| 7092 | /* finished with error. */ |
| 7093 | case HLUA_E_ERRMSG: |
| 7094 | /* Display log. */ |
| 7095 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7096 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7097 | lua_pop(hlua->T, 1); |
| 7098 | goto error; |
| 7099 | |
| 7100 | case HLUA_E_ETMOUT: |
| 7101 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7102 | rule->arg.hlua_rule->fcn.name); |
| 7103 | goto error; |
| 7104 | |
| 7105 | case HLUA_E_NOMEM: |
| 7106 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7107 | rule->arg.hlua_rule->fcn.name); |
| 7108 | goto error; |
| 7109 | |
| 7110 | case HLUA_E_YIELD: /* unexpected */ |
| 7111 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7112 | rule->arg.hlua_rule->fcn.name); |
| 7113 | goto error; |
| 7114 | |
| 7115 | case HLUA_E_ERR: |
| 7116 | /* Display log. */ |
| 7117 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7118 | rule->arg.hlua_rule->fcn.name); |
| 7119 | goto error; |
| 7120 | |
| 7121 | default: |
| 7122 | goto error; |
| 7123 | } |
| 7124 | } |
| 7125 | |
| 7126 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7127 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7128 | goto done; |
| 7129 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7130 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7131 | goto error; |
| 7132 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7133 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7134 | 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] | 7135 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7136 | si_rx_room_blk(si); |
| 7137 | goto out; |
| 7138 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7139 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7140 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7141 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7142 | } |
| 7143 | |
| 7144 | done: |
| 7145 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7146 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7147 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7148 | si_shutr(si); |
| 7149 | } |
| 7150 | |
| 7151 | /* eat the whole request */ |
| 7152 | if (co_data(req)) { |
| 7153 | req_htx = htx_from_buf(&req->buf); |
| 7154 | co_htx_skip(req, req_htx, co_data(req)); |
| 7155 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7156 | } |
| 7157 | } |
| 7158 | |
| 7159 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7160 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7161 | return; |
| 7162 | |
| 7163 | error: |
| 7164 | |
| 7165 | /* If we are in HTTP mode, and we are not send any |
| 7166 | * data, return a 500 server error in best effort: |
| 7167 | * if there is no room available in the buffer, |
| 7168 | * just close the connection. |
| 7169 | */ |
| 7170 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7171 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7172 | |
| 7173 | channel_erase(res); |
| 7174 | res->buf.data = b_data(err); |
| 7175 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7176 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7177 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7178 | } |
| 7179 | if (!(strm->flags & SF_ERR_MASK)) |
| 7180 | strm->flags |= SF_ERR_RESOURCE; |
| 7181 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7182 | goto done; |
| 7183 | } |
| 7184 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7185 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7186 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7187 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7188 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7189 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7190 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7191 | } |
| 7192 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7193 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7194 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7195 | * |
| 7196 | * This function can fail with an abort() due to an Lua critical error. |
| 7197 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7198 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7199 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7200 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7201 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7202 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7203 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7204 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7205 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7206 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7207 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7208 | if (!rule->arg.hlua_rule) { |
| 7209 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7210 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7211 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7212 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7213 | /* Memory for arguments. */ |
| 7214 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7215 | if (!rule->arg.hlua_rule->args) { |
| 7216 | memprintf(err, "out of memory error"); |
| 7217 | return ACT_RET_PRS_ERR; |
| 7218 | } |
| 7219 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7220 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7221 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7222 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7223 | /* Expect some arguments */ |
| 7224 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7225 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7226 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7227 | return ACT_RET_PRS_ERR; |
| 7228 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7229 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7230 | if (!rule->arg.hlua_rule->args[i]) { |
| 7231 | memprintf(err, "out of memory error"); |
| 7232 | return ACT_RET_PRS_ERR; |
| 7233 | } |
| 7234 | (*cur_arg)++; |
| 7235 | } |
| 7236 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7237 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7238 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7239 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7240 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7241 | } |
| 7242 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7243 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7244 | struct act_rule *rule, char **err) |
| 7245 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7246 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7247 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7248 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7249 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7250 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7251 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7252 | * the call of this analyzer. |
| 7253 | */ |
| 7254 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7255 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7256 | return ACT_RET_PRS_ERR; |
| 7257 | } |
| 7258 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7259 | /* Memory for the rule. */ |
| 7260 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7261 | if (!rule->arg.hlua_rule) { |
| 7262 | memprintf(err, "out of memory error"); |
| 7263 | return ACT_RET_PRS_ERR; |
| 7264 | } |
| 7265 | |
| 7266 | /* Reference the Lua function and store the reference. */ |
| 7267 | rule->arg.hlua_rule->fcn = *fcn; |
| 7268 | |
| 7269 | /* TODO: later accept arguments. */ |
| 7270 | rule->arg.hlua_rule->args = NULL; |
| 7271 | |
| 7272 | /* Add applet pointer in the rule. */ |
| 7273 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7274 | rule->applet.name = fcn->name; |
| 7275 | rule->applet.init = hlua_applet_http_init; |
| 7276 | rule->applet.fct = hlua_applet_http_fct; |
| 7277 | rule->applet.release = hlua_applet_http_release; |
| 7278 | rule->applet.timeout = hlua_timeout_applet; |
| 7279 | |
| 7280 | return ACT_RET_PRS_OK; |
| 7281 | } |
| 7282 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7283 | /* This function is an LUA binding used for registering |
| 7284 | * "sample-conv" functions. It expects a converter name used |
| 7285 | * in the haproxy configuration file, and an LUA function. |
| 7286 | */ |
| 7287 | __LJMP static int hlua_register_action(lua_State *L) |
| 7288 | { |
| 7289 | struct action_kw_list *akl; |
| 7290 | const char *name; |
| 7291 | int ref; |
| 7292 | int len; |
| 7293 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7294 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7295 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7296 | /* Initialise the number of expected arguments at 0. */ |
| 7297 | nargs = 0; |
| 7298 | |
| 7299 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7300 | 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] | 7301 | |
| 7302 | /* First argument : converter name. */ |
| 7303 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7304 | |
| 7305 | /* Second argument : environment. */ |
| 7306 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7307 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7308 | |
| 7309 | /* Third argument : lua function. */ |
| 7310 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7311 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7312 | /* 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] | 7313 | if (lua_gettop(L) >= 4) |
| 7314 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7315 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7316 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7317 | lua_pushnil(L); |
| 7318 | while (lua_next(L, 2) != 0) { |
| 7319 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7320 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7321 | |
| 7322 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7323 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7324 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7325 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7326 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7327 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7328 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7329 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7330 | |
| 7331 | /* Fill fcn. */ |
| 7332 | fcn->name = strdup(name); |
| 7333 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7334 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7335 | fcn->function_ref = ref; |
| 7336 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7337 | /* Set the expected number od arguments. */ |
| 7338 | fcn->nargs = nargs; |
| 7339 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7340 | /* List head */ |
| 7341 | akl->list.n = akl->list.p = NULL; |
| 7342 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7343 | /* action keyword. */ |
| 7344 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7345 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7346 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7347 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7348 | |
| 7349 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7350 | |
| 7351 | akl->kw[0].match_pfx = 0; |
| 7352 | akl->kw[0].private = fcn; |
| 7353 | akl->kw[0].parse = action_register_lua; |
| 7354 | |
| 7355 | /* select the action registering point. */ |
| 7356 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7357 | tcp_req_cont_keywords_register(akl); |
| 7358 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7359 | tcp_res_cont_keywords_register(akl); |
| 7360 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7361 | http_req_keywords_register(akl); |
| 7362 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7363 | http_res_keywords_register(akl); |
| 7364 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7365 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7366 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7367 | "are expected.", lua_tostring(L, -1))); |
| 7368 | |
| 7369 | /* pop the environment string. */ |
| 7370 | lua_pop(L, 1); |
| 7371 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7372 | return ACT_RET_PRS_OK; |
| 7373 | } |
| 7374 | |
| 7375 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7376 | struct act_rule *rule, char **err) |
| 7377 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7378 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7379 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7380 | if (px->mode == PR_MODE_HTTP) { |
| 7381 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7382 | return ACT_RET_PRS_ERR; |
| 7383 | } |
| 7384 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7385 | /* Memory for the rule. */ |
| 7386 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7387 | if (!rule->arg.hlua_rule) { |
| 7388 | memprintf(err, "out of memory error"); |
| 7389 | return ACT_RET_PRS_ERR; |
| 7390 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7391 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7392 | /* Reference the Lua function and store the reference. */ |
| 7393 | rule->arg.hlua_rule->fcn = *fcn; |
| 7394 | |
| 7395 | /* TODO: later accept arguments. */ |
| 7396 | rule->arg.hlua_rule->args = NULL; |
| 7397 | |
| 7398 | /* Add applet pointer in the rule. */ |
| 7399 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7400 | rule->applet.name = fcn->name; |
| 7401 | rule->applet.init = hlua_applet_tcp_init; |
| 7402 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7403 | rule->applet.release = hlua_applet_tcp_release; |
| 7404 | rule->applet.timeout = hlua_timeout_applet; |
| 7405 | |
| 7406 | return 0; |
| 7407 | } |
| 7408 | |
| 7409 | /* This function is an LUA binding used for registering |
| 7410 | * "sample-conv" functions. It expects a converter name used |
| 7411 | * in the haproxy configuration file, and an LUA function. |
| 7412 | */ |
| 7413 | __LJMP static int hlua_register_service(lua_State *L) |
| 7414 | { |
| 7415 | struct action_kw_list *akl; |
| 7416 | const char *name; |
| 7417 | const char *env; |
| 7418 | int ref; |
| 7419 | int len; |
| 7420 | struct hlua_function *fcn; |
| 7421 | |
| 7422 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7423 | |
| 7424 | /* First argument : converter name. */ |
| 7425 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7426 | |
| 7427 | /* Second argument : environment. */ |
| 7428 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7429 | |
| 7430 | /* Third argument : lua function. */ |
| 7431 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7432 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7433 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7434 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7435 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7436 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7437 | fcn = calloc(1, sizeof(*fcn)); |
| 7438 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7439 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7440 | |
| 7441 | /* Fill fcn. */ |
| 7442 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7443 | fcn->name = calloc(1, len); |
| 7444 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7445 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7446 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7447 | fcn->function_ref = ref; |
| 7448 | |
| 7449 | /* List head */ |
| 7450 | akl->list.n = akl->list.p = NULL; |
| 7451 | |
| 7452 | /* converter keyword. */ |
| 7453 | len = strlen("lua.") + strlen(name) + 1; |
| 7454 | akl->kw[0].kw = calloc(1, len); |
| 7455 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7456 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7457 | |
| 7458 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7459 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7460 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7461 | if (strcmp(env, "tcp") == 0) |
| 7462 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7463 | else if (strcmp(env, "http") == 0) |
| 7464 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7465 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7466 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7467 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7468 | |
| 7469 | akl->kw[0].match_pfx = 0; |
| 7470 | akl->kw[0].private = fcn; |
| 7471 | |
| 7472 | /* End of array. */ |
| 7473 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7474 | |
| 7475 | /* Register this new converter */ |
| 7476 | service_keywords_register(akl); |
| 7477 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7478 | return 0; |
| 7479 | } |
| 7480 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7481 | /* This function initialises Lua cli handler. It copies the |
| 7482 | * arguments in the Lua stack and create channel IO objects. |
| 7483 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7484 | 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] | 7485 | { |
| 7486 | struct hlua *hlua; |
| 7487 | struct hlua_function *fcn; |
| 7488 | int i; |
| 7489 | const char *error; |
| 7490 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7491 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7492 | appctx->ctx.hlua_cli.fcn = private; |
| 7493 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7494 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7495 | if (!hlua) { |
| 7496 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7497 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7498 | } |
| 7499 | HLUA_INIT(hlua); |
| 7500 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7501 | |
| 7502 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7503 | * 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] | 7504 | * applet_http. It is absolutely compatible. |
| 7505 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7506 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7507 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7508 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7509 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7510 | } |
| 7511 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7512 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7513 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7514 | |
| 7515 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7516 | 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] | 7517 | 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] | 7518 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7519 | } |
| 7520 | |
| 7521 | /* The following Lua calls can fail. */ |
| 7522 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7523 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7524 | error = lua_tostring(hlua->T, -1); |
| 7525 | else |
| 7526 | error = "critical error"; |
| 7527 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7528 | goto error; |
| 7529 | } |
| 7530 | |
| 7531 | /* Check stack available size. */ |
| 7532 | if (!lua_checkstack(hlua->T, 2)) { |
| 7533 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7534 | goto error; |
| 7535 | } |
| 7536 | |
| 7537 | /* Restore the function in the stack. */ |
| 7538 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7539 | |
| 7540 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7541 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7542 | */ |
| 7543 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7544 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7545 | goto error; |
| 7546 | } |
| 7547 | hlua->nargs = 1; |
| 7548 | |
| 7549 | /* push keywords in the stack. */ |
| 7550 | for (i = 0; *args[i]; i++) { |
| 7551 | /* Check stack available size. */ |
| 7552 | if (!lua_checkstack(hlua->T, 1)) { |
| 7553 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7554 | goto error; |
| 7555 | } |
| 7556 | lua_pushstring(hlua->T, args[i]); |
| 7557 | hlua->nargs++; |
| 7558 | } |
| 7559 | |
| 7560 | /* We must initialize the execution timeouts. */ |
| 7561 | hlua->max_time = hlua_timeout_session; |
| 7562 | |
| 7563 | /* At this point the execution is safe. */ |
| 7564 | RESET_SAFE_LJMP(hlua->T); |
| 7565 | |
| 7566 | /* It's ok */ |
| 7567 | return 0; |
| 7568 | |
| 7569 | /* It's not ok. */ |
| 7570 | error: |
| 7571 | RESET_SAFE_LJMP(hlua->T); |
| 7572 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7573 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7574 | return 1; |
| 7575 | } |
| 7576 | |
| 7577 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7578 | { |
| 7579 | struct hlua *hlua; |
| 7580 | struct stream_interface *si; |
| 7581 | struct hlua_function *fcn; |
| 7582 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7583 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7584 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7585 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7586 | |
| 7587 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7588 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7589 | return 1; |
| 7590 | |
| 7591 | /* Execute the function. */ |
| 7592 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7593 | |
| 7594 | /* finished. */ |
| 7595 | case HLUA_E_OK: |
| 7596 | return 1; |
| 7597 | |
| 7598 | /* yield. */ |
| 7599 | case HLUA_E_AGAIN: |
| 7600 | /* We want write. */ |
| 7601 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7602 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7603 | /* Set the timeout. */ |
| 7604 | if (hlua->wake_time != TICK_ETERNITY) |
| 7605 | task_schedule(hlua->task, hlua->wake_time); |
| 7606 | return 0; |
| 7607 | |
| 7608 | /* finished with error. */ |
| 7609 | case HLUA_E_ERRMSG: |
| 7610 | /* Display log. */ |
| 7611 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7612 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7613 | lua_pop(hlua->T, 1); |
| 7614 | return 1; |
| 7615 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7616 | case HLUA_E_ETMOUT: |
| 7617 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7618 | fcn->name); |
| 7619 | return 1; |
| 7620 | |
| 7621 | case HLUA_E_NOMEM: |
| 7622 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7623 | fcn->name); |
| 7624 | return 1; |
| 7625 | |
| 7626 | case HLUA_E_YIELD: /* unexpected */ |
| 7627 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7628 | fcn->name); |
| 7629 | return 1; |
| 7630 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7631 | case HLUA_E_ERR: |
| 7632 | /* Display log. */ |
| 7633 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7634 | fcn->name); |
| 7635 | return 1; |
| 7636 | |
| 7637 | default: |
| 7638 | return 1; |
| 7639 | } |
| 7640 | |
| 7641 | return 1; |
| 7642 | } |
| 7643 | |
| 7644 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7645 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7646 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7647 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7648 | } |
| 7649 | |
| 7650 | /* This function is an LUA binding used for registering |
| 7651 | * new keywords in the cli. It expects a list of keywords |
| 7652 | * which are the "path". It is limited to 5 keywords. A |
| 7653 | * description of the command, a function to be executed |
| 7654 | * for the parsing and a function for io handlers. |
| 7655 | */ |
| 7656 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7657 | { |
| 7658 | struct cli_kw_list *cli_kws; |
| 7659 | const char *message; |
| 7660 | int ref_io; |
| 7661 | int len; |
| 7662 | struct hlua_function *fcn; |
| 7663 | int index; |
| 7664 | int i; |
| 7665 | |
| 7666 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7667 | |
| 7668 | /* First argument : an array of maximum 5 keywords. */ |
| 7669 | if (!lua_istable(L, 1)) |
| 7670 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7671 | |
| 7672 | /* Second argument : string with contextual message. */ |
| 7673 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7674 | |
| 7675 | /* Third and fourth argument : lua function. */ |
| 7676 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7677 | |
| 7678 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7679 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7680 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7681 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7682 | fcn = calloc(1, sizeof(*fcn)); |
| 7683 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7684 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7685 | |
| 7686 | /* Fill path. */ |
| 7687 | index = 0; |
| 7688 | lua_pushnil(L); |
| 7689 | while(lua_next(L, 1) != 0) { |
| 7690 | if (index >= 5) |
| 7691 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7692 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7693 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7694 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7695 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7696 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7697 | index++; |
| 7698 | lua_pop(L, 1); |
| 7699 | } |
| 7700 | |
| 7701 | /* Copy help message. */ |
| 7702 | cli_kws->kw[0].usage = strdup(message); |
| 7703 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7704 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7705 | |
| 7706 | /* Fill fcn io handler. */ |
| 7707 | len = strlen("<lua.cli>") + 1; |
| 7708 | for (i = 0; i < index; i++) |
| 7709 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7710 | fcn->name = calloc(1, len); |
| 7711 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7712 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7713 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7714 | for (i = 0; i < index; i++) { |
| 7715 | strncat((char *)fcn->name, ".", len); |
| 7716 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7717 | } |
| 7718 | strncat((char *)fcn->name, ">", len); |
| 7719 | fcn->function_ref = ref_io; |
| 7720 | |
| 7721 | /* Fill last entries. */ |
| 7722 | cli_kws->kw[0].private = fcn; |
| 7723 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7724 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7725 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7726 | |
| 7727 | /* Register this new converter */ |
| 7728 | cli_register_kw(cli_kws); |
| 7729 | |
| 7730 | return 0; |
| 7731 | } |
| 7732 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7733 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7734 | struct proxy *defpx, const char *file, int line, |
| 7735 | char **err, unsigned int *timeout) |
| 7736 | { |
| 7737 | const char *error; |
| 7738 | |
| 7739 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7740 | if (error == PARSE_TIME_OVER) { |
| 7741 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7742 | args[1], args[0]); |
| 7743 | return -1; |
| 7744 | } |
| 7745 | else if (error == PARSE_TIME_UNDER) { |
| 7746 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7747 | args[1], args[0]); |
| 7748 | return -1; |
| 7749 | } |
| 7750 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7751 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7752 | return -1; |
| 7753 | } |
| 7754 | return 0; |
| 7755 | } |
| 7756 | |
| 7757 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7758 | struct proxy *defpx, const char *file, int line, |
| 7759 | char **err) |
| 7760 | { |
| 7761 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7762 | file, line, err, &hlua_timeout_session); |
| 7763 | } |
| 7764 | |
| 7765 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7766 | struct proxy *defpx, const char *file, int line, |
| 7767 | char **err) |
| 7768 | { |
| 7769 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7770 | file, line, err, &hlua_timeout_task); |
| 7771 | } |
| 7772 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7773 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7774 | struct proxy *defpx, const char *file, int line, |
| 7775 | char **err) |
| 7776 | { |
| 7777 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7778 | file, line, err, &hlua_timeout_applet); |
| 7779 | } |
| 7780 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7781 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7782 | struct proxy *defpx, const char *file, int line, |
| 7783 | char **err) |
| 7784 | { |
| 7785 | char *error; |
| 7786 | |
| 7787 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7788 | if (*error != '\0') { |
| 7789 | memprintf(err, "%s: invalid number", args[0]); |
| 7790 | return -1; |
| 7791 | } |
| 7792 | return 0; |
| 7793 | } |
| 7794 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7795 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7796 | struct proxy *defpx, const char *file, int line, |
| 7797 | char **err) |
| 7798 | { |
| 7799 | char *error; |
| 7800 | |
| 7801 | if (*(args[1]) == 0) { |
| 7802 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7803 | return -1; |
| 7804 | } |
| 7805 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7806 | if (*error != '\0') { |
| 7807 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7808 | return -1; |
| 7809 | } |
| 7810 | return 0; |
| 7811 | } |
| 7812 | |
| 7813 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7814 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7815 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7816 | * the main lua entry point. |
| 7817 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7818 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7819 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7820 | * |
| 7821 | * In some error case, LUA set an error message in top of the stack. This function |
| 7822 | * 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] | 7823 | * |
| 7824 | * This function can fail with an abort() due to an Lua critical error. |
| 7825 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7826 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7827 | */ |
| 7828 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7829 | struct proxy *defpx, const char *file, int line, |
| 7830 | char **err) |
| 7831 | { |
| 7832 | int error; |
| 7833 | |
| 7834 | /* Just load and compile the file. */ |
| 7835 | error = luaL_loadfile(gL.T, args[1]); |
| 7836 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7837 | 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] | 7838 | lua_pop(gL.T, 1); |
| 7839 | return -1; |
| 7840 | } |
| 7841 | |
| 7842 | /* If no syntax error where detected, execute the code. */ |
| 7843 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7844 | switch (error) { |
| 7845 | case LUA_OK: |
| 7846 | break; |
| 7847 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7848 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7849 | lua_pop(gL.T, 1); |
| 7850 | return -1; |
| 7851 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7852 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7853 | return -1; |
| 7854 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7855 | 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] | 7856 | lua_pop(gL.T, 1); |
| 7857 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 7858 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7859 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7860 | 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] | 7861 | lua_pop(gL.T, 1); |
| 7862 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 7863 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7864 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7865 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7866 | lua_pop(gL.T, 1); |
| 7867 | return -1; |
| 7868 | } |
| 7869 | |
| 7870 | return 0; |
| 7871 | } |
| 7872 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7873 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7874 | * in the given <ctx>. |
| 7875 | */ |
| 7876 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7877 | { |
| 7878 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7879 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7880 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7881 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7882 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7883 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7884 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7885 | |
| 7886 | return 0; |
| 7887 | } |
| 7888 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7889 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7890 | struct proxy *defpx, const char *file, int line, |
| 7891 | char **err) |
| 7892 | { |
| 7893 | char *path; |
| 7894 | char *type = "path"; |
| 7895 | if (too_many_args(2, args, err, NULL)) { |
| 7896 | return -1; |
| 7897 | } |
| 7898 | |
| 7899 | if (!(*args[1])) { |
| 7900 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7901 | return -1; |
| 7902 | } |
| 7903 | path = args[1]; |
| 7904 | |
| 7905 | if (*args[2]) { |
| 7906 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7907 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7908 | return -1; |
| 7909 | } |
| 7910 | type = args[2]; |
| 7911 | } |
| 7912 | |
| 7913 | return hlua_prepend_path(gL, type, path); |
| 7914 | } |
| 7915 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7916 | /* configuration keywords declaration */ |
| 7917 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7918 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7919 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7920 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7921 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7922 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7923 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7924 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7925 | { 0, NULL, NULL }, |
| 7926 | }}; |
| 7927 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7928 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7929 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7930 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7931 | /* This function can fail with an abort() due to an Lua critical error. |
| 7932 | * We are in the initialisation process of HAProxy, this abort() is |
| 7933 | * tolerated. |
| 7934 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7935 | int hlua_post_init() |
| 7936 | { |
| 7937 | struct hlua_init_function *init; |
| 7938 | const char *msg; |
| 7939 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7940 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7941 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7942 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7943 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7944 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7945 | error = lua_tostring(gL.T, -1); |
| 7946 | else |
| 7947 | error = "critical error"; |
| 7948 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7949 | exit(1); |
| 7950 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7951 | |
| 7952 | #if USE_OPENSSL |
| 7953 | /* Initialize SSL server. */ |
| 7954 | if (socket_ssl.xprt->prepare_srv) { |
| 7955 | int saved_used_backed = global.ssl_used_backend; |
| 7956 | // don't affect maxconn automatic computation |
| 7957 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7958 | global.ssl_used_backend = saved_used_backed; |
| 7959 | } |
| 7960 | #endif |
| 7961 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7962 | hlua_fcn_post_init(gL.T); |
| 7963 | RESET_SAFE_LJMP(gL.T); |
| 7964 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7965 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7966 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7967 | ret = hlua_ctx_resume(&gL, 0); |
| 7968 | switch (ret) { |
| 7969 | case HLUA_E_OK: |
| 7970 | lua_pop(gL.T, -1); |
| 7971 | return 1; |
| 7972 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7973 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7974 | return 0; |
| 7975 | case HLUA_E_ERRMSG: |
| 7976 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7977 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7978 | return 0; |
| 7979 | case HLUA_E_ERR: |
| 7980 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7981 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7982 | return 0; |
| 7983 | } |
| 7984 | } |
| 7985 | return 1; |
| 7986 | } |
| 7987 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7988 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7989 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7990 | * is the previously allocated size or the kind of object in case of a new |
| 7991 | * allocation. <nsize> is the requested new size. |
| 7992 | */ |
| 7993 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7994 | { |
| 7995 | struct hlua_mem_allocator *zone = ud; |
| 7996 | |
| 7997 | if (nsize == 0) { |
| 7998 | /* it's a free */ |
| 7999 | if (ptr) |
| 8000 | zone->allocated -= osize; |
| 8001 | free(ptr); |
| 8002 | return NULL; |
| 8003 | } |
| 8004 | |
| 8005 | if (!ptr) { |
| 8006 | /* it's a new allocation */ |
| 8007 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8008 | return NULL; |
| 8009 | |
| 8010 | ptr = malloc(nsize); |
| 8011 | if (ptr) |
| 8012 | zone->allocated += nsize; |
| 8013 | return ptr; |
| 8014 | } |
| 8015 | |
| 8016 | /* it's a realloc */ |
| 8017 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8018 | return NULL; |
| 8019 | |
| 8020 | ptr = realloc(ptr, nsize); |
| 8021 | if (ptr) |
| 8022 | zone->allocated += nsize - osize; |
| 8023 | return ptr; |
| 8024 | } |
| 8025 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8026 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8027 | * We are in the initialisation process of HAProxy, this abort() is |
| 8028 | * tolerated. |
| 8029 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8030 | void hlua_init(void) |
| 8031 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8032 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8033 | int idx; |
| 8034 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8035 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8036 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8037 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8038 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8039 | struct srv_kw *kw; |
| 8040 | int tmp_error; |
| 8041 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8042 | char *args[] = { /* SSL client configuration. */ |
| 8043 | "ssl", |
| 8044 | "verify", |
| 8045 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8046 | NULL |
| 8047 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8048 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8049 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8050 | /* Init main lua stack. */ |
| 8051 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8052 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8053 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8054 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8055 | hlua_sethlua(&gL); |
| 8056 | gL.Tref = LUA_REFNIL; |
| 8057 | gL.task = NULL; |
| 8058 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8059 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8060 | * the Lua function can fail with an abort. We are in the initialisation |
| 8061 | * process of HAProxy, this abort() is tolerated. |
| 8062 | */ |
| 8063 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8064 | /* Initialise lua. */ |
| 8065 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8066 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8067 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8068 | #ifdef HLUA_PREPEND_PATH |
| 8069 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8070 | #endif |
| 8071 | #ifdef HLUA_PREPEND_CPATH |
| 8072 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8073 | #endif |
| 8074 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8075 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8076 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8077 | /* Set safe environment for the initialisation. */ |
| 8078 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8079 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8080 | error_msg = lua_tostring(gL.T, -1); |
| 8081 | else |
| 8082 | error_msg = "critical error"; |
| 8083 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8084 | exit(1); |
| 8085 | } |
| 8086 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8087 | /* |
| 8088 | * |
| 8089 | * Create "core" object. |
| 8090 | * |
| 8091 | */ |
| 8092 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8093 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8094 | lua_newtable(gL.T); |
| 8095 | |
| 8096 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8097 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8098 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8099 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8100 | /* Register special functions. */ |
| 8101 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8102 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8103 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8104 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8105 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8106 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8107 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8108 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8109 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8110 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8111 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8112 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8113 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8114 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8115 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8116 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8117 | hlua_class_function(gL.T, "log", hlua_log); |
| 8118 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8119 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8120 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8121 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8122 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8123 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8124 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8125 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8126 | |
| 8127 | /* |
| 8128 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8129 | * Create "act" object. |
| 8130 | * |
| 8131 | */ |
| 8132 | |
| 8133 | /* This table entry is the object "act" base. */ |
| 8134 | lua_newtable(gL.T); |
| 8135 | |
| 8136 | /* push action return constants */ |
| 8137 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8138 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8139 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8140 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8141 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8142 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8143 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8144 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8145 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8146 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8147 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8148 | lua_setglobal(gL.T, "act"); |
| 8149 | |
| 8150 | /* |
| 8151 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8152 | * Register class Map |
| 8153 | * |
| 8154 | */ |
| 8155 | |
| 8156 | /* This table entry is the object "Map" base. */ |
| 8157 | lua_newtable(gL.T); |
| 8158 | |
| 8159 | /* register pattern types. */ |
| 8160 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8161 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8162 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8163 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8164 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8165 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8166 | |
| 8167 | /* register constructor. */ |
| 8168 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8169 | |
| 8170 | /* Create and fill the metatable. */ |
| 8171 | lua_newtable(gL.T); |
| 8172 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8173 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8174 | lua_pushstring(gL.T, "__index"); |
| 8175 | lua_newtable(gL.T); |
| 8176 | |
| 8177 | /* Register . */ |
| 8178 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8179 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8180 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8181 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8182 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8183 | /* Register previous table in the registry with reference and named entry. |
| 8184 | * The function hlua_register_metatable() pops the stack, so we |
| 8185 | * previously create a copy of the table. |
| 8186 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8187 | 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] | 8188 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8189 | |
| 8190 | /* Assign the metatable to the mai Map object. */ |
| 8191 | lua_setmetatable(gL.T, -2); |
| 8192 | |
| 8193 | /* Set a name to the table. */ |
| 8194 | lua_setglobal(gL.T, "Map"); |
| 8195 | |
| 8196 | /* |
| 8197 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8198 | * Register class Channel |
| 8199 | * |
| 8200 | */ |
| 8201 | |
| 8202 | /* Create and fill the metatable. */ |
| 8203 | lua_newtable(gL.T); |
| 8204 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8205 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8206 | lua_pushstring(gL.T, "__index"); |
| 8207 | lua_newtable(gL.T); |
| 8208 | |
| 8209 | /* Register . */ |
| 8210 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8211 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8212 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8213 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8214 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8215 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8216 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8217 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8218 | 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] | 8219 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8220 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8221 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8222 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8223 | |
| 8224 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8225 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8226 | |
| 8227 | /* |
| 8228 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8229 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8230 | * |
| 8231 | */ |
| 8232 | |
| 8233 | /* Create and fill the metatable. */ |
| 8234 | lua_newtable(gL.T); |
| 8235 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8236 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8237 | lua_pushstring(gL.T, "__index"); |
| 8238 | lua_newtable(gL.T); |
| 8239 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8240 | /* Browse existing fetches and create the associated |
| 8241 | * object method. |
| 8242 | */ |
| 8243 | sf = NULL; |
| 8244 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8245 | |
| 8246 | /* Dont register the keywork if the arguments check function are |
| 8247 | * not safe during the runtime. |
| 8248 | */ |
| 8249 | if ((sf->val_args != NULL) && |
| 8250 | (sf->val_args != val_payload_lv) && |
| 8251 | (sf->val_args != val_hdr)) |
| 8252 | continue; |
| 8253 | |
| 8254 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8255 | * by an underscore. |
| 8256 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8257 | strncpy(trash.area, sf->kw, trash.size); |
| 8258 | trash.area[trash.size - 1] = '\0'; |
| 8259 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8260 | if (*p == '.' || *p == '-' || *p == '+') |
| 8261 | *p = '_'; |
| 8262 | |
| 8263 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8264 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8265 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8266 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8267 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8268 | } |
| 8269 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8270 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8271 | |
| 8272 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8273 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8274 | |
| 8275 | /* |
| 8276 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8277 | * Register class Converters |
| 8278 | * |
| 8279 | */ |
| 8280 | |
| 8281 | /* Create and fill the metatable. */ |
| 8282 | lua_newtable(gL.T); |
| 8283 | |
| 8284 | /* Create and fill the __index entry. */ |
| 8285 | lua_pushstring(gL.T, "__index"); |
| 8286 | lua_newtable(gL.T); |
| 8287 | |
| 8288 | /* Browse existing converters and create the associated |
| 8289 | * object method. |
| 8290 | */ |
| 8291 | sc = NULL; |
| 8292 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8293 | /* Dont register the keywork if the arguments check function are |
| 8294 | * not safe during the runtime. |
| 8295 | */ |
| 8296 | if (sc->val_args != NULL) |
| 8297 | continue; |
| 8298 | |
| 8299 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8300 | * by an underscore. |
| 8301 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8302 | strncpy(trash.area, sc->kw, trash.size); |
| 8303 | trash.area[trash.size - 1] = '\0'; |
| 8304 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8305 | if (*p == '.' || *p == '-' || *p == '+') |
| 8306 | *p = '_'; |
| 8307 | |
| 8308 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8309 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8310 | lua_pushlightuserdata(gL.T, sc); |
| 8311 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8312 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8313 | } |
| 8314 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8315 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8316 | |
| 8317 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8318 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8319 | |
| 8320 | /* |
| 8321 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8322 | * Register class HTTP |
| 8323 | * |
| 8324 | */ |
| 8325 | |
| 8326 | /* Create and fill the metatable. */ |
| 8327 | lua_newtable(gL.T); |
| 8328 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8329 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8330 | lua_pushstring(gL.T, "__index"); |
| 8331 | lua_newtable(gL.T); |
| 8332 | |
| 8333 | /* Register Lua functions. */ |
| 8334 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8335 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8336 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8337 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8338 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8339 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8340 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8341 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8342 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8343 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8344 | |
| 8345 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8346 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8347 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8348 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8349 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8350 | 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] | 8351 | 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] | 8352 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8353 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8354 | |
| 8355 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8356 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8357 | |
| 8358 | /* |
| 8359 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8360 | * Register class AppletTCP |
| 8361 | * |
| 8362 | */ |
| 8363 | |
| 8364 | /* Create and fill the metatable. */ |
| 8365 | lua_newtable(gL.T); |
| 8366 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8367 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8368 | lua_pushstring(gL.T, "__index"); |
| 8369 | lua_newtable(gL.T); |
| 8370 | |
| 8371 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8372 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8373 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8374 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8375 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8376 | 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] | 8377 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8378 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8379 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8380 | |
| 8381 | lua_settable(gL.T, -3); |
| 8382 | |
| 8383 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8384 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8385 | |
| 8386 | /* |
| 8387 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8388 | * Register class AppletHTTP |
| 8389 | * |
| 8390 | */ |
| 8391 | |
| 8392 | /* Create and fill the metatable. */ |
| 8393 | lua_newtable(gL.T); |
| 8394 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8395 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8396 | lua_pushstring(gL.T, "__index"); |
| 8397 | lua_newtable(gL.T); |
| 8398 | |
| 8399 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8400 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8401 | 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] | 8402 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8403 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8404 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8405 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8406 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8407 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8408 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8409 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8410 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8411 | |
| 8412 | lua_settable(gL.T, -3); |
| 8413 | |
| 8414 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8415 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8416 | |
| 8417 | /* |
| 8418 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8419 | * Register class TXN |
| 8420 | * |
| 8421 | */ |
| 8422 | |
| 8423 | /* Create and fill the metatable. */ |
| 8424 | lua_newtable(gL.T); |
| 8425 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8426 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8427 | lua_pushstring(gL.T, "__index"); |
| 8428 | lua_newtable(gL.T); |
| 8429 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8430 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8431 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8432 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8433 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8434 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8435 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8436 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8437 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8438 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8439 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8440 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8441 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8442 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8443 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8444 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8445 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8446 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8447 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8448 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8449 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8450 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8451 | |
| 8452 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8453 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8454 | |
| 8455 | /* |
| 8456 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8457 | * Register class reply |
| 8458 | * |
| 8459 | */ |
| 8460 | lua_newtable(gL.T); |
| 8461 | lua_pushstring(gL.T, "__index"); |
| 8462 | lua_newtable(gL.T); |
| 8463 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8464 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8465 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8466 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8467 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8468 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8469 | |
| 8470 | |
| 8471 | /* |
| 8472 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8473 | * Register class Socket |
| 8474 | * |
| 8475 | */ |
| 8476 | |
| 8477 | /* Create and fill the metatable. */ |
| 8478 | lua_newtable(gL.T); |
| 8479 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8480 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8481 | lua_pushstring(gL.T, "__index"); |
| 8482 | lua_newtable(gL.T); |
| 8483 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8484 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8485 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8486 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8487 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8488 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8489 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8490 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8491 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8492 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8493 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8494 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8495 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8496 | 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] | 8497 | |
| 8498 | /* Register the garbage collector entry. */ |
| 8499 | lua_pushstring(gL.T, "__gc"); |
| 8500 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8501 | 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] | 8502 | |
| 8503 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8504 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8505 | |
| 8506 | /* Proxy and server configuration initialisation. */ |
| 8507 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8508 | init_new_proxy(&socket_proxy); |
| 8509 | socket_proxy.parent = NULL; |
| 8510 | socket_proxy.last_change = now.tv_sec; |
| 8511 | socket_proxy.id = "LUA-SOCKET"; |
| 8512 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8513 | socket_proxy.maxconn = 0; |
| 8514 | socket_proxy.accept = NULL; |
| 8515 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8516 | socket_proxy.srv = NULL; |
| 8517 | socket_proxy.conn_retries = 0; |
| 8518 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8519 | |
| 8520 | /* Init TCP server: unchanged parameters */ |
| 8521 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8522 | socket_tcp.next = NULL; |
| 8523 | socket_tcp.proxy = &socket_proxy; |
| 8524 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8525 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8526 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8527 | socket_tcp.idle_conns = NULL; |
| 8528 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8529 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8530 | socket_tcp.last_change = 0; |
| 8531 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8532 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8533 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8534 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8535 | |
| 8536 | /* XXX: Copy default parameter from default server, |
| 8537 | * but the default server is not initialized. |
| 8538 | */ |
| 8539 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8540 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8541 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8542 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8543 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8544 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8545 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8546 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8547 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8548 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8549 | |
| 8550 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8551 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8552 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8553 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8554 | socket_tcp.check.server = &socket_tcp; |
| 8555 | |
| 8556 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8557 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8558 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8559 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8560 | socket_tcp.agent.server = &socket_tcp; |
| 8561 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8562 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8563 | |
| 8564 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8565 | /* Init TCP server: unchanged parameters */ |
| 8566 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8567 | socket_ssl.next = NULL; |
| 8568 | socket_ssl.proxy = &socket_proxy; |
| 8569 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8570 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8571 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8572 | socket_ssl.idle_conns = NULL; |
| 8573 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8574 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8575 | socket_ssl.last_change = 0; |
| 8576 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8577 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8578 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8579 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8580 | |
| 8581 | /* XXX: Copy default parameter from default server, |
| 8582 | * but the default server is not initialized. |
| 8583 | */ |
| 8584 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8585 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8586 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8587 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8588 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8589 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8590 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8591 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8592 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8593 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8594 | |
| 8595 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8596 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8597 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8598 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8599 | socket_ssl.check.server = &socket_ssl; |
| 8600 | |
| 8601 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8602 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8603 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8604 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8605 | socket_ssl.agent.server = &socket_ssl; |
| 8606 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8607 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8608 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8609 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8610 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8611 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8612 | /* |
| 8613 | * |
| 8614 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8615 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8616 | * features like client certificates and ssl_verify. |
| 8617 | * |
| 8618 | */ |
| 8619 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8620 | if (tmp_error != 0) { |
| 8621 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8622 | abort(); /* This must be never arrives because the command line |
| 8623 | not editable by the user. */ |
| 8624 | } |
| 8625 | idx += kw->skip; |
| 8626 | } |
| 8627 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8628 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8629 | |
| 8630 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8631 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8632 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8633 | static void hlua_deinit() |
| 8634 | { |
| 8635 | lua_close(gL.T); |
| 8636 | } |
| 8637 | |
| 8638 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8639 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8640 | static void hlua_register_build_options(void) |
| 8641 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8642 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8643 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8644 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8645 | hap_register_build_opts(ptr, 1); |
| 8646 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8647 | |
| 8648 | INITCALL0(STG_REGISTER, hlua_register_build_options); |