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: |
Christopher Faulet | 8e09ac8 | 2020-08-07 09:07:26 +0200 | [diff] [blame^] | 744 | if (argp[idx].type != ARGT_STR) |
| 745 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 746 | memcpy(trash.area, argp[idx].data.str.area, |
| 747 | argp[idx].data.str.data); |
| 748 | trash.area[argp[idx].data.str.data] = 0; |
| 749 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 751 | argp[idx].type = ARGT_IPV4; |
| 752 | break; |
| 753 | |
| 754 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 755 | memcpy(trash.area, argp[idx].data.str.area, |
| 756 | argp[idx].data.str.data); |
| 757 | trash.area[argp[idx].data.str.data] = 0; |
| 758 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 759 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 760 | argp[idx].type = ARGT_MSK4; |
| 761 | break; |
| 762 | |
| 763 | case ARGT_IPV6: |
Christopher Faulet | 8e09ac8 | 2020-08-07 09:07:26 +0200 | [diff] [blame^] | 764 | if (argp[idx].type != ARGT_STR) |
| 765 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 766 | memcpy(trash.area, argp[idx].data.str.area, |
| 767 | argp[idx].data.str.data); |
| 768 | trash.area[argp[idx].data.str.data] = 0; |
| 769 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 771 | argp[idx].type = ARGT_IPV6; |
| 772 | break; |
| 773 | |
| 774 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 775 | memcpy(trash.area, argp[idx].data.str.area, |
| 776 | argp[idx].data.str.data); |
| 777 | trash.area[argp[idx].data.str.data] = 0; |
| 778 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 779 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 780 | argp[idx].type = ARGT_MSK6; |
| 781 | break; |
| 782 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 783 | case ARGT_MAP: |
| 784 | case ARGT_REG: |
| 785 | case ARGT_USR: |
| 786 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 787 | break; |
| 788 | } |
| 789 | |
| 790 | /* Check for type of argument. */ |
| 791 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 792 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 793 | arg_type_names[(mask & ARGT_MASK)], |
| 794 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 795 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 796 | } |
| 797 | |
| 798 | /* Next argument. */ |
| 799 | mask >>= ARGT_BITS; |
| 800 | idx++; |
| 801 | } |
| 802 | } |
| 803 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 804 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 805 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 807 | * |
| 808 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 809 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 810 | */ |
| 811 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 812 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 813 | struct hlua **hlua = lua_getextraspace(L); |
| 814 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 815 | } |
| 816 | static inline void hlua_sethlua(struct hlua *hlua) |
| 817 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 818 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 819 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 822 | /* This function is used to send logs. It try to send on screen (stderr) |
| 823 | * and on the default syslog server. |
| 824 | */ |
| 825 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 826 | { |
| 827 | struct tm tm; |
| 828 | char *p; |
| 829 | |
| 830 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 831 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 832 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 833 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 834 | /* Break the message if exceed the buffer size. */ |
| 835 | *(p-4) = ' '; |
| 836 | *(p-3) = '.'; |
| 837 | *(p-2) = '.'; |
| 838 | *(p-1) = '.'; |
| 839 | break; |
| 840 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 841 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 842 | *p = *msg; |
| 843 | else |
| 844 | *p = '.'; |
| 845 | } |
| 846 | *p = '\0'; |
| 847 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 848 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 849 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 850 | get_localtime(date.tv_sec, &tm); |
| 851 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 852 | 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] | 853 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 854 | fflush(stderr); |
| 855 | } |
| 856 | } |
| 857 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 858 | /* This function just ensure that the yield will be always |
| 859 | * returned with a timeout and permit to set some flags |
| 860 | */ |
| 861 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 862 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 863 | { |
| 864 | struct hlua *hlua = hlua_gethlua(L); |
| 865 | |
| 866 | /* Set the wake timeout. If timeout is required, we set |
| 867 | * the expiration time. |
| 868 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 869 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 870 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 871 | hlua->flags |= flags; |
| 872 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 873 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 874 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 877 | /* This function initialises the Lua environment stored in the stream. |
| 878 | * 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] | 879 | * 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] | 880 | * |
| 881 | * This function is particular. it initialises a new Lua thread. If the |
| 882 | * initialisation fails (example: out of memory error), the lua function |
| 883 | * throws an error (longjmp). |
| 884 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 885 | * 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] | 886 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 887 | * threads appear, the safe environment set a lock to ensure only one |
| 888 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 889 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 890 | * |
| 891 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 892 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 893 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 894 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 895 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 896 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 897 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 898 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 899 | 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] | 900 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 901 | if (!already_safe) { |
| 902 | if (!SET_SAFE_LJMP(gL.T)) { |
| 903 | lua->Tref = LUA_REFNIL; |
| 904 | return 0; |
| 905 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 906 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 908 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 909 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 910 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 911 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 912 | lua->T = lua_newthread(gL.T); |
| 913 | if (!lua->T) { |
| 914 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 915 | if (!already_safe) |
| 916 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | hlua_sethlua(lua); |
| 920 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 921 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 922 | if (!already_safe) |
| 923 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | return 1; |
| 925 | } |
| 926 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 927 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 928 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 929 | * is not freed. |
| 930 | */ |
| 931 | void hlua_ctx_destroy(struct hlua *lua) |
| 932 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 933 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 934 | return; |
| 935 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 936 | if (!lua->T) |
| 937 | goto end; |
| 938 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 939 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 940 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 941 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 942 | if (!SET_SAFE_LJMP(lua->T)) |
| 943 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 944 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 945 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 946 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 947 | if (!SET_SAFE_LJMP(gL.T)) |
| 948 | return; |
| 949 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 950 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 951 | /* Forces a garbage collecting process. If the Lua program is finished |
| 952 | * without error, we run the GC on the thread pointer. Its freed all |
| 953 | * the unused memory. |
| 954 | * If the thread is finnish with an error or is currently yielded, |
| 955 | * it seems that the GC applied on the thread doesn't clean anything, |
| 956 | * so e run the GC on the main thread. |
| 957 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 958 | * the garbage collection. |
| 959 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 960 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 961 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 962 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 963 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 964 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 965 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 966 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 967 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 968 | |
| 969 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 970 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* This function is used to restore the Lua context when a coroutine |
| 974 | * fails. This function copy the common memory between old coroutine |
| 975 | * and the new coroutine. The old coroutine is destroyed, and its |
| 976 | * replaced by the new coroutine. |
| 977 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 978 | * as string error message and it is copied in the new stack. |
| 979 | */ |
| 980 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 981 | { |
| 982 | lua_State *T; |
| 983 | int new_ref; |
| 984 | |
| 985 | /* Renew the main LUA stack doesn't have sense. */ |
| 986 | if (lua == &gL) |
| 987 | return 0; |
| 988 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 989 | /* New Lua coroutine. */ |
| 990 | T = lua_newthread(gL.T); |
| 991 | if (!T) |
| 992 | return 0; |
| 993 | |
| 994 | /* Copy last error message. */ |
| 995 | if (keep_msg) |
| 996 | lua_xmove(lua->T, T, 1); |
| 997 | |
| 998 | /* Copy data between the coroutines. */ |
| 999 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1000 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1001 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1002 | |
| 1003 | /* Destroy old data. */ |
| 1004 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1005 | |
| 1006 | /* The thread is garbage collected by Lua. */ |
| 1007 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1008 | |
| 1009 | /* Fill the struct with the new coroutine values. */ |
| 1010 | lua->Mref = new_ref; |
| 1011 | lua->T = T; |
| 1012 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1013 | |
| 1014 | /* Set context. */ |
| 1015 | hlua_sethlua(lua); |
| 1016 | |
| 1017 | return 1; |
| 1018 | } |
| 1019 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1020 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1021 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1022 | struct hlua *hlua = hlua_gethlua(L); |
| 1023 | |
| 1024 | /* Lua cannot yield when its returning from a function, |
| 1025 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1026 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1027 | */ |
| 1028 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1029 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | /* restore the interrupt condition. */ |
| 1034 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1035 | |
| 1036 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1037 | * If the state is not yieldable, trying yield causes an error. |
| 1038 | */ |
| 1039 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1040 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1041 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1042 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1043 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1044 | hlua->run_time += now_ms - hlua->start_time; |
| 1045 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1046 | lua_pushfstring(L, "execution timeout"); |
| 1047 | WILL_LJMP(lua_error(L)); |
| 1048 | } |
| 1049 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1050 | /* Update the start time. */ |
| 1051 | hlua->start_time = now_ms; |
| 1052 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1053 | /* Try to interrupt the process at the end of the current |
| 1054 | * unyieldable function. |
| 1055 | */ |
| 1056 | 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] | 1057 | } |
| 1058 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1059 | /* This function start or resumes the Lua stack execution. If the flag |
| 1060 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1061 | * The function return an error. |
| 1062 | * |
| 1063 | * The function can returns 4 values: |
| 1064 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1065 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1066 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * - 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] | 1068 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1069 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1071 | * 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] | 1072 | * LUA code. |
| 1073 | */ |
| 1074 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1075 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1076 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1077 | int nres; |
| 1078 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1079 | int ret; |
| 1080 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1081 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1082 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1083 | /* Initialise run time counter. */ |
| 1084 | if (!HLUA_IS_RUNNING(lua)) |
| 1085 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1086 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1087 | /* Lock the whole Lua execution. This lock must be before the |
| 1088 | * label "resume_execution". |
| 1089 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1090 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1091 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1092 | resume_execution: |
| 1093 | |
| 1094 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1095 | * instructions. it is used for preventing infinite loops. |
| 1096 | */ |
| 1097 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1098 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1099 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1100 | HLUA_SET_RUN(lua); |
| 1101 | HLUA_CLR_CTRLYIELD(lua); |
| 1102 | HLUA_CLR_WAKERESWR(lua); |
| 1103 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1104 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1105 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1106 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1107 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1108 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1109 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1110 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1111 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1112 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1113 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1114 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1115 | switch (ret) { |
| 1116 | |
| 1117 | case LUA_OK: |
| 1118 | ret = HLUA_E_OK; |
| 1119 | break; |
| 1120 | |
| 1121 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1122 | /* Check if the execution timeout is expired. It it is the case, we |
| 1123 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1124 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1125 | tv_update_date(0, 1); |
| 1126 | lua->run_time += now_ms - lua->start_time; |
| 1127 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1128 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1129 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1130 | break; |
| 1131 | } |
| 1132 | /* Process the forced yield. if the general yield is not allowed or |
| 1133 | * if no task were associated this the current Lua execution |
| 1134 | * coroutine, we resume the execution. Else we want to return in the |
| 1135 | * scheduler and we want to be waked up again, to continue the |
| 1136 | * current Lua execution. So we schedule our own task. |
| 1137 | */ |
| 1138 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1139 | if (!yield_allowed || !lua->task) |
| 1140 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1141 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1142 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1143 | if (!yield_allowed) { |
| 1144 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1145 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1146 | break; |
| 1147 | } |
| 1148 | ret = HLUA_E_AGAIN; |
| 1149 | break; |
| 1150 | |
| 1151 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1152 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1153 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1154 | * because the errors ares the only one mean to return immediately |
| 1155 | * from and lua execution. |
| 1156 | */ |
| 1157 | if (lua->flags & HLUA_EXIT) { |
| 1158 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1159 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1160 | break; |
| 1161 | } |
| 1162 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1163 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | if (!lua_checkstack(lua->T, 1)) { |
| 1165 | ret = HLUA_E_ERR; |
| 1166 | break; |
| 1167 | } |
| 1168 | msg = lua_tostring(lua->T, -1); |
| 1169 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1170 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1171 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1173 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1174 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1175 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1176 | ret = HLUA_E_ERRMSG; |
| 1177 | break; |
| 1178 | |
| 1179 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1180 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1181 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1182 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | break; |
| 1184 | |
| 1185 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1186 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1187 | if (!lua_checkstack(lua->T, 1)) { |
| 1188 | ret = HLUA_E_ERR; |
| 1189 | break; |
| 1190 | } |
| 1191 | msg = lua_tostring(lua->T, -1); |
| 1192 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1193 | lua_pop(lua->T, 1); |
| 1194 | if (msg) |
| 1195 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1196 | else |
| 1197 | lua_pushfstring(lua->T, "message handler error"); |
| 1198 | ret = HLUA_E_ERRMSG; |
| 1199 | break; |
| 1200 | |
| 1201 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1202 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1203 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1204 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | break; |
| 1206 | } |
| 1207 | |
| 1208 | switch (ret) { |
| 1209 | case HLUA_E_AGAIN: |
| 1210 | break; |
| 1211 | |
| 1212 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1213 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1215 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | break; |
| 1217 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1218 | case HLUA_E_ETMOUT: |
| 1219 | case HLUA_E_NOMEM: |
| 1220 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1221 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1222 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1223 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1224 | hlua_ctx_renew(lua, 0); |
| 1225 | break; |
| 1226 | |
| 1227 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1228 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1229 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1230 | break; |
| 1231 | } |
| 1232 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1233 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1234 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1235 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1236 | return ret; |
| 1237 | } |
| 1238 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1239 | /* This function exit the current code. */ |
| 1240 | __LJMP static int hlua_done(lua_State *L) |
| 1241 | { |
| 1242 | struct hlua *hlua = hlua_gethlua(L); |
| 1243 | |
| 1244 | hlua->flags |= HLUA_EXIT; |
| 1245 | WILL_LJMP(lua_error(L)); |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1250 | /* This function is an LUA binding. It provides a function |
| 1251 | * for deleting ACL from a referenced ACL file. |
| 1252 | */ |
| 1253 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1254 | { |
| 1255 | const char *name; |
| 1256 | const char *key; |
| 1257 | struct pat_ref *ref; |
| 1258 | |
| 1259 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1260 | |
| 1261 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1262 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1263 | |
| 1264 | ref = pat_ref_lookup(name); |
| 1265 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1266 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1267 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1268 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1269 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1270 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | /* This function is an LUA binding. It provides a function |
| 1275 | * for deleting map entry from a referenced map file. |
| 1276 | */ |
| 1277 | static int hlua_del_map(lua_State *L) |
| 1278 | { |
| 1279 | const char *name; |
| 1280 | const char *key; |
| 1281 | struct pat_ref *ref; |
| 1282 | |
| 1283 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1284 | |
| 1285 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1286 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1287 | |
| 1288 | ref = pat_ref_lookup(name); |
| 1289 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1290 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1291 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1292 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1293 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1294 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | /* This function is an LUA binding. It provides a function |
| 1299 | * for adding ACL pattern from a referenced ACL file. |
| 1300 | */ |
| 1301 | static int hlua_add_acl(lua_State *L) |
| 1302 | { |
| 1303 | const char *name; |
| 1304 | const char *key; |
| 1305 | struct pat_ref *ref; |
| 1306 | |
| 1307 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1308 | |
| 1309 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1310 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1311 | |
| 1312 | ref = pat_ref_lookup(name); |
| 1313 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1314 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1315 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1316 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1317 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1318 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1319 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /* This function is an LUA binding. It provides a function |
| 1324 | * for setting map pattern and sample from a referenced map |
| 1325 | * file. |
| 1326 | */ |
| 1327 | static int hlua_set_map(lua_State *L) |
| 1328 | { |
| 1329 | const char *name; |
| 1330 | const char *key; |
| 1331 | const char *value; |
| 1332 | struct pat_ref *ref; |
| 1333 | |
| 1334 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1335 | |
| 1336 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1337 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1338 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1339 | |
| 1340 | ref = pat_ref_lookup(name); |
| 1341 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1342 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1343 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1344 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1345 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1346 | pat_ref_set(ref, key, value, NULL); |
| 1347 | else |
| 1348 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1349 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1350 | return 0; |
| 1351 | } |
| 1352 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1353 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1354 | * 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] | 1355 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1356 | * the name of the object (_G[<name>] = <metable> ). |
| 1357 | * |
| 1358 | * A metable is a table that modify the standard behavior of a standard |
| 1359 | * access to the associated data. The entries of this new metatable are |
| 1360 | * defined as is: |
| 1361 | * |
| 1362 | * http://lua-users.org/wiki/MetatableEvents |
| 1363 | * |
| 1364 | * __index |
| 1365 | * |
| 1366 | * we access an absent field in a table, the result is nil. This is |
| 1367 | * true, but it is not the whole truth. Actually, such access triggers |
| 1368 | * the interpreter to look for an __index metamethod: If there is no |
| 1369 | * such method, as usually happens, then the access results in nil; |
| 1370 | * otherwise, the metamethod will provide the result. |
| 1371 | * |
| 1372 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1373 | * the key does not appear in the table, but the metatable has an __index |
| 1374 | * property: |
| 1375 | * |
| 1376 | * - if the value is a function, the function is called, passing in the |
| 1377 | * table and the key; the return value of that function is returned as |
| 1378 | * the result. |
| 1379 | * |
| 1380 | * - if the value is another table, the value of the key in that table is |
| 1381 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1382 | * table's metatable has an __index property, then it continues on up) |
| 1383 | * |
| 1384 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1385 | * |
| 1386 | * http://www.lua.org/pil/13.4.1.html |
| 1387 | * |
| 1388 | * __newindex |
| 1389 | * |
| 1390 | * Like __index, but control property assignment. |
| 1391 | * |
| 1392 | * __mode - Control weak references. A string value with one or both |
| 1393 | * of the characters 'k' and 'v' which specifies that the the |
| 1394 | * keys and/or values in the table are weak references. |
| 1395 | * |
| 1396 | * __call - Treat a table like a function. When a table is followed by |
| 1397 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1398 | * a __call key pointing to a function, that function is invoked |
| 1399 | * (passing any specified arguments) and the return value is |
| 1400 | * returned. |
| 1401 | * |
| 1402 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1403 | * called, if the metatable for myTable has a __metatable |
| 1404 | * key, the value of that key is returned instead of the |
| 1405 | * actual metatable. |
| 1406 | * |
| 1407 | * __tostring - Control string representation. When the builtin |
| 1408 | * "tostring( myTable )" function is called, if the metatable |
| 1409 | * for myTable has a __tostring property set to a function, |
| 1410 | * that function is invoked (passing myTable to it) and the |
| 1411 | * return value is used as the string representation. |
| 1412 | * |
| 1413 | * __len - Control table length. When the table length is requested using |
| 1414 | * the length operator ( '#' ), if the metatable for myTable has |
| 1415 | * a __len key pointing to a function, that function is invoked |
| 1416 | * (passing myTable to it) and the return value used as the value |
| 1417 | * of "#myTable". |
| 1418 | * |
| 1419 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1420 | * collected, if the metatable has a __gc field pointing to a |
| 1421 | * function, that function is first invoked, passing the userdata |
| 1422 | * to it. The __gc metamethod is not called for tables. |
| 1423 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1424 | * |
| 1425 | * Special metamethods for redefining standard operators: |
| 1426 | * http://www.lua.org/pil/13.1.html |
| 1427 | * |
| 1428 | * __add "+" |
| 1429 | * __sub "-" |
| 1430 | * __mul "*" |
| 1431 | * __div "/" |
| 1432 | * __unm "!" |
| 1433 | * __pow "^" |
| 1434 | * __concat ".." |
| 1435 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1436 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1437 | * http://www.lua.org/pil/13.2.html |
| 1438 | * |
| 1439 | * __eq "==" |
| 1440 | * __lt "<" |
| 1441 | * __le "<=" |
| 1442 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | * |
| 1446 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1447 | * Class Map |
| 1448 | * |
| 1449 | * |
| 1450 | */ |
| 1451 | |
| 1452 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1453 | * a class session, otherwise it throws an error. |
| 1454 | */ |
| 1455 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1456 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1457 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /* This function is the map constructor. It don't need |
| 1461 | * the class Map object. It creates and return a new Map |
| 1462 | * object. It must be called only during "body" or "init" |
| 1463 | * context because it process some filesystem accesses. |
| 1464 | */ |
| 1465 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1466 | { |
| 1467 | const char *fn; |
| 1468 | int match = PAT_MATCH_STR; |
| 1469 | struct sample_conv conv; |
| 1470 | const char *file = ""; |
| 1471 | int line = 0; |
| 1472 | lua_Debug ar; |
| 1473 | char *err = NULL; |
| 1474 | struct arg args[2]; |
| 1475 | |
| 1476 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1477 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1478 | |
| 1479 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1480 | |
| 1481 | if (lua_gettop(L) >= 2) { |
| 1482 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1483 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1484 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1485 | } |
| 1486 | |
| 1487 | /* Get Lua filename and line number. */ |
| 1488 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1489 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1490 | if (ar.currentline > 0) { /* is there info? */ |
| 1491 | file = ar.short_src; |
| 1492 | line = ar.currentline; |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | /* fill fake sample_conv struct. */ |
| 1497 | conv.kw = ""; /* unused. */ |
| 1498 | conv.process = NULL; /* unused. */ |
| 1499 | conv.arg_mask = 0; /* unused. */ |
| 1500 | conv.val_args = NULL; /* unused. */ |
| 1501 | conv.out_type = SMP_T_STR; |
| 1502 | conv.private = (void *)(long)match; |
| 1503 | switch (match) { |
| 1504 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1505 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1506 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1507 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1508 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1509 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1510 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1511 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1512 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1513 | default: |
| 1514 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1515 | } |
| 1516 | |
| 1517 | /* fill fake args. */ |
| 1518 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1519 | args[0].data.str.area = strdup(fn); |
| 1520 | args[0].data.str.data = strlen(fn); |
| 1521 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1522 | args[1].type = ARGT_STOP; |
| 1523 | |
| 1524 | /* load the map. */ |
| 1525 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1526 | /* error case: we cant use luaL_error because we must |
| 1527 | * free the err variable. |
| 1528 | */ |
| 1529 | luaL_where(L, 1); |
| 1530 | lua_pushfstring(L, "'new': %s.", err); |
| 1531 | lua_concat(L, 2); |
| 1532 | free(err); |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1533 | free(args[0].data.str.area); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1534 | WILL_LJMP(lua_error(L)); |
| 1535 | } |
| 1536 | |
| 1537 | /* create the lua object. */ |
| 1538 | lua_newtable(L); |
| 1539 | lua_pushlightuserdata(L, args[0].data.map); |
| 1540 | lua_rawseti(L, -2, 0); |
| 1541 | |
| 1542 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1543 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1544 | lua_setmetatable(L, -2); |
| 1545 | |
| 1546 | |
| 1547 | return 1; |
| 1548 | } |
| 1549 | |
| 1550 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1551 | { |
| 1552 | struct map_descriptor *desc; |
| 1553 | struct pattern *pat; |
| 1554 | struct sample smp; |
| 1555 | |
| 1556 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1557 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1558 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1559 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1560 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1561 | } |
| 1562 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1563 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1564 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1565 | 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] | 1566 | } |
| 1567 | |
| 1568 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1569 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1570 | if (str) |
| 1571 | lua_pushstring(L, ""); |
| 1572 | else |
| 1573 | lua_pushnil(L); |
| 1574 | return 1; |
| 1575 | } |
| 1576 | |
| 1577 | /* 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] | 1578 | 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] | 1579 | return 1; |
| 1580 | } |
| 1581 | |
| 1582 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1583 | { |
| 1584 | return _hlua_map_lookup(L, 0); |
| 1585 | } |
| 1586 | |
| 1587 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1588 | { |
| 1589 | return _hlua_map_lookup(L, 1); |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * |
| 1594 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1595 | * Class Socket |
| 1596 | * |
| 1597 | * |
| 1598 | */ |
| 1599 | |
| 1600 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1601 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1602 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /* 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] | 1606 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1607 | * received. |
| 1608 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1609 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1610 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1611 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1612 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1613 | if (appctx->ctx.hlua_cosocket.die) { |
| 1614 | si_shutw(si); |
| 1615 | si_shutr(si); |
| 1616 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1617 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1618 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1619 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1620 | } |
| 1621 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1622 | /* If we cant write, wakeup the pending write signals. */ |
| 1623 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1624 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1625 | |
| 1626 | /* If we cant read, wakeup the pending read signals. */ |
| 1627 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1628 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1629 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1630 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1631 | * to be notified whenever the connection completes. |
| 1632 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1633 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1634 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1635 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1636 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1637 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1638 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1639 | |
| 1640 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1641 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1642 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1643 | /* 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] | 1644 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1645 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1646 | |
| 1647 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1648 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1649 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1650 | |
| 1651 | /* Some data were injected in the buffer, notify the stream |
| 1652 | * interface. |
| 1653 | */ |
| 1654 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1655 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1656 | |
| 1657 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1658 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1659 | */ |
| 1660 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1661 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | } |
| 1663 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1664 | /* This function is called when the "struct stream" is destroyed. |
| 1665 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | * Wake all the pending signals. |
| 1667 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1668 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1669 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1670 | struct xref *peer; |
| 1671 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1672 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1673 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1674 | if (peer) |
| 1675 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1676 | |
| 1677 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1678 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1679 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1683 | * uses this object. If the stream does not exists, just quit. |
| 1684 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | * pending signal can rest in the read and write lists. destroy |
| 1686 | * it. |
| 1687 | */ |
| 1688 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1689 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1690 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1692 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1693 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1694 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1695 | |
| 1696 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1697 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1698 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1700 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
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 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1703 | appctx->ctx.hlua_cosocket.die = 1; |
| 1704 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1706 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1707 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1712 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1714 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1715 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1716 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1718 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1719 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1720 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1721 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1722 | |
| 1723 | /* Check if we run on the same thread than the xreator thread. |
| 1724 | * We cannot access to the socket if the thread is different. |
| 1725 | */ |
| 1726 | if (socket->tid != tid) |
| 1727 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1728 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1729 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1730 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1731 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1732 | |
| 1733 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1734 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
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 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1737 | appctx->ctx.hlua_cosocket.die = 1; |
| 1738 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1739 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1740 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1741 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
| 1744 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1745 | /* The close function calls close_helper. |
| 1746 | */ |
| 1747 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1748 | { |
| 1749 | MAY_LJMP(check_args(L, 1, "close")); |
| 1750 | return hlua_socket_close_helper(L); |
| 1751 | } |
| 1752 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1753 | /* This Lua function assumes that the stack contain three parameters. |
| 1754 | * 1 - USERDATA containing a struct socket |
| 1755 | * 2 - INTEGER with values of the macro defined below |
| 1756 | * If the integer is -1, we must read at most one line. |
| 1757 | * If the integer is -2, we ust read all the data until the |
| 1758 | * end of the stream. |
| 1759 | * If the integer is positive value, we must read a number of |
| 1760 | * bytes corresponding to this value. |
| 1761 | */ |
| 1762 | #define HLSR_READ_LINE (-1) |
| 1763 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1764 | __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] | 1765 | { |
| 1766 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1767 | int wanted = lua_tointeger(L, 2); |
| 1768 | struct hlua *hlua = hlua_gethlua(L); |
| 1769 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1770 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1771 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1772 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1773 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1774 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1775 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1776 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1777 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1778 | struct stream_interface *si; |
| 1779 | struct stream *s; |
| 1780 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1781 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | |
| 1783 | /* Check if this lua stack is schedulable. */ |
| 1784 | if (!hlua || !hlua->task) |
| 1785 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1786 | "'frontend', 'backend' or 'task'")); |
| 1787 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1788 | /* Check if we run on the same thread than the xreator thread. |
| 1789 | * We cannot access to the socket if the thread is different. |
| 1790 | */ |
| 1791 | if (socket->tid != tid) |
| 1792 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1793 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1794 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1795 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1796 | if (!peer) |
| 1797 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1798 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1799 | si = appctx->owner; |
| 1800 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1801 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1802 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1803 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1804 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1805 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1806 | if (nblk < 0) /* Connection close. */ |
| 1807 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1808 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1809 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1810 | |
| 1811 | /* remove final \r\n. */ |
| 1812 | if (nblk == 1) { |
| 1813 | if (blk1[len1-1] == '\n') { |
| 1814 | len1--; |
| 1815 | skip_at_end++; |
| 1816 | if (blk1[len1-1] == '\r') { |
| 1817 | len1--; |
| 1818 | skip_at_end++; |
| 1819 | } |
| 1820 | } |
| 1821 | } |
| 1822 | else { |
| 1823 | if (blk2[len2-1] == '\n') { |
| 1824 | len2--; |
| 1825 | skip_at_end++; |
| 1826 | if (blk2[len2-1] == '\r') { |
| 1827 | len2--; |
| 1828 | skip_at_end++; |
| 1829 | } |
| 1830 | } |
| 1831 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1836 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | if (nblk < 0) /* Connection close. */ |
| 1838 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1839 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | goto connection_empty; |
| 1841 | } |
| 1842 | |
| 1843 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1844 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1845 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1846 | if (nblk < 0) /* Connection close. */ |
| 1847 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1848 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | goto connection_empty; |
| 1850 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1851 | missing_bytes = wanted - socket->b.n; |
| 1852 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1853 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1854 | len1 = missing_bytes; |
| 1855 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1856 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | len = len1; |
| 1860 | |
| 1861 | luaL_addlstring(&socket->b, blk1, len1); |
| 1862 | if (nblk == 2) { |
| 1863 | len += len2; |
| 1864 | luaL_addlstring(&socket->b, blk2, len2); |
| 1865 | } |
| 1866 | |
| 1867 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1868 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | |
| 1870 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1871 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | |
| 1873 | /* If the pattern reclaim to read all the data |
| 1874 | * in the connection, got out. |
| 1875 | */ |
| 1876 | if (wanted == HLSR_READ_ALL) |
| 1877 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1878 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1879 | goto connection_empty; |
| 1880 | |
| 1881 | /* Return result. */ |
| 1882 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1883 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | return 1; |
| 1885 | |
| 1886 | connection_closed: |
| 1887 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1888 | xref_unlock(&socket->xref, peer); |
| 1889 | |
| 1890 | no_peer: |
| 1891 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1892 | /* If the buffer containds data. */ |
| 1893 | if (socket->b.n > 0) { |
| 1894 | luaL_pushresult(&socket->b); |
| 1895 | return 1; |
| 1896 | } |
| 1897 | lua_pushnil(L); |
| 1898 | lua_pushstring(L, "connection closed."); |
| 1899 | return 2; |
| 1900 | |
| 1901 | connection_empty: |
| 1902 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1903 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1904 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1906 | } |
| 1907 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1908 | 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] | 1909 | return 0; |
| 1910 | } |
| 1911 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1912 | /* This Lua function gets two parameters. The first one can be string |
| 1913 | * or a number. If the string is "*l", the user requires one line. If |
| 1914 | * 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] | 1915 | * If the value is a number, the user require a number of bytes equal |
| 1916 | * to the value. The default value is "*l" (a line). |
| 1917 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1918 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | * integer takes this values: |
| 1920 | * -1 : read a line |
| 1921 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1922 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1924 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | * concatenated with the read data. |
| 1926 | */ |
| 1927 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1928 | { |
| 1929 | int wanted = HLSR_READ_LINE; |
| 1930 | const char *pattern; |
| 1931 | int type; |
| 1932 | char *error; |
| 1933 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1934 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1935 | |
| 1936 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1937 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1938 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1939 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1940 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1941 | /* Check if we run on the same thread than the xreator thread. |
| 1942 | * We cannot access to the socket if the thread is different. |
| 1943 | */ |
| 1944 | if (socket->tid != tid) |
| 1945 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1946 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | /* check for pattern. */ |
| 1948 | if (lua_gettop(L) >= 2) { |
| 1949 | type = lua_type(L, 2); |
| 1950 | if (type == LUA_TSTRING) { |
| 1951 | pattern = lua_tostring(L, 2); |
| 1952 | if (strcmp(pattern, "*a") == 0) |
| 1953 | wanted = HLSR_READ_ALL; |
| 1954 | else if (strcmp(pattern, "*l") == 0) |
| 1955 | wanted = HLSR_READ_LINE; |
| 1956 | else { |
| 1957 | wanted = strtoll(pattern, &error, 10); |
| 1958 | if (*error != '\0') |
| 1959 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1960 | } |
| 1961 | } |
| 1962 | else if (type == LUA_TNUMBER) { |
| 1963 | wanted = lua_tointeger(L, 2); |
| 1964 | if (wanted < 0) |
| 1965 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | /* Set pattern. */ |
| 1970 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1971 | |
| 1972 | /* Check if we would replace the top by itself. */ |
| 1973 | if (lua_gettop(L) != 2) |
| 1974 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1975 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1976 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | luaL_buffinit(L, &socket->b); |
| 1978 | |
| 1979 | /* Check prefix. */ |
| 1980 | if (lua_gettop(L) >= 3) { |
| 1981 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1982 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1983 | pattern = lua_tolstring(L, 3, &len); |
| 1984 | luaL_addlstring(&socket->b, pattern, len); |
| 1985 | } |
| 1986 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1987 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1991 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1993 | 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] | 1994 | { |
| 1995 | struct hlua_socket *socket; |
| 1996 | struct hlua *hlua = hlua_gethlua(L); |
| 1997 | struct appctx *appctx; |
| 1998 | size_t buf_len; |
| 1999 | const char *buf; |
| 2000 | int len; |
| 2001 | int send_len; |
| 2002 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2003 | struct xref *peer; |
| 2004 | struct stream_interface *si; |
| 2005 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | |
| 2007 | /* Check if this lua stack is schedulable. */ |
| 2008 | if (!hlua || !hlua->task) |
| 2009 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2010 | "'frontend', 'backend' or 'task'")); |
| 2011 | |
| 2012 | /* Get object */ |
| 2013 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2014 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2015 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2016 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2017 | /* Check if we run on the same thread than the xreator thread. |
| 2018 | * We cannot access to the socket if the thread is different. |
| 2019 | */ |
| 2020 | if (socket->tid != tid) |
| 2021 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2022 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2023 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2024 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2025 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2026 | lua_pushinteger(L, -1); |
| 2027 | return 1; |
| 2028 | } |
| 2029 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2030 | si = appctx->owner; |
| 2031 | s = si_strm(si); |
| 2032 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2033 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2034 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2035 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2036 | lua_pushinteger(L, -1); |
| 2037 | return 1; |
| 2038 | } |
| 2039 | |
| 2040 | /* Update the input buffer data. */ |
| 2041 | buf += sent; |
| 2042 | send_len = buf_len - sent; |
| 2043 | |
| 2044 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2045 | if (sent >= buf_len) { |
| 2046 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2047 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2048 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2050 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2051 | * the request buffer if its not required. |
| 2052 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2053 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2054 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2055 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2056 | } |
| 2057 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2058 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2059 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2060 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2062 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2063 | |
| 2064 | /* send data */ |
| 2065 | if (len < send_len) |
| 2066 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2067 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2068 | |
| 2069 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2070 | * the data" (-2) are not expected because the available length |
| 2071 | * is tested. |
| 2072 | * Other unknown error are also not expected. |
| 2073 | */ |
| 2074 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2075 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2076 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2077 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2078 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2080 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2081 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2082 | return 1; |
| 2083 | } |
| 2084 | |
| 2085 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2086 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2087 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2088 | s->req.rex = TICK_ETERNITY; |
| 2089 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* Update length sent. */ |
| 2092 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2093 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | |
| 2095 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2096 | if (sent + len >= buf_len) { |
| 2097 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2099 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | |
| 2101 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2102 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2103 | xref_unlock(&socket->xref, peer); |
| 2104 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2105 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2106 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2107 | 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] | 2108 | return 0; |
| 2109 | } |
| 2110 | |
| 2111 | /* This function initiate the send of data. It just check the input |
| 2112 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2113 | * 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] | 2114 | * "hlua_socket_write_yield" that can yield. |
| 2115 | * |
| 2116 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2117 | * the associated object. The second is a string buffer. The third is |
| 2118 | * a facultative integer that represents where is the buffer position |
| 2119 | * of the start of the data that can send. The first byte is the |
| 2120 | * position "1". The default value is "1". The fourth argument is a |
| 2121 | * facultative integer that represents where is the buffer position |
| 2122 | * of the end of the data that can send. The default is the last byte. |
| 2123 | */ |
| 2124 | static int hlua_socket_send(struct lua_State *L) |
| 2125 | { |
| 2126 | int i; |
| 2127 | int j; |
| 2128 | const char *buf; |
| 2129 | size_t buf_len; |
| 2130 | |
| 2131 | /* Check number of arguments. */ |
| 2132 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2133 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2134 | |
| 2135 | /* Get the string. */ |
| 2136 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2137 | |
| 2138 | /* Get and check j. */ |
| 2139 | if (lua_gettop(L) == 4) { |
| 2140 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2141 | if (j < 0) |
| 2142 | j = buf_len + j + 1; |
| 2143 | if (j > buf_len) |
| 2144 | j = buf_len + 1; |
| 2145 | lua_pop(L, 1); |
| 2146 | } |
| 2147 | else |
| 2148 | j = buf_len; |
| 2149 | |
| 2150 | /* Get and check i. */ |
| 2151 | if (lua_gettop(L) == 3) { |
| 2152 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2153 | if (i < 0) |
| 2154 | i = buf_len + i + 1; |
| 2155 | if (i > buf_len) |
| 2156 | i = buf_len + 1; |
| 2157 | lua_pop(L, 1); |
| 2158 | } else |
| 2159 | i = 1; |
| 2160 | |
| 2161 | /* Check bth i and j. */ |
| 2162 | if (i > j) { |
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 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2167 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | return 1; |
| 2169 | } |
| 2170 | if (i == 0) |
| 2171 | i = 1; |
| 2172 | if (j == 0) |
| 2173 | j = 1; |
| 2174 | |
| 2175 | /* Pop the string. */ |
| 2176 | lua_pop(L, 1); |
| 2177 | |
| 2178 | /* Update the buffer length. */ |
| 2179 | buf += i - 1; |
| 2180 | buf_len = j - i + 1; |
| 2181 | lua_pushlstring(L, buf, buf_len); |
| 2182 | |
| 2183 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2184 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2186 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2187 | } |
| 2188 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2189 | #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] | 2190 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2191 | { |
| 2192 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2193 | int ret; |
| 2194 | int len; |
| 2195 | char *p; |
| 2196 | |
| 2197 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2198 | if (ret <= 0) { |
| 2199 | lua_pushnil(L); |
| 2200 | return 1; |
| 2201 | } |
| 2202 | |
| 2203 | if (ret == AF_UNIX) { |
| 2204 | lua_pushstring(L, buffer+1); |
| 2205 | return 1; |
| 2206 | } |
| 2207 | else if (ret == AF_INET6) { |
| 2208 | buffer[0] = '['; |
| 2209 | len = strlen(buffer); |
| 2210 | buffer[len] = ']'; |
| 2211 | len++; |
| 2212 | buffer[len] = ':'; |
| 2213 | len++; |
| 2214 | p = buffer; |
| 2215 | } |
| 2216 | else if (ret == AF_INET) { |
| 2217 | p = buffer + 1; |
| 2218 | len = strlen(p); |
| 2219 | p[len] = ':'; |
| 2220 | len++; |
| 2221 | } |
| 2222 | else { |
| 2223 | lua_pushnil(L); |
| 2224 | return 1; |
| 2225 | } |
| 2226 | |
| 2227 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2228 | lua_pushnil(L); |
| 2229 | return 1; |
| 2230 | } |
| 2231 | |
| 2232 | lua_pushstring(L, p); |
| 2233 | return 1; |
| 2234 | } |
| 2235 | |
| 2236 | /* Returns information about the peer of the connection. */ |
| 2237 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2238 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2239 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2240 | struct xref *peer; |
| 2241 | struct appctx *appctx; |
| 2242 | struct stream_interface *si; |
| 2243 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2244 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2245 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2247 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2248 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2250 | /* Check if we run on the same thread than the xreator thread. |
| 2251 | * We cannot access to the socket if the thread is different. |
| 2252 | */ |
| 2253 | if (socket->tid != tid) |
| 2254 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2255 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2256 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2257 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2258 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2259 | lua_pushnil(L); |
| 2260 | return 1; |
| 2261 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2262 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2263 | si = appctx->owner; |
| 2264 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2266 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2267 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2268 | lua_pushnil(L); |
| 2269 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2272 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2273 | xref_unlock(&socket->xref, peer); |
| 2274 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | /* Returns information about my connection side. */ |
| 2278 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2279 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2280 | struct hlua_socket *socket; |
| 2281 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2282 | struct appctx *appctx; |
| 2283 | struct xref *peer; |
| 2284 | struct stream_interface *si; |
| 2285 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2286 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2287 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2289 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2290 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2292 | /* Check if we run on the same thread than the xreator thread. |
| 2293 | * We cannot access to the socket if the thread is different. |
| 2294 | */ |
| 2295 | if (socket->tid != tid) |
| 2296 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2297 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2298 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2299 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2300 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | lua_pushnil(L); |
| 2302 | return 1; |
| 2303 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2304 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2305 | si = appctx->owner; |
| 2306 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2308 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2309 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2310 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2311 | lua_pushnil(L); |
| 2312 | return 1; |
| 2313 | } |
| 2314 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2315 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2316 | xref_unlock(&socket->xref, peer); |
| 2317 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2321 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2322 | .obj_type = OBJ_TYPE_APPLET, |
| 2323 | .name = "<LUA_TCP>", |
| 2324 | .fct = hlua_socket_handler, |
| 2325 | .release = hlua_socket_release, |
| 2326 | }; |
| 2327 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2328 | __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] | 2329 | { |
| 2330 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2331 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2332 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2333 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2334 | struct stream_interface *si; |
| 2335 | struct stream *s; |
| 2336 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2337 | /* Check if we run on the same thread than the xreator thread. |
| 2338 | * We cannot access to the socket if the thread is different. |
| 2339 | */ |
| 2340 | if (socket->tid != tid) |
| 2341 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2342 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2343 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2344 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2345 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2346 | lua_pushnil(L); |
| 2347 | lua_pushstring(L, "Can't connect"); |
| 2348 | return 2; |
| 2349 | } |
| 2350 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2351 | si = appctx->owner; |
| 2352 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2353 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2354 | /* Check if we run on the same thread than the xreator thread. |
| 2355 | * We cannot access to the socket if the thread is different. |
| 2356 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2357 | if (socket->tid != tid) { |
| 2358 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2359 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2361 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2362 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2363 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2364 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | lua_pushnil(L); |
| 2366 | lua_pushstring(L, "Can't connect"); |
| 2367 | return 2; |
| 2368 | } |
| 2369 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2370 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | |
| 2372 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2373 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2374 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | lua_pushinteger(L, 1); |
| 2376 | return 1; |
| 2377 | } |
| 2378 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2379 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2380 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2381 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2382 | } |
| 2383 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2384 | 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] | 2385 | return 0; |
| 2386 | } |
| 2387 | |
| 2388 | /* This function fail or initite the connection. */ |
| 2389 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2390 | { |
| 2391 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2392 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2394 | struct hlua *hlua; |
| 2395 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2396 | int low, high; |
| 2397 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2398 | struct xref *peer; |
| 2399 | struct stream_interface *si; |
| 2400 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2401 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2402 | if (lua_gettop(L) < 2) |
| 2403 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2404 | |
| 2405 | /* Get args. */ |
| 2406 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2407 | |
| 2408 | /* Check if we run on the same thread than the xreator thread. |
| 2409 | * We cannot access to the socket if the thread is different. |
| 2410 | */ |
| 2411 | if (socket->tid != tid) |
| 2412 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2413 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2415 | if (lua_gettop(L) >= 3) { |
| 2416 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2417 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2419 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2420 | * that are not enclosed within square brackets. |
| 2421 | */ |
| 2422 | if (port > 0) { |
| 2423 | luaL_buffinit(L, &b); |
| 2424 | luaL_addstring(&b, ip); |
| 2425 | luaL_addchar(&b, ':'); |
| 2426 | luaL_pushresult(&b); |
| 2427 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2428 | } |
| 2429 | } |
| 2430 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2431 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2432 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2433 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | lua_pushnil(L); |
| 2435 | return 1; |
| 2436 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2437 | |
| 2438 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2439 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2440 | if (!addr) { |
| 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: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | } |
| 2444 | if (low != high) { |
| 2445 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2446 | 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] | 2447 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2448 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2450 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2451 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2452 | if (port == -1) { |
| 2453 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2454 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2455 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2456 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2457 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2458 | if (port == -1) { |
| 2459 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2460 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2461 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2462 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2463 | } |
| 2464 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2465 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2466 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2467 | si = appctx->owner; |
| 2468 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2469 | |
| 2470 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2471 | xref_unlock(&socket->xref, peer); |
| 2472 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2473 | } |
| 2474 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2475 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2477 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2478 | |
| 2479 | /* inform the stream that we want to be notified whenever the |
| 2480 | * connection completes. |
| 2481 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2482 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2483 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2484 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2485 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2486 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2487 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2488 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2489 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2490 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2491 | } |
| 2492 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2493 | |
| 2494 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2495 | /* Return yield waiting for connection. */ |
| 2496 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2497 | 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] | 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2502 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2503 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2504 | { |
| 2505 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2506 | struct xref *peer; |
| 2507 | struct appctx *appctx; |
| 2508 | struct stream_interface *si; |
| 2509 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2510 | |
| 2511 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2512 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2513 | |
| 2514 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2515 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2516 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2517 | lua_pushnil(L); |
| 2518 | return 1; |
| 2519 | } |
| 2520 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2521 | si = appctx->owner; |
| 2522 | s = si_strm(si); |
| 2523 | |
| 2524 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2525 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2526 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2527 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2528 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2529 | |
| 2530 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2531 | { |
| 2532 | return 0; |
| 2533 | } |
| 2534 | |
| 2535 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2536 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2537 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2538 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2539 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2540 | struct xref *peer; |
| 2541 | struct appctx *appctx; |
| 2542 | struct stream_interface *si; |
| 2543 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2544 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2545 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2546 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2547 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2548 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2549 | /* convert the timeout to millis */ |
| 2550 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2551 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2552 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2553 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2554 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2555 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2556 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2557 | 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] | 2558 | |
| 2559 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2560 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2561 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2562 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2563 | /* Check if we run on the same thread than the xreator thread. |
| 2564 | * We cannot access to the socket if the thread is different. |
| 2565 | */ |
| 2566 | if (socket->tid != tid) |
| 2567 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2568 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2569 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2570 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2572 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2573 | WILL_LJMP(lua_error(L)); |
| 2574 | return 0; |
| 2575 | } |
| 2576 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2577 | si = appctx->owner; |
| 2578 | s = si_strm(si); |
| 2579 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2580 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2581 | s->req.rto = tmout; |
| 2582 | s->req.wto = tmout; |
| 2583 | s->res.rto = tmout; |
| 2584 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2585 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2586 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2587 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2588 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2589 | |
| 2590 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2591 | task_queue(s->task); |
| 2592 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2593 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2594 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2595 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2596 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2600 | { |
| 2601 | struct hlua_socket *socket; |
| 2602 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2603 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2604 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2605 | |
| 2606 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2607 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2608 | hlua_pusherror(L, "socket: full stack"); |
| 2609 | goto out_fail_conf; |
| 2610 | } |
| 2611 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2612 | /* Create the object: obj[0] = userdata. */ |
| 2613 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2615 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2617 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2619 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2620 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2621 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2622 | goto out_fail_conf; |
| 2623 | } |
| 2624 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2625 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2626 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2627 | lua_setmetatable(L, -2); |
| 2628 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2629 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2630 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2631 | if (!appctx) { |
| 2632 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2633 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2634 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2636 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2637 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2638 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2639 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2640 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2641 | /* Now create a session, task and stream for this applet */ |
| 2642 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2643 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2645 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2646 | } |
| 2647 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2648 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2649 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2651 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | } |
| 2653 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2654 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2655 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | /* Configure "right" stream interface. this "si" is used to connect |
| 2658 | * and retrieve data from the server. The connection is initialized |
| 2659 | * with the "struct server". |
| 2660 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2661 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | |
| 2663 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2664 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2665 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | return 1; |
| 2668 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2669 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2670 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2671 | out_fail_sess: |
| 2672 | appctx_free(appctx); |
| 2673 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | WILL_LJMP(lua_error(L)); |
| 2675 | return 0; |
| 2676 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2677 | |
| 2678 | /* |
| 2679 | * |
| 2680 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2681 | * Class Channel |
| 2682 | * |
| 2683 | * |
| 2684 | */ |
| 2685 | |
| 2686 | /* Returns the struct hlua_channel join to the class channel in the |
| 2687 | * stack entry "ud" or throws an argument error. |
| 2688 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2689 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2690 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2691 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2692 | } |
| 2693 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2694 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2695 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2696 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2697 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2698 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2699 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2700 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2701 | return 0; |
| 2702 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2703 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2704 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2705 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2706 | |
| 2707 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2708 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2709 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2710 | return 1; |
| 2711 | } |
| 2712 | |
| 2713 | /* Duplicate all the data present in the input channel and put it |
| 2714 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2715 | * the stack if the channel is closed and all the data are consumed, |
| 2716 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2717 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2718 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2719 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2720 | { |
| 2721 | char *blk1; |
| 2722 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2723 | size_t len1; |
| 2724 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2725 | int ret; |
| 2726 | luaL_Buffer b; |
| 2727 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2728 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2729 | if (unlikely(ret == 0)) |
| 2730 | return 0; |
| 2731 | |
| 2732 | if (unlikely(ret < 0)) { |
| 2733 | lua_pushnil(L); |
| 2734 | return -1; |
| 2735 | } |
| 2736 | |
| 2737 | luaL_buffinit(L, &b); |
| 2738 | luaL_addlstring(&b, blk1, len1); |
| 2739 | if (unlikely(ret == 2)) |
| 2740 | luaL_addlstring(&b, blk2, len2); |
| 2741 | luaL_pushresult(&b); |
| 2742 | |
| 2743 | if (unlikely(ret == 2)) |
| 2744 | return len1 + len2; |
| 2745 | return len1; |
| 2746 | } |
| 2747 | |
| 2748 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2749 | * a yield. This function keep the data in the buffer. |
| 2750 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2751 | __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] | 2752 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2753 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2754 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2755 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2756 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2757 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2758 | WILL_LJMP(lua_error(L)); |
| 2759 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2760 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2761 | 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] | 2762 | return 1; |
| 2763 | } |
| 2764 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2765 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2766 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2767 | { |
| 2768 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2769 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2770 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2771 | } |
| 2772 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2773 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2774 | * a yield. This function consumes the data in the buffer. It returns |
| 2775 | * a string containing the data or a nil pointer if no data are available |
| 2776 | * and the channel is closed. |
| 2777 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2778 | __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] | 2779 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2780 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2781 | int ret; |
| 2782 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2783 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2784 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2785 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2786 | WILL_LJMP(lua_error(L)); |
| 2787 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2788 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2789 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2790 | 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] | 2791 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | if (unlikely(ret == -1)) |
| 2793 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2794 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2795 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | return 1; |
| 2797 | } |
| 2798 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2799 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2800 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2801 | { |
| 2802 | MAY_LJMP(check_args(L, 1, "get")); |
| 2803 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2804 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2805 | } |
| 2806 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2807 | /* This functions consumes and returns one line. If the channel is closed, |
| 2808 | * 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] | 2809 | * 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] | 2810 | * value. |
| 2811 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2812 | __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] | 2813 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2814 | char *blk1; |
| 2815 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2816 | size_t len1; |
| 2817 | size_t len2; |
| 2818 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2819 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2820 | int ret; |
| 2821 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2822 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2823 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2824 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2825 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2826 | WILL_LJMP(lua_error(L)); |
| 2827 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2828 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2829 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2830 | 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] | 2831 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2832 | if (ret == -1) { |
| 2833 | lua_pushnil(L); |
| 2834 | return 1; |
| 2835 | } |
| 2836 | |
| 2837 | luaL_buffinit(L, &b); |
| 2838 | luaL_addlstring(&b, blk1, len1); |
| 2839 | len = len1; |
| 2840 | if (unlikely(ret == 2)) { |
| 2841 | luaL_addlstring(&b, blk2, len2); |
| 2842 | len += len2; |
| 2843 | } |
| 2844 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2845 | 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] | 2846 | return 1; |
| 2847 | } |
| 2848 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2849 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2850 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2851 | { |
| 2852 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2853 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2854 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2855 | } |
| 2856 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2857 | /* This function takes a string as input, and append it at the |
| 2858 | * input side of channel. If the data is too big, but a space |
| 2859 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2860 | * yields. If the data is bigger than the buffer, or if the |
| 2861 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2862 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2864 | __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] | 2865 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2866 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2867 | size_t len; |
| 2868 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2869 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2870 | int ret; |
| 2871 | int max; |
| 2872 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2873 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2874 | WILL_LJMP(lua_error(L)); |
| 2875 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2876 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2877 | * the request buffer if its not required. |
| 2878 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2879 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2880 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2881 | 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] | 2882 | } |
| 2883 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2884 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2885 | if (max > len - l) |
| 2886 | max = len - l; |
| 2887 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2888 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2889 | if (ret == -2 || ret == -3) { |
| 2890 | lua_pushinteger(L, -1); |
| 2891 | return 1; |
| 2892 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2893 | if (ret == -1) { |
| 2894 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2895 | 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] | 2896 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | l += ret; |
| 2898 | lua_pop(L, 1); |
| 2899 | lua_pushinteger(L, l); |
| 2900 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2901 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2902 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2903 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2904 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2905 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2906 | */ |
| 2907 | return 1; |
| 2908 | } |
| 2909 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2910 | 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] | 2911 | return 1; |
| 2912 | } |
| 2913 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2914 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2915 | * 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] | 2916 | * buffer size is too little for the data. |
| 2917 | */ |
| 2918 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2919 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2920 | size_t len; |
| 2921 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2923 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2924 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2925 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2926 | lua_pushinteger(L, 0); |
| 2927 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2928 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2929 | } |
| 2930 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2931 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2933 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | * or -1 if the channel is closed or if the buffer size is too |
| 2935 | * little for the data. |
| 2936 | */ |
| 2937 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2938 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2939 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2940 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2941 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2942 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2943 | lua_pushinteger(L, 0); |
| 2944 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2945 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2946 | WILL_LJMP(lua_error(L)); |
| 2947 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2948 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2950 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2951 | } |
| 2952 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2953 | /* Append data in the output side of the buffer. This data is immediately |
| 2954 | * sent. The function returns the amount of data written. If the buffer |
| 2955 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2956 | * if the channel is closed. |
| 2957 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2958 | __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] | 2959 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2960 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2961 | size_t len; |
| 2962 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2963 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2964 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2965 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2966 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2967 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2968 | WILL_LJMP(lua_error(L)); |
| 2969 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2970 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2971 | lua_pushinteger(L, -1); |
| 2972 | return 1; |
| 2973 | } |
| 2974 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2975 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2976 | * the request buffer if its not required. |
| 2977 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2978 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2979 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2980 | 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] | 2981 | } |
| 2982 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2983 | /* The written data will be immediately sent, so we can check |
| 2984 | * the available space without taking in account the reserve. |
| 2985 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2986 | * data, because the buffer will be flushed. |
| 2987 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2988 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2989 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2990 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2991 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2992 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2993 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2994 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2995 | return 1; |
| 2996 | |
| 2997 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | if (max > len - l) |
| 2999 | max = len - l; |
| 3000 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3001 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3002 | * detects a non contiguous buffer and realign it. |
| 3003 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3004 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3005 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3006 | |
| 3007 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3008 | 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] | 3009 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | /* buffer replace considers that the input part is filled. |
| 3011 | * so, I must forward these new data in the output part. |
| 3012 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3013 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | |
| 3015 | l += max; |
| 3016 | lua_pop(L, 1); |
| 3017 | lua_pushinteger(L, l); |
| 3018 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3019 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3020 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3021 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3022 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3023 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3024 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3026 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3027 | if (l < len) { |
| 3028 | /* If we are waiting for space in the response buffer, we |
| 3029 | * must set the flag WAKERESWR. This flag required the task |
| 3030 | * wake up if any activity is detected on the response buffer. |
| 3031 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3032 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3033 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3034 | else |
| 3035 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3036 | 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] | 3037 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3038 | |
| 3039 | return 1; |
| 3040 | } |
| 3041 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3042 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3043 | * yield the LUA process, and resume it without checking the |
| 3044 | * input arguments. |
| 3045 | */ |
| 3046 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3047 | { |
| 3048 | MAY_LJMP(check_args(L, 2, "send")); |
| 3049 | lua_pushinteger(L, 0); |
| 3050 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3051 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | /* This function forward and amount of butes. The data pass from |
| 3055 | * the input side of the buffer to the output side, and can be |
| 3056 | * forwarded. This function never fails. |
| 3057 | * |
| 3058 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3059 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3061 | __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] | 3062 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3063 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3064 | int len; |
| 3065 | int l; |
| 3066 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3067 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | |
| 3069 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3070 | |
| 3071 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3072 | WILL_LJMP(lua_error(L)); |
| 3073 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3075 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3076 | |
| 3077 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3078 | if (max > ci_data(chn)) |
| 3079 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3080 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3081 | l += max; |
| 3082 | |
| 3083 | lua_pop(L, 1); |
| 3084 | lua_pushinteger(L, l); |
| 3085 | |
| 3086 | /* Check if it miss bytes to forward. */ |
| 3087 | if (l < len) { |
| 3088 | /* The the input channel or the output channel are closed, we |
| 3089 | * must return the amount of data forwarded. |
| 3090 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3091 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3092 | return 1; |
| 3093 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3094 | /* If we are waiting for space data in the response buffer, we |
| 3095 | * must set the flag WAKERESWR. This flag required the task |
| 3096 | * wake up if any activity is detected on the response buffer. |
| 3097 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3098 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3099 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3100 | else |
| 3101 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3102 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3104 | 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] | 3105 | } |
| 3106 | |
| 3107 | return 1; |
| 3108 | } |
| 3109 | |
| 3110 | /* Just check the input and prepare the stack for the previous |
| 3111 | * function "hlua_channel_forward_yield" |
| 3112 | */ |
| 3113 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3114 | { |
| 3115 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3116 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3117 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3118 | |
| 3119 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3120 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | /* Just returns the number of bytes available in the input |
| 3124 | * side of the buffer. This function never fails. |
| 3125 | */ |
| 3126 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3127 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3128 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3129 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3130 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3131 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3132 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3133 | struct htx *htx = htxbuf(&chn->buf); |
| 3134 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3135 | } |
| 3136 | else |
| 3137 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3138 | return 1; |
| 3139 | } |
| 3140 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3141 | /* Returns true if the channel is full. */ |
| 3142 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3143 | { |
| 3144 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3145 | |
| 3146 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3147 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3148 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3149 | * applet). |
| 3150 | */ |
| 3151 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3152 | return 1; |
| 3153 | } |
| 3154 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3155 | /* Returns true if the channel is the response channel. */ |
| 3156 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3157 | { |
| 3158 | struct channel *chn; |
| 3159 | |
| 3160 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3161 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3162 | |
| 3163 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3164 | return 1; |
| 3165 | } |
| 3166 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3167 | /* Just returns the number of bytes available in the output |
| 3168 | * side of the buffer. This function never fails. |
| 3169 | */ |
| 3170 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3171 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3172 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3173 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3175 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3176 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3177 | return 1; |
| 3178 | } |
| 3179 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3180 | /* |
| 3181 | * |
| 3182 | * |
| 3183 | * Class Fetches |
| 3184 | * |
| 3185 | * |
| 3186 | */ |
| 3187 | |
| 3188 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3189 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3190 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3191 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3192 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3193 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /* This function creates and push in the stack a fetch object according |
| 3197 | * with a current TXN. |
| 3198 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3199 | 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] | 3200 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3201 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3202 | |
| 3203 | /* Check stack size. */ |
| 3204 | if (!lua_checkstack(L, 3)) |
| 3205 | return 0; |
| 3206 | |
| 3207 | /* Create the object: obj[0] = userdata. |
| 3208 | * Note that the base of the Fetches object is the |
| 3209 | * transaction object. |
| 3210 | */ |
| 3211 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3212 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3213 | lua_rawseti(L, -2, 0); |
| 3214 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3215 | hsmp->s = txn->s; |
| 3216 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3217 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3218 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3219 | |
| 3220 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3221 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3222 | lua_setmetatable(L, -2); |
| 3223 | |
| 3224 | return 1; |
| 3225 | } |
| 3226 | |
| 3227 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3228 | * It uses closure argument to store the associated sample-fetch. It |
| 3229 | * returns only one argument or throws an error. An error is thrown |
| 3230 | * only if an error is encountered during the argument parsing. If |
| 3231 | * the "sample-fetch" function fails, nil is returned. |
| 3232 | */ |
| 3233 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3234 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3235 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3236 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3237 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3238 | int i; |
| 3239 | struct sample smp; |
| 3240 | |
| 3241 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3242 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3243 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3244 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3245 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3246 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3247 | /* Check execution authorization. */ |
| 3248 | if (f->use & SMP_USE_HTTP_ANY && |
| 3249 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3250 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3251 | "is not available in Lua services", f->kw); |
| 3252 | WILL_LJMP(lua_error(L)); |
| 3253 | } |
| 3254 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3255 | /* Get extra arguments. */ |
| 3256 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3257 | if (i >= ARGM_NBARGS) |
| 3258 | break; |
| 3259 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3260 | } |
| 3261 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3262 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3263 | |
| 3264 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3265 | 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] | 3266 | |
| 3267 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3268 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3269 | lua_pushfstring(L, "error in arguments"); |
| 3270 | WILL_LJMP(lua_error(L)); |
| 3271 | } |
| 3272 | |
| 3273 | /* Initialise the sample. */ |
| 3274 | memset(&smp, 0, sizeof(smp)); |
| 3275 | |
| 3276 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3277 | 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] | 3278 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3279 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3280 | lua_pushstring(L, ""); |
| 3281 | else |
| 3282 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3283 | return 1; |
| 3284 | } |
| 3285 | |
| 3286 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3287 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3288 | hlua_smp2lua_str(L, &smp); |
| 3289 | else |
| 3290 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3291 | return 1; |
| 3292 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3293 | |
| 3294 | /* |
| 3295 | * |
| 3296 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3297 | * Class Converters |
| 3298 | * |
| 3299 | * |
| 3300 | */ |
| 3301 | |
| 3302 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3303 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3304 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3305 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3306 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3307 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | /* This function creates and push in the stack a Converters object |
| 3311 | * according with a current TXN. |
| 3312 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3313 | 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] | 3314 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3315 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3316 | |
| 3317 | /* Check stack size. */ |
| 3318 | if (!lua_checkstack(L, 3)) |
| 3319 | return 0; |
| 3320 | |
| 3321 | /* Create the object: obj[0] = userdata. |
| 3322 | * Note that the base of the Converters object is the |
| 3323 | * same than the TXN object. |
| 3324 | */ |
| 3325 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3326 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3327 | lua_rawseti(L, -2, 0); |
| 3328 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3329 | hsmp->s = txn->s; |
| 3330 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3331 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3332 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3333 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3334 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3335 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3336 | lua_setmetatable(L, -2); |
| 3337 | |
| 3338 | return 1; |
| 3339 | } |
| 3340 | |
| 3341 | /* This function is an LUA binding. It is called with each converter. |
| 3342 | * It uses closure argument to store the associated converter. It |
| 3343 | * returns only one argument or throws an error. An error is thrown |
| 3344 | * only if an error is encountered during the argument parsing. If |
| 3345 | * the converter function function fails, nil is returned. |
| 3346 | */ |
| 3347 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3348 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3349 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3350 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3351 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3352 | int i; |
| 3353 | struct sample smp; |
| 3354 | |
| 3355 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3356 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3357 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3358 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3359 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3360 | |
| 3361 | /* Get extra arguments. */ |
| 3362 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3363 | if (i >= ARGM_NBARGS) |
| 3364 | break; |
| 3365 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3366 | } |
| 3367 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3368 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3369 | |
| 3370 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3371 | 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] | 3372 | |
| 3373 | /* Run the special args checker. */ |
| 3374 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3375 | hlua_pusherror(L, "error in arguments"); |
| 3376 | WILL_LJMP(lua_error(L)); |
| 3377 | } |
| 3378 | |
| 3379 | /* Initialise the sample. */ |
| 3380 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3381 | hlua_pusherror(L, "error in the input argument"); |
| 3382 | WILL_LJMP(lua_error(L)); |
| 3383 | } |
| 3384 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3385 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3386 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3387 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3388 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3389 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3390 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3391 | WILL_LJMP(lua_error(L)); |
| 3392 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3393 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3394 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3395 | hlua_pusherror(L, "error during the input argument casting"); |
| 3396 | WILL_LJMP(lua_error(L)); |
| 3397 | } |
| 3398 | |
| 3399 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3400 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3401 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3402 | lua_pushstring(L, ""); |
| 3403 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3404 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3405 | return 1; |
| 3406 | } |
| 3407 | |
| 3408 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3409 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3410 | hlua_smp2lua_str(L, &smp); |
| 3411 | else |
| 3412 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3413 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3414 | } |
| 3415 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3416 | /* |
| 3417 | * |
| 3418 | * |
| 3419 | * Class AppletTCP |
| 3420 | * |
| 3421 | * |
| 3422 | */ |
| 3423 | |
| 3424 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3425 | * a class stream, otherwise it throws an error. |
| 3426 | */ |
| 3427 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3428 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3429 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | /* This function creates and push in the stack an Applet object |
| 3433 | * according with a current TXN. |
| 3434 | */ |
| 3435 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3436 | { |
| 3437 | struct hlua_appctx *appctx; |
| 3438 | struct stream_interface *si = ctx->owner; |
| 3439 | struct stream *s = si_strm(si); |
| 3440 | struct proxy *p = s->be; |
| 3441 | |
| 3442 | /* Check stack size. */ |
| 3443 | if (!lua_checkstack(L, 3)) |
| 3444 | return 0; |
| 3445 | |
| 3446 | /* Create the object: obj[0] = userdata. |
| 3447 | * Note that the base of the Converters object is the |
| 3448 | * same than the TXN object. |
| 3449 | */ |
| 3450 | lua_newtable(L); |
| 3451 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3452 | lua_rawseti(L, -2, 0); |
| 3453 | appctx->appctx = ctx; |
| 3454 | appctx->htxn.s = s; |
| 3455 | appctx->htxn.p = p; |
| 3456 | |
| 3457 | /* Create the "f" field that contains a list of fetches. */ |
| 3458 | lua_pushstring(L, "f"); |
| 3459 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3460 | return 0; |
| 3461 | lua_settable(L, -3); |
| 3462 | |
| 3463 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3464 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3465 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3466 | return 0; |
| 3467 | lua_settable(L, -3); |
| 3468 | |
| 3469 | /* Create the "c" field that contains a list of converters. */ |
| 3470 | lua_pushstring(L, "c"); |
| 3471 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3472 | return 0; |
| 3473 | lua_settable(L, -3); |
| 3474 | |
| 3475 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3476 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3477 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3478 | return 0; |
| 3479 | lua_settable(L, -3); |
| 3480 | |
| 3481 | /* Pop a class stream metatable and affect it to the table. */ |
| 3482 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3483 | lua_setmetatable(L, -2); |
| 3484 | |
| 3485 | return 1; |
| 3486 | } |
| 3487 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3488 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3489 | { |
| 3490 | struct hlua_appctx *appctx; |
| 3491 | struct stream *s; |
| 3492 | const char *name; |
| 3493 | size_t len; |
| 3494 | struct sample smp; |
| 3495 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3496 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3497 | 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] | 3498 | |
| 3499 | /* It is useles to retrieve the stream, but this function |
| 3500 | * runs only in a stream context. |
| 3501 | */ |
| 3502 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3503 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3504 | s = appctx->htxn.s; |
| 3505 | |
| 3506 | /* Converts the third argument in a sample. */ |
| 3507 | hlua_lua2smp(L, 3, &smp); |
| 3508 | |
| 3509 | /* Store the sample in a variable. */ |
| 3510 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3511 | |
| 3512 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3513 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3514 | else |
| 3515 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3516 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3517 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3521 | { |
| 3522 | struct hlua_appctx *appctx; |
| 3523 | struct stream *s; |
| 3524 | const char *name; |
| 3525 | size_t len; |
| 3526 | struct sample smp; |
| 3527 | |
| 3528 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3529 | |
| 3530 | /* It is useles to retrieve the stream, but this function |
| 3531 | * runs only in a stream context. |
| 3532 | */ |
| 3533 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3534 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3535 | s = appctx->htxn.s; |
| 3536 | |
| 3537 | /* Unset the variable. */ |
| 3538 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3539 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3540 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3544 | { |
| 3545 | struct hlua_appctx *appctx; |
| 3546 | struct stream *s; |
| 3547 | const char *name; |
| 3548 | size_t len; |
| 3549 | struct sample smp; |
| 3550 | |
| 3551 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3552 | |
| 3553 | /* It is useles to retrieve the stream, but this function |
| 3554 | * runs only in a stream context. |
| 3555 | */ |
| 3556 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3557 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3558 | s = appctx->htxn.s; |
| 3559 | |
| 3560 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3561 | if (!vars_get_by_name(name, len, &smp)) { |
| 3562 | lua_pushnil(L); |
| 3563 | return 1; |
| 3564 | } |
| 3565 | |
| 3566 | return hlua_smp2lua(L, &smp); |
| 3567 | } |
| 3568 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3569 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3570 | { |
| 3571 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3572 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3573 | struct hlua *hlua; |
| 3574 | |
| 3575 | /* 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] | 3576 | if (!s->hlua) |
| 3577 | return 0; |
| 3578 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3579 | |
| 3580 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3581 | |
| 3582 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3583 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3584 | |
| 3585 | /* Get and store new value. */ |
| 3586 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3587 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3588 | |
| 3589 | return 0; |
| 3590 | } |
| 3591 | |
| 3592 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3593 | { |
| 3594 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3595 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3596 | struct hlua *hlua; |
| 3597 | |
| 3598 | /* 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] | 3599 | if (!s->hlua) { |
| 3600 | lua_pushnil(L); |
| 3601 | return 1; |
| 3602 | } |
| 3603 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3604 | |
| 3605 | /* Push configuration index in the stack. */ |
| 3606 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3607 | |
| 3608 | return 1; |
| 3609 | } |
| 3610 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3611 | /* If expected data not yet available, it returns a yield. This function |
| 3612 | * consumes the data in the buffer. It returns a string containing the |
| 3613 | * data. This string can be empty. |
| 3614 | */ |
| 3615 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3616 | { |
| 3617 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3618 | struct stream_interface *si = appctx->appctx->owner; |
| 3619 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3620 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3621 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3622 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3623 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3624 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3625 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3626 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3627 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3628 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3629 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3630 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3631 | 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] | 3632 | } |
| 3633 | |
| 3634 | /* End of data: commit the total strings and return. */ |
| 3635 | if (ret < 0) { |
| 3636 | luaL_pushresult(&appctx->b); |
| 3637 | return 1; |
| 3638 | } |
| 3639 | |
| 3640 | /* Ensure that the block 2 length is usable. */ |
| 3641 | if (ret == 1) |
| 3642 | len2 = 0; |
| 3643 | |
| 3644 | /* dont check the max length read and dont check. */ |
| 3645 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3646 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3647 | |
| 3648 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3649 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3650 | luaL_pushresult(&appctx->b); |
| 3651 | return 1; |
| 3652 | } |
| 3653 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3654 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3655 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3656 | { |
| 3657 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3658 | |
| 3659 | /* Initialise the string catenation. */ |
| 3660 | luaL_buffinit(L, &appctx->b); |
| 3661 | |
| 3662 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3663 | } |
| 3664 | |
| 3665 | /* If expected data not yet available, it returns a yield. This function |
| 3666 | * consumes the data in the buffer. It returns a string containing the |
| 3667 | * data. This string can be empty. |
| 3668 | */ |
| 3669 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3670 | { |
| 3671 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3672 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3673 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3674 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3675 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3676 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3677 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3678 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3679 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3680 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3681 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3682 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3683 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3684 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3685 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3686 | 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] | 3687 | } |
| 3688 | |
| 3689 | /* End of data: commit the total strings and return. */ |
| 3690 | if (ret < 0) { |
| 3691 | luaL_pushresult(&appctx->b); |
| 3692 | return 1; |
| 3693 | } |
| 3694 | |
| 3695 | /* Ensure that the block 2 length is usable. */ |
| 3696 | if (ret == 1) |
| 3697 | len2 = 0; |
| 3698 | |
| 3699 | if (len == -1) { |
| 3700 | |
| 3701 | /* If len == -1, catenate all the data avalaile and |
| 3702 | * yield because we want to get all the data until |
| 3703 | * the end of data stream. |
| 3704 | */ |
| 3705 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3706 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3707 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3708 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3709 | 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] | 3710 | |
| 3711 | } else { |
| 3712 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3713 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3714 | if (len1 > len) |
| 3715 | len1 = len; |
| 3716 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3717 | len -= len1; |
| 3718 | |
| 3719 | /* Copy the second block. */ |
| 3720 | if (len2 > len) |
| 3721 | len2 = len; |
| 3722 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3723 | len -= len2; |
| 3724 | |
| 3725 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3726 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3727 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3728 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3729 | if (len > 0) { |
| 3730 | lua_pushinteger(L, len); |
| 3731 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3732 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3733 | 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] | 3734 | } |
| 3735 | |
| 3736 | /* return the result. */ |
| 3737 | luaL_pushresult(&appctx->b); |
| 3738 | return 1; |
| 3739 | } |
| 3740 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3741 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3742 | hlua_pusherror(L, "Lua: internal error"); |
| 3743 | WILL_LJMP(lua_error(L)); |
| 3744 | return 0; |
| 3745 | } |
| 3746 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3747 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3748 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3749 | { |
| 3750 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3751 | int len = -1; |
| 3752 | |
| 3753 | if (lua_gettop(L) > 2) |
| 3754 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3755 | if (lua_gettop(L) >= 2) { |
| 3756 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3757 | lua_pop(L, 1); |
| 3758 | } |
| 3759 | |
| 3760 | /* Confirm or set the required length */ |
| 3761 | lua_pushinteger(L, len); |
| 3762 | |
| 3763 | /* Initialise the string catenation. */ |
| 3764 | luaL_buffinit(L, &appctx->b); |
| 3765 | |
| 3766 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3767 | } |
| 3768 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3769 | /* Append data in the output side of the buffer. This data is immediately |
| 3770 | * sent. The function returns the amount of data written. If the buffer |
| 3771 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3772 | * if the channel is closed. |
| 3773 | */ |
| 3774 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3775 | { |
| 3776 | size_t len; |
| 3777 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3778 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3779 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3780 | struct stream_interface *si = appctx->appctx->owner; |
| 3781 | struct channel *chn = si_ic(si); |
| 3782 | int max; |
| 3783 | |
| 3784 | /* Get the max amount of data which can write as input in the channel. */ |
| 3785 | max = channel_recv_max(chn); |
| 3786 | if (max > (len - l)) |
| 3787 | max = len - l; |
| 3788 | |
| 3789 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3790 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3791 | |
| 3792 | /* update counters. */ |
| 3793 | l += max; |
| 3794 | lua_pop(L, 1); |
| 3795 | lua_pushinteger(L, l); |
| 3796 | |
| 3797 | /* If some data is not send, declares the situation to the |
| 3798 | * applet, and returns a yield. |
| 3799 | */ |
| 3800 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3801 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3802 | 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] | 3803 | } |
| 3804 | |
| 3805 | return 1; |
| 3806 | } |
| 3807 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3808 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3809 | * yield the LUA process, and resume it without checking the |
| 3810 | * input arguments. |
| 3811 | */ |
| 3812 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3813 | { |
| 3814 | MAY_LJMP(check_args(L, 2, "send")); |
| 3815 | lua_pushinteger(L, 0); |
| 3816 | |
| 3817 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3818 | } |
| 3819 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3820 | /* |
| 3821 | * |
| 3822 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3823 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3824 | * |
| 3825 | * |
| 3826 | */ |
| 3827 | |
| 3828 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3829 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3830 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3831 | __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] | 3832 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3833 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3834 | } |
| 3835 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3836 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3837 | * according with a current TXN. |
| 3838 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3839 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3840 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3841 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3842 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3843 | struct stream_interface *si = ctx->owner; |
| 3844 | struct stream *s = si_strm(si); |
| 3845 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3846 | struct htx *htx; |
| 3847 | struct htx_blk *blk; |
| 3848 | struct htx_sl *sl; |
| 3849 | struct ist path; |
| 3850 | unsigned long long len = 0; |
| 3851 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3852 | |
| 3853 | /* Check stack size. */ |
| 3854 | if (!lua_checkstack(L, 3)) |
| 3855 | return 0; |
| 3856 | |
| 3857 | /* Create the object: obj[0] = userdata. |
| 3858 | * Note that the base of the Converters object is the |
| 3859 | * same than the TXN object. |
| 3860 | */ |
| 3861 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3862 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3863 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3864 | appctx->appctx = ctx; |
| 3865 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3866 | 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] | 3867 | appctx->htxn.s = s; |
| 3868 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3869 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3870 | /* Create the "f" field that contains a list of fetches. */ |
| 3871 | lua_pushstring(L, "f"); |
| 3872 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3873 | return 0; |
| 3874 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3875 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3876 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3877 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3878 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3879 | return 0; |
| 3880 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3881 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3882 | /* Create the "c" field that contains a list of converters. */ |
| 3883 | lua_pushstring(L, "c"); |
| 3884 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3885 | return 0; |
| 3886 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3887 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3888 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3889 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3890 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3891 | return 0; |
| 3892 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3893 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3894 | htx = htxbuf(&s->req.buf); |
| 3895 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3896 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3897 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3898 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3899 | /* Stores the request method. */ |
| 3900 | lua_pushstring(L, "method"); |
| 3901 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3902 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3903 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3904 | /* Stores the http version. */ |
| 3905 | lua_pushstring(L, "version"); |
| 3906 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3907 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3908 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3909 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3910 | * the array on the top of the stack. |
| 3911 | */ |
| 3912 | lua_pushstring(L, "headers"); |
| 3913 | htxn.s = s; |
| 3914 | htxn.p = px; |
| 3915 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 3916 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3917 | return 0; |
| 3918 | lua_settable(L, -3); |
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 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3921 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3922 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3923 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3924 | p = path.ptr; |
| 3925 | end = path.ptr + path.len; |
| 3926 | q = p; |
| 3927 | while (q < end && *q != '?') |
| 3928 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3929 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3930 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3931 | lua_pushstring(L, "path"); |
| 3932 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3933 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3934 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3935 | /* Stores the query string. */ |
| 3936 | lua_pushstring(L, "qs"); |
| 3937 | if (*q == '?') |
| 3938 | q++; |
| 3939 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3940 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3941 | } |
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 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3944 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3945 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3946 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3947 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3948 | break; |
| 3949 | if (type == HTX_BLK_DATA) |
| 3950 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3951 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3952 | if (htx->extra != ULLONG_MAX) |
| 3953 | len += htx->extra; |
| 3954 | |
| 3955 | /* Stores the request path. */ |
| 3956 | lua_pushstring(L, "length"); |
| 3957 | lua_pushinteger(L, len); |
| 3958 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3959 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3960 | /* Create an empty array of HTTP request headers. */ |
| 3961 | lua_pushstring(L, "response"); |
| 3962 | lua_newtable(L); |
| 3963 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3964 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3965 | /* Pop a class stream metatable and affect it to the table. */ |
| 3966 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3967 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3968 | |
| 3969 | return 1; |
| 3970 | } |
| 3971 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3972 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3973 | { |
| 3974 | struct hlua_appctx *appctx; |
| 3975 | struct stream *s; |
| 3976 | const char *name; |
| 3977 | size_t len; |
| 3978 | struct sample smp; |
| 3979 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3980 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3981 | 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] | 3982 | |
| 3983 | /* It is useles to retrieve the stream, but this function |
| 3984 | * runs only in a stream context. |
| 3985 | */ |
| 3986 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3987 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3988 | s = appctx->htxn.s; |
| 3989 | |
| 3990 | /* Converts the third argument in a sample. */ |
| 3991 | hlua_lua2smp(L, 3, &smp); |
| 3992 | |
| 3993 | /* Store the sample in a variable. */ |
| 3994 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3995 | |
| 3996 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3997 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3998 | else |
| 3999 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4000 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4001 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4005 | { |
| 4006 | struct hlua_appctx *appctx; |
| 4007 | struct stream *s; |
| 4008 | const char *name; |
| 4009 | size_t len; |
| 4010 | struct sample smp; |
| 4011 | |
| 4012 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4013 | |
| 4014 | /* It is useles to retrieve the stream, but this function |
| 4015 | * runs only in a stream context. |
| 4016 | */ |
| 4017 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4018 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4019 | s = appctx->htxn.s; |
| 4020 | |
| 4021 | /* Unset the variable. */ |
| 4022 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4023 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4024 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4028 | { |
| 4029 | struct hlua_appctx *appctx; |
| 4030 | struct stream *s; |
| 4031 | const char *name; |
| 4032 | size_t len; |
| 4033 | struct sample smp; |
| 4034 | |
| 4035 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4036 | |
| 4037 | /* It is useles to retrieve the stream, but this function |
| 4038 | * runs only in a stream context. |
| 4039 | */ |
| 4040 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4041 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4042 | s = appctx->htxn.s; |
| 4043 | |
| 4044 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4045 | if (!vars_get_by_name(name, len, &smp)) { |
| 4046 | lua_pushnil(L); |
| 4047 | return 1; |
| 4048 | } |
| 4049 | |
| 4050 | return hlua_smp2lua(L, &smp); |
| 4051 | } |
| 4052 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4053 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4054 | { |
| 4055 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4056 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4057 | struct hlua *hlua; |
| 4058 | |
| 4059 | /* 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] | 4060 | if (!s->hlua) |
| 4061 | return 0; |
| 4062 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4063 | |
| 4064 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4065 | |
| 4066 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4067 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4068 | |
| 4069 | /* Get and store new value. */ |
| 4070 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4071 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4072 | |
| 4073 | return 0; |
| 4074 | } |
| 4075 | |
| 4076 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4077 | { |
| 4078 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4079 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4080 | struct hlua *hlua; |
| 4081 | |
| 4082 | /* 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] | 4083 | if (!s->hlua) { |
| 4084 | lua_pushnil(L); |
| 4085 | return 1; |
| 4086 | } |
| 4087 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4088 | |
| 4089 | /* Push configuration index in the stack. */ |
| 4090 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4091 | |
| 4092 | return 1; |
| 4093 | } |
| 4094 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4095 | /* If expected data not yet available, it returns a yield. This function |
| 4096 | * consumes the data in the buffer. It returns a string containing the |
| 4097 | * data. This string can be empty. |
| 4098 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4099 | __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] | 4100 | { |
| 4101 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4102 | struct stream_interface *si = appctx->appctx->owner; |
| 4103 | struct channel *req = si_oc(si); |
| 4104 | struct htx *htx; |
| 4105 | struct htx_blk *blk; |
| 4106 | size_t count; |
| 4107 | int stop = 0; |
| 4108 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4109 | htx = htx_from_buf(&req->buf); |
| 4110 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4111 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4112 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4113 | while (count && !stop && blk) { |
| 4114 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4115 | uint32_t sz = htx_get_blksz(blk); |
| 4116 | struct ist v; |
| 4117 | uint32_t vlen; |
| 4118 | char *nl; |
| 4119 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4120 | if (type == HTX_BLK_EOM) { |
| 4121 | stop = 1; |
| 4122 | break; |
| 4123 | } |
| 4124 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4125 | vlen = sz; |
| 4126 | if (vlen > count) { |
| 4127 | if (type != HTX_BLK_DATA) |
| 4128 | break; |
| 4129 | vlen = count; |
| 4130 | } |
| 4131 | |
| 4132 | switch (type) { |
| 4133 | case HTX_BLK_UNUSED: |
| 4134 | break; |
| 4135 | |
| 4136 | case HTX_BLK_DATA: |
| 4137 | v = htx_get_blk_value(htx, blk); |
| 4138 | v.len = vlen; |
| 4139 | nl = istchr(v, '\n'); |
| 4140 | if (nl != NULL) { |
| 4141 | stop = 1; |
| 4142 | vlen = nl - v.ptr + 1; |
| 4143 | } |
| 4144 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4145 | break; |
| 4146 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4147 | case HTX_BLK_TLR: |
| 4148 | case HTX_BLK_EOM: |
| 4149 | stop = 1; |
| 4150 | break; |
| 4151 | |
| 4152 | default: |
| 4153 | break; |
| 4154 | } |
| 4155 | |
| 4156 | co_set_data(req, co_data(req) - vlen); |
| 4157 | count -= vlen; |
| 4158 | if (sz == vlen) |
| 4159 | blk = htx_remove_blk(htx, blk); |
| 4160 | else { |
| 4161 | htx_cut_data_blk(htx, blk, vlen); |
| 4162 | break; |
| 4163 | } |
| 4164 | } |
| 4165 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4166 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4167 | if (!stop) { |
| 4168 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4169 | 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] | 4170 | } |
| 4171 | |
| 4172 | /* return the result. */ |
| 4173 | luaL_pushresult(&appctx->b); |
| 4174 | return 1; |
| 4175 | } |
| 4176 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4177 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4178 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4179 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4180 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4181 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4182 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4183 | /* Initialise the string catenation. */ |
| 4184 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4185 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4186 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4187 | } |
| 4188 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4189 | /* If expected data not yet available, it returns a yield. This function |
| 4190 | * consumes the data in the buffer. It returns a string containing the |
| 4191 | * data. This string can be empty. |
| 4192 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4193 | __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] | 4194 | { |
| 4195 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4196 | struct stream_interface *si = appctx->appctx->owner; |
| 4197 | struct channel *req = si_oc(si); |
| 4198 | struct htx *htx; |
| 4199 | struct htx_blk *blk; |
| 4200 | size_t count; |
| 4201 | int len; |
| 4202 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4203 | htx = htx_from_buf(&req->buf); |
| 4204 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4205 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4206 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4207 | while (count && len && blk) { |
| 4208 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4209 | uint32_t sz = htx_get_blksz(blk); |
| 4210 | struct ist v; |
| 4211 | uint32_t vlen; |
| 4212 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4213 | if (type == HTX_BLK_EOM) { |
| 4214 | len = 0; |
| 4215 | break; |
| 4216 | } |
| 4217 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4218 | vlen = sz; |
| 4219 | if (len > 0 && vlen > len) |
| 4220 | vlen = len; |
| 4221 | if (vlen > count) { |
| 4222 | if (type != HTX_BLK_DATA) |
| 4223 | break; |
| 4224 | vlen = count; |
| 4225 | } |
| 4226 | |
| 4227 | switch (type) { |
| 4228 | case HTX_BLK_UNUSED: |
| 4229 | break; |
| 4230 | |
| 4231 | case HTX_BLK_DATA: |
| 4232 | v = htx_get_blk_value(htx, blk); |
| 4233 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4234 | break; |
| 4235 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4236 | case HTX_BLK_TLR: |
| 4237 | case HTX_BLK_EOM: |
| 4238 | len = 0; |
| 4239 | break; |
| 4240 | |
| 4241 | default: |
| 4242 | break; |
| 4243 | } |
| 4244 | |
| 4245 | co_set_data(req, co_data(req) - vlen); |
| 4246 | count -= vlen; |
| 4247 | if (len > 0) |
| 4248 | len -= vlen; |
| 4249 | if (sz == vlen) |
| 4250 | blk = htx_remove_blk(htx, blk); |
| 4251 | else { |
| 4252 | htx_cut_data_blk(htx, blk, vlen); |
| 4253 | break; |
| 4254 | } |
| 4255 | } |
| 4256 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4257 | htx_to_buf(htx, &req->buf); |
| 4258 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4259 | /* If we are no other data available, yield waiting for new data. */ |
| 4260 | if (len) { |
| 4261 | if (len > 0) { |
| 4262 | lua_pushinteger(L, len); |
| 4263 | lua_replace(L, 2); |
| 4264 | } |
| 4265 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4266 | 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] | 4267 | } |
| 4268 | |
| 4269 | /* return the result. */ |
| 4270 | luaL_pushresult(&appctx->b); |
| 4271 | return 1; |
| 4272 | } |
| 4273 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4274 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4275 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4276 | { |
| 4277 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4278 | int len = -1; |
| 4279 | |
| 4280 | /* Check arguments. */ |
| 4281 | if (lua_gettop(L) > 2) |
| 4282 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4283 | if (lua_gettop(L) >= 2) { |
| 4284 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4285 | lua_pop(L, 1); |
| 4286 | } |
| 4287 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4288 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4289 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4290 | /* Initialise the string catenation. */ |
| 4291 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4292 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4293 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | /* Append data in the output side of the buffer. This data is immediately |
| 4297 | * sent. The function returns the amount of data written. If the buffer |
| 4298 | * cannot contain the data, the function yields. The function returns -1 |
| 4299 | * if the channel is closed. |
| 4300 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4301 | __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] | 4302 | { |
| 4303 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4304 | struct stream_interface *si = appctx->appctx->owner; |
| 4305 | struct channel *res = si_ic(si); |
| 4306 | struct htx *htx = htx_from_buf(&res->buf); |
| 4307 | const char *data; |
| 4308 | size_t len; |
| 4309 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4310 | int max; |
| 4311 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4312 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4313 | if (!max) |
| 4314 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4315 | |
| 4316 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4317 | |
| 4318 | /* Get the max amount of data which can write as input in the channel. */ |
| 4319 | if (max > (len - l)) |
| 4320 | max = len - l; |
| 4321 | |
| 4322 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4323 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4324 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4325 | |
| 4326 | /* update counters. */ |
| 4327 | l += max; |
| 4328 | lua_pop(L, 1); |
| 4329 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4330 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4331 | /* If some data is not send, declares the situation to the |
| 4332 | * applet, and returns a yield. |
| 4333 | */ |
| 4334 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4335 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4336 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4337 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4338 | 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] | 4339 | } |
| 4340 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4341 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4342 | return 1; |
| 4343 | } |
| 4344 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4345 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4346 | * yield the LUA process, and resume it without checking the |
| 4347 | * input arguments. |
| 4348 | */ |
| 4349 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4350 | { |
| 4351 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4352 | |
| 4353 | /* We want to send some data. Headers must be sent. */ |
| 4354 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4355 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4356 | WILL_LJMP(lua_error(L)); |
| 4357 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4358 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4359 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4360 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4361 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4362 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4366 | { |
| 4367 | const char *name; |
| 4368 | int ret; |
| 4369 | |
| 4370 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4371 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4372 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4373 | |
| 4374 | /* Push in the stack the "response" entry. */ |
| 4375 | ret = lua_getfield(L, 1, "response"); |
| 4376 | if (ret != LUA_TTABLE) { |
| 4377 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4378 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4379 | WILL_LJMP(lua_error(L)); |
| 4380 | } |
| 4381 | |
| 4382 | /* check if the header is already registered if it is not |
| 4383 | * the case, register it. |
| 4384 | */ |
| 4385 | ret = lua_getfield(L, -1, name); |
| 4386 | if (ret == LUA_TNIL) { |
| 4387 | |
| 4388 | /* Entry not found. */ |
| 4389 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4390 | |
| 4391 | /* Insert the new header name in the array in the top of the stack. |
| 4392 | * It left the new array in the top of the stack. |
| 4393 | */ |
| 4394 | lua_newtable(L); |
| 4395 | lua_pushvalue(L, 2); |
| 4396 | lua_pushvalue(L, -2); |
| 4397 | lua_settable(L, -4); |
| 4398 | |
| 4399 | } else if (ret != LUA_TTABLE) { |
| 4400 | |
| 4401 | /* corruption error. */ |
| 4402 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4403 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4404 | WILL_LJMP(lua_error(L)); |
| 4405 | } |
| 4406 | |
| 4407 | /* Now the top od thestack is an array of values. We push |
| 4408 | * the header value as new entry. |
| 4409 | */ |
| 4410 | lua_pushvalue(L, 3); |
| 4411 | ret = lua_rawlen(L, -2); |
| 4412 | lua_rawseti(L, -2, ret + 1); |
| 4413 | lua_pushboolean(L, 1); |
| 4414 | return 1; |
| 4415 | } |
| 4416 | |
| 4417 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4418 | { |
| 4419 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4420 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4421 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4422 | |
| 4423 | if (status < 100 || status > 599) { |
| 4424 | lua_pushboolean(L, 0); |
| 4425 | return 1; |
| 4426 | } |
| 4427 | |
| 4428 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4429 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4430 | lua_pushboolean(L, 1); |
| 4431 | return 1; |
| 4432 | } |
| 4433 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4434 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4435 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4436 | { |
| 4437 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4438 | struct stream_interface *si = appctx->appctx->owner; |
| 4439 | struct channel *res = si_ic(si); |
| 4440 | struct htx *htx; |
| 4441 | struct htx_sl *sl; |
| 4442 | struct h1m h1m; |
| 4443 | const char *status, *reason; |
| 4444 | const char *name, *value; |
| 4445 | size_t nlen, vlen; |
| 4446 | unsigned int flags; |
| 4447 | |
| 4448 | /* Send the message at once. */ |
| 4449 | htx = htx_from_buf(&res->buf); |
| 4450 | h1m_init_res(&h1m); |
| 4451 | |
| 4452 | /* Use the same http version than the request. */ |
| 4453 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4454 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4455 | if (reason == NULL) |
| 4456 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4457 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4458 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4459 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4460 | } |
| 4461 | else { |
| 4462 | flags = HTX_SL_F_IS_RESP; |
| 4463 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4464 | } |
| 4465 | if (!sl) { |
| 4466 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4467 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4468 | WILL_LJMP(lua_error(L)); |
| 4469 | } |
| 4470 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4471 | |
| 4472 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4473 | lua_pushvalue(L, 0); |
| 4474 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4475 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4476 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4477 | WILL_LJMP(lua_error(L)); |
| 4478 | } |
| 4479 | |
| 4480 | /* Browse the list of headers. */ |
| 4481 | lua_pushnil(L); |
| 4482 | while(lua_next(L, -2) != 0) { |
| 4483 | /* We expect a string as -2. */ |
| 4484 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4485 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4486 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4487 | lua_typename(L, lua_type(L, -2))); |
| 4488 | WILL_LJMP(lua_error(L)); |
| 4489 | } |
| 4490 | name = lua_tolstring(L, -2, &nlen); |
| 4491 | |
| 4492 | /* We expect an array as -1. */ |
| 4493 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4494 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4495 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4496 | name, |
| 4497 | lua_typename(L, lua_type(L, -1))); |
| 4498 | WILL_LJMP(lua_error(L)); |
| 4499 | } |
| 4500 | |
| 4501 | /* Browse the table who is on the top of the stack. */ |
| 4502 | lua_pushnil(L); |
| 4503 | while(lua_next(L, -2) != 0) { |
| 4504 | int id; |
| 4505 | |
| 4506 | /* We expect a number as -2. */ |
| 4507 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4508 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4509 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4510 | name, |
| 4511 | lua_typename(L, lua_type(L, -2))); |
| 4512 | WILL_LJMP(lua_error(L)); |
| 4513 | } |
| 4514 | id = lua_tointeger(L, -2); |
| 4515 | |
| 4516 | /* We expect a string as -2. */ |
| 4517 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4518 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4519 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4520 | name, id, |
| 4521 | lua_typename(L, lua_type(L, -1))); |
| 4522 | WILL_LJMP(lua_error(L)); |
| 4523 | } |
| 4524 | value = lua_tolstring(L, -1, &vlen); |
| 4525 | |
| 4526 | /* Simple Protocol checks. */ |
| 4527 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4528 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4529 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4530 | struct ist v = ist2(value, vlen); |
| 4531 | int ret; |
| 4532 | |
| 4533 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4534 | if (ret < 0) { |
| 4535 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4536 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4537 | name); |
| 4538 | WILL_LJMP(lua_error(L)); |
| 4539 | } |
| 4540 | else if (ret == 0) |
| 4541 | goto next; /* Skip it */ |
| 4542 | } |
| 4543 | |
| 4544 | /* Add a new header */ |
| 4545 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4546 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4547 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4548 | name); |
| 4549 | WILL_LJMP(lua_error(L)); |
| 4550 | } |
| 4551 | next: |
| 4552 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4553 | lua_pop(L, 1); |
| 4554 | } |
| 4555 | |
| 4556 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4557 | lua_pop(L, 1); |
| 4558 | } |
| 4559 | |
| 4560 | if (h1m.flags & H1_MF_CHNK) |
| 4561 | h1m.flags &= ~H1_MF_CLEN; |
| 4562 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4563 | h1m.flags |= H1_MF_XFER_LEN; |
| 4564 | |
| 4565 | /* Uset HTX start-line flags */ |
| 4566 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4567 | flags |= HTX_SL_F_XFER_ENC; |
| 4568 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4569 | flags |= HTX_SL_F_XFER_LEN; |
| 4570 | if (h1m.flags & H1_MF_CHNK) |
| 4571 | flags |= HTX_SL_F_CHNK; |
| 4572 | else if (h1m.flags & H1_MF_CLEN) |
| 4573 | flags |= HTX_SL_F_CLEN; |
| 4574 | if (h1m.body_len == 0) |
| 4575 | flags |= HTX_SL_F_BODYLESS; |
| 4576 | } |
| 4577 | sl->flags |= flags; |
| 4578 | |
| 4579 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4580 | * and the status code implies the presence of a message body, we must |
| 4581 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4582 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4583 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4584 | */ |
| 4585 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4586 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4587 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4588 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4589 | /* Add a new header */ |
| 4590 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4591 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4592 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4593 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4594 | WILL_LJMP(lua_error(L)); |
| 4595 | } |
| 4596 | } |
| 4597 | |
| 4598 | /* Finalize headers. */ |
| 4599 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4600 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4601 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4602 | WILL_LJMP(lua_error(L)); |
| 4603 | } |
| 4604 | |
| 4605 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4606 | b_reset(&res->buf); |
| 4607 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4608 | WILL_LJMP(lua_error(L)); |
| 4609 | } |
| 4610 | |
| 4611 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4612 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4613 | |
| 4614 | /* Headers sent, set the flag. */ |
| 4615 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4616 | return 0; |
| 4617 | |
| 4618 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4619 | /* We will build the status line and the headers of the HTTP response. |
| 4620 | * We will try send at once if its not possible, we give back the hand |
| 4621 | * waiting for more room. |
| 4622 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4623 | __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] | 4624 | { |
| 4625 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4626 | struct stream_interface *si = appctx->appctx->owner; |
| 4627 | struct channel *res = si_ic(si); |
| 4628 | |
| 4629 | if (co_data(res)) { |
| 4630 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4631 | 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] | 4632 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4633 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4637 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4638 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4639 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4640 | } |
| 4641 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4642 | /* |
| 4643 | * |
| 4644 | * |
| 4645 | * Class HTTP |
| 4646 | * |
| 4647 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4648 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4649 | |
| 4650 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4651 | * a class stream, otherwise it throws an error. |
| 4652 | */ |
| 4653 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4654 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4655 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4656 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4657 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4658 | /* This function creates and push in the stack a HTTP object |
| 4659 | * according with a current TXN. |
| 4660 | */ |
| 4661 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4662 | { |
| 4663 | struct hlua_txn *htxn; |
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 | /* Check stack size. */ |
| 4666 | if (!lua_checkstack(L, 3)) |
| 4667 | return 0; |
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 | /* Create the object: obj[0] = userdata. |
| 4670 | * Note that the base of the Converters object is the |
| 4671 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4673 | lua_newtable(L); |
| 4674 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4675 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4676 | |
| 4677 | htxn->s = txn->s; |
| 4678 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4679 | htxn->dir = txn->dir; |
| 4680 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4681 | |
| 4682 | /* Pop a class stream metatable and affect it to the table. */ |
| 4683 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4684 | lua_setmetatable(L, -2); |
| 4685 | |
| 4686 | return 1; |
| 4687 | } |
| 4688 | |
| 4689 | /* This function creates ans returns an array of HTTP headers. |
| 4690 | * This function does not fails. It is used as wrapper with the |
| 4691 | * 2 following functions. |
| 4692 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4693 | __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] | 4694 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4695 | struct htx *htx; |
| 4696 | int32_t pos; |
| 4697 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4698 | /* Create the table. */ |
| 4699 | lua_newtable(L); |
| 4700 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4701 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4702 | htx = htxbuf(&msg->chn->buf); |
| 4703 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4704 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4705 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4706 | struct ist n, v; |
| 4707 | int len; |
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 | if (type == HTX_BLK_HDR) { |
| 4710 | n = htx_get_blk_name(htx,blk); |
| 4711 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4712 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4713 | else if (type == HTX_BLK_EOH) |
| 4714 | break; |
| 4715 | else |
| 4716 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4717 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4718 | /* Check for existing entry: |
| 4719 | * assume that the table is on the top of the stack, and |
| 4720 | * push the key in the stack, the function lua_gettable() |
| 4721 | * perform the lookup. |
| 4722 | */ |
| 4723 | lua_pushlstring(L, n.ptr, n.len); |
| 4724 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4725 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4726 | switch (lua_type(L, -1)) { |
| 4727 | case LUA_TNIL: |
| 4728 | /* Table not found, create it. */ |
| 4729 | lua_pop(L, 1); /* remove the nil value. */ |
| 4730 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4731 | lua_newtable(L); /* create and push empty table. */ |
| 4732 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4733 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4734 | 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] | 4735 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4736 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4737 | case LUA_TTABLE: |
| 4738 | /* Entry found: push the value in the table. */ |
| 4739 | len = lua_rawlen(L, -1); |
| 4740 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4741 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4742 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4743 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4744 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4745 | default: |
| 4746 | /* Other cases are errors. */ |
| 4747 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4748 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4749 | } |
| 4750 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4751 | return 1; |
| 4752 | } |
| 4753 | |
| 4754 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4755 | { |
| 4756 | struct hlua_txn *htxn; |
| 4757 | |
| 4758 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4759 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4760 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4761 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4762 | WILL_LJMP(lua_error(L)); |
| 4763 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4764 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4768 | { |
| 4769 | struct hlua_txn *htxn; |
| 4770 | |
| 4771 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4772 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4773 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4774 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4775 | WILL_LJMP(lua_error(L)); |
| 4776 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4777 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | /* This function replace full header, or just a value in |
| 4781 | * the request or in the response. It is a wrapper fir the |
| 4782 | * 4 following functions. |
| 4783 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4784 | __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] | 4785 | { |
| 4786 | size_t name_len; |
| 4787 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4788 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4789 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4790 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4791 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4792 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4793 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4794 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4795 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4796 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4797 | 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] | 4798 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4799 | return 0; |
| 4800 | } |
| 4801 | |
| 4802 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4803 | { |
| 4804 | struct hlua_txn *htxn; |
| 4805 | |
| 4806 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4807 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4808 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4809 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4810 | WILL_LJMP(lua_error(L)); |
| 4811 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4812 | 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] | 4813 | } |
| 4814 | |
| 4815 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4816 | { |
| 4817 | struct hlua_txn *htxn; |
| 4818 | |
| 4819 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4820 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4821 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4822 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4823 | WILL_LJMP(lua_error(L)); |
| 4824 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4825 | 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] | 4826 | } |
| 4827 | |
| 4828 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4829 | { |
| 4830 | struct hlua_txn *htxn; |
| 4831 | |
| 4832 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4833 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4834 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4835 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4836 | WILL_LJMP(lua_error(L)); |
| 4837 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4838 | 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] | 4839 | } |
| 4840 | |
| 4841 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4842 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4843 | struct hlua_txn *htxn; |
| 4844 | |
| 4845 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4846 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4847 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4848 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4849 | WILL_LJMP(lua_error(L)); |
| 4850 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4851 | 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] | 4852 | } |
| 4853 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4854 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4855 | * It is a wrapper for the 2 following functions. |
| 4856 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4857 | __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] | 4858 | { |
| 4859 | size_t len; |
| 4860 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4861 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4862 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4863 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4864 | ctx.blk = NULL; |
| 4865 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4866 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4867 | return 0; |
| 4868 | } |
| 4869 | |
| 4870 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4871 | { |
| 4872 | struct hlua_txn *htxn; |
| 4873 | |
| 4874 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4875 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4876 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4877 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4878 | WILL_LJMP(lua_error(L)); |
| 4879 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4880 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4884 | { |
| 4885 | struct hlua_txn *htxn; |
| 4886 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4887 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4888 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4889 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4890 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4891 | WILL_LJMP(lua_error(L)); |
| 4892 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4893 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | /* This function adds an header. It is a wrapper used by |
| 4897 | * the 2 following functions. |
| 4898 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4899 | __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] | 4900 | { |
| 4901 | size_t name_len; |
| 4902 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4903 | size_t value_len; |
| 4904 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4905 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4906 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4907 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4908 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4909 | return 0; |
| 4910 | } |
| 4911 | |
| 4912 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4913 | { |
| 4914 | struct hlua_txn *htxn; |
| 4915 | |
| 4916 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4917 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4918 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4919 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4920 | WILL_LJMP(lua_error(L)); |
| 4921 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4922 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4926 | { |
| 4927 | struct hlua_txn *htxn; |
| 4928 | |
| 4929 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4930 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4931 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4932 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4933 | WILL_LJMP(lua_error(L)); |
| 4934 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4935 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4939 | { |
| 4940 | struct hlua_txn *htxn; |
| 4941 | |
| 4942 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4943 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4944 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4945 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4946 | WILL_LJMP(lua_error(L)); |
| 4947 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4948 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 4949 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4953 | { |
| 4954 | struct hlua_txn *htxn; |
| 4955 | |
| 4956 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4957 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4958 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4959 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4960 | WILL_LJMP(lua_error(L)); |
| 4961 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4962 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 4963 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | /* This function set the method. */ |
| 4967 | static int hlua_http_req_set_meth(lua_State *L) |
| 4968 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4969 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4970 | size_t name_len; |
| 4971 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4972 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4973 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4974 | WILL_LJMP(lua_error(L)); |
| 4975 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4976 | 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] | 4977 | return 1; |
| 4978 | } |
| 4979 | |
| 4980 | /* This function set the method. */ |
| 4981 | static int hlua_http_req_set_path(lua_State *L) |
| 4982 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4983 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4984 | size_t name_len; |
| 4985 | 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] | 4986 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4987 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4988 | WILL_LJMP(lua_error(L)); |
| 4989 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4990 | 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] | 4991 | return 1; |
| 4992 | } |
| 4993 | |
| 4994 | /* This function set the query-string. */ |
| 4995 | static int hlua_http_req_set_query(lua_State *L) |
| 4996 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4997 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4998 | size_t name_len; |
| 4999 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5000 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5001 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5002 | WILL_LJMP(lua_error(L)); |
| 5003 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5004 | /* Check length. */ |
| 5005 | if (name_len > trash.size - 1) { |
| 5006 | lua_pushboolean(L, 0); |
| 5007 | return 1; |
| 5008 | } |
| 5009 | |
| 5010 | /* Add the mark question as prefix. */ |
| 5011 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5012 | trash.area[trash.data++] = '?'; |
| 5013 | memcpy(trash.area + trash.data, name, name_len); |
| 5014 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5016 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5017 | 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] | 5018 | return 1; |
| 5019 | } |
| 5020 | |
| 5021 | /* This function set the uri. */ |
| 5022 | static int hlua_http_req_set_uri(lua_State *L) |
| 5023 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5024 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5025 | size_t name_len; |
| 5026 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5027 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5028 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5029 | WILL_LJMP(lua_error(L)); |
| 5030 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5031 | 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] | 5032 | return 1; |
| 5033 | } |
| 5034 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5035 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5036 | static int hlua_http_res_set_status(lua_State *L) |
| 5037 | { |
| 5038 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5039 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5040 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5041 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5042 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5043 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5044 | WILL_LJMP(lua_error(L)); |
| 5045 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5046 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5047 | return 0; |
| 5048 | } |
| 5049 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5050 | /* |
| 5051 | * |
| 5052 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5053 | * Class TXN |
| 5054 | * |
| 5055 | * |
| 5056 | */ |
| 5057 | |
| 5058 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5059 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5060 | */ |
| 5061 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5062 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5063 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5064 | } |
| 5065 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5066 | __LJMP static int hlua_set_var(lua_State *L) |
| 5067 | { |
| 5068 | struct hlua_txn *htxn; |
| 5069 | const char *name; |
| 5070 | size_t len; |
| 5071 | struct sample smp; |
| 5072 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5073 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5074 | 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] | 5075 | |
| 5076 | /* It is useles to retrieve the stream, but this function |
| 5077 | * runs only in a stream context. |
| 5078 | */ |
| 5079 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5080 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5081 | |
| 5082 | /* Converts the third argument in a sample. */ |
| 5083 | hlua_lua2smp(L, 3, &smp); |
| 5084 | |
| 5085 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5086 | 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] | 5087 | |
| 5088 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5089 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5090 | else |
| 5091 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5092 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5093 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5094 | } |
| 5095 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5096 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5097 | { |
| 5098 | struct hlua_txn *htxn; |
| 5099 | const char *name; |
| 5100 | size_t len; |
| 5101 | struct sample smp; |
| 5102 | |
| 5103 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5104 | |
| 5105 | /* It is useles to retrieve the stream, but this function |
| 5106 | * runs only in a stream context. |
| 5107 | */ |
| 5108 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5109 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5110 | |
| 5111 | /* Unset the variable. */ |
| 5112 | 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] | 5113 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5114 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5115 | } |
| 5116 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5117 | __LJMP static int hlua_get_var(lua_State *L) |
| 5118 | { |
| 5119 | struct hlua_txn *htxn; |
| 5120 | const char *name; |
| 5121 | size_t len; |
| 5122 | struct sample smp; |
| 5123 | |
| 5124 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5125 | |
| 5126 | /* It is useles to retrieve the stream, but this function |
| 5127 | * runs only in a stream context. |
| 5128 | */ |
| 5129 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5130 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5131 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5132 | 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] | 5133 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5134 | lua_pushnil(L); |
| 5135 | return 1; |
| 5136 | } |
| 5137 | |
| 5138 | return hlua_smp2lua(L, &smp); |
| 5139 | } |
| 5140 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5141 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5142 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5143 | struct hlua *hlua; |
| 5144 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5145 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5146 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5147 | /* It is useles to retrieve the stream, but this function |
| 5148 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5149 | */ |
| 5150 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5151 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5152 | |
| 5153 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5154 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5155 | |
| 5156 | /* Get and store new value. */ |
| 5157 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5158 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5159 | |
| 5160 | return 0; |
| 5161 | } |
| 5162 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5163 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5164 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5165 | struct hlua *hlua; |
| 5166 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5167 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5168 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5169 | /* It is useles to retrieve the stream, but this function |
| 5170 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5171 | */ |
| 5172 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5173 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5174 | |
| 5175 | /* Push configuration index in the stack. */ |
| 5176 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5177 | |
| 5178 | return 1; |
| 5179 | } |
| 5180 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5181 | /* Create stack entry containing a class TXN. This function |
| 5182 | * return 0 if the stack does not contains free slots, |
| 5183 | * otherwise it returns 1. |
| 5184 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5185 | 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] | 5186 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5187 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5188 | |
| 5189 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5190 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5191 | return 0; |
| 5192 | |
| 5193 | /* NOTE: The allocation never fails. The failure |
| 5194 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5195 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5196 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5197 | /* Create the object: obj[0] = userdata. */ |
| 5198 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5199 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5200 | lua_rawseti(L, -2, 0); |
| 5201 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5202 | htxn->s = s; |
| 5203 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5204 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5205 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5206 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5207 | /* Create the "f" field that contains a list of fetches. */ |
| 5208 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5209 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5210 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5211 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5212 | |
| 5213 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5214 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5215 | 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] | 5216 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5217 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5218 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5219 | /* Create the "c" field that contains a list of converters. */ |
| 5220 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5221 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5222 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5223 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5224 | |
| 5225 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5226 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5227 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5228 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5229 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5230 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5231 | /* Create the "req" field that contains the request channel object. */ |
| 5232 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5233 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5234 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5235 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5236 | |
| 5237 | /* Create the "res" field that contains the response channel object. */ |
| 5238 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5239 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5240 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5241 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5242 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5243 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5244 | lua_pushstring(L, "http"); |
| 5245 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5246 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5247 | return 0; |
| 5248 | } |
| 5249 | else |
| 5250 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5251 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5252 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5253 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5254 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5255 | lua_setmetatable(L, -2); |
| 5256 | |
| 5257 | return 1; |
| 5258 | } |
| 5259 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5260 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5261 | { |
| 5262 | const char *msg; |
| 5263 | struct hlua_txn *htxn; |
| 5264 | |
| 5265 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5266 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5267 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5268 | |
| 5269 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5270 | return 0; |
| 5271 | } |
| 5272 | |
| 5273 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5274 | { |
| 5275 | int level; |
| 5276 | const char *msg; |
| 5277 | struct hlua_txn *htxn; |
| 5278 | |
| 5279 | MAY_LJMP(check_args(L, 3, "log")); |
| 5280 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5281 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5282 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5283 | |
| 5284 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5285 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5286 | |
| 5287 | hlua_sendlog(htxn->s->be, level, msg); |
| 5288 | return 0; |
| 5289 | } |
| 5290 | |
| 5291 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5292 | { |
| 5293 | const char *msg; |
| 5294 | struct hlua_txn *htxn; |
| 5295 | |
| 5296 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5297 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5298 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5299 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5300 | return 0; |
| 5301 | } |
| 5302 | |
| 5303 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5304 | { |
| 5305 | const char *msg; |
| 5306 | struct hlua_txn *htxn; |
| 5307 | |
| 5308 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5309 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5310 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5311 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5312 | return 0; |
| 5313 | } |
| 5314 | |
| 5315 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5316 | { |
| 5317 | const char *msg; |
| 5318 | struct hlua_txn *htxn; |
| 5319 | |
| 5320 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5321 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5322 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5323 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5324 | return 0; |
| 5325 | } |
| 5326 | |
| 5327 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5328 | { |
| 5329 | const char *msg; |
| 5330 | struct hlua_txn *htxn; |
| 5331 | |
| 5332 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5333 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5334 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5335 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5336 | return 0; |
| 5337 | } |
| 5338 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5339 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5340 | { |
| 5341 | struct hlua_txn *htxn; |
| 5342 | int ll; |
| 5343 | |
| 5344 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5345 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5346 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5347 | |
| 5348 | if (ll < 0 || ll > 7) |
| 5349 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5350 | |
| 5351 | htxn->s->logs.level = ll; |
| 5352 | return 0; |
| 5353 | } |
| 5354 | |
| 5355 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5356 | { |
| 5357 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5358 | int tos; |
| 5359 | |
| 5360 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5361 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5362 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5363 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5364 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5365 | return 0; |
| 5366 | } |
| 5367 | |
| 5368 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5369 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5370 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5371 | int mark; |
| 5372 | |
| 5373 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5374 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5375 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5376 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5377 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5378 | return 0; |
| 5379 | } |
| 5380 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5381 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5382 | { |
| 5383 | struct hlua_txn *htxn; |
| 5384 | |
| 5385 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5386 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5387 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5388 | return 0; |
| 5389 | } |
| 5390 | |
| 5391 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5392 | { |
| 5393 | struct hlua_txn *htxn; |
| 5394 | |
| 5395 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5396 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5397 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5398 | return 0; |
| 5399 | } |
| 5400 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5401 | /* 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] | 5402 | * 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] | 5403 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5404 | * error. The Reply must be on top of the stack. |
| 5405 | */ |
| 5406 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5407 | { |
| 5408 | struct htx *htx; |
| 5409 | struct htx_sl *sl; |
| 5410 | struct h1m h1m; |
| 5411 | const char *status, *reason, *body; |
| 5412 | size_t status_len, reason_len, body_len; |
| 5413 | int ret, code, flags; |
| 5414 | |
| 5415 | code = 200; |
| 5416 | status = "200"; |
| 5417 | status_len = 3; |
| 5418 | ret = lua_getfield(L, -1, "status"); |
| 5419 | if (ret == LUA_TNUMBER) { |
| 5420 | code = lua_tointeger(L, -1); |
| 5421 | status = lua_tolstring(L, -1, &status_len); |
| 5422 | } |
| 5423 | lua_pop(L, 1); |
| 5424 | |
| 5425 | reason = http_get_reason(code); |
| 5426 | reason_len = strlen(reason); |
| 5427 | ret = lua_getfield(L, -1, "reason"); |
| 5428 | if (ret == LUA_TSTRING) |
| 5429 | reason = lua_tolstring(L, -1, &reason_len); |
| 5430 | lua_pop(L, 1); |
| 5431 | |
| 5432 | body = NULL; |
| 5433 | body_len = 0; |
| 5434 | ret = lua_getfield(L, -1, "body"); |
| 5435 | if (ret == LUA_TSTRING) |
| 5436 | body = lua_tolstring(L, -1, &body_len); |
| 5437 | lua_pop(L, 1); |
| 5438 | |
| 5439 | /* Prepare the response before inserting the headers */ |
| 5440 | h1m_init_res(&h1m); |
| 5441 | htx = htx_from_buf(&s->res.buf); |
| 5442 | channel_htx_truncate(&s->res, htx); |
| 5443 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5444 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5445 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5446 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5447 | } |
| 5448 | else { |
| 5449 | flags = HTX_SL_F_IS_RESP; |
| 5450 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5451 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5452 | } |
| 5453 | if (!sl) |
| 5454 | goto fail; |
| 5455 | sl->info.res.status = code; |
| 5456 | |
| 5457 | /* Push in the stack the "headers" entry. */ |
| 5458 | ret = lua_getfield(L, -1, "headers"); |
| 5459 | if (ret != LUA_TTABLE) |
| 5460 | goto skip_headers; |
| 5461 | |
| 5462 | lua_pushnil(L); |
| 5463 | while (lua_next(L, -2) != 0) { |
| 5464 | struct ist name, value; |
| 5465 | const char *n, *v; |
| 5466 | size_t nlen, vlen; |
| 5467 | |
| 5468 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5469 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5470 | goto next_hdr; |
| 5471 | } |
| 5472 | |
| 5473 | n = lua_tolstring(L, -2, &nlen); |
| 5474 | name = ist2(n, nlen); |
| 5475 | if (isteqi(name, ist("content-length"))) { |
| 5476 | /* Always skip content-length header. It will be added |
| 5477 | * later with the correct len |
| 5478 | */ |
| 5479 | goto next_hdr; |
| 5480 | } |
| 5481 | |
| 5482 | /* Loop on header's values */ |
| 5483 | lua_pushnil(L); |
| 5484 | while (lua_next(L, -2)) { |
| 5485 | if (!lua_isstring(L, -1)) { |
| 5486 | /* Skip the value if it is not a string */ |
| 5487 | goto next_value; |
| 5488 | } |
| 5489 | |
| 5490 | v = lua_tolstring(L, -1, &vlen); |
| 5491 | value = ist2(v, vlen); |
| 5492 | |
| 5493 | if (isteqi(name, ist("transfer-encoding"))) |
| 5494 | h1_parse_xfer_enc_header(&h1m, value); |
| 5495 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5496 | goto fail; |
| 5497 | |
| 5498 | next_value: |
| 5499 | lua_pop(L, 1); |
| 5500 | } |
| 5501 | |
| 5502 | next_hdr: |
| 5503 | lua_pop(L, 1); |
| 5504 | } |
| 5505 | skip_headers: |
| 5506 | lua_pop(L, 1); |
| 5507 | |
| 5508 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5509 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5510 | const char *clen = ultoa(body_len); |
| 5511 | |
| 5512 | h1m.flags |= H1_MF_CLEN; |
| 5513 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5514 | goto fail; |
| 5515 | } |
| 5516 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5517 | h1m.flags |= H1_MF_XFER_LEN; |
| 5518 | |
| 5519 | /* Update HTX start-line flags */ |
| 5520 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5521 | flags |= HTX_SL_F_XFER_ENC; |
| 5522 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5523 | flags |= HTX_SL_F_XFER_LEN; |
| 5524 | if (h1m.flags & H1_MF_CHNK) |
| 5525 | flags |= HTX_SL_F_CHNK; |
| 5526 | else if (h1m.flags & H1_MF_CLEN) |
| 5527 | flags |= HTX_SL_F_CLEN; |
| 5528 | if (h1m.body_len == 0) |
| 5529 | flags |= HTX_SL_F_BODYLESS; |
| 5530 | } |
| 5531 | sl->flags |= flags; |
| 5532 | |
| 5533 | |
| 5534 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5535 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5536 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5537 | goto fail; |
| 5538 | |
| 5539 | /* Now, forward the response and terminate the transaction */ |
| 5540 | s->txn->status = code; |
| 5541 | htx_to_buf(htx, &s->res.buf); |
| 5542 | if (!http_forward_proxy_resp(s, 1)) |
| 5543 | goto fail; |
| 5544 | |
| 5545 | return 1; |
| 5546 | |
| 5547 | fail: |
| 5548 | channel_htx_truncate(&s->res, htx); |
| 5549 | return 0; |
| 5550 | } |
| 5551 | |
| 5552 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5553 | * processing is just aborted. Nothing is returned to the client and all |
| 5554 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5555 | * is forwarded to the client before terminating the transaction. On success, |
| 5556 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5557 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5558 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5559 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5560 | __LJMP static int hlua_txn_done(lua_State *L) |
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 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5563 | struct stream *s; |
| 5564 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5565 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5566 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5567 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5568 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5569 | * just end the execution of the current lua code. */ |
| 5570 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5571 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5572 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5573 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5574 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5575 | struct channel *req = &s->req; |
| 5576 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5577 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5578 | channel_auto_read(req); |
| 5579 | channel_abort(req); |
| 5580 | channel_auto_close(req); |
| 5581 | channel_erase(req); |
| 5582 | |
| 5583 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5584 | channel_auto_read(res); |
| 5585 | channel_auto_close(res); |
| 5586 | channel_shutr_now(res); |
| 5587 | |
| 5588 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5589 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5590 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5591 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5592 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5593 | /* No reply or invalid reply */ |
| 5594 | s->txn->status = 0; |
| 5595 | http_reply_and_close(s, 0, NULL); |
| 5596 | } |
| 5597 | else { |
| 5598 | /* Remove extra args to have the reply on top of the stack */ |
| 5599 | if (lua_gettop(L) > 2) |
| 5600 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5601 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5602 | if (!hlua_txn_forward_reply(L, s)) { |
| 5603 | if (!(s->flags & SF_ERR_MASK)) |
| 5604 | s->flags |= SF_ERR_PRXCOND; |
| 5605 | lua_pushinteger(L, ACT_RET_ERR); |
| 5606 | WILL_LJMP(hlua_done(L)); |
| 5607 | return 0; /* Never reached */ |
| 5608 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5609 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5610 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5611 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5612 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5613 | /* let's log the request time */ |
| 5614 | s->logs.tv_request = now; |
| 5615 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5616 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5617 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5618 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5619 | done: |
| 5620 | if (!(s->flags & SF_ERR_MASK)) |
| 5621 | s->flags |= SF_ERR_LOCAL; |
| 5622 | if (!(s->flags & SF_FINST_MASK)) |
| 5623 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5624 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5625 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5626 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5627 | return 0; |
| 5628 | } |
| 5629 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5630 | /* |
| 5631 | * |
| 5632 | * |
| 5633 | * Class REPLY |
| 5634 | * |
| 5635 | * |
| 5636 | */ |
| 5637 | |
| 5638 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5639 | * free slots, the function fails and returns 0; |
| 5640 | */ |
| 5641 | static int hlua_txn_reply_new(lua_State *L) |
| 5642 | { |
| 5643 | struct hlua_txn *htxn; |
| 5644 | const char *reason, *body = NULL; |
| 5645 | int ret, status; |
| 5646 | |
| 5647 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5648 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5649 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5650 | WILL_LJMP(lua_error(L)); |
| 5651 | } |
| 5652 | |
| 5653 | /* Default value */ |
| 5654 | status = 200; |
| 5655 | reason = http_get_reason(status); |
| 5656 | |
| 5657 | if (lua_istable(L, 2)) { |
| 5658 | /* load status and reason from the table argument at index 2 */ |
| 5659 | ret = lua_getfield(L, 2, "status"); |
| 5660 | if (ret == LUA_TNIL) |
| 5661 | goto reason; |
| 5662 | else if (ret != LUA_TNUMBER) { |
| 5663 | /* invalid status: ignore the reason */ |
| 5664 | goto body; |
| 5665 | } |
| 5666 | status = lua_tointeger(L, -1); |
| 5667 | |
| 5668 | reason: |
| 5669 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5670 | ret = lua_getfield(L, 2, "reason"); |
| 5671 | if (ret == LUA_TSTRING) |
| 5672 | reason = lua_tostring(L, -1); |
| 5673 | |
| 5674 | body: |
| 5675 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5676 | ret = lua_getfield(L, 2, "body"); |
| 5677 | if (ret == LUA_TSTRING) |
| 5678 | body = lua_tostring(L, -1); |
| 5679 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5680 | } |
| 5681 | |
| 5682 | /* Create the Reply table */ |
| 5683 | lua_newtable(L); |
| 5684 | |
| 5685 | /* Add status element */ |
| 5686 | lua_pushstring(L, "status"); |
| 5687 | lua_pushinteger(L, status); |
| 5688 | lua_settable(L, -3); |
| 5689 | |
| 5690 | /* Add reason element */ |
| 5691 | reason = http_get_reason(status); |
| 5692 | lua_pushstring(L, "reason"); |
| 5693 | lua_pushstring(L, reason); |
| 5694 | lua_settable(L, -3); |
| 5695 | |
| 5696 | /* Add body element, nil if undefined */ |
| 5697 | lua_pushstring(L, "body"); |
| 5698 | if (body) |
| 5699 | lua_pushstring(L, body); |
| 5700 | else |
| 5701 | lua_pushnil(L); |
| 5702 | lua_settable(L, -3); |
| 5703 | |
| 5704 | /* Add headers element */ |
| 5705 | lua_pushstring(L, "headers"); |
| 5706 | lua_newtable(L); |
| 5707 | |
| 5708 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5709 | if (lua_istable(L, 2)) { |
| 5710 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5711 | ret = lua_getfield(L, 2, "headers"); |
| 5712 | if (ret == LUA_TTABLE) { |
| 5713 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5714 | lua_pushnil(L); |
| 5715 | while (lua_next(L, -2) != 0) { |
| 5716 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5717 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5718 | /* invalid value type, skip it */ |
| 5719 | lua_pop(L, 1); |
| 5720 | continue; |
| 5721 | } |
| 5722 | |
| 5723 | |
| 5724 | /* Duplicate the key and swap it with the value. */ |
| 5725 | lua_pushvalue(L, -2); |
| 5726 | lua_insert(L, -2); |
| 5727 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5728 | |
| 5729 | lua_newtable(L); |
| 5730 | lua_insert(L, -2); |
| 5731 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5732 | |
| 5733 | if (lua_isstring(L, -1)) { |
| 5734 | /* push the value in the inner table */ |
| 5735 | lua_rawseti(L, -2, 1); |
| 5736 | } |
| 5737 | else { /* table */ |
| 5738 | lua_pushnil(L); |
| 5739 | while (lua_next(L, -2) != 0) { |
| 5740 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5741 | if (!lua_isstring(L, -1)) { |
| 5742 | /* invalid value type, skip it*/ |
| 5743 | lua_pop(L, 1); |
| 5744 | continue; |
| 5745 | } |
| 5746 | /* push the value in the inner table */ |
| 5747 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5748 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5749 | } |
| 5750 | lua_pop(L, 1); |
| 5751 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5752 | } |
| 5753 | |
| 5754 | /* push (k,v) on the stack in the headers table: |
| 5755 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5756 | */ |
| 5757 | lua_settable(L, -5); |
| 5758 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5759 | } |
| 5760 | } |
| 5761 | lua_pop(L, 1); |
| 5762 | } |
| 5763 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5764 | lua_settable(L, -3); |
| 5765 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5766 | |
| 5767 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5768 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5769 | lua_setmetatable(L, -2); |
| 5770 | return 1; |
| 5771 | } |
| 5772 | |
| 5773 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5774 | * provided, the default one corresponding to the status code is used. |
| 5775 | */ |
| 5776 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5777 | { |
| 5778 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5779 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5780 | |
| 5781 | /* First argument (self) must be a table */ |
| 5782 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5783 | |
| 5784 | if (status < 100 || status > 599) { |
| 5785 | lua_pushboolean(L, 0); |
| 5786 | return 1; |
| 5787 | } |
| 5788 | if (!reason) |
| 5789 | reason = http_get_reason(status); |
| 5790 | |
| 5791 | lua_pushinteger(L, status); |
| 5792 | lua_setfield(L, 1, "status"); |
| 5793 | |
| 5794 | lua_pushstring(L, reason); |
| 5795 | lua_setfield(L, 1, "reason"); |
| 5796 | |
| 5797 | lua_pushboolean(L, 1); |
| 5798 | return 1; |
| 5799 | } |
| 5800 | |
| 5801 | /* Add a header into the reply object. Each header name is associated to an |
| 5802 | * array of values in the "headers" table. If the header name is not found, a |
| 5803 | * new entry is created. |
| 5804 | */ |
| 5805 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5806 | { |
| 5807 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5808 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5809 | int ret; |
| 5810 | |
| 5811 | /* First argument (self) must be a table */ |
| 5812 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5813 | |
| 5814 | /* Push in the stack the "headers" entry. */ |
| 5815 | ret = lua_getfield(L, 1, "headers"); |
| 5816 | if (ret != LUA_TTABLE) { |
| 5817 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5818 | WILL_LJMP(lua_error(L)); |
| 5819 | } |
| 5820 | |
| 5821 | /* check if the header is already registered. If not, register it. */ |
| 5822 | ret = lua_getfield(L, -1, name); |
| 5823 | if (ret == LUA_TNIL) { |
| 5824 | /* Entry not found. */ |
| 5825 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5826 | |
| 5827 | /* Insert the new header name in the array in the top of the stack. |
| 5828 | * It left the new array in the top of the stack. |
| 5829 | */ |
| 5830 | lua_newtable(L); |
| 5831 | lua_pushstring(L, name); |
| 5832 | lua_pushvalue(L, -2); |
| 5833 | lua_settable(L, -4); |
| 5834 | } |
| 5835 | else if (ret != LUA_TTABLE) { |
| 5836 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5837 | WILL_LJMP(lua_error(L)); |
| 5838 | } |
| 5839 | |
| 5840 | /* Now the top od thestack is an array of values. We push |
| 5841 | * the header value as new entry. |
| 5842 | */ |
| 5843 | lua_pushstring(L, value); |
| 5844 | ret = lua_rawlen(L, -2); |
| 5845 | lua_rawseti(L, -2, ret + 1); |
| 5846 | |
| 5847 | lua_pushboolean(L, 1); |
| 5848 | return 1; |
| 5849 | } |
| 5850 | |
| 5851 | /* Remove all occurrences of a given header name. */ |
| 5852 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5853 | { |
| 5854 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5855 | int ret; |
| 5856 | |
| 5857 | /* First argument (self) must be a table */ |
| 5858 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5859 | |
| 5860 | /* Push in the stack the "headers" entry. */ |
| 5861 | ret = lua_getfield(L, 1, "headers"); |
| 5862 | if (ret != LUA_TTABLE) { |
| 5863 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 5864 | WILL_LJMP(lua_error(L)); |
| 5865 | } |
| 5866 | |
| 5867 | lua_pushstring(L, name); |
| 5868 | lua_pushnil(L); |
| 5869 | lua_settable(L, -3); |
| 5870 | |
| 5871 | lua_pushboolean(L, 1); |
| 5872 | return 1; |
| 5873 | } |
| 5874 | |
| 5875 | /* Set the reply's body. Overwrite any existing entry. */ |
| 5876 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 5877 | { |
| 5878 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5879 | |
| 5880 | /* First argument (self) must be a table */ |
| 5881 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5882 | |
| 5883 | lua_pushstring(L, payload); |
| 5884 | lua_setfield(L, 1, "body"); |
| 5885 | |
| 5886 | lua_pushboolean(L, 1); |
| 5887 | return 1; |
| 5888 | } |
| 5889 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5890 | __LJMP static int hlua_log(lua_State *L) |
| 5891 | { |
| 5892 | int level; |
| 5893 | const char *msg; |
| 5894 | |
| 5895 | MAY_LJMP(check_args(L, 2, "log")); |
| 5896 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5897 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5898 | |
| 5899 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5900 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5901 | |
| 5902 | hlua_sendlog(NULL, level, msg); |
| 5903 | return 0; |
| 5904 | } |
| 5905 | |
| 5906 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5907 | { |
| 5908 | const char *msg; |
| 5909 | |
| 5910 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5911 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5912 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5913 | return 0; |
| 5914 | } |
| 5915 | |
| 5916 | __LJMP static int hlua_log_info(lua_State *L) |
| 5917 | { |
| 5918 | const char *msg; |
| 5919 | |
| 5920 | MAY_LJMP(check_args(L, 1, "info")); |
| 5921 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5922 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5923 | return 0; |
| 5924 | } |
| 5925 | |
| 5926 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5927 | { |
| 5928 | const char *msg; |
| 5929 | |
| 5930 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5931 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5932 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5933 | return 0; |
| 5934 | } |
| 5935 | |
| 5936 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5937 | { |
| 5938 | const char *msg; |
| 5939 | |
| 5940 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5941 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5942 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5943 | return 0; |
| 5944 | } |
| 5945 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5946 | __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] | 5947 | { |
| 5948 | int wakeup_ms = lua_tointeger(L, -1); |
| 5949 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5950 | 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] | 5951 | return 0; |
| 5952 | } |
| 5953 | |
| 5954 | __LJMP static int hlua_sleep(lua_State *L) |
| 5955 | { |
| 5956 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5957 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5958 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5959 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5960 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5961 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5962 | wakeup_ms = tick_add(now_ms, delay); |
| 5963 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5964 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5965 | 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] | 5966 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5967 | } |
| 5968 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5969 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5970 | { |
| 5971 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5972 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5973 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5974 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5975 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5976 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5977 | wakeup_ms = tick_add(now_ms, delay); |
| 5978 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5979 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5980 | 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] | 5981 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5982 | } |
| 5983 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5984 | /* This functionis an LUA binding. it permits to give back |
| 5985 | * the hand at the HAProxy scheduler. It is used when the |
| 5986 | * LUA processing consumes a lot of time. |
| 5987 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5988 | __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] | 5989 | { |
| 5990 | return 0; |
| 5991 | } |
| 5992 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5993 | __LJMP static int hlua_yield(lua_State *L) |
| 5994 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5995 | 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] | 5996 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5997 | } |
| 5998 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5999 | /* This function change the nice of the currently executed |
| 6000 | * task. It is used set low or high priority at the current |
| 6001 | * task. |
| 6002 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6003 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6004 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6005 | struct hlua *hlua; |
| 6006 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6007 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6008 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6009 | hlua = hlua_gethlua(L); |
| 6010 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6011 | |
| 6012 | /* If he task is not set, I'm in a start mode. */ |
| 6013 | if (!hlua || !hlua->task) |
| 6014 | return 0; |
| 6015 | |
| 6016 | if (nice < -1024) |
| 6017 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6018 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6019 | nice = 1024; |
| 6020 | |
| 6021 | hlua->task->nice = nice; |
| 6022 | return 0; |
| 6023 | } |
| 6024 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6025 | /* 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] | 6026 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6027 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6028 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6029 | * |
| 6030 | * Task wrapper are longjmp safe because the only one Lua code |
| 6031 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6032 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6033 | 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] | 6034 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6035 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6036 | enum hlua_exec status; |
| 6037 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6038 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6039 | task_set_affinity(task, tid_bit); |
| 6040 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6041 | /* If it is the first call to the task, we must initialize the |
| 6042 | * execution timeouts. |
| 6043 | */ |
| 6044 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6045 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6046 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6047 | /* Execute the Lua code. */ |
| 6048 | status = hlua_ctx_resume(hlua, 1); |
| 6049 | |
| 6050 | switch (status) { |
| 6051 | /* finished or yield */ |
| 6052 | case HLUA_E_OK: |
| 6053 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6054 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6055 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6056 | break; |
| 6057 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6058 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6059 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6060 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6061 | break; |
| 6062 | |
| 6063 | /* finished with error. */ |
| 6064 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6065 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6066 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6067 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6068 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6069 | break; |
| 6070 | |
| 6071 | case HLUA_E_ERR: |
| 6072 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6073 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6074 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6075 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6076 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6077 | break; |
| 6078 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6079 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6080 | } |
| 6081 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6082 | /* This function is an LUA binding that register LUA function to be |
| 6083 | * executed after the HAProxy configuration parsing and before the |
| 6084 | * HAProxy scheduler starts. This function expect only one LUA |
| 6085 | * argument that is a function. This function returns nothing, but |
| 6086 | * throws if an error is encountered. |
| 6087 | */ |
| 6088 | __LJMP static int hlua_register_init(lua_State *L) |
| 6089 | { |
| 6090 | struct hlua_init_function *init; |
| 6091 | int ref; |
| 6092 | |
| 6093 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6094 | |
| 6095 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6096 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6097 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6098 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6099 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6100 | |
| 6101 | init->function_ref = ref; |
| 6102 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6103 | return 0; |
| 6104 | } |
| 6105 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6106 | /* This functio is an LUA binding. It permits to register a task |
| 6107 | * executed in parallel of the main HAroxy activity. The task is |
| 6108 | * created and it is set in the HAProxy scheduler. It can be called |
| 6109 | * from the "init" section, "post init" or during the runtime. |
| 6110 | * |
| 6111 | * Lua prototype: |
| 6112 | * |
| 6113 | * <none> core.register_task(<function>) |
| 6114 | */ |
| 6115 | static int hlua_register_task(lua_State *L) |
| 6116 | { |
| 6117 | struct hlua *hlua; |
| 6118 | struct task *task; |
| 6119 | int ref; |
| 6120 | |
| 6121 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6122 | |
| 6123 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6124 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6125 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6126 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6127 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6128 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6129 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6130 | if (!task) |
| 6131 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6132 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6133 | task->context = hlua; |
| 6134 | task->process = hlua_process_task; |
| 6135 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6136 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6137 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6138 | |
| 6139 | /* Restore the function in the stack. */ |
| 6140 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6141 | hlua->nargs = 0; |
| 6142 | |
| 6143 | /* Schedule task. */ |
| 6144 | task_schedule(task, now_ms); |
| 6145 | |
| 6146 | return 0; |
| 6147 | } |
| 6148 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6149 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6150 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6151 | * resume converters. |
| 6152 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6153 | 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] | 6154 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6155 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6156 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6157 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6158 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6159 | if (!stream) |
| 6160 | return 0; |
| 6161 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6162 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6163 | * Lua context can be not initialized. This behavior |
| 6164 | * permits to save performances because a systematic |
| 6165 | * Lua initialization cause 5% performances loss. |
| 6166 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6167 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6168 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6169 | if (!stream->hlua) { |
| 6170 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6171 | return 0; |
| 6172 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6173 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6174 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6175 | return 0; |
| 6176 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6177 | } |
| 6178 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6179 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6180 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6181 | |
| 6182 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6183 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6184 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6185 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6186 | else |
| 6187 | error = "critical error"; |
| 6188 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6189 | return 0; |
| 6190 | } |
| 6191 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6192 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6193 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6194 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6195 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6196 | return 0; |
| 6197 | } |
| 6198 | |
| 6199 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6200 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6201 | |
| 6202 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6203 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6204 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6205 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6206 | return 0; |
| 6207 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6208 | hlua_smp2lua(stream->hlua->T, smp); |
| 6209 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6210 | |
| 6211 | /* push keywords in the stack. */ |
| 6212 | if (arg_p) { |
| 6213 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6214 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6215 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6216 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6217 | return 0; |
| 6218 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6219 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6220 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6221 | } |
| 6222 | } |
| 6223 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6224 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6225 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6226 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6227 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6228 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6232 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6233 | /* finished. */ |
| 6234 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6235 | /* If the stack is empty, the function fails. */ |
| 6236 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6237 | return 0; |
| 6238 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6239 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6240 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6241 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6242 | return 1; |
| 6243 | |
| 6244 | /* yield. */ |
| 6245 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6246 | 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] | 6247 | return 0; |
| 6248 | |
| 6249 | /* finished with error. */ |
| 6250 | case HLUA_E_ERRMSG: |
| 6251 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6252 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6253 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6254 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6255 | return 0; |
| 6256 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6257 | case HLUA_E_ETMOUT: |
| 6258 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6259 | return 0; |
| 6260 | |
| 6261 | case HLUA_E_NOMEM: |
| 6262 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6263 | return 0; |
| 6264 | |
| 6265 | case HLUA_E_YIELD: |
| 6266 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6267 | return 0; |
| 6268 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6269 | case HLUA_E_ERR: |
| 6270 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6271 | 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] | 6272 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6273 | |
| 6274 | default: |
| 6275 | return 0; |
| 6276 | } |
| 6277 | } |
| 6278 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6279 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6280 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6281 | * resume sample-fetches. This function will be called by the sample |
| 6282 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6283 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6284 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6285 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6286 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6287 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6288 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6289 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6290 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6291 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6292 | if (!stream) |
| 6293 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6294 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6295 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6296 | * Lua context can be not initialized. This behavior |
| 6297 | * permits to save performances because a systematic |
| 6298 | * Lua initialization cause 5% performances loss. |
| 6299 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6300 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6301 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6302 | if (!stream->hlua) { |
| 6303 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6304 | return 0; |
| 6305 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6306 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6307 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6308 | return 0; |
| 6309 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6310 | } |
| 6311 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6312 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6313 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6314 | |
| 6315 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6316 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6317 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6318 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6319 | else |
| 6320 | error = "critical error"; |
| 6321 | 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] | 6322 | return 0; |
| 6323 | } |
| 6324 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6325 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6326 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6327 | 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] | 6328 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6329 | return 0; |
| 6330 | } |
| 6331 | |
| 6332 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6333 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6334 | |
| 6335 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6336 | 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] | 6337 | 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] | 6338 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6339 | return 0; |
| 6340 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6341 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6342 | |
| 6343 | /* push keywords in the stack. */ |
| 6344 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6345 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6346 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6347 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6348 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6349 | return 0; |
| 6350 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6351 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6352 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6353 | } |
| 6354 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6355 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6356 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6357 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6358 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6359 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6360 | } |
| 6361 | |
| 6362 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6363 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6364 | /* finished. */ |
| 6365 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6366 | /* If the stack is empty, the function fails. */ |
| 6367 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6368 | return 0; |
| 6369 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6370 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6371 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6372 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6373 | |
| 6374 | /* Set the end of execution flag. */ |
| 6375 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6376 | return 1; |
| 6377 | |
| 6378 | /* yield. */ |
| 6379 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6380 | 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] | 6381 | return 0; |
| 6382 | |
| 6383 | /* finished with error. */ |
| 6384 | case HLUA_E_ERRMSG: |
| 6385 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6386 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6387 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6388 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6389 | return 0; |
| 6390 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6391 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6392 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6393 | return 0; |
| 6394 | |
| 6395 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6396 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6397 | return 0; |
| 6398 | |
| 6399 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6400 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6401 | return 0; |
| 6402 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6403 | case HLUA_E_ERR: |
| 6404 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6405 | 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] | 6406 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6407 | |
| 6408 | default: |
| 6409 | return 0; |
| 6410 | } |
| 6411 | } |
| 6412 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6413 | /* This function is an LUA binding used for registering |
| 6414 | * "sample-conv" functions. It expects a converter name used |
| 6415 | * in the haproxy configuration file, and an LUA function. |
| 6416 | */ |
| 6417 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6418 | { |
| 6419 | struct sample_conv_kw_list *sck; |
| 6420 | const char *name; |
| 6421 | int ref; |
| 6422 | int len; |
| 6423 | struct hlua_function *fcn; |
| 6424 | |
| 6425 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6426 | |
| 6427 | /* First argument : converter name. */ |
| 6428 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6429 | |
| 6430 | /* Second argument : lua function. */ |
| 6431 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6432 | |
| 6433 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6434 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6435 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6436 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6437 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6438 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6439 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6440 | |
| 6441 | /* Fill fcn. */ |
| 6442 | fcn->name = strdup(name); |
| 6443 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6444 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6445 | fcn->function_ref = ref; |
| 6446 | |
| 6447 | /* List head */ |
| 6448 | sck->list.n = sck->list.p = NULL; |
| 6449 | |
| 6450 | /* converter keyword. */ |
| 6451 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6452 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6453 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6454 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6455 | |
| 6456 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6457 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6458 | 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] | 6459 | sck->kw[0].val_args = NULL; |
| 6460 | sck->kw[0].in_type = SMP_T_STR; |
| 6461 | sck->kw[0].out_type = SMP_T_STR; |
| 6462 | sck->kw[0].private = fcn; |
| 6463 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6464 | /* Register this new converter */ |
| 6465 | sample_register_convs(sck); |
| 6466 | |
| 6467 | return 0; |
| 6468 | } |
| 6469 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6470 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6471 | * "sample-fetch" functions. It expects a converter name used |
| 6472 | * in the haproxy configuration file, and an LUA function. |
| 6473 | */ |
| 6474 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6475 | { |
| 6476 | const char *name; |
| 6477 | int ref; |
| 6478 | int len; |
| 6479 | struct sample_fetch_kw_list *sfk; |
| 6480 | struct hlua_function *fcn; |
| 6481 | |
| 6482 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6483 | |
| 6484 | /* First argument : sample-fetch name. */ |
| 6485 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6486 | |
| 6487 | /* Second argument : lua function. */ |
| 6488 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6489 | |
| 6490 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6491 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6492 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6493 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6494 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6495 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6496 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6497 | |
| 6498 | /* Fill fcn. */ |
| 6499 | fcn->name = strdup(name); |
| 6500 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6501 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6502 | fcn->function_ref = ref; |
| 6503 | |
| 6504 | /* List head */ |
| 6505 | sfk->list.n = sfk->list.p = NULL; |
| 6506 | |
| 6507 | /* sample-fetch keyword. */ |
| 6508 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6509 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6510 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6511 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6512 | |
| 6513 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6514 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6515 | 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] | 6516 | sfk->kw[0].val_args = NULL; |
| 6517 | sfk->kw[0].out_type = SMP_T_STR; |
| 6518 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6519 | sfk->kw[0].val = 0; |
| 6520 | sfk->kw[0].private = fcn; |
| 6521 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6522 | /* Register this new fetch. */ |
| 6523 | sample_register_fetches(sfk); |
| 6524 | |
| 6525 | return 0; |
| 6526 | } |
| 6527 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6528 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6529 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6530 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6531 | { |
| 6532 | struct hlua *hlua = hlua_gethlua(L); |
| 6533 | unsigned int delay; |
| 6534 | unsigned int wakeup_ms; |
| 6535 | |
| 6536 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6537 | |
| 6538 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6539 | wakeup_ms = tick_add(now_ms, delay); |
| 6540 | hlua->wake_time = wakeup_ms; |
| 6541 | return 0; |
| 6542 | } |
| 6543 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6544 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6545 | * wrapper during the initialisation period. This function may return any |
| 6546 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6547 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6548 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6549 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6550 | 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] | 6551 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6552 | { |
| 6553 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6554 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6555 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6556 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6557 | |
| 6558 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6559 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6560 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6561 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6562 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6563 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6564 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6565 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6566 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6567 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6568 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6569 | * Lua context can be not initialized. This behavior |
| 6570 | * permits to save performances because a systematic |
| 6571 | * Lua initialization cause 5% performances loss. |
| 6572 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6573 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6574 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6575 | if (!s->hlua) { |
| 6576 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6577 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6578 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6579 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6580 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6581 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6582 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6583 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6584 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6585 | } |
| 6586 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6587 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6588 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6589 | |
| 6590 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6591 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6592 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6593 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6594 | else |
| 6595 | error = "critical error"; |
| 6596 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6597 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6598 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6599 | } |
| 6600 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6601 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6602 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6603 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6604 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6605 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6606 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6607 | } |
| 6608 | |
| 6609 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | 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] | 6611 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6612 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6613 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6614 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6615 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6616 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6617 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6618 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6619 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6620 | |
| 6621 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6622 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6623 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6624 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6625 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6626 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6627 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6628 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6629 | lua_pushstring(s->hlua->T, *arg); |
| 6630 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6631 | } |
| 6632 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6633 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6634 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6635 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6636 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6637 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6638 | } |
| 6639 | |
| 6640 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6641 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6642 | /* finished. */ |
| 6643 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6644 | /* Catch the return value */ |
| 6645 | if (lua_gettop(s->hlua->T) > 0) |
| 6646 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6647 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6648 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6649 | if (act_ret == ACT_RET_YIELD) { |
| 6650 | if (flags & ACT_OPT_FINAL) |
| 6651 | goto err_yield; |
| 6652 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6653 | if (dir == SMP_OPT_DIR_REQ) |
| 6654 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6655 | s->hlua->wake_time); |
| 6656 | else |
| 6657 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6658 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6659 | } |
| 6660 | goto end; |
| 6661 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6662 | /* yield. */ |
| 6663 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6664 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6665 | if (dir == SMP_OPT_DIR_REQ) |
| 6666 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6667 | s->hlua->wake_time); |
| 6668 | else |
| 6669 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6670 | s->hlua->wake_time); |
| 6671 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6672 | /* Some actions can be wake up when a "write" event |
| 6673 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6674 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6675 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6676 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6677 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6678 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6679 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6680 | act_ret = ACT_RET_YIELD; |
| 6681 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6682 | |
| 6683 | /* finished with error. */ |
| 6684 | case HLUA_E_ERRMSG: |
| 6685 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6686 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6687 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6688 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6689 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6690 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6691 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6692 | 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] | 6693 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6694 | |
| 6695 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6696 | 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] | 6697 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6698 | |
| 6699 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6700 | err_yield: |
| 6701 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6702 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6703 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6704 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6705 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6706 | case HLUA_E_ERR: |
| 6707 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6708 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6709 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6710 | |
| 6711 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6712 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6713 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6714 | |
| 6715 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6716 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6717 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6718 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6719 | } |
| 6720 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6721 | 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] | 6722 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6723 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6724 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6725 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6726 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6727 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6728 | } |
| 6729 | |
| 6730 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6731 | { |
| 6732 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6733 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6734 | struct task *task; |
| 6735 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6736 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6737 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6738 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6739 | if (!hlua) { |
| 6740 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6741 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6742 | return 0; |
| 6743 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6744 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6745 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6746 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6747 | |
| 6748 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6749 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6750 | if (!task) { |
| 6751 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6752 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6753 | return 0; |
| 6754 | } |
| 6755 | task->nice = 0; |
| 6756 | task->context = ctx; |
| 6757 | task->process = hlua_applet_wakeup; |
| 6758 | ctx->ctx.hlua_apptcp.task = task; |
| 6759 | |
| 6760 | /* In the execution wrappers linked with a stream, the |
| 6761 | * Lua context can be not initialized. This behavior |
| 6762 | * permits to save performances because a systematic |
| 6763 | * Lua initialization cause 5% performances loss. |
| 6764 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6765 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6766 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6767 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6768 | return 0; |
| 6769 | } |
| 6770 | |
| 6771 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6772 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6773 | |
| 6774 | /* The following Lua calls can fail. */ |
| 6775 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6776 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6777 | error = lua_tostring(hlua->T, -1); |
| 6778 | else |
| 6779 | error = "critical error"; |
| 6780 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6781 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6782 | return 0; |
| 6783 | } |
| 6784 | |
| 6785 | /* Check stack available size. */ |
| 6786 | if (!lua_checkstack(hlua->T, 1)) { |
| 6787 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6788 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6789 | RESET_SAFE_LJMP(hlua->T); |
| 6790 | return 0; |
| 6791 | } |
| 6792 | |
| 6793 | /* Restore the function in the stack. */ |
| 6794 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6795 | |
| 6796 | /* Create and and push object stream in the stack. */ |
| 6797 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6798 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6799 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6800 | RESET_SAFE_LJMP(hlua->T); |
| 6801 | return 0; |
| 6802 | } |
| 6803 | hlua->nargs = 1; |
| 6804 | |
| 6805 | /* push keywords in the stack. */ |
| 6806 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6807 | if (!lua_checkstack(hlua->T, 1)) { |
| 6808 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6809 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6810 | RESET_SAFE_LJMP(hlua->T); |
| 6811 | return 0; |
| 6812 | } |
| 6813 | lua_pushstring(hlua->T, *arg); |
| 6814 | hlua->nargs++; |
| 6815 | } |
| 6816 | |
| 6817 | RESET_SAFE_LJMP(hlua->T); |
| 6818 | |
| 6819 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6820 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6821 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6822 | |
| 6823 | return 1; |
| 6824 | } |
| 6825 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6826 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6827 | { |
| 6828 | struct stream_interface *si = ctx->owner; |
| 6829 | struct stream *strm = si_strm(si); |
| 6830 | struct channel *res = si_ic(si); |
| 6831 | struct act_rule *rule = ctx->rule; |
| 6832 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6833 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6834 | |
| 6835 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6836 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6837 | /* eat the whole request */ |
| 6838 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6839 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6840 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6841 | |
| 6842 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6843 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6844 | return; |
| 6845 | |
| 6846 | /* Execute the function. */ |
| 6847 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6848 | /* finished. */ |
| 6849 | case HLUA_E_OK: |
| 6850 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6851 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6852 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6853 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6854 | res->flags |= CF_READ_NULL; |
| 6855 | si_shutr(si); |
| 6856 | return; |
| 6857 | |
| 6858 | /* yield. */ |
| 6859 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6860 | if (hlua->wake_time != TICK_ETERNITY) |
| 6861 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6862 | return; |
| 6863 | |
| 6864 | /* finished with error. */ |
| 6865 | case HLUA_E_ERRMSG: |
| 6866 | /* Display log. */ |
| 6867 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6868 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6869 | lua_pop(hlua->T, 1); |
| 6870 | goto error; |
| 6871 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6872 | case HLUA_E_ETMOUT: |
| 6873 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6874 | rule->arg.hlua_rule->fcn.name); |
| 6875 | goto error; |
| 6876 | |
| 6877 | case HLUA_E_NOMEM: |
| 6878 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6879 | rule->arg.hlua_rule->fcn.name); |
| 6880 | goto error; |
| 6881 | |
| 6882 | case HLUA_E_YIELD: /* unexpected */ |
| 6883 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6884 | rule->arg.hlua_rule->fcn.name); |
| 6885 | goto error; |
| 6886 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6887 | case HLUA_E_ERR: |
| 6888 | /* Display log. */ |
| 6889 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6890 | rule->arg.hlua_rule->fcn.name); |
| 6891 | goto error; |
| 6892 | |
| 6893 | default: |
| 6894 | goto error; |
| 6895 | } |
| 6896 | |
| 6897 | error: |
| 6898 | |
| 6899 | /* For all other cases, just close the stream. */ |
| 6900 | si_shutw(si); |
| 6901 | si_shutr(si); |
| 6902 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6903 | } |
| 6904 | |
| 6905 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6906 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6907 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6908 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6909 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6910 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6911 | } |
| 6912 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6913 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6914 | * an errors occurs and -1 if more data are required for initializing |
| 6915 | * the applet. |
| 6916 | */ |
| 6917 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6918 | { |
| 6919 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6920 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6921 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6922 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6923 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6924 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6925 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6926 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6927 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6928 | if (!hlua) { |
| 6929 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6930 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6931 | return 0; |
| 6932 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6933 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6934 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6935 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6936 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6937 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6938 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6939 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6940 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6941 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6942 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6943 | if (!task) { |
| 6944 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6945 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6946 | return 0; |
| 6947 | } |
| 6948 | task->nice = 0; |
| 6949 | task->context = ctx; |
| 6950 | task->process = hlua_applet_wakeup; |
| 6951 | ctx->ctx.hlua_apphttp.task = task; |
| 6952 | |
| 6953 | /* In the execution wrappers linked with a stream, the |
| 6954 | * Lua context can be not initialized. This behavior |
| 6955 | * permits to save performances because a systematic |
| 6956 | * Lua initialization cause 5% performances loss. |
| 6957 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6958 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6959 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6960 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6961 | return 0; |
| 6962 | } |
| 6963 | |
| 6964 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6965 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6966 | |
| 6967 | /* The following Lua calls can fail. */ |
| 6968 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6969 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6970 | error = lua_tostring(hlua->T, -1); |
| 6971 | else |
| 6972 | error = "critical error"; |
| 6973 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6974 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6975 | return 0; |
| 6976 | } |
| 6977 | |
| 6978 | /* Check stack available size. */ |
| 6979 | if (!lua_checkstack(hlua->T, 1)) { |
| 6980 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6981 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6982 | RESET_SAFE_LJMP(hlua->T); |
| 6983 | return 0; |
| 6984 | } |
| 6985 | |
| 6986 | /* Restore the function in the stack. */ |
| 6987 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6988 | |
| 6989 | /* Create and and push object stream in the stack. */ |
| 6990 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6991 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6992 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6993 | RESET_SAFE_LJMP(hlua->T); |
| 6994 | return 0; |
| 6995 | } |
| 6996 | hlua->nargs = 1; |
| 6997 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6998 | /* push keywords in the stack. */ |
| 6999 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7000 | if (!lua_checkstack(hlua->T, 1)) { |
| 7001 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7002 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7003 | RESET_SAFE_LJMP(hlua->T); |
| 7004 | return 0; |
| 7005 | } |
| 7006 | lua_pushstring(hlua->T, *arg); |
| 7007 | hlua->nargs++; |
| 7008 | } |
| 7009 | |
| 7010 | RESET_SAFE_LJMP(hlua->T); |
| 7011 | |
| 7012 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7013 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7014 | |
| 7015 | return 1; |
| 7016 | } |
| 7017 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7018 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7019 | { |
| 7020 | struct stream_interface *si = ctx->owner; |
| 7021 | struct stream *strm = si_strm(si); |
| 7022 | struct channel *req = si_oc(si); |
| 7023 | struct channel *res = si_ic(si); |
| 7024 | struct act_rule *rule = ctx->rule; |
| 7025 | struct proxy *px = strm->be; |
| 7026 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7027 | struct htx *req_htx, *res_htx; |
| 7028 | |
| 7029 | res_htx = htx_from_buf(&res->buf); |
| 7030 | |
| 7031 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7032 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7033 | goto out; |
| 7034 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7035 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7036 | if (!b_size(&res->buf)) { |
| 7037 | si_rx_room_blk(si); |
| 7038 | goto out; |
| 7039 | } |
| 7040 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7041 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7042 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7043 | |
| 7044 | /* Set the currently running flag. */ |
| 7045 | if (!HLUA_IS_RUNNING(hlua) && |
| 7046 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7047 | struct htx_blk *blk; |
| 7048 | size_t count = co_data(req); |
| 7049 | |
| 7050 | if (!count) { |
| 7051 | si_cant_get(si); |
| 7052 | goto out; |
| 7053 | } |
| 7054 | |
| 7055 | /* We need to flush the request header. This left the body for |
| 7056 | * the Lua. |
| 7057 | */ |
| 7058 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7059 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7060 | while (count && blk) { |
| 7061 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7062 | uint32_t sz = htx_get_blksz(blk); |
| 7063 | |
| 7064 | if (sz > count) { |
| 7065 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7066 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7067 | goto out; |
| 7068 | } |
| 7069 | |
| 7070 | count -= sz; |
| 7071 | co_set_data(req, co_data(req) - sz); |
| 7072 | blk = htx_remove_blk(req_htx, blk); |
| 7073 | |
| 7074 | if (type == HTX_BLK_EOH) |
| 7075 | break; |
| 7076 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7077 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7078 | } |
| 7079 | |
| 7080 | /* Executes The applet if it is not done. */ |
| 7081 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7082 | |
| 7083 | /* Execute the function. */ |
| 7084 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7085 | /* finished. */ |
| 7086 | case HLUA_E_OK: |
| 7087 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7088 | break; |
| 7089 | |
| 7090 | /* yield. */ |
| 7091 | case HLUA_E_AGAIN: |
| 7092 | if (hlua->wake_time != TICK_ETERNITY) |
| 7093 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7094 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7095 | |
| 7096 | /* finished with error. */ |
| 7097 | case HLUA_E_ERRMSG: |
| 7098 | /* Display log. */ |
| 7099 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7100 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7101 | lua_pop(hlua->T, 1); |
| 7102 | goto error; |
| 7103 | |
| 7104 | case HLUA_E_ETMOUT: |
| 7105 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7106 | rule->arg.hlua_rule->fcn.name); |
| 7107 | goto error; |
| 7108 | |
| 7109 | case HLUA_E_NOMEM: |
| 7110 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7111 | rule->arg.hlua_rule->fcn.name); |
| 7112 | goto error; |
| 7113 | |
| 7114 | case HLUA_E_YIELD: /* unexpected */ |
| 7115 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7116 | rule->arg.hlua_rule->fcn.name); |
| 7117 | goto error; |
| 7118 | |
| 7119 | case HLUA_E_ERR: |
| 7120 | /* Display log. */ |
| 7121 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7122 | rule->arg.hlua_rule->fcn.name); |
| 7123 | goto error; |
| 7124 | |
| 7125 | default: |
| 7126 | goto error; |
| 7127 | } |
| 7128 | } |
| 7129 | |
| 7130 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7131 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7132 | goto done; |
| 7133 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7134 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7135 | goto error; |
| 7136 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7137 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7138 | 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] | 7139 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7140 | si_rx_room_blk(si); |
| 7141 | goto out; |
| 7142 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7143 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7144 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7145 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7146 | } |
| 7147 | |
| 7148 | done: |
| 7149 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7150 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7151 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7152 | si_shutr(si); |
| 7153 | } |
| 7154 | |
| 7155 | /* eat the whole request */ |
| 7156 | if (co_data(req)) { |
| 7157 | req_htx = htx_from_buf(&req->buf); |
| 7158 | co_htx_skip(req, req_htx, co_data(req)); |
| 7159 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7160 | } |
| 7161 | } |
| 7162 | |
| 7163 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7164 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7165 | return; |
| 7166 | |
| 7167 | error: |
| 7168 | |
| 7169 | /* If we are in HTTP mode, and we are not send any |
| 7170 | * data, return a 500 server error in best effort: |
| 7171 | * if there is no room available in the buffer, |
| 7172 | * just close the connection. |
| 7173 | */ |
| 7174 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7175 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7176 | |
| 7177 | channel_erase(res); |
| 7178 | res->buf.data = b_data(err); |
| 7179 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7180 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7181 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7182 | } |
| 7183 | if (!(strm->flags & SF_ERR_MASK)) |
| 7184 | strm->flags |= SF_ERR_RESOURCE; |
| 7185 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7186 | goto done; |
| 7187 | } |
| 7188 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7189 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7190 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7191 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7192 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7193 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7194 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7195 | } |
| 7196 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7197 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7198 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7199 | * |
| 7200 | * This function can fail with an abort() due to an Lua critical error. |
| 7201 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7202 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7203 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7204 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7205 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7206 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7207 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7208 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7209 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7210 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7211 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7212 | if (!rule->arg.hlua_rule) { |
| 7213 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7214 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7215 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7216 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7217 | /* Memory for arguments. */ |
| 7218 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7219 | if (!rule->arg.hlua_rule->args) { |
| 7220 | memprintf(err, "out of memory error"); |
| 7221 | return ACT_RET_PRS_ERR; |
| 7222 | } |
| 7223 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7224 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7225 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7226 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7227 | /* Expect some arguments */ |
| 7228 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7229 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7230 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7231 | return ACT_RET_PRS_ERR; |
| 7232 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7233 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7234 | if (!rule->arg.hlua_rule->args[i]) { |
| 7235 | memprintf(err, "out of memory error"); |
| 7236 | return ACT_RET_PRS_ERR; |
| 7237 | } |
| 7238 | (*cur_arg)++; |
| 7239 | } |
| 7240 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7241 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7242 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7243 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7244 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7245 | } |
| 7246 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7247 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7248 | struct act_rule *rule, char **err) |
| 7249 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7250 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7251 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7252 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7253 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7254 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7255 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7256 | * the call of this analyzer. |
| 7257 | */ |
| 7258 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7259 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7260 | return ACT_RET_PRS_ERR; |
| 7261 | } |
| 7262 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7263 | /* Memory for the rule. */ |
| 7264 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7265 | if (!rule->arg.hlua_rule) { |
| 7266 | memprintf(err, "out of memory error"); |
| 7267 | return ACT_RET_PRS_ERR; |
| 7268 | } |
| 7269 | |
| 7270 | /* Reference the Lua function and store the reference. */ |
| 7271 | rule->arg.hlua_rule->fcn = *fcn; |
| 7272 | |
| 7273 | /* TODO: later accept arguments. */ |
| 7274 | rule->arg.hlua_rule->args = NULL; |
| 7275 | |
| 7276 | /* Add applet pointer in the rule. */ |
| 7277 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7278 | rule->applet.name = fcn->name; |
| 7279 | rule->applet.init = hlua_applet_http_init; |
| 7280 | rule->applet.fct = hlua_applet_http_fct; |
| 7281 | rule->applet.release = hlua_applet_http_release; |
| 7282 | rule->applet.timeout = hlua_timeout_applet; |
| 7283 | |
| 7284 | return ACT_RET_PRS_OK; |
| 7285 | } |
| 7286 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7287 | /* This function is an LUA binding used for registering |
| 7288 | * "sample-conv" functions. It expects a converter name used |
| 7289 | * in the haproxy configuration file, and an LUA function. |
| 7290 | */ |
| 7291 | __LJMP static int hlua_register_action(lua_State *L) |
| 7292 | { |
| 7293 | struct action_kw_list *akl; |
| 7294 | const char *name; |
| 7295 | int ref; |
| 7296 | int len; |
| 7297 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7298 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7299 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7300 | /* Initialise the number of expected arguments at 0. */ |
| 7301 | nargs = 0; |
| 7302 | |
| 7303 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7304 | 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] | 7305 | |
| 7306 | /* First argument : converter name. */ |
| 7307 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7308 | |
| 7309 | /* Second argument : environment. */ |
| 7310 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7311 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7312 | |
| 7313 | /* Third argument : lua function. */ |
| 7314 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7315 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7316 | /* 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] | 7317 | if (lua_gettop(L) >= 4) |
| 7318 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7319 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7320 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7321 | lua_pushnil(L); |
| 7322 | while (lua_next(L, 2) != 0) { |
| 7323 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7324 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7325 | |
| 7326 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7327 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7328 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7329 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7330 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7331 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7332 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7333 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7334 | |
| 7335 | /* Fill fcn. */ |
| 7336 | fcn->name = strdup(name); |
| 7337 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7338 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7339 | fcn->function_ref = ref; |
| 7340 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7341 | /* Set the expected number od arguments. */ |
| 7342 | fcn->nargs = nargs; |
| 7343 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7344 | /* List head */ |
| 7345 | akl->list.n = akl->list.p = NULL; |
| 7346 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7347 | /* action keyword. */ |
| 7348 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7349 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7350 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7351 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7352 | |
| 7353 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7354 | |
| 7355 | akl->kw[0].match_pfx = 0; |
| 7356 | akl->kw[0].private = fcn; |
| 7357 | akl->kw[0].parse = action_register_lua; |
| 7358 | |
| 7359 | /* select the action registering point. */ |
| 7360 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7361 | tcp_req_cont_keywords_register(akl); |
| 7362 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7363 | tcp_res_cont_keywords_register(akl); |
| 7364 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7365 | http_req_keywords_register(akl); |
| 7366 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7367 | http_res_keywords_register(akl); |
| 7368 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7369 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7370 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7371 | "are expected.", lua_tostring(L, -1))); |
| 7372 | |
| 7373 | /* pop the environment string. */ |
| 7374 | lua_pop(L, 1); |
| 7375 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7376 | return ACT_RET_PRS_OK; |
| 7377 | } |
| 7378 | |
| 7379 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7380 | struct act_rule *rule, char **err) |
| 7381 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7382 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7383 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7384 | if (px->mode == PR_MODE_HTTP) { |
| 7385 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7386 | return ACT_RET_PRS_ERR; |
| 7387 | } |
| 7388 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7389 | /* Memory for the rule. */ |
| 7390 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7391 | if (!rule->arg.hlua_rule) { |
| 7392 | memprintf(err, "out of memory error"); |
| 7393 | return ACT_RET_PRS_ERR; |
| 7394 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7395 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7396 | /* Reference the Lua function and store the reference. */ |
| 7397 | rule->arg.hlua_rule->fcn = *fcn; |
| 7398 | |
| 7399 | /* TODO: later accept arguments. */ |
| 7400 | rule->arg.hlua_rule->args = NULL; |
| 7401 | |
| 7402 | /* Add applet pointer in the rule. */ |
| 7403 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7404 | rule->applet.name = fcn->name; |
| 7405 | rule->applet.init = hlua_applet_tcp_init; |
| 7406 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7407 | rule->applet.release = hlua_applet_tcp_release; |
| 7408 | rule->applet.timeout = hlua_timeout_applet; |
| 7409 | |
| 7410 | return 0; |
| 7411 | } |
| 7412 | |
| 7413 | /* This function is an LUA binding used for registering |
| 7414 | * "sample-conv" functions. It expects a converter name used |
| 7415 | * in the haproxy configuration file, and an LUA function. |
| 7416 | */ |
| 7417 | __LJMP static int hlua_register_service(lua_State *L) |
| 7418 | { |
| 7419 | struct action_kw_list *akl; |
| 7420 | const char *name; |
| 7421 | const char *env; |
| 7422 | int ref; |
| 7423 | int len; |
| 7424 | struct hlua_function *fcn; |
| 7425 | |
| 7426 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7427 | |
| 7428 | /* First argument : converter name. */ |
| 7429 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7430 | |
| 7431 | /* Second argument : environment. */ |
| 7432 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7433 | |
| 7434 | /* Third argument : lua function. */ |
| 7435 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7436 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7437 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7438 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7439 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7440 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7441 | fcn = calloc(1, sizeof(*fcn)); |
| 7442 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7443 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7444 | |
| 7445 | /* Fill fcn. */ |
| 7446 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7447 | fcn->name = calloc(1, len); |
| 7448 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7449 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7450 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7451 | fcn->function_ref = ref; |
| 7452 | |
| 7453 | /* List head */ |
| 7454 | akl->list.n = akl->list.p = NULL; |
| 7455 | |
| 7456 | /* converter keyword. */ |
| 7457 | len = strlen("lua.") + strlen(name) + 1; |
| 7458 | akl->kw[0].kw = calloc(1, len); |
| 7459 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7460 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7461 | |
| 7462 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7463 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7464 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7465 | if (strcmp(env, "tcp") == 0) |
| 7466 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7467 | else if (strcmp(env, "http") == 0) |
| 7468 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7469 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7470 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7471 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7472 | |
| 7473 | akl->kw[0].match_pfx = 0; |
| 7474 | akl->kw[0].private = fcn; |
| 7475 | |
| 7476 | /* End of array. */ |
| 7477 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7478 | |
| 7479 | /* Register this new converter */ |
| 7480 | service_keywords_register(akl); |
| 7481 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7482 | return 0; |
| 7483 | } |
| 7484 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7485 | /* This function initialises Lua cli handler. It copies the |
| 7486 | * arguments in the Lua stack and create channel IO objects. |
| 7487 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7488 | 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] | 7489 | { |
| 7490 | struct hlua *hlua; |
| 7491 | struct hlua_function *fcn; |
| 7492 | int i; |
| 7493 | const char *error; |
| 7494 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7495 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7496 | appctx->ctx.hlua_cli.fcn = private; |
| 7497 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7498 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7499 | if (!hlua) { |
| 7500 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7501 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7502 | } |
| 7503 | HLUA_INIT(hlua); |
| 7504 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7505 | |
| 7506 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7507 | * 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] | 7508 | * applet_http. It is absolutely compatible. |
| 7509 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7510 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7511 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7512 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7513 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7514 | } |
| 7515 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7516 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7517 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7518 | |
| 7519 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7520 | 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] | 7521 | 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] | 7522 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7523 | } |
| 7524 | |
| 7525 | /* The following Lua calls can fail. */ |
| 7526 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7527 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7528 | error = lua_tostring(hlua->T, -1); |
| 7529 | else |
| 7530 | error = "critical error"; |
| 7531 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7532 | goto error; |
| 7533 | } |
| 7534 | |
| 7535 | /* Check stack available size. */ |
| 7536 | if (!lua_checkstack(hlua->T, 2)) { |
| 7537 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7538 | goto error; |
| 7539 | } |
| 7540 | |
| 7541 | /* Restore the function in the stack. */ |
| 7542 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7543 | |
| 7544 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7545 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7546 | */ |
| 7547 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7548 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7549 | goto error; |
| 7550 | } |
| 7551 | hlua->nargs = 1; |
| 7552 | |
| 7553 | /* push keywords in the stack. */ |
| 7554 | for (i = 0; *args[i]; i++) { |
| 7555 | /* Check stack available size. */ |
| 7556 | if (!lua_checkstack(hlua->T, 1)) { |
| 7557 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7558 | goto error; |
| 7559 | } |
| 7560 | lua_pushstring(hlua->T, args[i]); |
| 7561 | hlua->nargs++; |
| 7562 | } |
| 7563 | |
| 7564 | /* We must initialize the execution timeouts. */ |
| 7565 | hlua->max_time = hlua_timeout_session; |
| 7566 | |
| 7567 | /* At this point the execution is safe. */ |
| 7568 | RESET_SAFE_LJMP(hlua->T); |
| 7569 | |
| 7570 | /* It's ok */ |
| 7571 | return 0; |
| 7572 | |
| 7573 | /* It's not ok. */ |
| 7574 | error: |
| 7575 | RESET_SAFE_LJMP(hlua->T); |
| 7576 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7577 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7578 | return 1; |
| 7579 | } |
| 7580 | |
| 7581 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7582 | { |
| 7583 | struct hlua *hlua; |
| 7584 | struct stream_interface *si; |
| 7585 | struct hlua_function *fcn; |
| 7586 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7587 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7588 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7589 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7590 | |
| 7591 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7592 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7593 | return 1; |
| 7594 | |
| 7595 | /* Execute the function. */ |
| 7596 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7597 | |
| 7598 | /* finished. */ |
| 7599 | case HLUA_E_OK: |
| 7600 | return 1; |
| 7601 | |
| 7602 | /* yield. */ |
| 7603 | case HLUA_E_AGAIN: |
| 7604 | /* We want write. */ |
| 7605 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7606 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7607 | /* Set the timeout. */ |
| 7608 | if (hlua->wake_time != TICK_ETERNITY) |
| 7609 | task_schedule(hlua->task, hlua->wake_time); |
| 7610 | return 0; |
| 7611 | |
| 7612 | /* finished with error. */ |
| 7613 | case HLUA_E_ERRMSG: |
| 7614 | /* Display log. */ |
| 7615 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7616 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7617 | lua_pop(hlua->T, 1); |
| 7618 | return 1; |
| 7619 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7620 | case HLUA_E_ETMOUT: |
| 7621 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7622 | fcn->name); |
| 7623 | return 1; |
| 7624 | |
| 7625 | case HLUA_E_NOMEM: |
| 7626 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7627 | fcn->name); |
| 7628 | return 1; |
| 7629 | |
| 7630 | case HLUA_E_YIELD: /* unexpected */ |
| 7631 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7632 | fcn->name); |
| 7633 | return 1; |
| 7634 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7635 | case HLUA_E_ERR: |
| 7636 | /* Display log. */ |
| 7637 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7638 | fcn->name); |
| 7639 | return 1; |
| 7640 | |
| 7641 | default: |
| 7642 | return 1; |
| 7643 | } |
| 7644 | |
| 7645 | return 1; |
| 7646 | } |
| 7647 | |
| 7648 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7649 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7650 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7651 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7652 | } |
| 7653 | |
| 7654 | /* This function is an LUA binding used for registering |
| 7655 | * new keywords in the cli. It expects a list of keywords |
| 7656 | * which are the "path". It is limited to 5 keywords. A |
| 7657 | * description of the command, a function to be executed |
| 7658 | * for the parsing and a function for io handlers. |
| 7659 | */ |
| 7660 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7661 | { |
| 7662 | struct cli_kw_list *cli_kws; |
| 7663 | const char *message; |
| 7664 | int ref_io; |
| 7665 | int len; |
| 7666 | struct hlua_function *fcn; |
| 7667 | int index; |
| 7668 | int i; |
| 7669 | |
| 7670 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7671 | |
| 7672 | /* First argument : an array of maximum 5 keywords. */ |
| 7673 | if (!lua_istable(L, 1)) |
| 7674 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7675 | |
| 7676 | /* Second argument : string with contextual message. */ |
| 7677 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7678 | |
| 7679 | /* Third and fourth argument : lua function. */ |
| 7680 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7681 | |
| 7682 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7683 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7684 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7685 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7686 | fcn = calloc(1, sizeof(*fcn)); |
| 7687 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7688 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7689 | |
| 7690 | /* Fill path. */ |
| 7691 | index = 0; |
| 7692 | lua_pushnil(L); |
| 7693 | while(lua_next(L, 1) != 0) { |
| 7694 | if (index >= 5) |
| 7695 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7696 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7697 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7698 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7699 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7700 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7701 | index++; |
| 7702 | lua_pop(L, 1); |
| 7703 | } |
| 7704 | |
| 7705 | /* Copy help message. */ |
| 7706 | cli_kws->kw[0].usage = strdup(message); |
| 7707 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7708 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7709 | |
| 7710 | /* Fill fcn io handler. */ |
| 7711 | len = strlen("<lua.cli>") + 1; |
| 7712 | for (i = 0; i < index; i++) |
| 7713 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7714 | fcn->name = calloc(1, len); |
| 7715 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7716 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7717 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7718 | for (i = 0; i < index; i++) { |
| 7719 | strncat((char *)fcn->name, ".", len); |
| 7720 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7721 | } |
| 7722 | strncat((char *)fcn->name, ">", len); |
| 7723 | fcn->function_ref = ref_io; |
| 7724 | |
| 7725 | /* Fill last entries. */ |
| 7726 | cli_kws->kw[0].private = fcn; |
| 7727 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7728 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7729 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7730 | |
| 7731 | /* Register this new converter */ |
| 7732 | cli_register_kw(cli_kws); |
| 7733 | |
| 7734 | return 0; |
| 7735 | } |
| 7736 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7737 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7738 | struct proxy *defpx, const char *file, int line, |
| 7739 | char **err, unsigned int *timeout) |
| 7740 | { |
| 7741 | const char *error; |
| 7742 | |
| 7743 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7744 | if (error == PARSE_TIME_OVER) { |
| 7745 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7746 | args[1], args[0]); |
| 7747 | return -1; |
| 7748 | } |
| 7749 | else if (error == PARSE_TIME_UNDER) { |
| 7750 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7751 | args[1], args[0]); |
| 7752 | return -1; |
| 7753 | } |
| 7754 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7755 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7756 | return -1; |
| 7757 | } |
| 7758 | return 0; |
| 7759 | } |
| 7760 | |
| 7761 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7762 | struct proxy *defpx, const char *file, int line, |
| 7763 | char **err) |
| 7764 | { |
| 7765 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7766 | file, line, err, &hlua_timeout_session); |
| 7767 | } |
| 7768 | |
| 7769 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7770 | struct proxy *defpx, const char *file, int line, |
| 7771 | char **err) |
| 7772 | { |
| 7773 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7774 | file, line, err, &hlua_timeout_task); |
| 7775 | } |
| 7776 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7777 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7778 | struct proxy *defpx, const char *file, int line, |
| 7779 | char **err) |
| 7780 | { |
| 7781 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7782 | file, line, err, &hlua_timeout_applet); |
| 7783 | } |
| 7784 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7785 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7786 | struct proxy *defpx, const char *file, int line, |
| 7787 | char **err) |
| 7788 | { |
| 7789 | char *error; |
| 7790 | |
| 7791 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7792 | if (*error != '\0') { |
| 7793 | memprintf(err, "%s: invalid number", args[0]); |
| 7794 | return -1; |
| 7795 | } |
| 7796 | return 0; |
| 7797 | } |
| 7798 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7799 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7800 | struct proxy *defpx, const char *file, int line, |
| 7801 | char **err) |
| 7802 | { |
| 7803 | char *error; |
| 7804 | |
| 7805 | if (*(args[1]) == 0) { |
| 7806 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7807 | return -1; |
| 7808 | } |
| 7809 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7810 | if (*error != '\0') { |
| 7811 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7812 | return -1; |
| 7813 | } |
| 7814 | return 0; |
| 7815 | } |
| 7816 | |
| 7817 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7818 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7819 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7820 | * the main lua entry point. |
| 7821 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7822 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7823 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7824 | * |
| 7825 | * In some error case, LUA set an error message in top of the stack. This function |
| 7826 | * 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] | 7827 | * |
| 7828 | * This function can fail with an abort() due to an Lua critical error. |
| 7829 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7830 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7831 | */ |
| 7832 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7833 | struct proxy *defpx, const char *file, int line, |
| 7834 | char **err) |
| 7835 | { |
| 7836 | int error; |
| 7837 | |
| 7838 | /* Just load and compile the file. */ |
| 7839 | error = luaL_loadfile(gL.T, args[1]); |
| 7840 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7841 | 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] | 7842 | lua_pop(gL.T, 1); |
| 7843 | return -1; |
| 7844 | } |
| 7845 | |
| 7846 | /* If no syntax error where detected, execute the code. */ |
| 7847 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7848 | switch (error) { |
| 7849 | case LUA_OK: |
| 7850 | break; |
| 7851 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7852 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7853 | lua_pop(gL.T, 1); |
| 7854 | return -1; |
| 7855 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7856 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7857 | return -1; |
| 7858 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7859 | 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] | 7860 | lua_pop(gL.T, 1); |
| 7861 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 7862 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7863 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7864 | 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] | 7865 | lua_pop(gL.T, 1); |
| 7866 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 7867 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7868 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7869 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7870 | lua_pop(gL.T, 1); |
| 7871 | return -1; |
| 7872 | } |
| 7873 | |
| 7874 | return 0; |
| 7875 | } |
| 7876 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7877 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7878 | * in the given <ctx>. |
| 7879 | */ |
| 7880 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7881 | { |
| 7882 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7883 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7884 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7885 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7886 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7887 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7888 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7889 | |
| 7890 | return 0; |
| 7891 | } |
| 7892 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7893 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7894 | struct proxy *defpx, const char *file, int line, |
| 7895 | char **err) |
| 7896 | { |
| 7897 | char *path; |
| 7898 | char *type = "path"; |
| 7899 | if (too_many_args(2, args, err, NULL)) { |
| 7900 | return -1; |
| 7901 | } |
| 7902 | |
| 7903 | if (!(*args[1])) { |
| 7904 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7905 | return -1; |
| 7906 | } |
| 7907 | path = args[1]; |
| 7908 | |
| 7909 | if (*args[2]) { |
| 7910 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7911 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7912 | return -1; |
| 7913 | } |
| 7914 | type = args[2]; |
| 7915 | } |
| 7916 | |
| 7917 | return hlua_prepend_path(gL, type, path); |
| 7918 | } |
| 7919 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7920 | /* configuration keywords declaration */ |
| 7921 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7922 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7923 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7924 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7925 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7926 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7927 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7928 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7929 | { 0, NULL, NULL }, |
| 7930 | }}; |
| 7931 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7932 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7933 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7934 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7935 | /* This function can fail with an abort() due to an Lua critical error. |
| 7936 | * We are in the initialisation process of HAProxy, this abort() is |
| 7937 | * tolerated. |
| 7938 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7939 | int hlua_post_init() |
| 7940 | { |
| 7941 | struct hlua_init_function *init; |
| 7942 | const char *msg; |
| 7943 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7944 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7945 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7946 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7947 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7948 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7949 | error = lua_tostring(gL.T, -1); |
| 7950 | else |
| 7951 | error = "critical error"; |
| 7952 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7953 | exit(1); |
| 7954 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7955 | |
| 7956 | #if USE_OPENSSL |
| 7957 | /* Initialize SSL server. */ |
| 7958 | if (socket_ssl.xprt->prepare_srv) { |
| 7959 | int saved_used_backed = global.ssl_used_backend; |
| 7960 | // don't affect maxconn automatic computation |
| 7961 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7962 | global.ssl_used_backend = saved_used_backed; |
| 7963 | } |
| 7964 | #endif |
| 7965 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7966 | hlua_fcn_post_init(gL.T); |
| 7967 | RESET_SAFE_LJMP(gL.T); |
| 7968 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7969 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7970 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7971 | ret = hlua_ctx_resume(&gL, 0); |
| 7972 | switch (ret) { |
| 7973 | case HLUA_E_OK: |
| 7974 | lua_pop(gL.T, -1); |
| 7975 | return 1; |
| 7976 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7977 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7978 | return 0; |
| 7979 | case HLUA_E_ERRMSG: |
| 7980 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7981 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7982 | return 0; |
| 7983 | case HLUA_E_ERR: |
| 7984 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7985 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7986 | return 0; |
| 7987 | } |
| 7988 | } |
| 7989 | return 1; |
| 7990 | } |
| 7991 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7992 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7993 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7994 | * is the previously allocated size or the kind of object in case of a new |
| 7995 | * allocation. <nsize> is the requested new size. |
| 7996 | */ |
| 7997 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7998 | { |
| 7999 | struct hlua_mem_allocator *zone = ud; |
| 8000 | |
| 8001 | if (nsize == 0) { |
| 8002 | /* it's a free */ |
| 8003 | if (ptr) |
| 8004 | zone->allocated -= osize; |
| 8005 | free(ptr); |
| 8006 | return NULL; |
| 8007 | } |
| 8008 | |
| 8009 | if (!ptr) { |
| 8010 | /* it's a new allocation */ |
| 8011 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8012 | return NULL; |
| 8013 | |
| 8014 | ptr = malloc(nsize); |
| 8015 | if (ptr) |
| 8016 | zone->allocated += nsize; |
| 8017 | return ptr; |
| 8018 | } |
| 8019 | |
| 8020 | /* it's a realloc */ |
| 8021 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8022 | return NULL; |
| 8023 | |
| 8024 | ptr = realloc(ptr, nsize); |
| 8025 | if (ptr) |
| 8026 | zone->allocated += nsize - osize; |
| 8027 | return ptr; |
| 8028 | } |
| 8029 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8030 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8031 | * We are in the initialisation process of HAProxy, this abort() is |
| 8032 | * tolerated. |
| 8033 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8034 | void hlua_init(void) |
| 8035 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8036 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8037 | int idx; |
| 8038 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8039 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8040 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8041 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8042 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8043 | struct srv_kw *kw; |
| 8044 | int tmp_error; |
| 8045 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8046 | char *args[] = { /* SSL client configuration. */ |
| 8047 | "ssl", |
| 8048 | "verify", |
| 8049 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8050 | NULL |
| 8051 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8052 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8053 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8054 | /* Init main lua stack. */ |
| 8055 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8056 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8057 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8058 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8059 | hlua_sethlua(&gL); |
| 8060 | gL.Tref = LUA_REFNIL; |
| 8061 | gL.task = NULL; |
| 8062 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8063 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8064 | * the Lua function can fail with an abort. We are in the initialisation |
| 8065 | * process of HAProxy, this abort() is tolerated. |
| 8066 | */ |
| 8067 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8068 | /* Initialise lua. */ |
| 8069 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8070 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8071 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8072 | #ifdef HLUA_PREPEND_PATH |
| 8073 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8074 | #endif |
| 8075 | #ifdef HLUA_PREPEND_CPATH |
| 8076 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8077 | #endif |
| 8078 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8079 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8080 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8081 | /* Set safe environment for the initialisation. */ |
| 8082 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8083 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8084 | error_msg = lua_tostring(gL.T, -1); |
| 8085 | else |
| 8086 | error_msg = "critical error"; |
| 8087 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8088 | exit(1); |
| 8089 | } |
| 8090 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8091 | /* |
| 8092 | * |
| 8093 | * Create "core" object. |
| 8094 | * |
| 8095 | */ |
| 8096 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8097 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8098 | lua_newtable(gL.T); |
| 8099 | |
| 8100 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8101 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8102 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8103 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8104 | /* Register special functions. */ |
| 8105 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8106 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8107 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8108 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8109 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8110 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8111 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8112 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8113 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8114 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8115 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8116 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8117 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8118 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8119 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8120 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8121 | hlua_class_function(gL.T, "log", hlua_log); |
| 8122 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8123 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8124 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8125 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8126 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8127 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8128 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8129 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8130 | |
| 8131 | /* |
| 8132 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8133 | * Create "act" object. |
| 8134 | * |
| 8135 | */ |
| 8136 | |
| 8137 | /* This table entry is the object "act" base. */ |
| 8138 | lua_newtable(gL.T); |
| 8139 | |
| 8140 | /* push action return constants */ |
| 8141 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8142 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8143 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8144 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8145 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8146 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8147 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8148 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8149 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8150 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8151 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8152 | lua_setglobal(gL.T, "act"); |
| 8153 | |
| 8154 | /* |
| 8155 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8156 | * Register class Map |
| 8157 | * |
| 8158 | */ |
| 8159 | |
| 8160 | /* This table entry is the object "Map" base. */ |
| 8161 | lua_newtable(gL.T); |
| 8162 | |
| 8163 | /* register pattern types. */ |
| 8164 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8165 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8166 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8167 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8168 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8169 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8170 | |
| 8171 | /* register constructor. */ |
| 8172 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8173 | |
| 8174 | /* Create and fill the metatable. */ |
| 8175 | lua_newtable(gL.T); |
| 8176 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8177 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8178 | lua_pushstring(gL.T, "__index"); |
| 8179 | lua_newtable(gL.T); |
| 8180 | |
| 8181 | /* Register . */ |
| 8182 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8183 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8184 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8185 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8186 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8187 | /* Register previous table in the registry with reference and named entry. |
| 8188 | * The function hlua_register_metatable() pops the stack, so we |
| 8189 | * previously create a copy of the table. |
| 8190 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8191 | 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] | 8192 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8193 | |
| 8194 | /* Assign the metatable to the mai Map object. */ |
| 8195 | lua_setmetatable(gL.T, -2); |
| 8196 | |
| 8197 | /* Set a name to the table. */ |
| 8198 | lua_setglobal(gL.T, "Map"); |
| 8199 | |
| 8200 | /* |
| 8201 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8202 | * Register class Channel |
| 8203 | * |
| 8204 | */ |
| 8205 | |
| 8206 | /* Create and fill the metatable. */ |
| 8207 | lua_newtable(gL.T); |
| 8208 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8209 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8210 | lua_pushstring(gL.T, "__index"); |
| 8211 | lua_newtable(gL.T); |
| 8212 | |
| 8213 | /* Register . */ |
| 8214 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8215 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8216 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8217 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8218 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8219 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8220 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8221 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8222 | 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] | 8223 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8224 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8225 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8226 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8227 | |
| 8228 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8229 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8230 | |
| 8231 | /* |
| 8232 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8233 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8234 | * |
| 8235 | */ |
| 8236 | |
| 8237 | /* Create and fill the metatable. */ |
| 8238 | lua_newtable(gL.T); |
| 8239 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8240 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8241 | lua_pushstring(gL.T, "__index"); |
| 8242 | lua_newtable(gL.T); |
| 8243 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8244 | /* Browse existing fetches and create the associated |
| 8245 | * object method. |
| 8246 | */ |
| 8247 | sf = NULL; |
| 8248 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8249 | |
| 8250 | /* Dont register the keywork if the arguments check function are |
| 8251 | * not safe during the runtime. |
| 8252 | */ |
| 8253 | if ((sf->val_args != NULL) && |
| 8254 | (sf->val_args != val_payload_lv) && |
| 8255 | (sf->val_args != val_hdr)) |
| 8256 | continue; |
| 8257 | |
| 8258 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8259 | * by an underscore. |
| 8260 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8261 | strncpy(trash.area, sf->kw, trash.size); |
| 8262 | trash.area[trash.size - 1] = '\0'; |
| 8263 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8264 | if (*p == '.' || *p == '-' || *p == '+') |
| 8265 | *p = '_'; |
| 8266 | |
| 8267 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8268 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8269 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8270 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8271 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8272 | } |
| 8273 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8274 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8275 | |
| 8276 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8277 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8278 | |
| 8279 | /* |
| 8280 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8281 | * Register class Converters |
| 8282 | * |
| 8283 | */ |
| 8284 | |
| 8285 | /* Create and fill the metatable. */ |
| 8286 | lua_newtable(gL.T); |
| 8287 | |
| 8288 | /* Create and fill the __index entry. */ |
| 8289 | lua_pushstring(gL.T, "__index"); |
| 8290 | lua_newtable(gL.T); |
| 8291 | |
| 8292 | /* Browse existing converters and create the associated |
| 8293 | * object method. |
| 8294 | */ |
| 8295 | sc = NULL; |
| 8296 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8297 | /* Dont register the keywork if the arguments check function are |
| 8298 | * not safe during the runtime. |
| 8299 | */ |
| 8300 | if (sc->val_args != NULL) |
| 8301 | continue; |
| 8302 | |
| 8303 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8304 | * by an underscore. |
| 8305 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8306 | strncpy(trash.area, sc->kw, trash.size); |
| 8307 | trash.area[trash.size - 1] = '\0'; |
| 8308 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8309 | if (*p == '.' || *p == '-' || *p == '+') |
| 8310 | *p = '_'; |
| 8311 | |
| 8312 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8313 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8314 | lua_pushlightuserdata(gL.T, sc); |
| 8315 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8316 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8317 | } |
| 8318 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8319 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8320 | |
| 8321 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8322 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8323 | |
| 8324 | /* |
| 8325 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8326 | * Register class HTTP |
| 8327 | * |
| 8328 | */ |
| 8329 | |
| 8330 | /* Create and fill the metatable. */ |
| 8331 | lua_newtable(gL.T); |
| 8332 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8333 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8334 | lua_pushstring(gL.T, "__index"); |
| 8335 | lua_newtable(gL.T); |
| 8336 | |
| 8337 | /* Register Lua functions. */ |
| 8338 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8339 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8340 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8341 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8342 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8343 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8344 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8345 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8346 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8347 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8348 | |
| 8349 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8350 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8351 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8352 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8353 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8354 | 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] | 8355 | 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] | 8356 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8357 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8358 | |
| 8359 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8360 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8361 | |
| 8362 | /* |
| 8363 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8364 | * Register class AppletTCP |
| 8365 | * |
| 8366 | */ |
| 8367 | |
| 8368 | /* Create and fill the metatable. */ |
| 8369 | lua_newtable(gL.T); |
| 8370 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8371 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8372 | lua_pushstring(gL.T, "__index"); |
| 8373 | lua_newtable(gL.T); |
| 8374 | |
| 8375 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8376 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8377 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8378 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8379 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8380 | 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] | 8381 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8382 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8383 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8384 | |
| 8385 | lua_settable(gL.T, -3); |
| 8386 | |
| 8387 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8388 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8389 | |
| 8390 | /* |
| 8391 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8392 | * Register class AppletHTTP |
| 8393 | * |
| 8394 | */ |
| 8395 | |
| 8396 | /* Create and fill the metatable. */ |
| 8397 | lua_newtable(gL.T); |
| 8398 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8399 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8400 | lua_pushstring(gL.T, "__index"); |
| 8401 | lua_newtable(gL.T); |
| 8402 | |
| 8403 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8404 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8405 | 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] | 8406 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8407 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8408 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8409 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8410 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8411 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8412 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8413 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8414 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8415 | |
| 8416 | lua_settable(gL.T, -3); |
| 8417 | |
| 8418 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8419 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8420 | |
| 8421 | /* |
| 8422 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8423 | * Register class TXN |
| 8424 | * |
| 8425 | */ |
| 8426 | |
| 8427 | /* Create and fill the metatable. */ |
| 8428 | lua_newtable(gL.T); |
| 8429 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8430 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8431 | lua_pushstring(gL.T, "__index"); |
| 8432 | lua_newtable(gL.T); |
| 8433 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8434 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8435 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8436 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8437 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8438 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8439 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8440 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8441 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8442 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8443 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8444 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8445 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8446 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8447 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8448 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8449 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8450 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8451 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8452 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8453 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8454 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8455 | |
| 8456 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8457 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8458 | |
| 8459 | /* |
| 8460 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8461 | * Register class reply |
| 8462 | * |
| 8463 | */ |
| 8464 | lua_newtable(gL.T); |
| 8465 | lua_pushstring(gL.T, "__index"); |
| 8466 | lua_newtable(gL.T); |
| 8467 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8468 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8469 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8470 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8471 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8472 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8473 | |
| 8474 | |
| 8475 | /* |
| 8476 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8477 | * Register class Socket |
| 8478 | * |
| 8479 | */ |
| 8480 | |
| 8481 | /* Create and fill the metatable. */ |
| 8482 | lua_newtable(gL.T); |
| 8483 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8484 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8485 | lua_pushstring(gL.T, "__index"); |
| 8486 | lua_newtable(gL.T); |
| 8487 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8488 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8489 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8490 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8491 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8492 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8493 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8494 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8495 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8496 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8497 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8498 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8499 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8500 | 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] | 8501 | |
| 8502 | /* Register the garbage collector entry. */ |
| 8503 | lua_pushstring(gL.T, "__gc"); |
| 8504 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8505 | 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] | 8506 | |
| 8507 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8508 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8509 | |
| 8510 | /* Proxy and server configuration initialisation. */ |
| 8511 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8512 | init_new_proxy(&socket_proxy); |
| 8513 | socket_proxy.parent = NULL; |
| 8514 | socket_proxy.last_change = now.tv_sec; |
| 8515 | socket_proxy.id = "LUA-SOCKET"; |
| 8516 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8517 | socket_proxy.maxconn = 0; |
| 8518 | socket_proxy.accept = NULL; |
| 8519 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8520 | socket_proxy.srv = NULL; |
| 8521 | socket_proxy.conn_retries = 0; |
| 8522 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8523 | |
| 8524 | /* Init TCP server: unchanged parameters */ |
| 8525 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8526 | socket_tcp.next = NULL; |
| 8527 | socket_tcp.proxy = &socket_proxy; |
| 8528 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8529 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8530 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8531 | socket_tcp.idle_conns = NULL; |
| 8532 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8533 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8534 | socket_tcp.last_change = 0; |
| 8535 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8536 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8537 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8538 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8539 | |
| 8540 | /* XXX: Copy default parameter from default server, |
| 8541 | * but the default server is not initialized. |
| 8542 | */ |
| 8543 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8544 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8545 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8546 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8547 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8548 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8549 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8550 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8551 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8552 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8553 | |
| 8554 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8555 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8556 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8557 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8558 | socket_tcp.check.server = &socket_tcp; |
| 8559 | |
| 8560 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8561 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8562 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8563 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8564 | socket_tcp.agent.server = &socket_tcp; |
| 8565 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8566 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8567 | |
| 8568 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8569 | /* Init TCP server: unchanged parameters */ |
| 8570 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8571 | socket_ssl.next = NULL; |
| 8572 | socket_ssl.proxy = &socket_proxy; |
| 8573 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8574 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8575 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8576 | socket_ssl.idle_conns = NULL; |
| 8577 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8578 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8579 | socket_ssl.last_change = 0; |
| 8580 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8581 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8582 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8583 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8584 | |
| 8585 | /* XXX: Copy default parameter from default server, |
| 8586 | * but the default server is not initialized. |
| 8587 | */ |
| 8588 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8589 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8590 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8591 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8592 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8593 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8594 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8595 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8596 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8597 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8598 | |
| 8599 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8600 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8601 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8602 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8603 | socket_ssl.check.server = &socket_ssl; |
| 8604 | |
| 8605 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8606 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8607 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8608 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8609 | socket_ssl.agent.server = &socket_ssl; |
| 8610 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8611 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8612 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8613 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8614 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8615 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8616 | /* |
| 8617 | * |
| 8618 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8619 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8620 | * features like client certificates and ssl_verify. |
| 8621 | * |
| 8622 | */ |
| 8623 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8624 | if (tmp_error != 0) { |
| 8625 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8626 | abort(); /* This must be never arrives because the command line |
| 8627 | not editable by the user. */ |
| 8628 | } |
| 8629 | idx += kw->skip; |
| 8630 | } |
| 8631 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8632 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8633 | |
| 8634 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8635 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8636 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8637 | static void hlua_deinit() |
| 8638 | { |
| 8639 | lua_close(gL.T); |
| 8640 | } |
| 8641 | |
| 8642 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8643 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8644 | static void hlua_register_build_options(void) |
| 8645 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8646 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8647 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8648 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8649 | hap_register_build_opts(ptr, 1); |
| 8650 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8651 | |
| 8652 | INITCALL0(STG_REGISTER, hlua_register_build_options); |