Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 14 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 16 | #include <lauxlib.h> |
| 17 | #include <lua.h> |
| 18 | #include <lualib.h> |
| 19 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 20 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 21 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 24 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 26 | #include <haproxy/api.h> |
| 27 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 28 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 29 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 30 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 31 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 32 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 33 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 34 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 35 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 36 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 37 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 38 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 39 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 40 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 41 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 42 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 43 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 44 | #include <haproxy/payload.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 45 | #include <haproxy/proxy-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 46 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 47 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 48 | #include <haproxy/server-t.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 49 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 50 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 51 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 52 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 53 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 54 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 55 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 56 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 57 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/xref.h> |
| 59 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 60 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 61 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 62 | * macro is used only for identifying the function that can |
| 63 | * not return because a longjmp is executed. |
| 64 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 65 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 66 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 67 | */ |
| 68 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 69 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | #define MAY_LJMP(func) func |
| 71 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 72 | /* This couple of function executes securely some Lua calls outside of |
| 73 | * the lua runtime environment. Each Lua call can return a longjmp |
| 74 | * if it encounter a memory error. |
| 75 | * |
| 76 | * Lua documentation extract: |
| 77 | * |
| 78 | * If an error happens outside any protected environment, Lua calls |
| 79 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 80 | * exiting the host application. Your panic function can avoid this |
| 81 | * exit by never returning (e.g., doing a long jump to your own |
| 82 | * recovery point outside Lua). |
| 83 | * |
| 84 | * The panic function runs as if it were a message handler (see |
| 85 | * §2.3); in particular, the error message is at the top of the |
| 86 | * stack. However, there is no guarantee about stack space. To push |
| 87 | * anything on the stack, the panic function must first check the |
| 88 | * available space (see §4.2). |
| 89 | * |
| 90 | * We must check all the Lua entry point. This includes: |
| 91 | * - The include/proto/hlua.h exported functions |
| 92 | * - the task wrapper function |
| 93 | * - The action wrapper function |
| 94 | * - The converters wrapper function |
| 95 | * - The sample-fetch wrapper functions |
| 96 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 97 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 98 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 99 | * |
| 100 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 101 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 102 | * because they must be exists in the program stack when the longjmp |
| 103 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 104 | * |
| 105 | * Note that the Lua processing is not really thread safe. It provides |
| 106 | * heavy system which consists to add our own lock function in the Lua |
| 107 | * code and recompile the library. This system will probably not accepted |
| 108 | * by maintainers of various distribs. |
| 109 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 110 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 111 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 112 | * of function and a lua_unlock() at the end of the function. So I |
| 113 | * conclude that the Lua thread safe mode just perform a mutex around |
| 114 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 115 | * easier for distro maintainers. |
| 116 | * |
| 117 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 118 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 119 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 120 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 121 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 122 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 124 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 125 | |
| 126 | #define SET_SAFE_LJMP(__L) \ |
| 127 | ({ \ |
| 128 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 129 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 130 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 131 | lua_atpanic(__L, hlua_panic_safe); \ |
| 132 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 133 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | } else { \ |
| 135 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 136 | ret = 1; \ |
| 137 | } \ |
| 138 | ret; \ |
| 139 | }) |
| 140 | |
| 141 | /* If we are the last function catching Lua errors, we |
| 142 | * must reset the panic function. |
| 143 | */ |
| 144 | #define RESET_SAFE_LJMP(__L) \ |
| 145 | do { \ |
| 146 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 147 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 148 | } while(0) |
| 149 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 150 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 151 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 152 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 153 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 154 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 155 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 156 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 157 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 158 | /* The main Lua execution context. */ |
| 159 | struct hlua gL; |
| 160 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 161 | /* This is the memory pool containing struct lua for applets |
| 162 | * (including cli). |
| 163 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 164 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 165 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 166 | /* Used for Socket connection. */ |
| 167 | static struct proxy socket_proxy; |
| 168 | static struct server socket_tcp; |
| 169 | #ifdef USE_OPENSSL |
| 170 | static struct server socket_ssl; |
| 171 | #endif |
| 172 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 173 | /* List head of the function called at the initialisation time. */ |
| 174 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 175 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 176 | /* The following variables contains the reference of the different |
| 177 | * Lua classes. These references are useful for identify metadata |
| 178 | * associated with an object. |
| 179 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 180 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 181 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 182 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 183 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 184 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 185 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 186 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 187 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 188 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 189 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 190 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 191 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 192 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 193 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 194 | * because a task may remain alive during all the haproxy execution. |
| 195 | */ |
| 196 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 197 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 198 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 199 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 200 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 201 | * it is used for preventing infinite loops. |
| 202 | * |
| 203 | * I test the scheer with an infinite loop containing one incrementation |
| 204 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 205 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 206 | * to one interrupt each 10 000 instructions. |
| 207 | * |
| 208 | * configured | Number of |
| 209 | * instructions | loops executed |
| 210 | * between two | in milions |
| 211 | * forced yields | |
| 212 | * ---------------+--------------- |
| 213 | * 10 | 160 |
| 214 | * 500 | 670 |
| 215 | * 1000 | 680 |
| 216 | * 5000 | 700 |
| 217 | * 7000 | 700 |
| 218 | * 8000 | 700 |
| 219 | * 9000 | 710 <- ceil |
| 220 | * 10000 | 710 |
| 221 | * 100000 | 710 |
| 222 | * 1000000 | 710 |
| 223 | * |
| 224 | */ |
| 225 | static unsigned int hlua_nb_instruction = 10000; |
| 226 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 227 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 228 | * be enforced on any memory allocation. |
| 229 | */ |
| 230 | struct hlua_mem_allocator { |
| 231 | size_t allocated; |
| 232 | size_t limit; |
| 233 | }; |
| 234 | |
| 235 | static struct hlua_mem_allocator hlua_global_allocator; |
| 236 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 237 | /* These functions converts types between HAProxy internal args or |
| 238 | * sample and LUA types. Another function permits to check if the |
| 239 | * LUA stack contains arguments according with an required ARG_T |
| 240 | * format. |
| 241 | */ |
| 242 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 243 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 244 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 245 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 246 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 247 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 248 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 249 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 250 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 251 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 252 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 253 | do { \ |
| 254 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 255 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 256 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 257 | } while (0) |
| 258 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 259 | /* Used to check an Lua function type in the stack. It creates and |
| 260 | * returns a reference of the function. This function throws an |
| 261 | * error if the rgument is not a "function". |
| 262 | */ |
| 263 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 264 | { |
| 265 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 266 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 267 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 268 | } |
| 269 | lua_pushvalue(L, argno); |
| 270 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 271 | } |
| 272 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 273 | /* Return the string that is of the top of the stack. */ |
| 274 | const char *hlua_get_top_error_string(lua_State *L) |
| 275 | { |
| 276 | if (lua_gettop(L) < 1) |
| 277 | return "unknown error"; |
| 278 | if (lua_type(L, -1) != LUA_TSTRING) |
| 279 | return "unknown error"; |
| 280 | return lua_tostring(L, -1); |
| 281 | } |
| 282 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 283 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 284 | { |
| 285 | lua_Debug ar; |
| 286 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 287 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 288 | int filled = 0; |
| 289 | |
| 290 | while (lua_getstack(L, level++, &ar)) { |
| 291 | |
| 292 | /* Add separator */ |
| 293 | if (filled) |
| 294 | chunk_appendf(msg, ", "); |
| 295 | filled = 1; |
| 296 | |
| 297 | /* Fill fields: |
| 298 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 299 | * 'l': fills in the field currentline; |
| 300 | * 'n': fills in the field name and namewhat; |
| 301 | * 't': fills in the field istailcall; |
| 302 | */ |
| 303 | lua_getinfo(L, "Slnt", &ar); |
| 304 | |
| 305 | /* Append code localisation */ |
| 306 | if (ar.currentline > 0) |
| 307 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 308 | else |
| 309 | chunk_appendf(msg, "%s ", ar.short_src); |
| 310 | |
| 311 | /* |
| 312 | * Get function name |
| 313 | * |
| 314 | * if namewhat is no empty, name is defined. |
| 315 | * what contains "Lua" for Lua function, "C" for C function, |
| 316 | * or "main" for main code. |
| 317 | */ |
| 318 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 319 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 320 | |
| 321 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 322 | chunk_appendf(msg, "main chunk"); |
| 323 | |
| 324 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 325 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 326 | |
| 327 | else /* nothing left... */ |
| 328 | chunk_appendf(msg, "?"); |
| 329 | |
| 330 | |
| 331 | /* Display tailed call */ |
| 332 | if (ar.istailcall) |
| 333 | chunk_appendf(msg, " ..."); |
| 334 | } |
| 335 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 336 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 340 | /* This function check the number of arguments available in the |
| 341 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 342 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 343 | */ |
| 344 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 345 | { |
| 346 | if (lua_gettop(L) == nb) |
| 347 | return; |
| 348 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 349 | } |
| 350 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 351 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 352 | * and the line number where the error is encountered. |
| 353 | */ |
| 354 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 355 | { |
| 356 | va_list argp; |
| 357 | va_start(argp, fmt); |
| 358 | luaL_where(L, 1); |
| 359 | lua_pushvfstring(L, fmt, argp); |
| 360 | va_end(argp); |
| 361 | lua_concat(L, 2); |
| 362 | return 1; |
| 363 | } |
| 364 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 365 | /* This functions is used with sample fetch and converters. It |
| 366 | * converts the HAProxy configuration argument in a lua stack |
| 367 | * values. |
| 368 | * |
| 369 | * It takes an array of "arg", and each entry of the array is |
| 370 | * converted and pushed in the LUA stack. |
| 371 | */ |
| 372 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 373 | { |
| 374 | switch (arg->type) { |
| 375 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 376 | case ARGT_TIME: |
| 377 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 378 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | break; |
| 380 | |
| 381 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 382 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 383 | break; |
| 384 | |
| 385 | case ARGT_IPV4: |
| 386 | case ARGT_IPV6: |
| 387 | case ARGT_MSK4: |
| 388 | case ARGT_MSK6: |
| 389 | case ARGT_FE: |
| 390 | case ARGT_BE: |
| 391 | case ARGT_TAB: |
| 392 | case ARGT_SRV: |
| 393 | case ARGT_USR: |
| 394 | case ARGT_MAP: |
| 395 | default: |
| 396 | lua_pushnil(L); |
| 397 | break; |
| 398 | } |
| 399 | return 1; |
| 400 | } |
| 401 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 402 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 403 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 404 | * with sample fetch wrappers. The input arguments are given to the |
| 405 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 406 | */ |
| 407 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 408 | { |
| 409 | switch (lua_type(L, ud)) { |
| 410 | |
| 411 | case LUA_TNUMBER: |
| 412 | case LUA_TBOOLEAN: |
| 413 | arg->type = ARGT_SINT; |
| 414 | arg->data.sint = lua_tointeger(L, ud); |
| 415 | break; |
| 416 | |
| 417 | case LUA_TSTRING: |
| 418 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 419 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 420 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 421 | arg->data.str.size = arg->data.str.data; |
| 422 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case LUA_TUSERDATA: |
| 426 | case LUA_TNIL: |
| 427 | case LUA_TTABLE: |
| 428 | case LUA_TFUNCTION: |
| 429 | case LUA_TTHREAD: |
| 430 | case LUA_TLIGHTUSERDATA: |
| 431 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 432 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | /* the following functions are used to convert a struct sample |
| 439 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 440 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 441 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 442 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 443 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 444 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 445 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 447 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | case SMP_T_BIN: |
| 451 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 452 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 453 | break; |
| 454 | |
| 455 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 456 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 458 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 459 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 460 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 461 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 462 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 463 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 464 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 465 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 466 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 467 | break; |
| 468 | default: |
| 469 | lua_pushnil(L); |
| 470 | break; |
| 471 | } |
| 472 | break; |
| 473 | |
| 474 | case SMP_T_IPV4: |
| 475 | case SMP_T_IPV6: |
| 476 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 477 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 478 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 479 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 480 | else |
| 481 | lua_pushnil(L); |
| 482 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 483 | default: |
| 484 | lua_pushnil(L); |
| 485 | break; |
| 486 | } |
| 487 | return 1; |
| 488 | } |
| 489 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 490 | /* the following functions are used to convert a struct sample |
| 491 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 492 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 493 | */ |
| 494 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 495 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 496 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 497 | |
| 498 | case SMP_T_BIN: |
| 499 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 500 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 501 | break; |
| 502 | |
| 503 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 504 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 505 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 506 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 507 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 508 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 509 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 510 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 511 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 512 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 513 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 514 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 515 | break; |
| 516 | default: |
| 517 | lua_pushstring(L, ""); |
| 518 | break; |
| 519 | } |
| 520 | break; |
| 521 | |
| 522 | case SMP_T_SINT: |
| 523 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 524 | case SMP_T_IPV4: |
| 525 | case SMP_T_IPV6: |
| 526 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 527 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 528 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 529 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 530 | else |
| 531 | lua_pushstring(L, ""); |
| 532 | break; |
| 533 | default: |
| 534 | lua_pushstring(L, ""); |
| 535 | break; |
| 536 | } |
| 537 | return 1; |
| 538 | } |
| 539 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 540 | /* the following functions are used to convert an Lua type in a |
| 541 | * struct sample. This is useful to provide data from a converter |
| 542 | * to the LUA code. |
| 543 | */ |
| 544 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 545 | { |
| 546 | switch (lua_type(L, ud)) { |
| 547 | |
| 548 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 549 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 550 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 551 | break; |
| 552 | |
| 553 | |
| 554 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 555 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 556 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 557 | break; |
| 558 | |
| 559 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 560 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 562 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 563 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 564 | smp->data.u.str.size = smp->data.u.str.data; |
| 565 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | break; |
| 567 | |
| 568 | case LUA_TUSERDATA: |
| 569 | case LUA_TNIL: |
| 570 | case LUA_TTABLE: |
| 571 | case LUA_TFUNCTION: |
| 572 | case LUA_TTHREAD: |
| 573 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 574 | case LUA_TNONE: |
| 575 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 576 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 577 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | return 1; |
| 581 | } |
| 582 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 583 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 584 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 585 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 586 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 587 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 588 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 589 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 590 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 591 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | { |
| 593 | int min_arg; |
| 594 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 595 | struct proxy *px; |
| 596 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 597 | |
| 598 | idx = 0; |
| 599 | min_arg = ARGM(mask); |
| 600 | mask >>= ARGM_BITS; |
| 601 | |
| 602 | while (1) { |
| 603 | |
| 604 | /* Check oversize. */ |
| 605 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 606 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* Check for mandatory arguments. */ |
| 610 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 611 | if (idx < min_arg) { |
| 612 | |
| 613 | /* If miss other argument than the first one, we return an error. */ |
| 614 | if (idx > 0) |
| 615 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 616 | |
| 617 | /* If first argument have a certain type, some default values |
| 618 | * may be used. See the function smp_resolve_args(). |
| 619 | */ |
| 620 | switch (mask & ARGT_MASK) { |
| 621 | |
| 622 | case ARGT_FE: |
| 623 | if (!(p->cap & PR_CAP_FE)) |
| 624 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 625 | argp[idx].data.prx = p; |
| 626 | argp[idx].type = ARGT_FE; |
| 627 | argp[idx+1].type = ARGT_STOP; |
| 628 | break; |
| 629 | |
| 630 | case ARGT_BE: |
| 631 | if (!(p->cap & PR_CAP_BE)) |
| 632 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 633 | argp[idx].data.prx = p; |
| 634 | argp[idx].type = ARGT_BE; |
| 635 | argp[idx+1].type = ARGT_STOP; |
| 636 | break; |
| 637 | |
| 638 | case ARGT_TAB: |
| 639 | argp[idx].data.prx = p; |
| 640 | argp[idx].type = ARGT_TAB; |
| 641 | argp[idx+1].type = ARGT_STOP; |
| 642 | break; |
| 643 | |
| 644 | default: |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 646 | break; |
| 647 | } |
| 648 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 652 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 653 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 654 | argp[idx].type != ARGT_STOP) { |
| 655 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 656 | } |
| 657 | |
| 658 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 659 | argp[idx].type == ARGT_STOP) { |
| 660 | return 0; |
| 661 | } |
| 662 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 663 | /* Convert some argument types. */ |
| 664 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 665 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 666 | if (argp[idx].type != ARGT_SINT) |
| 667 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 668 | argp[idx].type = ARGT_SINT; |
| 669 | break; |
| 670 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 671 | case ARGT_TIME: |
| 672 | if (argp[idx].type != ARGT_SINT) |
| 673 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 674 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 675 | break; |
| 676 | |
| 677 | case ARGT_SIZE: |
| 678 | if (argp[idx].type != ARGT_SINT) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 680 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 681 | break; |
| 682 | |
| 683 | case ARGT_FE: |
| 684 | if (argp[idx].type != ARGT_STR) |
| 685 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 686 | memcpy(trash.area, argp[idx].data.str.area, |
| 687 | argp[idx].data.str.data); |
| 688 | trash.area[argp[idx].data.str.data] = 0; |
| 689 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 690 | if (!argp[idx].data.prx) |
| 691 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 692 | argp[idx].type = ARGT_FE; |
| 693 | break; |
| 694 | |
| 695 | case ARGT_BE: |
| 696 | if (argp[idx].type != ARGT_STR) |
| 697 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 698 | memcpy(trash.area, argp[idx].data.str.area, |
| 699 | argp[idx].data.str.data); |
| 700 | trash.area[argp[idx].data.str.data] = 0; |
| 701 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 702 | if (!argp[idx].data.prx) |
| 703 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 704 | argp[idx].type = ARGT_BE; |
| 705 | break; |
| 706 | |
| 707 | case ARGT_TAB: |
| 708 | if (argp[idx].type != ARGT_STR) |
| 709 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 710 | memcpy(trash.area, argp[idx].data.str.area, |
| 711 | argp[idx].data.str.data); |
| 712 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 713 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 714 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 716 | argp[idx].type = ARGT_TAB; |
| 717 | break; |
| 718 | |
| 719 | case ARGT_SRV: |
| 720 | if (argp[idx].type != ARGT_STR) |
| 721 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 722 | memcpy(trash.area, argp[idx].data.str.area, |
| 723 | argp[idx].data.str.data); |
| 724 | trash.area[argp[idx].data.str.data] = 0; |
| 725 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 726 | if (sname) { |
| 727 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 728 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 729 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 730 | if (!px) |
| 731 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 732 | } |
| 733 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 734 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 735 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 736 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 737 | argp[idx].data.srv = findserver(px, sname); |
| 738 | if (!argp[idx].data.srv) |
| 739 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 740 | argp[idx].type = ARGT_SRV; |
| 741 | break; |
| 742 | |
| 743 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 744 | memcpy(trash.area, argp[idx].data.str.area, |
| 745 | argp[idx].data.str.data); |
| 746 | trash.area[argp[idx].data.str.data] = 0; |
| 747 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 748 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 749 | argp[idx].type = ARGT_IPV4; |
| 750 | break; |
| 751 | |
| 752 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 753 | memcpy(trash.area, argp[idx].data.str.area, |
| 754 | argp[idx].data.str.data); |
| 755 | trash.area[argp[idx].data.str.data] = 0; |
| 756 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 757 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 758 | argp[idx].type = ARGT_MSK4; |
| 759 | break; |
| 760 | |
| 761 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 762 | memcpy(trash.area, argp[idx].data.str.area, |
| 763 | argp[idx].data.str.data); |
| 764 | trash.area[argp[idx].data.str.data] = 0; |
| 765 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 766 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 767 | argp[idx].type = ARGT_IPV6; |
| 768 | break; |
| 769 | |
| 770 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 771 | memcpy(trash.area, argp[idx].data.str.area, |
| 772 | argp[idx].data.str.data); |
| 773 | trash.area[argp[idx].data.str.data] = 0; |
| 774 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 775 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 776 | argp[idx].type = ARGT_MSK6; |
| 777 | break; |
| 778 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | case ARGT_MAP: |
| 780 | case ARGT_REG: |
| 781 | case ARGT_USR: |
| 782 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 783 | break; |
| 784 | } |
| 785 | |
| 786 | /* Check for type of argument. */ |
| 787 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 788 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 789 | arg_type_names[(mask & ARGT_MASK)], |
| 790 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 791 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 792 | } |
| 793 | |
| 794 | /* Next argument. */ |
| 795 | mask >>= ARGT_BITS; |
| 796 | idx++; |
| 797 | } |
| 798 | } |
| 799 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 800 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 801 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 802 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 803 | * |
| 804 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 805 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 806 | */ |
| 807 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 808 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 809 | struct hlua **hlua = lua_getextraspace(L); |
| 810 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 811 | } |
| 812 | static inline void hlua_sethlua(struct hlua *hlua) |
| 813 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 814 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 815 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 818 | /* This function is used to send logs. It try to send on screen (stderr) |
| 819 | * and on the default syslog server. |
| 820 | */ |
| 821 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 822 | { |
| 823 | struct tm tm; |
| 824 | char *p; |
| 825 | |
| 826 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 827 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 828 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 829 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 830 | /* Break the message if exceed the buffer size. */ |
| 831 | *(p-4) = ' '; |
| 832 | *(p-3) = '.'; |
| 833 | *(p-2) = '.'; |
| 834 | *(p-1) = '.'; |
| 835 | break; |
| 836 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 837 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 838 | *p = *msg; |
| 839 | else |
| 840 | *p = '.'; |
| 841 | } |
| 842 | *p = '\0'; |
| 843 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 844 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 845 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 846 | get_localtime(date.tv_sec, &tm); |
| 847 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 848 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 849 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 850 | fflush(stderr); |
| 851 | } |
| 852 | } |
| 853 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 854 | /* This function just ensure that the yield will be always |
| 855 | * returned with a timeout and permit to set some flags |
| 856 | */ |
| 857 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 858 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 859 | { |
| 860 | struct hlua *hlua = hlua_gethlua(L); |
| 861 | |
| 862 | /* Set the wake timeout. If timeout is required, we set |
| 863 | * the expiration time. |
| 864 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 865 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 866 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 867 | hlua->flags |= flags; |
| 868 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 869 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 870 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 871 | } |
| 872 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 873 | /* This function initialises the Lua environment stored in the stream. |
| 874 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 875 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 876 | * |
| 877 | * This function is particular. it initialises a new Lua thread. If the |
| 878 | * initialisation fails (example: out of memory error), the lua function |
| 879 | * throws an error (longjmp). |
| 880 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 881 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 882 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 883 | * threads appear, the safe environment set a lock to ensure only one |
| 884 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 885 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 886 | * |
| 887 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 888 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 889 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 890 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 891 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 892 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 893 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 894 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 895 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 896 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 897 | if (!already_safe) { |
| 898 | if (!SET_SAFE_LJMP(gL.T)) { |
| 899 | lua->Tref = LUA_REFNIL; |
| 900 | return 0; |
| 901 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 902 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 903 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 904 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 905 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 906 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 907 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 908 | lua->T = lua_newthread(gL.T); |
| 909 | if (!lua->T) { |
| 910 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 911 | if (!already_safe) |
| 912 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 913 | return 0; |
| 914 | } |
| 915 | hlua_sethlua(lua); |
| 916 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 917 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 918 | if (!already_safe) |
| 919 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 920 | return 1; |
| 921 | } |
| 922 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 923 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 925 | * is not freed. |
| 926 | */ |
| 927 | void hlua_ctx_destroy(struct hlua *lua) |
| 928 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 929 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 930 | return; |
| 931 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 932 | if (!lua->T) |
| 933 | goto end; |
| 934 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 935 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 936 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 937 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 938 | if (!SET_SAFE_LJMP(lua->T)) |
| 939 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 940 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 941 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 942 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 943 | if (!SET_SAFE_LJMP(gL.T)) |
| 944 | return; |
| 945 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 946 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 947 | /* Forces a garbage collecting process. If the Lua program is finished |
| 948 | * without error, we run the GC on the thread pointer. Its freed all |
| 949 | * the unused memory. |
| 950 | * If the thread is finnish with an error or is currently yielded, |
| 951 | * it seems that the GC applied on the thread doesn't clean anything, |
| 952 | * so e run the GC on the main thread. |
| 953 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 954 | * the garbage collection. |
| 955 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 956 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 957 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 958 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 959 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 960 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 961 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 962 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 963 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 964 | |
| 965 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 966 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* This function is used to restore the Lua context when a coroutine |
| 970 | * fails. This function copy the common memory between old coroutine |
| 971 | * and the new coroutine. The old coroutine is destroyed, and its |
| 972 | * replaced by the new coroutine. |
| 973 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 974 | * as string error message and it is copied in the new stack. |
| 975 | */ |
| 976 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 977 | { |
| 978 | lua_State *T; |
| 979 | int new_ref; |
| 980 | |
| 981 | /* Renew the main LUA stack doesn't have sense. */ |
| 982 | if (lua == &gL) |
| 983 | return 0; |
| 984 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 985 | /* New Lua coroutine. */ |
| 986 | T = lua_newthread(gL.T); |
| 987 | if (!T) |
| 988 | return 0; |
| 989 | |
| 990 | /* Copy last error message. */ |
| 991 | if (keep_msg) |
| 992 | lua_xmove(lua->T, T, 1); |
| 993 | |
| 994 | /* Copy data between the coroutines. */ |
| 995 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 996 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 997 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 998 | |
| 999 | /* Destroy old data. */ |
| 1000 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1001 | |
| 1002 | /* The thread is garbage collected by Lua. */ |
| 1003 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1004 | |
| 1005 | /* Fill the struct with the new coroutine values. */ |
| 1006 | lua->Mref = new_ref; |
| 1007 | lua->T = T; |
| 1008 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1009 | |
| 1010 | /* Set context. */ |
| 1011 | hlua_sethlua(lua); |
| 1012 | |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1016 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1017 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1018 | struct hlua *hlua = hlua_gethlua(L); |
| 1019 | |
| 1020 | /* Lua cannot yield when its returning from a function, |
| 1021 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1022 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1023 | */ |
| 1024 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1025 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | /* restore the interrupt condition. */ |
| 1030 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1031 | |
| 1032 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1033 | * If the state is not yieldable, trying yield causes an error. |
| 1034 | */ |
| 1035 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1036 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1037 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1038 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1039 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1040 | hlua->run_time += now_ms - hlua->start_time; |
| 1041 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1042 | lua_pushfstring(L, "execution timeout"); |
| 1043 | WILL_LJMP(lua_error(L)); |
| 1044 | } |
| 1045 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1046 | /* Update the start time. */ |
| 1047 | hlua->start_time = now_ms; |
| 1048 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1049 | /* Try to interrupt the process at the end of the current |
| 1050 | * unyieldable function. |
| 1051 | */ |
| 1052 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1055 | /* This function start or resumes the Lua stack execution. If the flag |
| 1056 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1057 | * The function return an error. |
| 1058 | * |
| 1059 | * The function can returns 4 values: |
| 1060 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1061 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1062 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1063 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1064 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1065 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1066 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1068 | * LUA code. |
| 1069 | */ |
| 1070 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1071 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1072 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1073 | int nres; |
| 1074 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1075 | int ret; |
| 1076 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1077 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1078 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1079 | /* Initialise run time counter. */ |
| 1080 | if (!HLUA_IS_RUNNING(lua)) |
| 1081 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1082 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1083 | /* Lock the whole Lua execution. This lock must be before the |
| 1084 | * label "resume_execution". |
| 1085 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1086 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1087 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1088 | resume_execution: |
| 1089 | |
| 1090 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1091 | * instructions. it is used for preventing infinite loops. |
| 1092 | */ |
| 1093 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1094 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1095 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1096 | HLUA_SET_RUN(lua); |
| 1097 | HLUA_CLR_CTRLYIELD(lua); |
| 1098 | HLUA_CLR_WAKERESWR(lua); |
| 1099 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1100 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1101 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1102 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1103 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1104 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1106 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1107 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1108 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1109 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1110 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1111 | switch (ret) { |
| 1112 | |
| 1113 | case LUA_OK: |
| 1114 | ret = HLUA_E_OK; |
| 1115 | break; |
| 1116 | |
| 1117 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1118 | /* Check if the execution timeout is expired. It it is the case, we |
| 1119 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1120 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1121 | tv_update_date(0, 1); |
| 1122 | lua->run_time += now_ms - lua->start_time; |
| 1123 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1124 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1125 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1126 | break; |
| 1127 | } |
| 1128 | /* Process the forced yield. if the general yield is not allowed or |
| 1129 | * if no task were associated this the current Lua execution |
| 1130 | * coroutine, we resume the execution. Else we want to return in the |
| 1131 | * scheduler and we want to be waked up again, to continue the |
| 1132 | * current Lua execution. So we schedule our own task. |
| 1133 | */ |
| 1134 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1135 | if (!yield_allowed || !lua->task) |
| 1136 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1137 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1138 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1139 | if (!yield_allowed) { |
| 1140 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1141 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1142 | break; |
| 1143 | } |
| 1144 | ret = HLUA_E_AGAIN; |
| 1145 | break; |
| 1146 | |
| 1147 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1148 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1149 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1150 | * because the errors ares the only one mean to return immediately |
| 1151 | * from and lua execution. |
| 1152 | */ |
| 1153 | if (lua->flags & HLUA_EXIT) { |
| 1154 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1155 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1156 | break; |
| 1157 | } |
| 1158 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1159 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | if (!lua_checkstack(lua->T, 1)) { |
| 1161 | ret = HLUA_E_ERR; |
| 1162 | break; |
| 1163 | } |
| 1164 | msg = lua_tostring(lua->T, -1); |
| 1165 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1166 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1167 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1168 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1169 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1171 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | ret = HLUA_E_ERRMSG; |
| 1173 | break; |
| 1174 | |
| 1175 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1176 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1177 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1178 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1179 | break; |
| 1180 | |
| 1181 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1182 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | if (!lua_checkstack(lua->T, 1)) { |
| 1184 | ret = HLUA_E_ERR; |
| 1185 | break; |
| 1186 | } |
| 1187 | msg = lua_tostring(lua->T, -1); |
| 1188 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1189 | lua_pop(lua->T, 1); |
| 1190 | if (msg) |
| 1191 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1192 | else |
| 1193 | lua_pushfstring(lua->T, "message handler error"); |
| 1194 | ret = HLUA_E_ERRMSG; |
| 1195 | break; |
| 1196 | |
| 1197 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1198 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1199 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1200 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1201 | break; |
| 1202 | } |
| 1203 | |
| 1204 | switch (ret) { |
| 1205 | case HLUA_E_AGAIN: |
| 1206 | break; |
| 1207 | |
| 1208 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1209 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1211 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | break; |
| 1213 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1214 | case HLUA_E_ETMOUT: |
| 1215 | case HLUA_E_NOMEM: |
| 1216 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1217 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1218 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1219 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1220 | hlua_ctx_renew(lua, 0); |
| 1221 | break; |
| 1222 | |
| 1223 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1224 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1225 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1229 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1230 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1231 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1232 | return ret; |
| 1233 | } |
| 1234 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1235 | /* This function exit the current code. */ |
| 1236 | __LJMP static int hlua_done(lua_State *L) |
| 1237 | { |
| 1238 | struct hlua *hlua = hlua_gethlua(L); |
| 1239 | |
| 1240 | hlua->flags |= HLUA_EXIT; |
| 1241 | WILL_LJMP(lua_error(L)); |
| 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1246 | /* This function is an LUA binding. It provides a function |
| 1247 | * for deleting ACL from a referenced ACL file. |
| 1248 | */ |
| 1249 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1250 | { |
| 1251 | const char *name; |
| 1252 | const char *key; |
| 1253 | struct pat_ref *ref; |
| 1254 | |
| 1255 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1256 | |
| 1257 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1258 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1259 | |
| 1260 | ref = pat_ref_lookup(name); |
| 1261 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1262 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1263 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1264 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1265 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1266 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | /* This function is an LUA binding. It provides a function |
| 1271 | * for deleting map entry from a referenced map file. |
| 1272 | */ |
| 1273 | static int hlua_del_map(lua_State *L) |
| 1274 | { |
| 1275 | const char *name; |
| 1276 | const char *key; |
| 1277 | struct pat_ref *ref; |
| 1278 | |
| 1279 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1280 | |
| 1281 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1282 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1283 | |
| 1284 | ref = pat_ref_lookup(name); |
| 1285 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1286 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1287 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1288 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1289 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1290 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | /* This function is an LUA binding. It provides a function |
| 1295 | * for adding ACL pattern from a referenced ACL file. |
| 1296 | */ |
| 1297 | static int hlua_add_acl(lua_State *L) |
| 1298 | { |
| 1299 | const char *name; |
| 1300 | const char *key; |
| 1301 | struct pat_ref *ref; |
| 1302 | |
| 1303 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1304 | |
| 1305 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1306 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1307 | |
| 1308 | ref = pat_ref_lookup(name); |
| 1309 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1310 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1311 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1312 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1313 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1314 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1315 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | /* This function is an LUA binding. It provides a function |
| 1320 | * for setting map pattern and sample from a referenced map |
| 1321 | * file. |
| 1322 | */ |
| 1323 | static int hlua_set_map(lua_State *L) |
| 1324 | { |
| 1325 | const char *name; |
| 1326 | const char *key; |
| 1327 | const char *value; |
| 1328 | struct pat_ref *ref; |
| 1329 | |
| 1330 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1331 | |
| 1332 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1333 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1334 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1335 | |
| 1336 | ref = pat_ref_lookup(name); |
| 1337 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1338 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1339 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1340 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1341 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1342 | pat_ref_set(ref, key, value, NULL); |
| 1343 | else |
| 1344 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1345 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1346 | return 0; |
| 1347 | } |
| 1348 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1349 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1350 | * an integer or user data. This data is associated with a metatable. This |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1351 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1352 | * the name of the object (_G[<name>] = <metable> ). |
| 1353 | * |
| 1354 | * A metable is a table that modify the standard behavior of a standard |
| 1355 | * access to the associated data. The entries of this new metatable are |
| 1356 | * defined as is: |
| 1357 | * |
| 1358 | * http://lua-users.org/wiki/MetatableEvents |
| 1359 | * |
| 1360 | * __index |
| 1361 | * |
| 1362 | * we access an absent field in a table, the result is nil. This is |
| 1363 | * true, but it is not the whole truth. Actually, such access triggers |
| 1364 | * the interpreter to look for an __index metamethod: If there is no |
| 1365 | * such method, as usually happens, then the access results in nil; |
| 1366 | * otherwise, the metamethod will provide the result. |
| 1367 | * |
| 1368 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1369 | * the key does not appear in the table, but the metatable has an __index |
| 1370 | * property: |
| 1371 | * |
| 1372 | * - if the value is a function, the function is called, passing in the |
| 1373 | * table and the key; the return value of that function is returned as |
| 1374 | * the result. |
| 1375 | * |
| 1376 | * - if the value is another table, the value of the key in that table is |
| 1377 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1378 | * table's metatable has an __index property, then it continues on up) |
| 1379 | * |
| 1380 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1381 | * |
| 1382 | * http://www.lua.org/pil/13.4.1.html |
| 1383 | * |
| 1384 | * __newindex |
| 1385 | * |
| 1386 | * Like __index, but control property assignment. |
| 1387 | * |
| 1388 | * __mode - Control weak references. A string value with one or both |
| 1389 | * of the characters 'k' and 'v' which specifies that the the |
| 1390 | * keys and/or values in the table are weak references. |
| 1391 | * |
| 1392 | * __call - Treat a table like a function. When a table is followed by |
| 1393 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1394 | * a __call key pointing to a function, that function is invoked |
| 1395 | * (passing any specified arguments) and the return value is |
| 1396 | * returned. |
| 1397 | * |
| 1398 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1399 | * called, if the metatable for myTable has a __metatable |
| 1400 | * key, the value of that key is returned instead of the |
| 1401 | * actual metatable. |
| 1402 | * |
| 1403 | * __tostring - Control string representation. When the builtin |
| 1404 | * "tostring( myTable )" function is called, if the metatable |
| 1405 | * for myTable has a __tostring property set to a function, |
| 1406 | * that function is invoked (passing myTable to it) and the |
| 1407 | * return value is used as the string representation. |
| 1408 | * |
| 1409 | * __len - Control table length. When the table length is requested using |
| 1410 | * the length operator ( '#' ), if the metatable for myTable has |
| 1411 | * a __len key pointing to a function, that function is invoked |
| 1412 | * (passing myTable to it) and the return value used as the value |
| 1413 | * of "#myTable". |
| 1414 | * |
| 1415 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1416 | * collected, if the metatable has a __gc field pointing to a |
| 1417 | * function, that function is first invoked, passing the userdata |
| 1418 | * to it. The __gc metamethod is not called for tables. |
| 1419 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1420 | * |
| 1421 | * Special metamethods for redefining standard operators: |
| 1422 | * http://www.lua.org/pil/13.1.html |
| 1423 | * |
| 1424 | * __add "+" |
| 1425 | * __sub "-" |
| 1426 | * __mul "*" |
| 1427 | * __div "/" |
| 1428 | * __unm "!" |
| 1429 | * __pow "^" |
| 1430 | * __concat ".." |
| 1431 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1432 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1433 | * http://www.lua.org/pil/13.2.html |
| 1434 | * |
| 1435 | * __eq "==" |
| 1436 | * __lt "<" |
| 1437 | * __le "<=" |
| 1438 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1439 | |
| 1440 | /* |
| 1441 | * |
| 1442 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1443 | * Class Map |
| 1444 | * |
| 1445 | * |
| 1446 | */ |
| 1447 | |
| 1448 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1449 | * a class session, otherwise it throws an error. |
| 1450 | */ |
| 1451 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1452 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1453 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /* This function is the map constructor. It don't need |
| 1457 | * the class Map object. It creates and return a new Map |
| 1458 | * object. It must be called only during "body" or "init" |
| 1459 | * context because it process some filesystem accesses. |
| 1460 | */ |
| 1461 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1462 | { |
| 1463 | const char *fn; |
| 1464 | int match = PAT_MATCH_STR; |
| 1465 | struct sample_conv conv; |
| 1466 | const char *file = ""; |
| 1467 | int line = 0; |
| 1468 | lua_Debug ar; |
| 1469 | char *err = NULL; |
| 1470 | struct arg args[2]; |
| 1471 | |
| 1472 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1473 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1474 | |
| 1475 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1476 | |
| 1477 | if (lua_gettop(L) >= 2) { |
| 1478 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1479 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1480 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1481 | } |
| 1482 | |
| 1483 | /* Get Lua filename and line number. */ |
| 1484 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1485 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1486 | if (ar.currentline > 0) { /* is there info? */ |
| 1487 | file = ar.short_src; |
| 1488 | line = ar.currentline; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | /* fill fake sample_conv struct. */ |
| 1493 | conv.kw = ""; /* unused. */ |
| 1494 | conv.process = NULL; /* unused. */ |
| 1495 | conv.arg_mask = 0; /* unused. */ |
| 1496 | conv.val_args = NULL; /* unused. */ |
| 1497 | conv.out_type = SMP_T_STR; |
| 1498 | conv.private = (void *)(long)match; |
| 1499 | switch (match) { |
| 1500 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1501 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1502 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1503 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1504 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1505 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1506 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1507 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1508 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1509 | default: |
| 1510 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1511 | } |
| 1512 | |
| 1513 | /* fill fake args. */ |
| 1514 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1515 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1516 | args[1].type = ARGT_STOP; |
| 1517 | |
| 1518 | /* load the map. */ |
| 1519 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1520 | /* error case: we cant use luaL_error because we must |
| 1521 | * free the err variable. |
| 1522 | */ |
| 1523 | luaL_where(L, 1); |
| 1524 | lua_pushfstring(L, "'new': %s.", err); |
| 1525 | lua_concat(L, 2); |
| 1526 | free(err); |
| 1527 | WILL_LJMP(lua_error(L)); |
| 1528 | } |
| 1529 | |
| 1530 | /* create the lua object. */ |
| 1531 | lua_newtable(L); |
| 1532 | lua_pushlightuserdata(L, args[0].data.map); |
| 1533 | lua_rawseti(L, -2, 0); |
| 1534 | |
| 1535 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1536 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1537 | lua_setmetatable(L, -2); |
| 1538 | |
| 1539 | |
| 1540 | return 1; |
| 1541 | } |
| 1542 | |
| 1543 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1544 | { |
| 1545 | struct map_descriptor *desc; |
| 1546 | struct pattern *pat; |
| 1547 | struct sample smp; |
| 1548 | |
| 1549 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1550 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1551 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1552 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1553 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1554 | } |
| 1555 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1556 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1557 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1558 | 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] | 1559 | } |
| 1560 | |
| 1561 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1562 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1563 | if (str) |
| 1564 | lua_pushstring(L, ""); |
| 1565 | else |
| 1566 | lua_pushnil(L); |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | /* 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] | 1571 | 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] | 1572 | return 1; |
| 1573 | } |
| 1574 | |
| 1575 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1576 | { |
| 1577 | return _hlua_map_lookup(L, 0); |
| 1578 | } |
| 1579 | |
| 1580 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1581 | { |
| 1582 | return _hlua_map_lookup(L, 1); |
| 1583 | } |
| 1584 | |
| 1585 | /* |
| 1586 | * |
| 1587 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1588 | * Class Socket |
| 1589 | * |
| 1590 | * |
| 1591 | */ |
| 1592 | |
| 1593 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1594 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1595 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /* 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] | 1599 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | * received. |
| 1601 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1602 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1604 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1605 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1606 | if (appctx->ctx.hlua_cosocket.die) { |
| 1607 | si_shutw(si); |
| 1608 | si_shutr(si); |
| 1609 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1610 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1611 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1612 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1613 | } |
| 1614 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1615 | /* If we cant write, wakeup the pending write signals. */ |
| 1616 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1617 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1618 | |
| 1619 | /* If we cant read, wakeup the pending read signals. */ |
| 1620 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1621 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1622 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1623 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1624 | * to be notified whenever the connection completes. |
| 1625 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1626 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1627 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1628 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1629 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1630 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1631 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1634 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1635 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1636 | /* 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] | 1637 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1638 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1639 | |
| 1640 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1641 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1642 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1643 | |
| 1644 | /* Some data were injected in the buffer, notify the stream |
| 1645 | * interface. |
| 1646 | */ |
| 1647 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1648 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1649 | |
| 1650 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1651 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1652 | */ |
| 1653 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1654 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | } |
| 1656 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1657 | /* This function is called when the "struct stream" is destroyed. |
| 1658 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1659 | * Wake all the pending signals. |
| 1660 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1661 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1663 | struct xref *peer; |
| 1664 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1665 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1666 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1667 | if (peer) |
| 1668 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1669 | |
| 1670 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1671 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1672 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1676 | * uses this object. If the stream does not exists, just quit. |
| 1677 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1678 | * pending signal can rest in the read and write lists. destroy |
| 1679 | * it. |
| 1680 | */ |
| 1681 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1682 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1683 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1685 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1686 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1687 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1688 | |
| 1689 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1690 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1691 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1692 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1693 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1694 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1695 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1696 | appctx->ctx.hlua_cosocket.die = 1; |
| 1697 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1698 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1699 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1700 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1705 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1707 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1708 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1709 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1710 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1711 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1712 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1714 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1715 | |
| 1716 | /* Check if we run on the same thread than the xreator thread. |
| 1717 | * We cannot access to the socket if the thread is different. |
| 1718 | */ |
| 1719 | if (socket->tid != tid) |
| 1720 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1721 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1722 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1723 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1724 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1725 | |
| 1726 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1727 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1728 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1729 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1730 | appctx->ctx.hlua_cosocket.die = 1; |
| 1731 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1732 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1733 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1734 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1735 | return 0; |
| 1736 | } |
| 1737 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1738 | /* The close function calls close_helper. |
| 1739 | */ |
| 1740 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1741 | { |
| 1742 | MAY_LJMP(check_args(L, 1, "close")); |
| 1743 | return hlua_socket_close_helper(L); |
| 1744 | } |
| 1745 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1746 | /* This Lua function assumes that the stack contain three parameters. |
| 1747 | * 1 - USERDATA containing a struct socket |
| 1748 | * 2 - INTEGER with values of the macro defined below |
| 1749 | * If the integer is -1, we must read at most one line. |
| 1750 | * If the integer is -2, we ust read all the data until the |
| 1751 | * end of the stream. |
| 1752 | * If the integer is positive value, we must read a number of |
| 1753 | * bytes corresponding to this value. |
| 1754 | */ |
| 1755 | #define HLSR_READ_LINE (-1) |
| 1756 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1757 | __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] | 1758 | { |
| 1759 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1760 | int wanted = lua_tointeger(L, 2); |
| 1761 | struct hlua *hlua = hlua_gethlua(L); |
| 1762 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1763 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1764 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1765 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1766 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1767 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1768 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1769 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1770 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1771 | struct stream_interface *si; |
| 1772 | struct stream *s; |
| 1773 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1774 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1775 | |
| 1776 | /* Check if this lua stack is schedulable. */ |
| 1777 | if (!hlua || !hlua->task) |
| 1778 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1779 | "'frontend', 'backend' or 'task'")); |
| 1780 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1781 | /* Check if we run on the same thread than the xreator thread. |
| 1782 | * We cannot access to the socket if the thread is different. |
| 1783 | */ |
| 1784 | if (socket->tid != tid) |
| 1785 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1786 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1787 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1788 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1789 | if (!peer) |
| 1790 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1791 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1792 | si = appctx->owner; |
| 1793 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1795 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1796 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1797 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1798 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | if (nblk < 0) /* Connection close. */ |
| 1800 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1801 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1802 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1803 | |
| 1804 | /* remove final \r\n. */ |
| 1805 | if (nblk == 1) { |
| 1806 | if (blk1[len1-1] == '\n') { |
| 1807 | len1--; |
| 1808 | skip_at_end++; |
| 1809 | if (blk1[len1-1] == '\r') { |
| 1810 | len1--; |
| 1811 | skip_at_end++; |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | else { |
| 1816 | if (blk2[len2-1] == '\n') { |
| 1817 | len2--; |
| 1818 | skip_at_end++; |
| 1819 | if (blk2[len2-1] == '\r') { |
| 1820 | len2--; |
| 1821 | skip_at_end++; |
| 1822 | } |
| 1823 | } |
| 1824 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1829 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1830 | if (nblk < 0) /* Connection close. */ |
| 1831 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1832 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | goto connection_empty; |
| 1834 | } |
| 1835 | |
| 1836 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1838 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1839 | if (nblk < 0) /* Connection close. */ |
| 1840 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1841 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1842 | goto connection_empty; |
| 1843 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1844 | missing_bytes = wanted - socket->b.n; |
| 1845 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1846 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1847 | len1 = missing_bytes; |
| 1848 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1849 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | len = len1; |
| 1853 | |
| 1854 | luaL_addlstring(&socket->b, blk1, len1); |
| 1855 | if (nblk == 2) { |
| 1856 | len += len2; |
| 1857 | luaL_addlstring(&socket->b, blk2, len2); |
| 1858 | } |
| 1859 | |
| 1860 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1861 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | |
| 1863 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1864 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1865 | |
| 1866 | /* If the pattern reclaim to read all the data |
| 1867 | * in the connection, got out. |
| 1868 | */ |
| 1869 | if (wanted == HLSR_READ_ALL) |
| 1870 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1871 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | goto connection_empty; |
| 1873 | |
| 1874 | /* Return result. */ |
| 1875 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1876 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | return 1; |
| 1878 | |
| 1879 | connection_closed: |
| 1880 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1881 | xref_unlock(&socket->xref, peer); |
| 1882 | |
| 1883 | no_peer: |
| 1884 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | /* If the buffer containds data. */ |
| 1886 | if (socket->b.n > 0) { |
| 1887 | luaL_pushresult(&socket->b); |
| 1888 | return 1; |
| 1889 | } |
| 1890 | lua_pushnil(L); |
| 1891 | lua_pushstring(L, "connection closed."); |
| 1892 | return 2; |
| 1893 | |
| 1894 | connection_empty: |
| 1895 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1896 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1897 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1898 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1899 | } |
| 1900 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1901 | 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] | 1902 | return 0; |
| 1903 | } |
| 1904 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1905 | /* This Lua function gets two parameters. The first one can be string |
| 1906 | * or a number. If the string is "*l", the user requires one line. If |
| 1907 | * 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] | 1908 | * If the value is a number, the user require a number of bytes equal |
| 1909 | * to the value. The default value is "*l" (a line). |
| 1910 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1911 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1912 | * integer takes this values: |
| 1913 | * -1 : read a line |
| 1914 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1915 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1916 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1917 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1918 | * concatenated with the read data. |
| 1919 | */ |
| 1920 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1921 | { |
| 1922 | int wanted = HLSR_READ_LINE; |
| 1923 | const char *pattern; |
| 1924 | int type; |
| 1925 | char *error; |
| 1926 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1927 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | |
| 1929 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1930 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1931 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1932 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1934 | /* Check if we run on the same thread than the xreator thread. |
| 1935 | * We cannot access to the socket if the thread is different. |
| 1936 | */ |
| 1937 | if (socket->tid != tid) |
| 1938 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1939 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1940 | /* check for pattern. */ |
| 1941 | if (lua_gettop(L) >= 2) { |
| 1942 | type = lua_type(L, 2); |
| 1943 | if (type == LUA_TSTRING) { |
| 1944 | pattern = lua_tostring(L, 2); |
| 1945 | if (strcmp(pattern, "*a") == 0) |
| 1946 | wanted = HLSR_READ_ALL; |
| 1947 | else if (strcmp(pattern, "*l") == 0) |
| 1948 | wanted = HLSR_READ_LINE; |
| 1949 | else { |
| 1950 | wanted = strtoll(pattern, &error, 10); |
| 1951 | if (*error != '\0') |
| 1952 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1953 | } |
| 1954 | } |
| 1955 | else if (type == LUA_TNUMBER) { |
| 1956 | wanted = lua_tointeger(L, 2); |
| 1957 | if (wanted < 0) |
| 1958 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | /* Set pattern. */ |
| 1963 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1964 | |
| 1965 | /* Check if we would replace the top by itself. */ |
| 1966 | if (lua_gettop(L) != 2) |
| 1967 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1969 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | luaL_buffinit(L, &socket->b); |
| 1971 | |
| 1972 | /* Check prefix. */ |
| 1973 | if (lua_gettop(L) >= 3) { |
| 1974 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1975 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1976 | pattern = lua_tolstring(L, 3, &len); |
| 1977 | luaL_addlstring(&socket->b, pattern, len); |
| 1978 | } |
| 1979 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1980 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1984 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1986 | 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] | 1987 | { |
| 1988 | struct hlua_socket *socket; |
| 1989 | struct hlua *hlua = hlua_gethlua(L); |
| 1990 | struct appctx *appctx; |
| 1991 | size_t buf_len; |
| 1992 | const char *buf; |
| 1993 | int len; |
| 1994 | int send_len; |
| 1995 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1996 | struct xref *peer; |
| 1997 | struct stream_interface *si; |
| 1998 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | |
| 2000 | /* Check if this lua stack is schedulable. */ |
| 2001 | if (!hlua || !hlua->task) |
| 2002 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2003 | "'frontend', 'backend' or 'task'")); |
| 2004 | |
| 2005 | /* Get object */ |
| 2006 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2007 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2008 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2009 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2010 | /* Check if we run on the same thread than the xreator thread. |
| 2011 | * We cannot access to the socket if the thread is different. |
| 2012 | */ |
| 2013 | if (socket->tid != tid) |
| 2014 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2015 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2016 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2017 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2018 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2019 | lua_pushinteger(L, -1); |
| 2020 | return 1; |
| 2021 | } |
| 2022 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2023 | si = appctx->owner; |
| 2024 | s = si_strm(si); |
| 2025 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2027 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2028 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | lua_pushinteger(L, -1); |
| 2030 | return 1; |
| 2031 | } |
| 2032 | |
| 2033 | /* Update the input buffer data. */ |
| 2034 | buf += sent; |
| 2035 | send_len = buf_len - sent; |
| 2036 | |
| 2037 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2038 | if (sent >= buf_len) { |
| 2039 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2041 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2043 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2044 | * the request buffer if its not required. |
| 2045 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2046 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2047 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2048 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2049 | } |
| 2050 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2051 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2052 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2053 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2055 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2056 | |
| 2057 | /* send data */ |
| 2058 | if (len < send_len) |
| 2059 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2060 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | |
| 2062 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2063 | * the data" (-2) are not expected because the available length |
| 2064 | * is tested. |
| 2065 | * Other unknown error are also not expected. |
| 2066 | */ |
| 2067 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2068 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2069 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2070 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2071 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2073 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2074 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | return 1; |
| 2076 | } |
| 2077 | |
| 2078 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2079 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2080 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2081 | s->req.rex = TICK_ETERNITY; |
| 2082 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | |
| 2084 | /* Update length sent. */ |
| 2085 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2086 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
| 2088 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2089 | if (sent + len >= buf_len) { |
| 2090 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2091 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2092 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2093 | |
| 2094 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2095 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2096 | xref_unlock(&socket->xref, peer); |
| 2097 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2098 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2099 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2100 | 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] | 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | /* This function initiate the send of data. It just check the input |
| 2105 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2106 | * 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] | 2107 | * "hlua_socket_write_yield" that can yield. |
| 2108 | * |
| 2109 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2110 | * the associated object. The second is a string buffer. The third is |
| 2111 | * a facultative integer that represents where is the buffer position |
| 2112 | * of the start of the data that can send. The first byte is the |
| 2113 | * position "1". The default value is "1". The fourth argument is a |
| 2114 | * facultative integer that represents where is the buffer position |
| 2115 | * of the end of the data that can send. The default is the last byte. |
| 2116 | */ |
| 2117 | static int hlua_socket_send(struct lua_State *L) |
| 2118 | { |
| 2119 | int i; |
| 2120 | int j; |
| 2121 | const char *buf; |
| 2122 | size_t buf_len; |
| 2123 | |
| 2124 | /* Check number of arguments. */ |
| 2125 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2126 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2127 | |
| 2128 | /* Get the string. */ |
| 2129 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2130 | |
| 2131 | /* Get and check j. */ |
| 2132 | if (lua_gettop(L) == 4) { |
| 2133 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2134 | if (j < 0) |
| 2135 | j = buf_len + j + 1; |
| 2136 | if (j > buf_len) |
| 2137 | j = buf_len + 1; |
| 2138 | lua_pop(L, 1); |
| 2139 | } |
| 2140 | else |
| 2141 | j = buf_len; |
| 2142 | |
| 2143 | /* Get and check i. */ |
| 2144 | if (lua_gettop(L) == 3) { |
| 2145 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2146 | if (i < 0) |
| 2147 | i = buf_len + i + 1; |
| 2148 | if (i > buf_len) |
| 2149 | i = buf_len + 1; |
| 2150 | lua_pop(L, 1); |
| 2151 | } else |
| 2152 | i = 1; |
| 2153 | |
| 2154 | /* Check bth i and j. */ |
| 2155 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2156 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | return 1; |
| 2158 | } |
| 2159 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2160 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | return 1; |
| 2162 | } |
| 2163 | if (i == 0) |
| 2164 | i = 1; |
| 2165 | if (j == 0) |
| 2166 | j = 1; |
| 2167 | |
| 2168 | /* Pop the string. */ |
| 2169 | lua_pop(L, 1); |
| 2170 | |
| 2171 | /* Update the buffer length. */ |
| 2172 | buf += i - 1; |
| 2173 | buf_len = j - i + 1; |
| 2174 | lua_pushlstring(L, buf, buf_len); |
| 2175 | |
| 2176 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2177 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2179 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | } |
| 2181 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2182 | #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] | 2183 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2184 | { |
| 2185 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2186 | int ret; |
| 2187 | int len; |
| 2188 | char *p; |
| 2189 | |
| 2190 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2191 | if (ret <= 0) { |
| 2192 | lua_pushnil(L); |
| 2193 | return 1; |
| 2194 | } |
| 2195 | |
| 2196 | if (ret == AF_UNIX) { |
| 2197 | lua_pushstring(L, buffer+1); |
| 2198 | return 1; |
| 2199 | } |
| 2200 | else if (ret == AF_INET6) { |
| 2201 | buffer[0] = '['; |
| 2202 | len = strlen(buffer); |
| 2203 | buffer[len] = ']'; |
| 2204 | len++; |
| 2205 | buffer[len] = ':'; |
| 2206 | len++; |
| 2207 | p = buffer; |
| 2208 | } |
| 2209 | else if (ret == AF_INET) { |
| 2210 | p = buffer + 1; |
| 2211 | len = strlen(p); |
| 2212 | p[len] = ':'; |
| 2213 | len++; |
| 2214 | } |
| 2215 | else { |
| 2216 | lua_pushnil(L); |
| 2217 | return 1; |
| 2218 | } |
| 2219 | |
| 2220 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2221 | lua_pushnil(L); |
| 2222 | return 1; |
| 2223 | } |
| 2224 | |
| 2225 | lua_pushstring(L, p); |
| 2226 | return 1; |
| 2227 | } |
| 2228 | |
| 2229 | /* Returns information about the peer of the connection. */ |
| 2230 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2231 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2232 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2233 | struct xref *peer; |
| 2234 | struct appctx *appctx; |
| 2235 | struct stream_interface *si; |
| 2236 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2237 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2238 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2239 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2240 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2241 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2243 | /* Check if we run on the same thread than the xreator thread. |
| 2244 | * We cannot access to the socket if the thread is different. |
| 2245 | */ |
| 2246 | if (socket->tid != tid) |
| 2247 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2248 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2249 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2250 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2251 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | lua_pushnil(L); |
| 2253 | return 1; |
| 2254 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2255 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2256 | si = appctx->owner; |
| 2257 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2259 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2260 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2261 | lua_pushnil(L); |
| 2262 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | } |
| 2264 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2265 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2266 | xref_unlock(&socket->xref, peer); |
| 2267 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | /* Returns information about my connection side. */ |
| 2271 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2272 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2273 | struct hlua_socket *socket; |
| 2274 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2275 | struct appctx *appctx; |
| 2276 | struct xref *peer; |
| 2277 | struct stream_interface *si; |
| 2278 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2280 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2282 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2283 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2285 | /* Check if we run on the same thread than the xreator thread. |
| 2286 | * We cannot access to the socket if the thread is different. |
| 2287 | */ |
| 2288 | if (socket->tid != tid) |
| 2289 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2290 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2291 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2292 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2293 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | lua_pushnil(L); |
| 2295 | return 1; |
| 2296 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2297 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2298 | si = appctx->owner; |
| 2299 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2301 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2302 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2303 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | lua_pushnil(L); |
| 2305 | return 1; |
| 2306 | } |
| 2307 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2308 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2309 | xref_unlock(&socket->xref, peer); |
| 2310 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2314 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | .obj_type = OBJ_TYPE_APPLET, |
| 2316 | .name = "<LUA_TCP>", |
| 2317 | .fct = hlua_socket_handler, |
| 2318 | .release = hlua_socket_release, |
| 2319 | }; |
| 2320 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2321 | __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] | 2322 | { |
| 2323 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2324 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2327 | struct stream_interface *si; |
| 2328 | struct stream *s; |
| 2329 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2330 | /* Check if we run on the same thread than the xreator thread. |
| 2331 | * We cannot access to the socket if the thread is different. |
| 2332 | */ |
| 2333 | if (socket->tid != tid) |
| 2334 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2335 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2336 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2337 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | lua_pushnil(L); |
| 2340 | lua_pushstring(L, "Can't connect"); |
| 2341 | return 2; |
| 2342 | } |
| 2343 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2344 | si = appctx->owner; |
| 2345 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2347 | /* Check if we run on the same thread than the xreator thread. |
| 2348 | * We cannot access to the socket if the thread is different. |
| 2349 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2350 | if (socket->tid != tid) { |
| 2351 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2352 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2354 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2355 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2356 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2357 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | lua_pushnil(L); |
| 2359 | lua_pushstring(L, "Can't connect"); |
| 2360 | return 2; |
| 2361 | } |
| 2362 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2363 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | |
| 2365 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2366 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2367 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | lua_pushinteger(L, 1); |
| 2369 | return 1; |
| 2370 | } |
| 2371 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2372 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2373 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2375 | } |
| 2376 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2377 | 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] | 2378 | return 0; |
| 2379 | } |
| 2380 | |
| 2381 | /* This function fail or initite the connection. */ |
| 2382 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2383 | { |
| 2384 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2385 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2387 | struct hlua *hlua; |
| 2388 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2389 | int low, high; |
| 2390 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2391 | struct xref *peer; |
| 2392 | struct stream_interface *si; |
| 2393 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2395 | if (lua_gettop(L) < 2) |
| 2396 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | |
| 2398 | /* Get args. */ |
| 2399 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2400 | |
| 2401 | /* Check if we run on the same thread than the xreator thread. |
| 2402 | * We cannot access to the socket if the thread is different. |
| 2403 | */ |
| 2404 | if (socket->tid != tid) |
| 2405 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2406 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2408 | if (lua_gettop(L) >= 3) { |
| 2409 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2410 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2412 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2413 | * that are not enclosed within square brackets. |
| 2414 | */ |
| 2415 | if (port > 0) { |
| 2416 | luaL_buffinit(L, &b); |
| 2417 | luaL_addstring(&b, ip); |
| 2418 | luaL_addchar(&b, ':'); |
| 2419 | luaL_pushresult(&b); |
| 2420 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2421 | } |
| 2422 | } |
| 2423 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2424 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2425 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2426 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2427 | lua_pushnil(L); |
| 2428 | return 1; |
| 2429 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2430 | |
| 2431 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2432 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2433 | if (!addr) { |
| 2434 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2435 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2436 | } |
| 2437 | if (low != high) { |
| 2438 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2439 | 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] | 2440 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2441 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2442 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2443 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2444 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2445 | if (port == -1) { |
| 2446 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2447 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2448 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2449 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2450 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2451 | if (port == -1) { |
| 2452 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2453 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2454 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2455 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2456 | } |
| 2457 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2458 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2459 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2460 | si = appctx->owner; |
| 2461 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2462 | |
| 2463 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2464 | xref_unlock(&socket->xref, peer); |
| 2465 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2466 | } |
| 2467 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2468 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2470 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2471 | |
| 2472 | /* inform the stream that we want to be notified whenever the |
| 2473 | * connection completes. |
| 2474 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2475 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2476 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2477 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2478 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2479 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2480 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2481 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2482 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2483 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2484 | } |
| 2485 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2486 | |
| 2487 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2488 | /* Return yield waiting for connection. */ |
| 2489 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2490 | 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] | 2491 | |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2495 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2497 | { |
| 2498 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2499 | struct xref *peer; |
| 2500 | struct appctx *appctx; |
| 2501 | struct stream_interface *si; |
| 2502 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2503 | |
| 2504 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2505 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2506 | |
| 2507 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2508 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2509 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2510 | lua_pushnil(L); |
| 2511 | return 1; |
| 2512 | } |
| 2513 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2514 | si = appctx->owner; |
| 2515 | s = si_strm(si); |
| 2516 | |
| 2517 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2518 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2520 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2521 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | |
| 2523 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2524 | { |
| 2525 | return 0; |
| 2526 | } |
| 2527 | |
| 2528 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2529 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2530 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2531 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2532 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2533 | struct xref *peer; |
| 2534 | struct appctx *appctx; |
| 2535 | struct stream_interface *si; |
| 2536 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2537 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2539 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2540 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2541 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2542 | /* convert the timeout to millis */ |
| 2543 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2544 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2545 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2546 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2547 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2548 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2549 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2550 | 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] | 2551 | |
| 2552 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2553 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2554 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2555 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2556 | /* Check if we run on the same thread than the xreator thread. |
| 2557 | * We cannot access to the socket if the thread is different. |
| 2558 | */ |
| 2559 | if (socket->tid != tid) |
| 2560 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2561 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2562 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2563 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2564 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2565 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2566 | WILL_LJMP(lua_error(L)); |
| 2567 | return 0; |
| 2568 | } |
| 2569 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2570 | si = appctx->owner; |
| 2571 | s = si_strm(si); |
| 2572 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2573 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2574 | s->req.rto = tmout; |
| 2575 | s->req.wto = tmout; |
| 2576 | s->res.rto = tmout; |
| 2577 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2578 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2579 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2580 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2581 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2582 | |
| 2583 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2584 | task_queue(s->task); |
| 2585 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2586 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2588 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2589 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2593 | { |
| 2594 | struct hlua_socket *socket; |
| 2595 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2596 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2597 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | |
| 2599 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2600 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | hlua_pusherror(L, "socket: full stack"); |
| 2602 | goto out_fail_conf; |
| 2603 | } |
| 2604 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2605 | /* Create the object: obj[0] = userdata. */ |
| 2606 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2607 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2608 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2610 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2611 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2612 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2613 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2614 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2615 | goto out_fail_conf; |
| 2616 | } |
| 2617 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2618 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2620 | lua_setmetatable(L, -2); |
| 2621 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2622 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2623 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2624 | if (!appctx) { |
| 2625 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2626 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2627 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2628 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2629 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2630 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2631 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2632 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2633 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2634 | /* Now create a session, task and stream for this applet */ |
| 2635 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2636 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2638 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2639 | } |
| 2640 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2641 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2642 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2643 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2644 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | } |
| 2646 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2647 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2648 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2649 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | /* Configure "right" stream interface. this "si" is used to connect |
| 2651 | * and retrieve data from the server. The connection is initialized |
| 2652 | * with the "struct server". |
| 2653 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2654 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2655 | |
| 2656 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2657 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2658 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2659 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2660 | return 1; |
| 2661 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2662 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2663 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2664 | out_fail_sess: |
| 2665 | appctx_free(appctx); |
| 2666 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | WILL_LJMP(lua_error(L)); |
| 2668 | return 0; |
| 2669 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2670 | |
| 2671 | /* |
| 2672 | * |
| 2673 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2674 | * Class Channel |
| 2675 | * |
| 2676 | * |
| 2677 | */ |
| 2678 | |
| 2679 | /* Returns the struct hlua_channel join to the class channel in the |
| 2680 | * stack entry "ud" or throws an argument error. |
| 2681 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2682 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2683 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2684 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2685 | } |
| 2686 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2687 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2688 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2689 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2690 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2691 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2692 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2693 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2694 | return 0; |
| 2695 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2696 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2697 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2698 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2699 | |
| 2700 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2701 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2702 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2703 | return 1; |
| 2704 | } |
| 2705 | |
| 2706 | /* Duplicate all the data present in the input channel and put it |
| 2707 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2708 | * the stack if the channel is closed and all the data are consumed, |
| 2709 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2710 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2711 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2712 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2713 | { |
| 2714 | char *blk1; |
| 2715 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2716 | size_t len1; |
| 2717 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2718 | int ret; |
| 2719 | luaL_Buffer b; |
| 2720 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2721 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2722 | if (unlikely(ret == 0)) |
| 2723 | return 0; |
| 2724 | |
| 2725 | if (unlikely(ret < 0)) { |
| 2726 | lua_pushnil(L); |
| 2727 | return -1; |
| 2728 | } |
| 2729 | |
| 2730 | luaL_buffinit(L, &b); |
| 2731 | luaL_addlstring(&b, blk1, len1); |
| 2732 | if (unlikely(ret == 2)) |
| 2733 | luaL_addlstring(&b, blk2, len2); |
| 2734 | luaL_pushresult(&b); |
| 2735 | |
| 2736 | if (unlikely(ret == 2)) |
| 2737 | return len1 + len2; |
| 2738 | return len1; |
| 2739 | } |
| 2740 | |
| 2741 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2742 | * a yield. This function keep the data in the buffer. |
| 2743 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2744 | __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] | 2745 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2746 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2747 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2748 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2749 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2750 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2751 | WILL_LJMP(lua_error(L)); |
| 2752 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2753 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2754 | 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] | 2755 | return 1; |
| 2756 | } |
| 2757 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2758 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2759 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2760 | { |
| 2761 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2762 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2763 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2764 | } |
| 2765 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2766 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2767 | * a yield. This function consumes the data in the buffer. It returns |
| 2768 | * a string containing the data or a nil pointer if no data are available |
| 2769 | * and the channel is closed. |
| 2770 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2771 | __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] | 2772 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2773 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2774 | int ret; |
| 2775 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2776 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2777 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2778 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2779 | WILL_LJMP(lua_error(L)); |
| 2780 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2781 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2783 | 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] | 2784 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2785 | if (unlikely(ret == -1)) |
| 2786 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2787 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2788 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2789 | return 1; |
| 2790 | } |
| 2791 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2792 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2793 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2794 | { |
| 2795 | MAY_LJMP(check_args(L, 1, "get")); |
| 2796 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2797 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2798 | } |
| 2799 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | /* This functions consumes and returns one line. If the channel is closed, |
| 2801 | * 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] | 2802 | * 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] | 2803 | * value. |
| 2804 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2805 | __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] | 2806 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2807 | char *blk1; |
| 2808 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2809 | size_t len1; |
| 2810 | size_t len2; |
| 2811 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2812 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2813 | int ret; |
| 2814 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2815 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2816 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2817 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2818 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2819 | WILL_LJMP(lua_error(L)); |
| 2820 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2821 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2822 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2823 | 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] | 2824 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2825 | if (ret == -1) { |
| 2826 | lua_pushnil(L); |
| 2827 | return 1; |
| 2828 | } |
| 2829 | |
| 2830 | luaL_buffinit(L, &b); |
| 2831 | luaL_addlstring(&b, blk1, len1); |
| 2832 | len = len1; |
| 2833 | if (unlikely(ret == 2)) { |
| 2834 | luaL_addlstring(&b, blk2, len2); |
| 2835 | len += len2; |
| 2836 | } |
| 2837 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2838 | 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] | 2839 | return 1; |
| 2840 | } |
| 2841 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2842 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2843 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2844 | { |
| 2845 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2846 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2847 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2848 | } |
| 2849 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2850 | /* This function takes a string as input, and append it at the |
| 2851 | * input side of channel. If the data is too big, but a space |
| 2852 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2853 | * yields. If the data is bigger than the buffer, or if the |
| 2854 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2855 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2856 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2857 | __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] | 2858 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2859 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2860 | size_t len; |
| 2861 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2862 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2863 | int ret; |
| 2864 | int max; |
| 2865 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2866 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2867 | WILL_LJMP(lua_error(L)); |
| 2868 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2869 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2870 | * the request buffer if its not required. |
| 2871 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2872 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2873 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2874 | 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] | 2875 | } |
| 2876 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2877 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | if (max > len - l) |
| 2879 | max = len - l; |
| 2880 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2881 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2882 | if (ret == -2 || ret == -3) { |
| 2883 | lua_pushinteger(L, -1); |
| 2884 | return 1; |
| 2885 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2886 | if (ret == -1) { |
| 2887 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2888 | 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] | 2889 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | l += ret; |
| 2891 | lua_pop(L, 1); |
| 2892 | lua_pushinteger(L, l); |
| 2893 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2894 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2895 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2896 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2898 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | */ |
| 2900 | return 1; |
| 2901 | } |
| 2902 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2903 | 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] | 2904 | return 1; |
| 2905 | } |
| 2906 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2907 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2908 | * 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] | 2909 | * buffer size is too little for the data. |
| 2910 | */ |
| 2911 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2912 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2913 | size_t len; |
| 2914 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2916 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2917 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2918 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2919 | lua_pushinteger(L, 0); |
| 2920 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2921 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | } |
| 2923 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2924 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2926 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | * or -1 if the channel is closed or if the buffer size is too |
| 2928 | * little for the data. |
| 2929 | */ |
| 2930 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2931 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2932 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2933 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2935 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2936 | lua_pushinteger(L, 0); |
| 2937 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2938 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2939 | WILL_LJMP(lua_error(L)); |
| 2940 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2941 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2942 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2943 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2944 | } |
| 2945 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2946 | /* Append data in the output side of the buffer. This data is immediately |
| 2947 | * sent. The function returns the amount of data written. If the buffer |
| 2948 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | * if the channel is closed. |
| 2950 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2951 | __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] | 2952 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2953 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2954 | size_t len; |
| 2955 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2956 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2957 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2958 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2959 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2960 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2961 | WILL_LJMP(lua_error(L)); |
| 2962 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2963 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2964 | lua_pushinteger(L, -1); |
| 2965 | return 1; |
| 2966 | } |
| 2967 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2968 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2969 | * the request buffer if its not required. |
| 2970 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2971 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2972 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2973 | 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] | 2974 | } |
| 2975 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2976 | /* The written data will be immediately sent, so we can check |
| 2977 | * the available space without taking in account the reserve. |
| 2978 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2979 | * data, because the buffer will be flushed. |
| 2980 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2981 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2982 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2983 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2984 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2985 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2986 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2987 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2988 | return 1; |
| 2989 | |
| 2990 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | if (max > len - l) |
| 2992 | max = len - l; |
| 2993 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2994 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2995 | * detects a non contiguous buffer and realign it. |
| 2996 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 2997 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2998 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2999 | |
| 3000 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3001 | 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] | 3002 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3003 | /* buffer replace considers that the input part is filled. |
| 3004 | * so, I must forward these new data in the output part. |
| 3005 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3006 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | |
| 3008 | l += max; |
| 3009 | lua_pop(L, 1); |
| 3010 | lua_pushinteger(L, l); |
| 3011 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3012 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3013 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3014 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3015 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3016 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3017 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3019 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3020 | if (l < len) { |
| 3021 | /* If we are waiting for space in the response buffer, we |
| 3022 | * must set the flag WAKERESWR. This flag required the task |
| 3023 | * wake up if any activity is detected on the response buffer. |
| 3024 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3025 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3026 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3027 | else |
| 3028 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3029 | 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] | 3030 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | |
| 3032 | return 1; |
| 3033 | } |
| 3034 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3035 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3036 | * yield the LUA process, and resume it without checking the |
| 3037 | * input arguments. |
| 3038 | */ |
| 3039 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3040 | { |
| 3041 | MAY_LJMP(check_args(L, 2, "send")); |
| 3042 | lua_pushinteger(L, 0); |
| 3043 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3044 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | /* This function forward and amount of butes. The data pass from |
| 3048 | * the input side of the buffer to the output side, and can be |
| 3049 | * forwarded. This function never fails. |
| 3050 | * |
| 3051 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3052 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3054 | __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] | 3055 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3056 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | int len; |
| 3058 | int l; |
| 3059 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3060 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | |
| 3062 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3063 | |
| 3064 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3065 | WILL_LJMP(lua_error(L)); |
| 3066 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3068 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3069 | |
| 3070 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3071 | if (max > ci_data(chn)) |
| 3072 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3073 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | l += max; |
| 3075 | |
| 3076 | lua_pop(L, 1); |
| 3077 | lua_pushinteger(L, l); |
| 3078 | |
| 3079 | /* Check if it miss bytes to forward. */ |
| 3080 | if (l < len) { |
| 3081 | /* The the input channel or the output channel are closed, we |
| 3082 | * must return the amount of data forwarded. |
| 3083 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3084 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | return 1; |
| 3086 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3087 | /* If we are waiting for space data in the response buffer, we |
| 3088 | * must set the flag WAKERESWR. This flag required the task |
| 3089 | * wake up if any activity is detected on the response buffer. |
| 3090 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3091 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3092 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3093 | else |
| 3094 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3095 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3096 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3097 | 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] | 3098 | } |
| 3099 | |
| 3100 | return 1; |
| 3101 | } |
| 3102 | |
| 3103 | /* Just check the input and prepare the stack for the previous |
| 3104 | * function "hlua_channel_forward_yield" |
| 3105 | */ |
| 3106 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3107 | { |
| 3108 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3109 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3110 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3111 | |
| 3112 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3113 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | /* Just returns the number of bytes available in the input |
| 3117 | * side of the buffer. This function never fails. |
| 3118 | */ |
| 3119 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3120 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3121 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3122 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3124 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3125 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3126 | struct htx *htx = htxbuf(&chn->buf); |
| 3127 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3128 | } |
| 3129 | else |
| 3130 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3131 | return 1; |
| 3132 | } |
| 3133 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3134 | /* Returns true if the channel is full. */ |
| 3135 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3136 | { |
| 3137 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3138 | |
| 3139 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3140 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3141 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3142 | * applet). |
| 3143 | */ |
| 3144 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3145 | return 1; |
| 3146 | } |
| 3147 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3148 | /* Returns true if the channel is the response channel. */ |
| 3149 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3150 | { |
| 3151 | struct channel *chn; |
| 3152 | |
| 3153 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3154 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3155 | |
| 3156 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3157 | return 1; |
| 3158 | } |
| 3159 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | /* Just returns the number of bytes available in the output |
| 3161 | * side of the buffer. This function never fails. |
| 3162 | */ |
| 3163 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3164 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3165 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3166 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3167 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3168 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3169 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | return 1; |
| 3171 | } |
| 3172 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3173 | /* |
| 3174 | * |
| 3175 | * |
| 3176 | * Class Fetches |
| 3177 | * |
| 3178 | * |
| 3179 | */ |
| 3180 | |
| 3181 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3182 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3183 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3184 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3185 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3186 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | /* This function creates and push in the stack a fetch object according |
| 3190 | * with a current TXN. |
| 3191 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3192 | 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] | 3193 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3194 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3195 | |
| 3196 | /* Check stack size. */ |
| 3197 | if (!lua_checkstack(L, 3)) |
| 3198 | return 0; |
| 3199 | |
| 3200 | /* Create the object: obj[0] = userdata. |
| 3201 | * Note that the base of the Fetches object is the |
| 3202 | * transaction object. |
| 3203 | */ |
| 3204 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3205 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3206 | lua_rawseti(L, -2, 0); |
| 3207 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3208 | hsmp->s = txn->s; |
| 3209 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3210 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3211 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3212 | |
| 3213 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3214 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3215 | lua_setmetatable(L, -2); |
| 3216 | |
| 3217 | return 1; |
| 3218 | } |
| 3219 | |
| 3220 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3221 | * It uses closure argument to store the associated sample-fetch. It |
| 3222 | * returns only one argument or throws an error. An error is thrown |
| 3223 | * only if an error is encountered during the argument parsing. If |
| 3224 | * the "sample-fetch" function fails, nil is returned. |
| 3225 | */ |
| 3226 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3227 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3228 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3229 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3230 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3231 | int i; |
| 3232 | struct sample smp; |
| 3233 | |
| 3234 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3235 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3236 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3237 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3238 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3239 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3240 | /* Check execution authorization. */ |
| 3241 | if (f->use & SMP_USE_HTTP_ANY && |
| 3242 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3243 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3244 | "is not available in Lua services", f->kw); |
| 3245 | WILL_LJMP(lua_error(L)); |
| 3246 | } |
| 3247 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3248 | /* Get extra arguments. */ |
| 3249 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3250 | if (i >= ARGM_NBARGS) |
| 3251 | break; |
| 3252 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3253 | } |
| 3254 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3255 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3256 | |
| 3257 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3258 | 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] | 3259 | |
| 3260 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3261 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3262 | lua_pushfstring(L, "error in arguments"); |
| 3263 | WILL_LJMP(lua_error(L)); |
| 3264 | } |
| 3265 | |
| 3266 | /* Initialise the sample. */ |
| 3267 | memset(&smp, 0, sizeof(smp)); |
| 3268 | |
| 3269 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3270 | 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] | 3271 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3272 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3273 | lua_pushstring(L, ""); |
| 3274 | else |
| 3275 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3276 | return 1; |
| 3277 | } |
| 3278 | |
| 3279 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3280 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3281 | hlua_smp2lua_str(L, &smp); |
| 3282 | else |
| 3283 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3284 | return 1; |
| 3285 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3286 | |
| 3287 | /* |
| 3288 | * |
| 3289 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3290 | * Class Converters |
| 3291 | * |
| 3292 | * |
| 3293 | */ |
| 3294 | |
| 3295 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3296 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3297 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3298 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3299 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3300 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | /* This function creates and push in the stack a Converters object |
| 3304 | * according with a current TXN. |
| 3305 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3306 | 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] | 3307 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3308 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3309 | |
| 3310 | /* Check stack size. */ |
| 3311 | if (!lua_checkstack(L, 3)) |
| 3312 | return 0; |
| 3313 | |
| 3314 | /* Create the object: obj[0] = userdata. |
| 3315 | * Note that the base of the Converters object is the |
| 3316 | * same than the TXN object. |
| 3317 | */ |
| 3318 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3319 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3320 | lua_rawseti(L, -2, 0); |
| 3321 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3322 | hsmp->s = txn->s; |
| 3323 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3324 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3325 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3326 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3327 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3328 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3329 | lua_setmetatable(L, -2); |
| 3330 | |
| 3331 | return 1; |
| 3332 | } |
| 3333 | |
| 3334 | /* This function is an LUA binding. It is called with each converter. |
| 3335 | * It uses closure argument to store the associated converter. It |
| 3336 | * returns only one argument or throws an error. An error is thrown |
| 3337 | * only if an error is encountered during the argument parsing. If |
| 3338 | * the converter function function fails, nil is returned. |
| 3339 | */ |
| 3340 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3341 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3342 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3343 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3344 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3345 | int i; |
| 3346 | struct sample smp; |
| 3347 | |
| 3348 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3349 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3350 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3351 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3352 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3353 | |
| 3354 | /* Get extra arguments. */ |
| 3355 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3356 | if (i >= ARGM_NBARGS) |
| 3357 | break; |
| 3358 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3359 | } |
| 3360 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3361 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3362 | |
| 3363 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3364 | 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] | 3365 | |
| 3366 | /* Run the special args checker. */ |
| 3367 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3368 | hlua_pusherror(L, "error in arguments"); |
| 3369 | WILL_LJMP(lua_error(L)); |
| 3370 | } |
| 3371 | |
| 3372 | /* Initialise the sample. */ |
| 3373 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3374 | hlua_pusherror(L, "error in the input argument"); |
| 3375 | WILL_LJMP(lua_error(L)); |
| 3376 | } |
| 3377 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3378 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3379 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3380 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3381 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3382 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3383 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3384 | WILL_LJMP(lua_error(L)); |
| 3385 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3386 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3387 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3388 | hlua_pusherror(L, "error during the input argument casting"); |
| 3389 | WILL_LJMP(lua_error(L)); |
| 3390 | } |
| 3391 | |
| 3392 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3393 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3394 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3395 | lua_pushstring(L, ""); |
| 3396 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3397 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3398 | return 1; |
| 3399 | } |
| 3400 | |
| 3401 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3402 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3403 | hlua_smp2lua_str(L, &smp); |
| 3404 | else |
| 3405 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3406 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3407 | } |
| 3408 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3409 | /* |
| 3410 | * |
| 3411 | * |
| 3412 | * Class AppletTCP |
| 3413 | * |
| 3414 | * |
| 3415 | */ |
| 3416 | |
| 3417 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3418 | * a class stream, otherwise it throws an error. |
| 3419 | */ |
| 3420 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3421 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3422 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | /* This function creates and push in the stack an Applet object |
| 3426 | * according with a current TXN. |
| 3427 | */ |
| 3428 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3429 | { |
| 3430 | struct hlua_appctx *appctx; |
| 3431 | struct stream_interface *si = ctx->owner; |
| 3432 | struct stream *s = si_strm(si); |
| 3433 | struct proxy *p = s->be; |
| 3434 | |
| 3435 | /* Check stack size. */ |
| 3436 | if (!lua_checkstack(L, 3)) |
| 3437 | return 0; |
| 3438 | |
| 3439 | /* Create the object: obj[0] = userdata. |
| 3440 | * Note that the base of the Converters object is the |
| 3441 | * same than the TXN object. |
| 3442 | */ |
| 3443 | lua_newtable(L); |
| 3444 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3445 | lua_rawseti(L, -2, 0); |
| 3446 | appctx->appctx = ctx; |
| 3447 | appctx->htxn.s = s; |
| 3448 | appctx->htxn.p = p; |
| 3449 | |
| 3450 | /* Create the "f" field that contains a list of fetches. */ |
| 3451 | lua_pushstring(L, "f"); |
| 3452 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3453 | return 0; |
| 3454 | lua_settable(L, -3); |
| 3455 | |
| 3456 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3457 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3458 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3459 | return 0; |
| 3460 | lua_settable(L, -3); |
| 3461 | |
| 3462 | /* Create the "c" field that contains a list of converters. */ |
| 3463 | lua_pushstring(L, "c"); |
| 3464 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3465 | return 0; |
| 3466 | lua_settable(L, -3); |
| 3467 | |
| 3468 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3469 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3470 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3471 | return 0; |
| 3472 | lua_settable(L, -3); |
| 3473 | |
| 3474 | /* Pop a class stream metatable and affect it to the table. */ |
| 3475 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3476 | lua_setmetatable(L, -2); |
| 3477 | |
| 3478 | return 1; |
| 3479 | } |
| 3480 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3481 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3482 | { |
| 3483 | struct hlua_appctx *appctx; |
| 3484 | struct stream *s; |
| 3485 | const char *name; |
| 3486 | size_t len; |
| 3487 | struct sample smp; |
| 3488 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3489 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3490 | 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] | 3491 | |
| 3492 | /* It is useles to retrieve the stream, but this function |
| 3493 | * runs only in a stream context. |
| 3494 | */ |
| 3495 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3496 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3497 | s = appctx->htxn.s; |
| 3498 | |
| 3499 | /* Converts the third argument in a sample. */ |
| 3500 | hlua_lua2smp(L, 3, &smp); |
| 3501 | |
| 3502 | /* Store the sample in a variable. */ |
| 3503 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3504 | |
| 3505 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3506 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3507 | else |
| 3508 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3509 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3510 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3514 | { |
| 3515 | struct hlua_appctx *appctx; |
| 3516 | struct stream *s; |
| 3517 | const char *name; |
| 3518 | size_t len; |
| 3519 | struct sample smp; |
| 3520 | |
| 3521 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3522 | |
| 3523 | /* It is useles to retrieve the stream, but this function |
| 3524 | * runs only in a stream context. |
| 3525 | */ |
| 3526 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3527 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3528 | s = appctx->htxn.s; |
| 3529 | |
| 3530 | /* Unset the variable. */ |
| 3531 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3532 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3533 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3537 | { |
| 3538 | struct hlua_appctx *appctx; |
| 3539 | struct stream *s; |
| 3540 | const char *name; |
| 3541 | size_t len; |
| 3542 | struct sample smp; |
| 3543 | |
| 3544 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3545 | |
| 3546 | /* It is useles to retrieve the stream, but this function |
| 3547 | * runs only in a stream context. |
| 3548 | */ |
| 3549 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3550 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3551 | s = appctx->htxn.s; |
| 3552 | |
| 3553 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3554 | if (!vars_get_by_name(name, len, &smp)) { |
| 3555 | lua_pushnil(L); |
| 3556 | return 1; |
| 3557 | } |
| 3558 | |
| 3559 | return hlua_smp2lua(L, &smp); |
| 3560 | } |
| 3561 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3562 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3563 | { |
| 3564 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3565 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3566 | struct hlua *hlua; |
| 3567 | |
| 3568 | /* 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] | 3569 | if (!s->hlua) |
| 3570 | return 0; |
| 3571 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3572 | |
| 3573 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3574 | |
| 3575 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3576 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3577 | |
| 3578 | /* Get and store new value. */ |
| 3579 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3580 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3581 | |
| 3582 | return 0; |
| 3583 | } |
| 3584 | |
| 3585 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3586 | { |
| 3587 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3588 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3589 | struct hlua *hlua; |
| 3590 | |
| 3591 | /* 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] | 3592 | if (!s->hlua) { |
| 3593 | lua_pushnil(L); |
| 3594 | return 1; |
| 3595 | } |
| 3596 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3597 | |
| 3598 | /* Push configuration index in the stack. */ |
| 3599 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3600 | |
| 3601 | return 1; |
| 3602 | } |
| 3603 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3604 | /* If expected data not yet available, it returns a yield. This function |
| 3605 | * consumes the data in the buffer. It returns a string containing the |
| 3606 | * data. This string can be empty. |
| 3607 | */ |
| 3608 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3609 | { |
| 3610 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3611 | struct stream_interface *si = appctx->appctx->owner; |
| 3612 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3613 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3614 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3615 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3616 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3617 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3618 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3619 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3620 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3621 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3622 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3623 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3624 | 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] | 3625 | } |
| 3626 | |
| 3627 | /* End of data: commit the total strings and return. */ |
| 3628 | if (ret < 0) { |
| 3629 | luaL_pushresult(&appctx->b); |
| 3630 | return 1; |
| 3631 | } |
| 3632 | |
| 3633 | /* Ensure that the block 2 length is usable. */ |
| 3634 | if (ret == 1) |
| 3635 | len2 = 0; |
| 3636 | |
| 3637 | /* dont check the max length read and dont check. */ |
| 3638 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3639 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3640 | |
| 3641 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3642 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3643 | luaL_pushresult(&appctx->b); |
| 3644 | return 1; |
| 3645 | } |
| 3646 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3647 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3648 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3649 | { |
| 3650 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3651 | |
| 3652 | /* Initialise the string catenation. */ |
| 3653 | luaL_buffinit(L, &appctx->b); |
| 3654 | |
| 3655 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3656 | } |
| 3657 | |
| 3658 | /* If expected data not yet available, it returns a yield. This function |
| 3659 | * consumes the data in the buffer. It returns a string containing the |
| 3660 | * data. This string can be empty. |
| 3661 | */ |
| 3662 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3663 | { |
| 3664 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3665 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3666 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3667 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3668 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3669 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3670 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3671 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3672 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3673 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3674 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3675 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3676 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3677 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3678 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3679 | 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] | 3680 | } |
| 3681 | |
| 3682 | /* End of data: commit the total strings and return. */ |
| 3683 | if (ret < 0) { |
| 3684 | luaL_pushresult(&appctx->b); |
| 3685 | return 1; |
| 3686 | } |
| 3687 | |
| 3688 | /* Ensure that the block 2 length is usable. */ |
| 3689 | if (ret == 1) |
| 3690 | len2 = 0; |
| 3691 | |
| 3692 | if (len == -1) { |
| 3693 | |
| 3694 | /* If len == -1, catenate all the data avalaile and |
| 3695 | * yield because we want to get all the data until |
| 3696 | * the end of data stream. |
| 3697 | */ |
| 3698 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3699 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3700 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3701 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3702 | 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] | 3703 | |
| 3704 | } else { |
| 3705 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3706 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3707 | if (len1 > len) |
| 3708 | len1 = len; |
| 3709 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3710 | len -= len1; |
| 3711 | |
| 3712 | /* Copy the second block. */ |
| 3713 | if (len2 > len) |
| 3714 | len2 = len; |
| 3715 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3716 | len -= len2; |
| 3717 | |
| 3718 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3719 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3720 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3721 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3722 | if (len > 0) { |
| 3723 | lua_pushinteger(L, len); |
| 3724 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3725 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3726 | 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] | 3727 | } |
| 3728 | |
| 3729 | /* return the result. */ |
| 3730 | luaL_pushresult(&appctx->b); |
| 3731 | return 1; |
| 3732 | } |
| 3733 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3734 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3735 | hlua_pusherror(L, "Lua: internal error"); |
| 3736 | WILL_LJMP(lua_error(L)); |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3740 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3741 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3742 | { |
| 3743 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3744 | int len = -1; |
| 3745 | |
| 3746 | if (lua_gettop(L) > 2) |
| 3747 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3748 | if (lua_gettop(L) >= 2) { |
| 3749 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3750 | lua_pop(L, 1); |
| 3751 | } |
| 3752 | |
| 3753 | /* Confirm or set the required length */ |
| 3754 | lua_pushinteger(L, len); |
| 3755 | |
| 3756 | /* Initialise the string catenation. */ |
| 3757 | luaL_buffinit(L, &appctx->b); |
| 3758 | |
| 3759 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3760 | } |
| 3761 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3762 | /* Append data in the output side of the buffer. This data is immediately |
| 3763 | * sent. The function returns the amount of data written. If the buffer |
| 3764 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3765 | * if the channel is closed. |
| 3766 | */ |
| 3767 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3768 | { |
| 3769 | size_t len; |
| 3770 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3771 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3772 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3773 | struct stream_interface *si = appctx->appctx->owner; |
| 3774 | struct channel *chn = si_ic(si); |
| 3775 | int max; |
| 3776 | |
| 3777 | /* Get the max amount of data which can write as input in the channel. */ |
| 3778 | max = channel_recv_max(chn); |
| 3779 | if (max > (len - l)) |
| 3780 | max = len - l; |
| 3781 | |
| 3782 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3783 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3784 | |
| 3785 | /* update counters. */ |
| 3786 | l += max; |
| 3787 | lua_pop(L, 1); |
| 3788 | lua_pushinteger(L, l); |
| 3789 | |
| 3790 | /* If some data is not send, declares the situation to the |
| 3791 | * applet, and returns a yield. |
| 3792 | */ |
| 3793 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3794 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3795 | 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] | 3796 | } |
| 3797 | |
| 3798 | return 1; |
| 3799 | } |
| 3800 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3801 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3802 | * yield the LUA process, and resume it without checking the |
| 3803 | * input arguments. |
| 3804 | */ |
| 3805 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3806 | { |
| 3807 | MAY_LJMP(check_args(L, 2, "send")); |
| 3808 | lua_pushinteger(L, 0); |
| 3809 | |
| 3810 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3811 | } |
| 3812 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3813 | /* |
| 3814 | * |
| 3815 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3816 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3817 | * |
| 3818 | * |
| 3819 | */ |
| 3820 | |
| 3821 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3822 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3823 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3824 | __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] | 3825 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3826 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3827 | } |
| 3828 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3829 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3830 | * according with a current TXN. |
| 3831 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3832 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3833 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3834 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3835 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3836 | struct stream_interface *si = ctx->owner; |
| 3837 | struct stream *s = si_strm(si); |
| 3838 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3839 | struct htx *htx; |
| 3840 | struct htx_blk *blk; |
| 3841 | struct htx_sl *sl; |
| 3842 | struct ist path; |
| 3843 | unsigned long long len = 0; |
| 3844 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3845 | |
| 3846 | /* Check stack size. */ |
| 3847 | if (!lua_checkstack(L, 3)) |
| 3848 | return 0; |
| 3849 | |
| 3850 | /* Create the object: obj[0] = userdata. |
| 3851 | * Note that the base of the Converters object is the |
| 3852 | * same than the TXN object. |
| 3853 | */ |
| 3854 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3855 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3856 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3857 | appctx->appctx = ctx; |
| 3858 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3859 | 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] | 3860 | appctx->htxn.s = s; |
| 3861 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3862 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3863 | /* Create the "f" field that contains a list of fetches. */ |
| 3864 | lua_pushstring(L, "f"); |
| 3865 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3866 | return 0; |
| 3867 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3868 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3869 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3870 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3871 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3872 | return 0; |
| 3873 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3874 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3875 | /* Create the "c" field that contains a list of converters. */ |
| 3876 | lua_pushstring(L, "c"); |
| 3877 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3878 | return 0; |
| 3879 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3880 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3881 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3882 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3883 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3884 | return 0; |
| 3885 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3886 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3887 | htx = htxbuf(&s->req.buf); |
| 3888 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3889 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3890 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3891 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3892 | /* Stores the request method. */ |
| 3893 | lua_pushstring(L, "method"); |
| 3894 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3895 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3896 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3897 | /* Stores the http version. */ |
| 3898 | lua_pushstring(L, "version"); |
| 3899 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3900 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3901 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3902 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3903 | * the array on the top of the stack. |
| 3904 | */ |
| 3905 | lua_pushstring(L, "headers"); |
| 3906 | htxn.s = s; |
| 3907 | htxn.p = px; |
| 3908 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 3909 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3910 | return 0; |
| 3911 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3912 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3913 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3914 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3915 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3916 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3917 | p = path.ptr; |
| 3918 | end = path.ptr + path.len; |
| 3919 | q = p; |
| 3920 | while (q < end && *q != '?') |
| 3921 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3922 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3923 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3924 | lua_pushstring(L, "path"); |
| 3925 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3926 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3927 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3928 | /* Stores the query string. */ |
| 3929 | lua_pushstring(L, "qs"); |
| 3930 | if (*q == '?') |
| 3931 | q++; |
| 3932 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3933 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3934 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3935 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3936 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3937 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3938 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3939 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3940 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3941 | break; |
| 3942 | if (type == HTX_BLK_DATA) |
| 3943 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3944 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3945 | if (htx->extra != ULLONG_MAX) |
| 3946 | len += htx->extra; |
| 3947 | |
| 3948 | /* Stores the request path. */ |
| 3949 | lua_pushstring(L, "length"); |
| 3950 | lua_pushinteger(L, len); |
| 3951 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3952 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3953 | /* Create an empty array of HTTP request headers. */ |
| 3954 | lua_pushstring(L, "response"); |
| 3955 | lua_newtable(L); |
| 3956 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3957 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3958 | /* Pop a class stream metatable and affect it to the table. */ |
| 3959 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3960 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3961 | |
| 3962 | return 1; |
| 3963 | } |
| 3964 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3965 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3966 | { |
| 3967 | struct hlua_appctx *appctx; |
| 3968 | struct stream *s; |
| 3969 | const char *name; |
| 3970 | size_t len; |
| 3971 | struct sample smp; |
| 3972 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3973 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3974 | 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] | 3975 | |
| 3976 | /* It is useles to retrieve the stream, but this function |
| 3977 | * runs only in a stream context. |
| 3978 | */ |
| 3979 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3980 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3981 | s = appctx->htxn.s; |
| 3982 | |
| 3983 | /* Converts the third argument in a sample. */ |
| 3984 | hlua_lua2smp(L, 3, &smp); |
| 3985 | |
| 3986 | /* Store the sample in a variable. */ |
| 3987 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3988 | |
| 3989 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3990 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3991 | else |
| 3992 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3993 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3994 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3998 | { |
| 3999 | struct hlua_appctx *appctx; |
| 4000 | struct stream *s; |
| 4001 | const char *name; |
| 4002 | size_t len; |
| 4003 | struct sample smp; |
| 4004 | |
| 4005 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4006 | |
| 4007 | /* It is useles to retrieve the stream, but this function |
| 4008 | * runs only in a stream context. |
| 4009 | */ |
| 4010 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4011 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4012 | s = appctx->htxn.s; |
| 4013 | |
| 4014 | /* Unset the variable. */ |
| 4015 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4016 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4017 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4021 | { |
| 4022 | struct hlua_appctx *appctx; |
| 4023 | struct stream *s; |
| 4024 | const char *name; |
| 4025 | size_t len; |
| 4026 | struct sample smp; |
| 4027 | |
| 4028 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4029 | |
| 4030 | /* It is useles to retrieve the stream, but this function |
| 4031 | * runs only in a stream context. |
| 4032 | */ |
| 4033 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4034 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4035 | s = appctx->htxn.s; |
| 4036 | |
| 4037 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4038 | if (!vars_get_by_name(name, len, &smp)) { |
| 4039 | lua_pushnil(L); |
| 4040 | return 1; |
| 4041 | } |
| 4042 | |
| 4043 | return hlua_smp2lua(L, &smp); |
| 4044 | } |
| 4045 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4046 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4047 | { |
| 4048 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4049 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4050 | struct hlua *hlua; |
| 4051 | |
| 4052 | /* 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] | 4053 | if (!s->hlua) |
| 4054 | return 0; |
| 4055 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4056 | |
| 4057 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4058 | |
| 4059 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4060 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4061 | |
| 4062 | /* Get and store new value. */ |
| 4063 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4064 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4065 | |
| 4066 | return 0; |
| 4067 | } |
| 4068 | |
| 4069 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4070 | { |
| 4071 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4072 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4073 | struct hlua *hlua; |
| 4074 | |
| 4075 | /* 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] | 4076 | if (!s->hlua) { |
| 4077 | lua_pushnil(L); |
| 4078 | return 1; |
| 4079 | } |
| 4080 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4081 | |
| 4082 | /* Push configuration index in the stack. */ |
| 4083 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4084 | |
| 4085 | return 1; |
| 4086 | } |
| 4087 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4088 | /* If expected data not yet available, it returns a yield. This function |
| 4089 | * consumes the data in the buffer. It returns a string containing the |
| 4090 | * data. This string can be empty. |
| 4091 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4092 | __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] | 4093 | { |
| 4094 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4095 | struct stream_interface *si = appctx->appctx->owner; |
| 4096 | struct channel *req = si_oc(si); |
| 4097 | struct htx *htx; |
| 4098 | struct htx_blk *blk; |
| 4099 | size_t count; |
| 4100 | int stop = 0; |
| 4101 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4102 | htx = htx_from_buf(&req->buf); |
| 4103 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4104 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4105 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4106 | while (count && !stop && blk) { |
| 4107 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4108 | uint32_t sz = htx_get_blksz(blk); |
| 4109 | struct ist v; |
| 4110 | uint32_t vlen; |
| 4111 | char *nl; |
| 4112 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4113 | if (type == HTX_BLK_EOM) { |
| 4114 | stop = 1; |
| 4115 | break; |
| 4116 | } |
| 4117 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4118 | vlen = sz; |
| 4119 | if (vlen > count) { |
| 4120 | if (type != HTX_BLK_DATA) |
| 4121 | break; |
| 4122 | vlen = count; |
| 4123 | } |
| 4124 | |
| 4125 | switch (type) { |
| 4126 | case HTX_BLK_UNUSED: |
| 4127 | break; |
| 4128 | |
| 4129 | case HTX_BLK_DATA: |
| 4130 | v = htx_get_blk_value(htx, blk); |
| 4131 | v.len = vlen; |
| 4132 | nl = istchr(v, '\n'); |
| 4133 | if (nl != NULL) { |
| 4134 | stop = 1; |
| 4135 | vlen = nl - v.ptr + 1; |
| 4136 | } |
| 4137 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4138 | break; |
| 4139 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4140 | case HTX_BLK_TLR: |
| 4141 | case HTX_BLK_EOM: |
| 4142 | stop = 1; |
| 4143 | break; |
| 4144 | |
| 4145 | default: |
| 4146 | break; |
| 4147 | } |
| 4148 | |
| 4149 | co_set_data(req, co_data(req) - vlen); |
| 4150 | count -= vlen; |
| 4151 | if (sz == vlen) |
| 4152 | blk = htx_remove_blk(htx, blk); |
| 4153 | else { |
| 4154 | htx_cut_data_blk(htx, blk, vlen); |
| 4155 | break; |
| 4156 | } |
| 4157 | } |
| 4158 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4159 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4160 | if (!stop) { |
| 4161 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4162 | 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] | 4163 | } |
| 4164 | |
| 4165 | /* return the result. */ |
| 4166 | luaL_pushresult(&appctx->b); |
| 4167 | return 1; |
| 4168 | } |
| 4169 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4170 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4171 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4172 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4173 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4174 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4175 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4176 | /* Initialise the string catenation. */ |
| 4177 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4178 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4179 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4180 | } |
| 4181 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4182 | /* If expected data not yet available, it returns a yield. This function |
| 4183 | * consumes the data in the buffer. It returns a string containing the |
| 4184 | * data. This string can be empty. |
| 4185 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4186 | __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] | 4187 | { |
| 4188 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4189 | struct stream_interface *si = appctx->appctx->owner; |
| 4190 | struct channel *req = si_oc(si); |
| 4191 | struct htx *htx; |
| 4192 | struct htx_blk *blk; |
| 4193 | size_t count; |
| 4194 | int len; |
| 4195 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4196 | htx = htx_from_buf(&req->buf); |
| 4197 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4198 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4199 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4200 | while (count && len && blk) { |
| 4201 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4202 | uint32_t sz = htx_get_blksz(blk); |
| 4203 | struct ist v; |
| 4204 | uint32_t vlen; |
| 4205 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4206 | if (type == HTX_BLK_EOM) { |
| 4207 | len = 0; |
| 4208 | break; |
| 4209 | } |
| 4210 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4211 | vlen = sz; |
| 4212 | if (len > 0 && vlen > len) |
| 4213 | vlen = len; |
| 4214 | if (vlen > count) { |
| 4215 | if (type != HTX_BLK_DATA) |
| 4216 | break; |
| 4217 | vlen = count; |
| 4218 | } |
| 4219 | |
| 4220 | switch (type) { |
| 4221 | case HTX_BLK_UNUSED: |
| 4222 | break; |
| 4223 | |
| 4224 | case HTX_BLK_DATA: |
| 4225 | v = htx_get_blk_value(htx, blk); |
| 4226 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4227 | break; |
| 4228 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4229 | case HTX_BLK_TLR: |
| 4230 | case HTX_BLK_EOM: |
| 4231 | len = 0; |
| 4232 | break; |
| 4233 | |
| 4234 | default: |
| 4235 | break; |
| 4236 | } |
| 4237 | |
| 4238 | co_set_data(req, co_data(req) - vlen); |
| 4239 | count -= vlen; |
| 4240 | if (len > 0) |
| 4241 | len -= vlen; |
| 4242 | if (sz == vlen) |
| 4243 | blk = htx_remove_blk(htx, blk); |
| 4244 | else { |
| 4245 | htx_cut_data_blk(htx, blk, vlen); |
| 4246 | break; |
| 4247 | } |
| 4248 | } |
| 4249 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4250 | htx_to_buf(htx, &req->buf); |
| 4251 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4252 | /* If we are no other data available, yield waiting for new data. */ |
| 4253 | if (len) { |
| 4254 | if (len > 0) { |
| 4255 | lua_pushinteger(L, len); |
| 4256 | lua_replace(L, 2); |
| 4257 | } |
| 4258 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4259 | 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] | 4260 | } |
| 4261 | |
| 4262 | /* return the result. */ |
| 4263 | luaL_pushresult(&appctx->b); |
| 4264 | return 1; |
| 4265 | } |
| 4266 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4267 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4268 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4269 | { |
| 4270 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4271 | int len = -1; |
| 4272 | |
| 4273 | /* Check arguments. */ |
| 4274 | if (lua_gettop(L) > 2) |
| 4275 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4276 | if (lua_gettop(L) >= 2) { |
| 4277 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4278 | lua_pop(L, 1); |
| 4279 | } |
| 4280 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4281 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4282 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4283 | /* Initialise the string catenation. */ |
| 4284 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4285 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4286 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | /* Append data in the output side of the buffer. This data is immediately |
| 4290 | * sent. The function returns the amount of data written. If the buffer |
| 4291 | * cannot contain the data, the function yields. The function returns -1 |
| 4292 | * if the channel is closed. |
| 4293 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4294 | __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] | 4295 | { |
| 4296 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4297 | struct stream_interface *si = appctx->appctx->owner; |
| 4298 | struct channel *res = si_ic(si); |
| 4299 | struct htx *htx = htx_from_buf(&res->buf); |
| 4300 | const char *data; |
| 4301 | size_t len; |
| 4302 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4303 | int max; |
| 4304 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4305 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4306 | if (!max) |
| 4307 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4308 | |
| 4309 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4310 | |
| 4311 | /* Get the max amount of data which can write as input in the channel. */ |
| 4312 | if (max > (len - l)) |
| 4313 | max = len - l; |
| 4314 | |
| 4315 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4316 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4317 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4318 | |
| 4319 | /* update counters. */ |
| 4320 | l += max; |
| 4321 | lua_pop(L, 1); |
| 4322 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4323 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4324 | /* If some data is not send, declares the situation to the |
| 4325 | * applet, and returns a yield. |
| 4326 | */ |
| 4327 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4328 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4329 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4330 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4331 | 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] | 4332 | } |
| 4333 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4334 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4335 | return 1; |
| 4336 | } |
| 4337 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4338 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4339 | * yield the LUA process, and resume it without checking the |
| 4340 | * input arguments. |
| 4341 | */ |
| 4342 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4343 | { |
| 4344 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4345 | |
| 4346 | /* We want to send some data. Headers must be sent. */ |
| 4347 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4348 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4349 | WILL_LJMP(lua_error(L)); |
| 4350 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4351 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4352 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4353 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4354 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4355 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4359 | { |
| 4360 | const char *name; |
| 4361 | int ret; |
| 4362 | |
| 4363 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4364 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4365 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4366 | |
| 4367 | /* Push in the stack the "response" entry. */ |
| 4368 | ret = lua_getfield(L, 1, "response"); |
| 4369 | if (ret != LUA_TTABLE) { |
| 4370 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4371 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4372 | WILL_LJMP(lua_error(L)); |
| 4373 | } |
| 4374 | |
| 4375 | /* check if the header is already registered if it is not |
| 4376 | * the case, register it. |
| 4377 | */ |
| 4378 | ret = lua_getfield(L, -1, name); |
| 4379 | if (ret == LUA_TNIL) { |
| 4380 | |
| 4381 | /* Entry not found. */ |
| 4382 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4383 | |
| 4384 | /* Insert the new header name in the array in the top of the stack. |
| 4385 | * It left the new array in the top of the stack. |
| 4386 | */ |
| 4387 | lua_newtable(L); |
| 4388 | lua_pushvalue(L, 2); |
| 4389 | lua_pushvalue(L, -2); |
| 4390 | lua_settable(L, -4); |
| 4391 | |
| 4392 | } else if (ret != LUA_TTABLE) { |
| 4393 | |
| 4394 | /* corruption error. */ |
| 4395 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4396 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4397 | WILL_LJMP(lua_error(L)); |
| 4398 | } |
| 4399 | |
| 4400 | /* Now the top od thestack is an array of values. We push |
| 4401 | * the header value as new entry. |
| 4402 | */ |
| 4403 | lua_pushvalue(L, 3); |
| 4404 | ret = lua_rawlen(L, -2); |
| 4405 | lua_rawseti(L, -2, ret + 1); |
| 4406 | lua_pushboolean(L, 1); |
| 4407 | return 1; |
| 4408 | } |
| 4409 | |
| 4410 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4411 | { |
| 4412 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4413 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4414 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4415 | |
| 4416 | if (status < 100 || status > 599) { |
| 4417 | lua_pushboolean(L, 0); |
| 4418 | return 1; |
| 4419 | } |
| 4420 | |
| 4421 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4422 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4423 | lua_pushboolean(L, 1); |
| 4424 | return 1; |
| 4425 | } |
| 4426 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4427 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4428 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4429 | { |
| 4430 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4431 | struct stream_interface *si = appctx->appctx->owner; |
| 4432 | struct channel *res = si_ic(si); |
| 4433 | struct htx *htx; |
| 4434 | struct htx_sl *sl; |
| 4435 | struct h1m h1m; |
| 4436 | const char *status, *reason; |
| 4437 | const char *name, *value; |
| 4438 | size_t nlen, vlen; |
| 4439 | unsigned int flags; |
| 4440 | |
| 4441 | /* Send the message at once. */ |
| 4442 | htx = htx_from_buf(&res->buf); |
| 4443 | h1m_init_res(&h1m); |
| 4444 | |
| 4445 | /* Use the same http version than the request. */ |
| 4446 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4447 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4448 | if (reason == NULL) |
| 4449 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4450 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4451 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4452 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4453 | } |
| 4454 | else { |
| 4455 | flags = HTX_SL_F_IS_RESP; |
| 4456 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4457 | } |
| 4458 | if (!sl) { |
| 4459 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4460 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4461 | WILL_LJMP(lua_error(L)); |
| 4462 | } |
| 4463 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4464 | |
| 4465 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4466 | lua_pushvalue(L, 0); |
| 4467 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4468 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4469 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4470 | WILL_LJMP(lua_error(L)); |
| 4471 | } |
| 4472 | |
| 4473 | /* Browse the list of headers. */ |
| 4474 | lua_pushnil(L); |
| 4475 | while(lua_next(L, -2) != 0) { |
| 4476 | /* We expect a string as -2. */ |
| 4477 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4478 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4479 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4480 | lua_typename(L, lua_type(L, -2))); |
| 4481 | WILL_LJMP(lua_error(L)); |
| 4482 | } |
| 4483 | name = lua_tolstring(L, -2, &nlen); |
| 4484 | |
| 4485 | /* We expect an array as -1. */ |
| 4486 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4487 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4488 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4489 | name, |
| 4490 | lua_typename(L, lua_type(L, -1))); |
| 4491 | WILL_LJMP(lua_error(L)); |
| 4492 | } |
| 4493 | |
| 4494 | /* Browse the table who is on the top of the stack. */ |
| 4495 | lua_pushnil(L); |
| 4496 | while(lua_next(L, -2) != 0) { |
| 4497 | int id; |
| 4498 | |
| 4499 | /* We expect a number as -2. */ |
| 4500 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4501 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4502 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4503 | name, |
| 4504 | lua_typename(L, lua_type(L, -2))); |
| 4505 | WILL_LJMP(lua_error(L)); |
| 4506 | } |
| 4507 | id = lua_tointeger(L, -2); |
| 4508 | |
| 4509 | /* We expect a string as -2. */ |
| 4510 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4511 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4512 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4513 | name, id, |
| 4514 | lua_typename(L, lua_type(L, -1))); |
| 4515 | WILL_LJMP(lua_error(L)); |
| 4516 | } |
| 4517 | value = lua_tolstring(L, -1, &vlen); |
| 4518 | |
| 4519 | /* Simple Protocol checks. */ |
| 4520 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4521 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4522 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4523 | struct ist v = ist2(value, vlen); |
| 4524 | int ret; |
| 4525 | |
| 4526 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4527 | if (ret < 0) { |
| 4528 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4529 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4530 | name); |
| 4531 | WILL_LJMP(lua_error(L)); |
| 4532 | } |
| 4533 | else if (ret == 0) |
| 4534 | goto next; /* Skip it */ |
| 4535 | } |
| 4536 | |
| 4537 | /* Add a new header */ |
| 4538 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4539 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4540 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4541 | name); |
| 4542 | WILL_LJMP(lua_error(L)); |
| 4543 | } |
| 4544 | next: |
| 4545 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4546 | lua_pop(L, 1); |
| 4547 | } |
| 4548 | |
| 4549 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4550 | lua_pop(L, 1); |
| 4551 | } |
| 4552 | |
| 4553 | if (h1m.flags & H1_MF_CHNK) |
| 4554 | h1m.flags &= ~H1_MF_CLEN; |
| 4555 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4556 | h1m.flags |= H1_MF_XFER_LEN; |
| 4557 | |
| 4558 | /* Uset HTX start-line flags */ |
| 4559 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4560 | flags |= HTX_SL_F_XFER_ENC; |
| 4561 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4562 | flags |= HTX_SL_F_XFER_LEN; |
| 4563 | if (h1m.flags & H1_MF_CHNK) |
| 4564 | flags |= HTX_SL_F_CHNK; |
| 4565 | else if (h1m.flags & H1_MF_CLEN) |
| 4566 | flags |= HTX_SL_F_CLEN; |
| 4567 | if (h1m.body_len == 0) |
| 4568 | flags |= HTX_SL_F_BODYLESS; |
| 4569 | } |
| 4570 | sl->flags |= flags; |
| 4571 | |
| 4572 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4573 | * and the status code implies the presence of a message body, we must |
| 4574 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4575 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4576 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4577 | */ |
| 4578 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4579 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4580 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4581 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4582 | /* Add a new header */ |
| 4583 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4584 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4585 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4586 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4587 | WILL_LJMP(lua_error(L)); |
| 4588 | } |
| 4589 | } |
| 4590 | |
| 4591 | /* Finalize headers. */ |
| 4592 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4593 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4594 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4595 | WILL_LJMP(lua_error(L)); |
| 4596 | } |
| 4597 | |
| 4598 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4599 | b_reset(&res->buf); |
| 4600 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4601 | WILL_LJMP(lua_error(L)); |
| 4602 | } |
| 4603 | |
| 4604 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4605 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4606 | |
| 4607 | /* Headers sent, set the flag. */ |
| 4608 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4609 | return 0; |
| 4610 | |
| 4611 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4612 | /* We will build the status line and the headers of the HTTP response. |
| 4613 | * We will try send at once if its not possible, we give back the hand |
| 4614 | * waiting for more room. |
| 4615 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4616 | __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] | 4617 | { |
| 4618 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4619 | struct stream_interface *si = appctx->appctx->owner; |
| 4620 | struct channel *res = si_ic(si); |
| 4621 | |
| 4622 | if (co_data(res)) { |
| 4623 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4624 | 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] | 4625 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4626 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4630 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4631 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4632 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4633 | } |
| 4634 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4635 | /* |
| 4636 | * |
| 4637 | * |
| 4638 | * Class HTTP |
| 4639 | * |
| 4640 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4641 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4642 | |
| 4643 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4644 | * a class stream, otherwise it throws an error. |
| 4645 | */ |
| 4646 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4647 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4648 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4649 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4650 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4651 | /* This function creates and push in the stack a HTTP object |
| 4652 | * according with a current TXN. |
| 4653 | */ |
| 4654 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4655 | { |
| 4656 | struct hlua_txn *htxn; |
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 | /* Check stack size. */ |
| 4659 | if (!lua_checkstack(L, 3)) |
| 4660 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4661 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4662 | /* Create the object: obj[0] = userdata. |
| 4663 | * Note that the base of the Converters object is the |
| 4664 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4665 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4666 | lua_newtable(L); |
| 4667 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4668 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4669 | |
| 4670 | htxn->s = txn->s; |
| 4671 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4672 | htxn->dir = txn->dir; |
| 4673 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4674 | |
| 4675 | /* Pop a class stream metatable and affect it to the table. */ |
| 4676 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4677 | lua_setmetatable(L, -2); |
| 4678 | |
| 4679 | return 1; |
| 4680 | } |
| 4681 | |
| 4682 | /* This function creates ans returns an array of HTTP headers. |
| 4683 | * This function does not fails. It is used as wrapper with the |
| 4684 | * 2 following functions. |
| 4685 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4686 | __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] | 4687 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4688 | struct htx *htx; |
| 4689 | int32_t pos; |
| 4690 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4691 | /* Create the table. */ |
| 4692 | lua_newtable(L); |
| 4693 | |
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 | htx = htxbuf(&msg->chn->buf); |
| 4696 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4697 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4698 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4699 | struct ist n, v; |
| 4700 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4701 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4702 | if (type == HTX_BLK_HDR) { |
| 4703 | n = htx_get_blk_name(htx,blk); |
| 4704 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4705 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4706 | else if (type == HTX_BLK_EOH) |
| 4707 | break; |
| 4708 | else |
| 4709 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4710 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4711 | /* Check for existing entry: |
| 4712 | * assume that the table is on the top of the stack, and |
| 4713 | * push the key in the stack, the function lua_gettable() |
| 4714 | * perform the lookup. |
| 4715 | */ |
| 4716 | lua_pushlstring(L, n.ptr, n.len); |
| 4717 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4718 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4719 | switch (lua_type(L, -1)) { |
| 4720 | case LUA_TNIL: |
| 4721 | /* Table not found, create it. */ |
| 4722 | lua_pop(L, 1); /* remove the nil value. */ |
| 4723 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4724 | lua_newtable(L); /* create and push empty table. */ |
| 4725 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4726 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4727 | 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] | 4728 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4729 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4730 | case LUA_TTABLE: |
| 4731 | /* Entry found: push the value in the table. */ |
| 4732 | len = lua_rawlen(L, -1); |
| 4733 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4734 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4735 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4736 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4737 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4738 | default: |
| 4739 | /* Other cases are errors. */ |
| 4740 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4741 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4742 | } |
| 4743 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4744 | return 1; |
| 4745 | } |
| 4746 | |
| 4747 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4748 | { |
| 4749 | struct hlua_txn *htxn; |
| 4750 | |
| 4751 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4752 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4753 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4754 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4755 | WILL_LJMP(lua_error(L)); |
| 4756 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4757 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4761 | { |
| 4762 | struct hlua_txn *htxn; |
| 4763 | |
| 4764 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4765 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4766 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4767 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4768 | WILL_LJMP(lua_error(L)); |
| 4769 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4770 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | /* This function replace full header, or just a value in |
| 4774 | * the request or in the response. It is a wrapper fir the |
| 4775 | * 4 following functions. |
| 4776 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4777 | __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] | 4778 | { |
| 4779 | size_t name_len; |
| 4780 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4781 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4782 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4783 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4784 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4785 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4786 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4787 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4788 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4789 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4790 | 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] | 4791 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4792 | return 0; |
| 4793 | } |
| 4794 | |
| 4795 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4796 | { |
| 4797 | struct hlua_txn *htxn; |
| 4798 | |
| 4799 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4800 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4801 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4802 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4803 | WILL_LJMP(lua_error(L)); |
| 4804 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4805 | 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] | 4806 | } |
| 4807 | |
| 4808 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4809 | { |
| 4810 | struct hlua_txn *htxn; |
| 4811 | |
| 4812 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4813 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4814 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4815 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4816 | WILL_LJMP(lua_error(L)); |
| 4817 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4818 | 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] | 4819 | } |
| 4820 | |
| 4821 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4822 | { |
| 4823 | struct hlua_txn *htxn; |
| 4824 | |
| 4825 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4826 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4827 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4828 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4829 | WILL_LJMP(lua_error(L)); |
| 4830 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4831 | 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] | 4832 | } |
| 4833 | |
| 4834 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4835 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4836 | struct hlua_txn *htxn; |
| 4837 | |
| 4838 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4839 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4840 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4841 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4842 | WILL_LJMP(lua_error(L)); |
| 4843 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4844 | 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] | 4845 | } |
| 4846 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4847 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4848 | * It is a wrapper for the 2 following functions. |
| 4849 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4850 | __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] | 4851 | { |
| 4852 | size_t len; |
| 4853 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4854 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4855 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4856 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4857 | ctx.blk = NULL; |
| 4858 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4859 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4860 | return 0; |
| 4861 | } |
| 4862 | |
| 4863 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4864 | { |
| 4865 | struct hlua_txn *htxn; |
| 4866 | |
| 4867 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4868 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4869 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4870 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4871 | WILL_LJMP(lua_error(L)); |
| 4872 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4873 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4877 | { |
| 4878 | struct hlua_txn *htxn; |
| 4879 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4880 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4881 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4882 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4883 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4884 | WILL_LJMP(lua_error(L)); |
| 4885 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4886 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | /* This function adds an header. It is a wrapper used by |
| 4890 | * the 2 following functions. |
| 4891 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4892 | __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] | 4893 | { |
| 4894 | size_t name_len; |
| 4895 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4896 | size_t value_len; |
| 4897 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4898 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4899 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4900 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4901 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4902 | return 0; |
| 4903 | } |
| 4904 | |
| 4905 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4906 | { |
| 4907 | struct hlua_txn *htxn; |
| 4908 | |
| 4909 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4910 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4911 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4912 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4913 | WILL_LJMP(lua_error(L)); |
| 4914 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4915 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4919 | { |
| 4920 | struct hlua_txn *htxn; |
| 4921 | |
| 4922 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4923 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4924 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4925 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4926 | WILL_LJMP(lua_error(L)); |
| 4927 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4928 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4932 | { |
| 4933 | struct hlua_txn *htxn; |
| 4934 | |
| 4935 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4936 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4937 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4938 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4939 | WILL_LJMP(lua_error(L)); |
| 4940 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4941 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 4942 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4946 | { |
| 4947 | struct hlua_txn *htxn; |
| 4948 | |
| 4949 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4950 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4951 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4952 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4953 | WILL_LJMP(lua_error(L)); |
| 4954 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4955 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 4956 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | /* This function set the method. */ |
| 4960 | static int hlua_http_req_set_meth(lua_State *L) |
| 4961 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4962 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4963 | size_t name_len; |
| 4964 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4965 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4966 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4967 | WILL_LJMP(lua_error(L)); |
| 4968 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4969 | 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] | 4970 | return 1; |
| 4971 | } |
| 4972 | |
| 4973 | /* This function set the method. */ |
| 4974 | static int hlua_http_req_set_path(lua_State *L) |
| 4975 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4976 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4977 | size_t name_len; |
| 4978 | 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] | 4979 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4980 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4981 | WILL_LJMP(lua_error(L)); |
| 4982 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4983 | 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] | 4984 | return 1; |
| 4985 | } |
| 4986 | |
| 4987 | /* This function set the query-string. */ |
| 4988 | static int hlua_http_req_set_query(lua_State *L) |
| 4989 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4990 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4991 | size_t name_len; |
| 4992 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4993 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4994 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4995 | WILL_LJMP(lua_error(L)); |
| 4996 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4997 | /* Check length. */ |
| 4998 | if (name_len > trash.size - 1) { |
| 4999 | lua_pushboolean(L, 0); |
| 5000 | return 1; |
| 5001 | } |
| 5002 | |
| 5003 | /* Add the mark question as prefix. */ |
| 5004 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5005 | trash.area[trash.data++] = '?'; |
| 5006 | memcpy(trash.area + trash.data, name, name_len); |
| 5007 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5008 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5009 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5010 | 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] | 5011 | return 1; |
| 5012 | } |
| 5013 | |
| 5014 | /* This function set the uri. */ |
| 5015 | static int hlua_http_req_set_uri(lua_State *L) |
| 5016 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5017 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5018 | size_t name_len; |
| 5019 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5020 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5021 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5022 | WILL_LJMP(lua_error(L)); |
| 5023 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5024 | 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] | 5025 | return 1; |
| 5026 | } |
| 5027 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5028 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5029 | static int hlua_http_res_set_status(lua_State *L) |
| 5030 | { |
| 5031 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5032 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5033 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5034 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5035 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5036 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5037 | WILL_LJMP(lua_error(L)); |
| 5038 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5039 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5040 | return 0; |
| 5041 | } |
| 5042 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5043 | /* |
| 5044 | * |
| 5045 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5046 | * Class TXN |
| 5047 | * |
| 5048 | * |
| 5049 | */ |
| 5050 | |
| 5051 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5052 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5053 | */ |
| 5054 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5055 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5056 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5057 | } |
| 5058 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5059 | __LJMP static int hlua_set_var(lua_State *L) |
| 5060 | { |
| 5061 | struct hlua_txn *htxn; |
| 5062 | const char *name; |
| 5063 | size_t len; |
| 5064 | struct sample smp; |
| 5065 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5066 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5067 | 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] | 5068 | |
| 5069 | /* It is useles to retrieve the stream, but this function |
| 5070 | * runs only in a stream context. |
| 5071 | */ |
| 5072 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5073 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5074 | |
| 5075 | /* Converts the third argument in a sample. */ |
| 5076 | hlua_lua2smp(L, 3, &smp); |
| 5077 | |
| 5078 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5079 | 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] | 5080 | |
| 5081 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5082 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5083 | else |
| 5084 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5085 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5086 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5087 | } |
| 5088 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5089 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5090 | { |
| 5091 | struct hlua_txn *htxn; |
| 5092 | const char *name; |
| 5093 | size_t len; |
| 5094 | struct sample smp; |
| 5095 | |
| 5096 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5097 | |
| 5098 | /* It is useles to retrieve the stream, but this function |
| 5099 | * runs only in a stream context. |
| 5100 | */ |
| 5101 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5102 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5103 | |
| 5104 | /* Unset the variable. */ |
| 5105 | 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] | 5106 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5107 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5108 | } |
| 5109 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5110 | __LJMP static int hlua_get_var(lua_State *L) |
| 5111 | { |
| 5112 | struct hlua_txn *htxn; |
| 5113 | const char *name; |
| 5114 | size_t len; |
| 5115 | struct sample smp; |
| 5116 | |
| 5117 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5118 | |
| 5119 | /* It is useles to retrieve the stream, but this function |
| 5120 | * runs only in a stream context. |
| 5121 | */ |
| 5122 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5123 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5124 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5125 | 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] | 5126 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5127 | lua_pushnil(L); |
| 5128 | return 1; |
| 5129 | } |
| 5130 | |
| 5131 | return hlua_smp2lua(L, &smp); |
| 5132 | } |
| 5133 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5134 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5135 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5136 | struct hlua *hlua; |
| 5137 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5138 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5139 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5140 | /* It is useles to retrieve the stream, but this function |
| 5141 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5142 | */ |
| 5143 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5144 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5145 | |
| 5146 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5147 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5148 | |
| 5149 | /* Get and store new value. */ |
| 5150 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5151 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5152 | |
| 5153 | return 0; |
| 5154 | } |
| 5155 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5156 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5157 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5158 | struct hlua *hlua; |
| 5159 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5160 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5161 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5162 | /* It is useles to retrieve the stream, but this function |
| 5163 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5164 | */ |
| 5165 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5166 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5167 | |
| 5168 | /* Push configuration index in the stack. */ |
| 5169 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5170 | |
| 5171 | return 1; |
| 5172 | } |
| 5173 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5174 | /* Create stack entry containing a class TXN. This function |
| 5175 | * return 0 if the stack does not contains free slots, |
| 5176 | * otherwise it returns 1. |
| 5177 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5178 | 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] | 5179 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5180 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5181 | |
| 5182 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5183 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5184 | return 0; |
| 5185 | |
| 5186 | /* NOTE: The allocation never fails. The failure |
| 5187 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5188 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5189 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5190 | /* Create the object: obj[0] = userdata. */ |
| 5191 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5192 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5193 | lua_rawseti(L, -2, 0); |
| 5194 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5195 | htxn->s = s; |
| 5196 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5197 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5198 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5199 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5200 | /* Create the "f" field that contains a list of fetches. */ |
| 5201 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5202 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5203 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5204 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5205 | |
| 5206 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5207 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5208 | 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] | 5209 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5210 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5211 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5212 | /* Create the "c" field that contains a list of converters. */ |
| 5213 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5214 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5215 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5216 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5217 | |
| 5218 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5219 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5220 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5221 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5222 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5223 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5224 | /* Create the "req" field that contains the request channel object. */ |
| 5225 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5226 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5227 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5228 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5229 | |
| 5230 | /* Create the "res" field that contains the response channel object. */ |
| 5231 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5232 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5233 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5234 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5235 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5236 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5237 | lua_pushstring(L, "http"); |
| 5238 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5239 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5240 | return 0; |
| 5241 | } |
| 5242 | else |
| 5243 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5244 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5245 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5246 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5247 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5248 | lua_setmetatable(L, -2); |
| 5249 | |
| 5250 | return 1; |
| 5251 | } |
| 5252 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5253 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5254 | { |
| 5255 | const char *msg; |
| 5256 | struct hlua_txn *htxn; |
| 5257 | |
| 5258 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5259 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5260 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5261 | |
| 5262 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5263 | return 0; |
| 5264 | } |
| 5265 | |
| 5266 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5267 | { |
| 5268 | int level; |
| 5269 | const char *msg; |
| 5270 | struct hlua_txn *htxn; |
| 5271 | |
| 5272 | MAY_LJMP(check_args(L, 3, "log")); |
| 5273 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5274 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5275 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5276 | |
| 5277 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5278 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5279 | |
| 5280 | hlua_sendlog(htxn->s->be, level, msg); |
| 5281 | return 0; |
| 5282 | } |
| 5283 | |
| 5284 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5285 | { |
| 5286 | const char *msg; |
| 5287 | struct hlua_txn *htxn; |
| 5288 | |
| 5289 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5290 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5291 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5292 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5293 | return 0; |
| 5294 | } |
| 5295 | |
| 5296 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5297 | { |
| 5298 | const char *msg; |
| 5299 | struct hlua_txn *htxn; |
| 5300 | |
| 5301 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5302 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5303 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5304 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5305 | return 0; |
| 5306 | } |
| 5307 | |
| 5308 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5309 | { |
| 5310 | const char *msg; |
| 5311 | struct hlua_txn *htxn; |
| 5312 | |
| 5313 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5314 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5315 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5316 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5317 | return 0; |
| 5318 | } |
| 5319 | |
| 5320 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5321 | { |
| 5322 | const char *msg; |
| 5323 | struct hlua_txn *htxn; |
| 5324 | |
| 5325 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5326 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5327 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5328 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5329 | return 0; |
| 5330 | } |
| 5331 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5332 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5333 | { |
| 5334 | struct hlua_txn *htxn; |
| 5335 | int ll; |
| 5336 | |
| 5337 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5338 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5339 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5340 | |
| 5341 | if (ll < 0 || ll > 7) |
| 5342 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5343 | |
| 5344 | htxn->s->logs.level = ll; |
| 5345 | return 0; |
| 5346 | } |
| 5347 | |
| 5348 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5349 | { |
| 5350 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5351 | int tos; |
| 5352 | |
| 5353 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5354 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5355 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5356 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5357 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5358 | return 0; |
| 5359 | } |
| 5360 | |
| 5361 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5362 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5363 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5364 | int mark; |
| 5365 | |
| 5366 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5367 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5368 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5369 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5370 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5371 | return 0; |
| 5372 | } |
| 5373 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5374 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5375 | { |
| 5376 | struct hlua_txn *htxn; |
| 5377 | |
| 5378 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5379 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5380 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5381 | return 0; |
| 5382 | } |
| 5383 | |
| 5384 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5385 | { |
| 5386 | struct hlua_txn *htxn; |
| 5387 | |
| 5388 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5389 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5390 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5391 | return 0; |
| 5392 | } |
| 5393 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5394 | /* 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] | 5395 | * 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] | 5396 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5397 | * error. The Reply must be on top of the stack. |
| 5398 | */ |
| 5399 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5400 | { |
| 5401 | struct htx *htx; |
| 5402 | struct htx_sl *sl; |
| 5403 | struct h1m h1m; |
| 5404 | const char *status, *reason, *body; |
| 5405 | size_t status_len, reason_len, body_len; |
| 5406 | int ret, code, flags; |
| 5407 | |
| 5408 | code = 200; |
| 5409 | status = "200"; |
| 5410 | status_len = 3; |
| 5411 | ret = lua_getfield(L, -1, "status"); |
| 5412 | if (ret == LUA_TNUMBER) { |
| 5413 | code = lua_tointeger(L, -1); |
| 5414 | status = lua_tolstring(L, -1, &status_len); |
| 5415 | } |
| 5416 | lua_pop(L, 1); |
| 5417 | |
| 5418 | reason = http_get_reason(code); |
| 5419 | reason_len = strlen(reason); |
| 5420 | ret = lua_getfield(L, -1, "reason"); |
| 5421 | if (ret == LUA_TSTRING) |
| 5422 | reason = lua_tolstring(L, -1, &reason_len); |
| 5423 | lua_pop(L, 1); |
| 5424 | |
| 5425 | body = NULL; |
| 5426 | body_len = 0; |
| 5427 | ret = lua_getfield(L, -1, "body"); |
| 5428 | if (ret == LUA_TSTRING) |
| 5429 | body = lua_tolstring(L, -1, &body_len); |
| 5430 | lua_pop(L, 1); |
| 5431 | |
| 5432 | /* Prepare the response before inserting the headers */ |
| 5433 | h1m_init_res(&h1m); |
| 5434 | htx = htx_from_buf(&s->res.buf); |
| 5435 | channel_htx_truncate(&s->res, htx); |
| 5436 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5437 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5438 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5439 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5440 | } |
| 5441 | else { |
| 5442 | flags = HTX_SL_F_IS_RESP; |
| 5443 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5444 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5445 | } |
| 5446 | if (!sl) |
| 5447 | goto fail; |
| 5448 | sl->info.res.status = code; |
| 5449 | |
| 5450 | /* Push in the stack the "headers" entry. */ |
| 5451 | ret = lua_getfield(L, -1, "headers"); |
| 5452 | if (ret != LUA_TTABLE) |
| 5453 | goto skip_headers; |
| 5454 | |
| 5455 | lua_pushnil(L); |
| 5456 | while (lua_next(L, -2) != 0) { |
| 5457 | struct ist name, value; |
| 5458 | const char *n, *v; |
| 5459 | size_t nlen, vlen; |
| 5460 | |
| 5461 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5462 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5463 | goto next_hdr; |
| 5464 | } |
| 5465 | |
| 5466 | n = lua_tolstring(L, -2, &nlen); |
| 5467 | name = ist2(n, nlen); |
| 5468 | if (isteqi(name, ist("content-length"))) { |
| 5469 | /* Always skip content-length header. It will be added |
| 5470 | * later with the correct len |
| 5471 | */ |
| 5472 | goto next_hdr; |
| 5473 | } |
| 5474 | |
| 5475 | /* Loop on header's values */ |
| 5476 | lua_pushnil(L); |
| 5477 | while (lua_next(L, -2)) { |
| 5478 | if (!lua_isstring(L, -1)) { |
| 5479 | /* Skip the value if it is not a string */ |
| 5480 | goto next_value; |
| 5481 | } |
| 5482 | |
| 5483 | v = lua_tolstring(L, -1, &vlen); |
| 5484 | value = ist2(v, vlen); |
| 5485 | |
| 5486 | if (isteqi(name, ist("transfer-encoding"))) |
| 5487 | h1_parse_xfer_enc_header(&h1m, value); |
| 5488 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5489 | goto fail; |
| 5490 | |
| 5491 | next_value: |
| 5492 | lua_pop(L, 1); |
| 5493 | } |
| 5494 | |
| 5495 | next_hdr: |
| 5496 | lua_pop(L, 1); |
| 5497 | } |
| 5498 | skip_headers: |
| 5499 | lua_pop(L, 1); |
| 5500 | |
| 5501 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5502 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5503 | const char *clen = ultoa(body_len); |
| 5504 | |
| 5505 | h1m.flags |= H1_MF_CLEN; |
| 5506 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5507 | goto fail; |
| 5508 | } |
| 5509 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5510 | h1m.flags |= H1_MF_XFER_LEN; |
| 5511 | |
| 5512 | /* Update HTX start-line flags */ |
| 5513 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5514 | flags |= HTX_SL_F_XFER_ENC; |
| 5515 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5516 | flags |= HTX_SL_F_XFER_LEN; |
| 5517 | if (h1m.flags & H1_MF_CHNK) |
| 5518 | flags |= HTX_SL_F_CHNK; |
| 5519 | else if (h1m.flags & H1_MF_CLEN) |
| 5520 | flags |= HTX_SL_F_CLEN; |
| 5521 | if (h1m.body_len == 0) |
| 5522 | flags |= HTX_SL_F_BODYLESS; |
| 5523 | } |
| 5524 | sl->flags |= flags; |
| 5525 | |
| 5526 | |
| 5527 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5528 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5529 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5530 | goto fail; |
| 5531 | |
| 5532 | /* Now, forward the response and terminate the transaction */ |
| 5533 | s->txn->status = code; |
| 5534 | htx_to_buf(htx, &s->res.buf); |
| 5535 | if (!http_forward_proxy_resp(s, 1)) |
| 5536 | goto fail; |
| 5537 | |
| 5538 | return 1; |
| 5539 | |
| 5540 | fail: |
| 5541 | channel_htx_truncate(&s->res, htx); |
| 5542 | return 0; |
| 5543 | } |
| 5544 | |
| 5545 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5546 | * processing is just aborted. Nothing is returned to the client and all |
| 5547 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5548 | * is forwarded to the client before terminating the transaction. On success, |
| 5549 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5550 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5551 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5552 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5553 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5554 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5555 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5556 | struct stream *s; |
| 5557 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5558 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5559 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5560 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5561 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5562 | * just end the execution of the current lua code. */ |
| 5563 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5564 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5565 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5566 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5567 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5568 | struct channel *req = &s->req; |
| 5569 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5570 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5571 | channel_auto_read(req); |
| 5572 | channel_abort(req); |
| 5573 | channel_auto_close(req); |
| 5574 | channel_erase(req); |
| 5575 | |
| 5576 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5577 | channel_auto_read(res); |
| 5578 | channel_auto_close(res); |
| 5579 | channel_shutr_now(res); |
| 5580 | |
| 5581 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5582 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5583 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5584 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5585 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5586 | /* No reply or invalid reply */ |
| 5587 | s->txn->status = 0; |
| 5588 | http_reply_and_close(s, 0, NULL); |
| 5589 | } |
| 5590 | else { |
| 5591 | /* Remove extra args to have the reply on top of the stack */ |
| 5592 | if (lua_gettop(L) > 2) |
| 5593 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5594 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5595 | if (!hlua_txn_forward_reply(L, s)) { |
| 5596 | if (!(s->flags & SF_ERR_MASK)) |
| 5597 | s->flags |= SF_ERR_PRXCOND; |
| 5598 | lua_pushinteger(L, ACT_RET_ERR); |
| 5599 | WILL_LJMP(hlua_done(L)); |
| 5600 | return 0; /* Never reached */ |
| 5601 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5602 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5603 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5604 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5605 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5606 | /* let's log the request time */ |
| 5607 | s->logs.tv_request = now; |
| 5608 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5609 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5610 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5611 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5612 | done: |
| 5613 | if (!(s->flags & SF_ERR_MASK)) |
| 5614 | s->flags |= SF_ERR_LOCAL; |
| 5615 | if (!(s->flags & SF_FINST_MASK)) |
| 5616 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5617 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5618 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5619 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5620 | return 0; |
| 5621 | } |
| 5622 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5623 | /* |
| 5624 | * |
| 5625 | * |
| 5626 | * Class REPLY |
| 5627 | * |
| 5628 | * |
| 5629 | */ |
| 5630 | |
| 5631 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5632 | * free slots, the function fails and returns 0; |
| 5633 | */ |
| 5634 | static int hlua_txn_reply_new(lua_State *L) |
| 5635 | { |
| 5636 | struct hlua_txn *htxn; |
| 5637 | const char *reason, *body = NULL; |
| 5638 | int ret, status; |
| 5639 | |
| 5640 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5641 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5642 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5643 | WILL_LJMP(lua_error(L)); |
| 5644 | } |
| 5645 | |
| 5646 | /* Default value */ |
| 5647 | status = 200; |
| 5648 | reason = http_get_reason(status); |
| 5649 | |
| 5650 | if (lua_istable(L, 2)) { |
| 5651 | /* load status and reason from the table argument at index 2 */ |
| 5652 | ret = lua_getfield(L, 2, "status"); |
| 5653 | if (ret == LUA_TNIL) |
| 5654 | goto reason; |
| 5655 | else if (ret != LUA_TNUMBER) { |
| 5656 | /* invalid status: ignore the reason */ |
| 5657 | goto body; |
| 5658 | } |
| 5659 | status = lua_tointeger(L, -1); |
| 5660 | |
| 5661 | reason: |
| 5662 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5663 | ret = lua_getfield(L, 2, "reason"); |
| 5664 | if (ret == LUA_TSTRING) |
| 5665 | reason = lua_tostring(L, -1); |
| 5666 | |
| 5667 | body: |
| 5668 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5669 | ret = lua_getfield(L, 2, "body"); |
| 5670 | if (ret == LUA_TSTRING) |
| 5671 | body = lua_tostring(L, -1); |
| 5672 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5673 | } |
| 5674 | |
| 5675 | /* Create the Reply table */ |
| 5676 | lua_newtable(L); |
| 5677 | |
| 5678 | /* Add status element */ |
| 5679 | lua_pushstring(L, "status"); |
| 5680 | lua_pushinteger(L, status); |
| 5681 | lua_settable(L, -3); |
| 5682 | |
| 5683 | /* Add reason element */ |
| 5684 | reason = http_get_reason(status); |
| 5685 | lua_pushstring(L, "reason"); |
| 5686 | lua_pushstring(L, reason); |
| 5687 | lua_settable(L, -3); |
| 5688 | |
| 5689 | /* Add body element, nil if undefined */ |
| 5690 | lua_pushstring(L, "body"); |
| 5691 | if (body) |
| 5692 | lua_pushstring(L, body); |
| 5693 | else |
| 5694 | lua_pushnil(L); |
| 5695 | lua_settable(L, -3); |
| 5696 | |
| 5697 | /* Add headers element */ |
| 5698 | lua_pushstring(L, "headers"); |
| 5699 | lua_newtable(L); |
| 5700 | |
| 5701 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5702 | if (lua_istable(L, 2)) { |
| 5703 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5704 | ret = lua_getfield(L, 2, "headers"); |
| 5705 | if (ret == LUA_TTABLE) { |
| 5706 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5707 | lua_pushnil(L); |
| 5708 | while (lua_next(L, -2) != 0) { |
| 5709 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5710 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5711 | /* invalid value type, skip it */ |
| 5712 | lua_pop(L, 1); |
| 5713 | continue; |
| 5714 | } |
| 5715 | |
| 5716 | |
| 5717 | /* Duplicate the key and swap it with the value. */ |
| 5718 | lua_pushvalue(L, -2); |
| 5719 | lua_insert(L, -2); |
| 5720 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5721 | |
| 5722 | lua_newtable(L); |
| 5723 | lua_insert(L, -2); |
| 5724 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5725 | |
| 5726 | if (lua_isstring(L, -1)) { |
| 5727 | /* push the value in the inner table */ |
| 5728 | lua_rawseti(L, -2, 1); |
| 5729 | } |
| 5730 | else { /* table */ |
| 5731 | lua_pushnil(L); |
| 5732 | while (lua_next(L, -2) != 0) { |
| 5733 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5734 | if (!lua_isstring(L, -1)) { |
| 5735 | /* invalid value type, skip it*/ |
| 5736 | lua_pop(L, 1); |
| 5737 | continue; |
| 5738 | } |
| 5739 | /* push the value in the inner table */ |
| 5740 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5741 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5742 | } |
| 5743 | lua_pop(L, 1); |
| 5744 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5745 | } |
| 5746 | |
| 5747 | /* push (k,v) on the stack in the headers table: |
| 5748 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5749 | */ |
| 5750 | lua_settable(L, -5); |
| 5751 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5752 | } |
| 5753 | } |
| 5754 | lua_pop(L, 1); |
| 5755 | } |
| 5756 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5757 | lua_settable(L, -3); |
| 5758 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5759 | |
| 5760 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5761 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5762 | lua_setmetatable(L, -2); |
| 5763 | return 1; |
| 5764 | } |
| 5765 | |
| 5766 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5767 | * provided, the default one corresponding to the status code is used. |
| 5768 | */ |
| 5769 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5770 | { |
| 5771 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5772 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5773 | |
| 5774 | /* First argument (self) must be a table */ |
| 5775 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5776 | |
| 5777 | if (status < 100 || status > 599) { |
| 5778 | lua_pushboolean(L, 0); |
| 5779 | return 1; |
| 5780 | } |
| 5781 | if (!reason) |
| 5782 | reason = http_get_reason(status); |
| 5783 | |
| 5784 | lua_pushinteger(L, status); |
| 5785 | lua_setfield(L, 1, "status"); |
| 5786 | |
| 5787 | lua_pushstring(L, reason); |
| 5788 | lua_setfield(L, 1, "reason"); |
| 5789 | |
| 5790 | lua_pushboolean(L, 1); |
| 5791 | return 1; |
| 5792 | } |
| 5793 | |
| 5794 | /* Add a header into the reply object. Each header name is associated to an |
| 5795 | * array of values in the "headers" table. If the header name is not found, a |
| 5796 | * new entry is created. |
| 5797 | */ |
| 5798 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5799 | { |
| 5800 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5801 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5802 | int ret; |
| 5803 | |
| 5804 | /* First argument (self) must be a table */ |
| 5805 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5806 | |
| 5807 | /* Push in the stack the "headers" entry. */ |
| 5808 | ret = lua_getfield(L, 1, "headers"); |
| 5809 | if (ret != LUA_TTABLE) { |
| 5810 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5811 | WILL_LJMP(lua_error(L)); |
| 5812 | } |
| 5813 | |
| 5814 | /* check if the header is already registered. If not, register it. */ |
| 5815 | ret = lua_getfield(L, -1, name); |
| 5816 | if (ret == LUA_TNIL) { |
| 5817 | /* Entry not found. */ |
| 5818 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5819 | |
| 5820 | /* Insert the new header name in the array in the top of the stack. |
| 5821 | * It left the new array in the top of the stack. |
| 5822 | */ |
| 5823 | lua_newtable(L); |
| 5824 | lua_pushstring(L, name); |
| 5825 | lua_pushvalue(L, -2); |
| 5826 | lua_settable(L, -4); |
| 5827 | } |
| 5828 | else if (ret != LUA_TTABLE) { |
| 5829 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5830 | WILL_LJMP(lua_error(L)); |
| 5831 | } |
| 5832 | |
| 5833 | /* Now the top od thestack is an array of values. We push |
| 5834 | * the header value as new entry. |
| 5835 | */ |
| 5836 | lua_pushstring(L, value); |
| 5837 | ret = lua_rawlen(L, -2); |
| 5838 | lua_rawseti(L, -2, ret + 1); |
| 5839 | |
| 5840 | lua_pushboolean(L, 1); |
| 5841 | return 1; |
| 5842 | } |
| 5843 | |
| 5844 | /* Remove all occurrences of a given header name. */ |
| 5845 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5846 | { |
| 5847 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5848 | int ret; |
| 5849 | |
| 5850 | /* First argument (self) must be a table */ |
| 5851 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5852 | |
| 5853 | /* Push in the stack the "headers" entry. */ |
| 5854 | ret = lua_getfield(L, 1, "headers"); |
| 5855 | if (ret != LUA_TTABLE) { |
| 5856 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 5857 | WILL_LJMP(lua_error(L)); |
| 5858 | } |
| 5859 | |
| 5860 | lua_pushstring(L, name); |
| 5861 | lua_pushnil(L); |
| 5862 | lua_settable(L, -3); |
| 5863 | |
| 5864 | lua_pushboolean(L, 1); |
| 5865 | return 1; |
| 5866 | } |
| 5867 | |
| 5868 | /* Set the reply's body. Overwrite any existing entry. */ |
| 5869 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 5870 | { |
| 5871 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5872 | |
| 5873 | /* First argument (self) must be a table */ |
| 5874 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5875 | |
| 5876 | lua_pushstring(L, payload); |
| 5877 | lua_setfield(L, 1, "body"); |
| 5878 | |
| 5879 | lua_pushboolean(L, 1); |
| 5880 | return 1; |
| 5881 | } |
| 5882 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5883 | __LJMP static int hlua_log(lua_State *L) |
| 5884 | { |
| 5885 | int level; |
| 5886 | const char *msg; |
| 5887 | |
| 5888 | MAY_LJMP(check_args(L, 2, "log")); |
| 5889 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5890 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5891 | |
| 5892 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5893 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5894 | |
| 5895 | hlua_sendlog(NULL, level, msg); |
| 5896 | return 0; |
| 5897 | } |
| 5898 | |
| 5899 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5900 | { |
| 5901 | const char *msg; |
| 5902 | |
| 5903 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5904 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5905 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5906 | return 0; |
| 5907 | } |
| 5908 | |
| 5909 | __LJMP static int hlua_log_info(lua_State *L) |
| 5910 | { |
| 5911 | const char *msg; |
| 5912 | |
| 5913 | MAY_LJMP(check_args(L, 1, "info")); |
| 5914 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5915 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5916 | return 0; |
| 5917 | } |
| 5918 | |
| 5919 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5920 | { |
| 5921 | const char *msg; |
| 5922 | |
| 5923 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5924 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5925 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5926 | return 0; |
| 5927 | } |
| 5928 | |
| 5929 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5930 | { |
| 5931 | const char *msg; |
| 5932 | |
| 5933 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5934 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5935 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5936 | return 0; |
| 5937 | } |
| 5938 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5939 | __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] | 5940 | { |
| 5941 | int wakeup_ms = lua_tointeger(L, -1); |
| 5942 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5943 | 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] | 5944 | return 0; |
| 5945 | } |
| 5946 | |
| 5947 | __LJMP static int hlua_sleep(lua_State *L) |
| 5948 | { |
| 5949 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5950 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5951 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5952 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5953 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5954 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5955 | wakeup_ms = tick_add(now_ms, delay); |
| 5956 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5957 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5958 | 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] | 5959 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5960 | } |
| 5961 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5962 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5963 | { |
| 5964 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5965 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5966 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5967 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5968 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5969 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5970 | wakeup_ms = tick_add(now_ms, delay); |
| 5971 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5972 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5973 | 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] | 5974 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5975 | } |
| 5976 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5977 | /* This functionis an LUA binding. it permits to give back |
| 5978 | * the hand at the HAProxy scheduler. It is used when the |
| 5979 | * LUA processing consumes a lot of time. |
| 5980 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5981 | __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] | 5982 | { |
| 5983 | return 0; |
| 5984 | } |
| 5985 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5986 | __LJMP static int hlua_yield(lua_State *L) |
| 5987 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5988 | 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] | 5989 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5990 | } |
| 5991 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5992 | /* This function change the nice of the currently executed |
| 5993 | * task. It is used set low or high priority at the current |
| 5994 | * task. |
| 5995 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5996 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5997 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5998 | struct hlua *hlua; |
| 5999 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6000 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6001 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6002 | hlua = hlua_gethlua(L); |
| 6003 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6004 | |
| 6005 | /* If he task is not set, I'm in a start mode. */ |
| 6006 | if (!hlua || !hlua->task) |
| 6007 | return 0; |
| 6008 | |
| 6009 | if (nice < -1024) |
| 6010 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6011 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6012 | nice = 1024; |
| 6013 | |
| 6014 | hlua->task->nice = nice; |
| 6015 | return 0; |
| 6016 | } |
| 6017 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6018 | /* 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] | 6019 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6020 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6021 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6022 | * |
| 6023 | * Task wrapper are longjmp safe because the only one Lua code |
| 6024 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6025 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6026 | 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] | 6027 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6028 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6029 | enum hlua_exec status; |
| 6030 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6031 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6032 | task_set_affinity(task, tid_bit); |
| 6033 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6034 | /* If it is the first call to the task, we must initialize the |
| 6035 | * execution timeouts. |
| 6036 | */ |
| 6037 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6038 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6039 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6040 | /* Execute the Lua code. */ |
| 6041 | status = hlua_ctx_resume(hlua, 1); |
| 6042 | |
| 6043 | switch (status) { |
| 6044 | /* finished or yield */ |
| 6045 | case HLUA_E_OK: |
| 6046 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6047 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6048 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6049 | break; |
| 6050 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6051 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6052 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6053 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6054 | break; |
| 6055 | |
| 6056 | /* finished with error. */ |
| 6057 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6058 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6059 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6060 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6061 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6062 | break; |
| 6063 | |
| 6064 | case HLUA_E_ERR: |
| 6065 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6066 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6067 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6068 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6069 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6070 | break; |
| 6071 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6072 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6073 | } |
| 6074 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6075 | /* This function is an LUA binding that register LUA function to be |
| 6076 | * executed after the HAProxy configuration parsing and before the |
| 6077 | * HAProxy scheduler starts. This function expect only one LUA |
| 6078 | * argument that is a function. This function returns nothing, but |
| 6079 | * throws if an error is encountered. |
| 6080 | */ |
| 6081 | __LJMP static int hlua_register_init(lua_State *L) |
| 6082 | { |
| 6083 | struct hlua_init_function *init; |
| 6084 | int ref; |
| 6085 | |
| 6086 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6087 | |
| 6088 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6089 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6090 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6091 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6092 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6093 | |
| 6094 | init->function_ref = ref; |
| 6095 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6096 | return 0; |
| 6097 | } |
| 6098 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6099 | /* This functio is an LUA binding. It permits to register a task |
| 6100 | * executed in parallel of the main HAroxy activity. The task is |
| 6101 | * created and it is set in the HAProxy scheduler. It can be called |
| 6102 | * from the "init" section, "post init" or during the runtime. |
| 6103 | * |
| 6104 | * Lua prototype: |
| 6105 | * |
| 6106 | * <none> core.register_task(<function>) |
| 6107 | */ |
| 6108 | static int hlua_register_task(lua_State *L) |
| 6109 | { |
| 6110 | struct hlua *hlua; |
| 6111 | struct task *task; |
| 6112 | int ref; |
| 6113 | |
| 6114 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6115 | |
| 6116 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6117 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6118 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6119 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6120 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6121 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6122 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6123 | if (!task) |
| 6124 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6125 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6126 | task->context = hlua; |
| 6127 | task->process = hlua_process_task; |
| 6128 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6129 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6130 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6131 | |
| 6132 | /* Restore the function in the stack. */ |
| 6133 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6134 | hlua->nargs = 0; |
| 6135 | |
| 6136 | /* Schedule task. */ |
| 6137 | task_schedule(task, now_ms); |
| 6138 | |
| 6139 | return 0; |
| 6140 | } |
| 6141 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6142 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6143 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6144 | * resume converters. |
| 6145 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6146 | 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] | 6147 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6148 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6149 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6150 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6151 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6152 | if (!stream) |
| 6153 | return 0; |
| 6154 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6155 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6156 | * Lua context can be not initialized. This behavior |
| 6157 | * permits to save performances because a systematic |
| 6158 | * Lua initialization cause 5% performances loss. |
| 6159 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6160 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6161 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6162 | if (!stream->hlua) { |
| 6163 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6164 | return 0; |
| 6165 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6166 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6167 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6168 | return 0; |
| 6169 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6170 | } |
| 6171 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6172 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6173 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6174 | |
| 6175 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6176 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6177 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6178 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6179 | else |
| 6180 | error = "critical error"; |
| 6181 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6182 | return 0; |
| 6183 | } |
| 6184 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6185 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6186 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6187 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6188 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6189 | return 0; |
| 6190 | } |
| 6191 | |
| 6192 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6193 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6194 | |
| 6195 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6196 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6197 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6198 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6199 | return 0; |
| 6200 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6201 | hlua_smp2lua(stream->hlua->T, smp); |
| 6202 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6203 | |
| 6204 | /* push keywords in the stack. */ |
| 6205 | if (arg_p) { |
| 6206 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6207 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6208 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6209 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6210 | return 0; |
| 6211 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6212 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6213 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6214 | } |
| 6215 | } |
| 6216 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6217 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6218 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6219 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6220 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6221 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6225 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6226 | /* finished. */ |
| 6227 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6228 | /* If the stack is empty, the function fails. */ |
| 6229 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6230 | return 0; |
| 6231 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6232 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6233 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6234 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6235 | return 1; |
| 6236 | |
| 6237 | /* yield. */ |
| 6238 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6239 | 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] | 6240 | return 0; |
| 6241 | |
| 6242 | /* finished with error. */ |
| 6243 | case HLUA_E_ERRMSG: |
| 6244 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6245 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6246 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6247 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6248 | return 0; |
| 6249 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6250 | case HLUA_E_ETMOUT: |
| 6251 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6252 | return 0; |
| 6253 | |
| 6254 | case HLUA_E_NOMEM: |
| 6255 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6256 | return 0; |
| 6257 | |
| 6258 | case HLUA_E_YIELD: |
| 6259 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6260 | return 0; |
| 6261 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6262 | case HLUA_E_ERR: |
| 6263 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6264 | 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] | 6265 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6266 | |
| 6267 | default: |
| 6268 | return 0; |
| 6269 | } |
| 6270 | } |
| 6271 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6272 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6273 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6274 | * resume sample-fetches. This function will be called by the sample |
| 6275 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6276 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6277 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6278 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6279 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6280 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6281 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6282 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6283 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6284 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6285 | if (!stream) |
| 6286 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6287 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6288 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6289 | * Lua context can be not initialized. This behavior |
| 6290 | * permits to save performances because a systematic |
| 6291 | * Lua initialization cause 5% performances loss. |
| 6292 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6293 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6294 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6295 | if (!stream->hlua) { |
| 6296 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6297 | return 0; |
| 6298 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6299 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6300 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6301 | return 0; |
| 6302 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6303 | } |
| 6304 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6305 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6306 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6307 | |
| 6308 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6309 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6310 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6311 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6312 | else |
| 6313 | error = "critical error"; |
| 6314 | 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] | 6315 | return 0; |
| 6316 | } |
| 6317 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6318 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6319 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6320 | 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] | 6321 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6322 | return 0; |
| 6323 | } |
| 6324 | |
| 6325 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6326 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6327 | |
| 6328 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6329 | 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] | 6330 | 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] | 6331 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6332 | return 0; |
| 6333 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6334 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6335 | |
| 6336 | /* push keywords in the stack. */ |
| 6337 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6338 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6339 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6340 | 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] | 6341 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6342 | return 0; |
| 6343 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6344 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6345 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6346 | } |
| 6347 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6348 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6349 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6350 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6351 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6352 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6353 | } |
| 6354 | |
| 6355 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6356 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6357 | /* finished. */ |
| 6358 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6359 | /* If the stack is empty, the function fails. */ |
| 6360 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6361 | return 0; |
| 6362 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6363 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6364 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6365 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6366 | |
| 6367 | /* Set the end of execution flag. */ |
| 6368 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6369 | return 1; |
| 6370 | |
| 6371 | /* yield. */ |
| 6372 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6373 | 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] | 6374 | return 0; |
| 6375 | |
| 6376 | /* finished with error. */ |
| 6377 | case HLUA_E_ERRMSG: |
| 6378 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6379 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6380 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6381 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6382 | return 0; |
| 6383 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6384 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6385 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6386 | return 0; |
| 6387 | |
| 6388 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6389 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6390 | return 0; |
| 6391 | |
| 6392 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6393 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6394 | return 0; |
| 6395 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6396 | case HLUA_E_ERR: |
| 6397 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6398 | 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] | 6399 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6400 | |
| 6401 | default: |
| 6402 | return 0; |
| 6403 | } |
| 6404 | } |
| 6405 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6406 | /* This function is an LUA binding used for registering |
| 6407 | * "sample-conv" functions. It expects a converter name used |
| 6408 | * in the haproxy configuration file, and an LUA function. |
| 6409 | */ |
| 6410 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6411 | { |
| 6412 | struct sample_conv_kw_list *sck; |
| 6413 | const char *name; |
| 6414 | int ref; |
| 6415 | int len; |
| 6416 | struct hlua_function *fcn; |
| 6417 | |
| 6418 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6419 | |
| 6420 | /* First argument : converter name. */ |
| 6421 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6422 | |
| 6423 | /* Second argument : lua function. */ |
| 6424 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6425 | |
| 6426 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6427 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6428 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6429 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6430 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6431 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6432 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6433 | |
| 6434 | /* Fill fcn. */ |
| 6435 | fcn->name = strdup(name); |
| 6436 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6437 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6438 | fcn->function_ref = ref; |
| 6439 | |
| 6440 | /* List head */ |
| 6441 | sck->list.n = sck->list.p = NULL; |
| 6442 | |
| 6443 | /* converter keyword. */ |
| 6444 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6445 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6446 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6447 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6448 | |
| 6449 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6450 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6451 | 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] | 6452 | sck->kw[0].val_args = NULL; |
| 6453 | sck->kw[0].in_type = SMP_T_STR; |
| 6454 | sck->kw[0].out_type = SMP_T_STR; |
| 6455 | sck->kw[0].private = fcn; |
| 6456 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6457 | /* Register this new converter */ |
| 6458 | sample_register_convs(sck); |
| 6459 | |
| 6460 | return 0; |
| 6461 | } |
| 6462 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6463 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6464 | * "sample-fetch" functions. It expects a converter name used |
| 6465 | * in the haproxy configuration file, and an LUA function. |
| 6466 | */ |
| 6467 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6468 | { |
| 6469 | const char *name; |
| 6470 | int ref; |
| 6471 | int len; |
| 6472 | struct sample_fetch_kw_list *sfk; |
| 6473 | struct hlua_function *fcn; |
| 6474 | |
| 6475 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6476 | |
| 6477 | /* First argument : sample-fetch name. */ |
| 6478 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6479 | |
| 6480 | /* Second argument : lua function. */ |
| 6481 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6482 | |
| 6483 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6484 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6485 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6486 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6487 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6488 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6489 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6490 | |
| 6491 | /* Fill fcn. */ |
| 6492 | fcn->name = strdup(name); |
| 6493 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6494 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6495 | fcn->function_ref = ref; |
| 6496 | |
| 6497 | /* List head */ |
| 6498 | sfk->list.n = sfk->list.p = NULL; |
| 6499 | |
| 6500 | /* sample-fetch keyword. */ |
| 6501 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6502 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6503 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6504 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6505 | |
| 6506 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6507 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6508 | 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] | 6509 | sfk->kw[0].val_args = NULL; |
| 6510 | sfk->kw[0].out_type = SMP_T_STR; |
| 6511 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6512 | sfk->kw[0].val = 0; |
| 6513 | sfk->kw[0].private = fcn; |
| 6514 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6515 | /* Register this new fetch. */ |
| 6516 | sample_register_fetches(sfk); |
| 6517 | |
| 6518 | return 0; |
| 6519 | } |
| 6520 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6521 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6522 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6523 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6524 | { |
| 6525 | struct hlua *hlua = hlua_gethlua(L); |
| 6526 | unsigned int delay; |
| 6527 | unsigned int wakeup_ms; |
| 6528 | |
| 6529 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6530 | |
| 6531 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6532 | wakeup_ms = tick_add(now_ms, delay); |
| 6533 | hlua->wake_time = wakeup_ms; |
| 6534 | return 0; |
| 6535 | } |
| 6536 | |
| 6537 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6538 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6539 | * wrapper during the initialisation period. This function may return any |
| 6540 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6541 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6542 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6543 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6544 | 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] | 6545 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6546 | { |
| 6547 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6548 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6549 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6550 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6551 | |
| 6552 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6553 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6554 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6555 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6556 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6557 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6558 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6559 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6560 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6561 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6562 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6563 | * Lua context can be not initialized. This behavior |
| 6564 | * permits to save performances because a systematic |
| 6565 | * Lua initialization cause 5% performances loss. |
| 6566 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6567 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6568 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6569 | if (!s->hlua) { |
| 6570 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6571 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6572 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6573 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6574 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6575 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6576 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6577 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6578 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6579 | } |
| 6580 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6581 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6582 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6583 | |
| 6584 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6585 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6586 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6587 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6588 | else |
| 6589 | error = "critical error"; |
| 6590 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6591 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6592 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6593 | } |
| 6594 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6595 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6596 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6597 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6598 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6599 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6600 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6604 | 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] | 6605 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6606 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6607 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6608 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6609 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6611 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6612 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6613 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6614 | |
| 6615 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6616 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6617 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6618 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6619 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6620 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6621 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6622 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6623 | lua_pushstring(s->hlua->T, *arg); |
| 6624 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6625 | } |
| 6626 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6627 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6628 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6629 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6630 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6631 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6632 | } |
| 6633 | |
Christopher Faulet | 23308eb | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6634 | /* Always reset the analyse expiration timeout for the corresponding |
| 6635 | * channel in case the lua script yield, to be sure to not keep an |
| 6636 | * expired timeout. |
| 6637 | */ |
| 6638 | if (dir == SMP_OPT_DIR_REQ) |
| 6639 | s->req.analyse_exp = TICK_ETERNITY; |
| 6640 | else |
| 6641 | s->res.analyse_exp = TICK_ETERNITY; |
| 6642 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6643 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6644 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6645 | /* finished. */ |
| 6646 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6647 | /* Catch the return value */ |
| 6648 | if (lua_gettop(s->hlua->T) > 0) |
| 6649 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6650 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6651 | /* Set timeout in the required channel. */ |
| 6652 | if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) { |
| 6653 | if (dir == SMP_OPT_DIR_REQ) |
| 6654 | s->req.analyse_exp = s->hlua->wake_time; |
| 6655 | else |
| 6656 | s->res.analyse_exp = s->hlua->wake_time; |
| 6657 | } |
| 6658 | goto end; |
| 6659 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6660 | /* yield. */ |
| 6661 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6662 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6663 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | 81921b1 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6664 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6665 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6666 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6667 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6668 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6669 | /* Some actions can be wake up when a "write" event |
| 6670 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6671 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6672 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6673 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6674 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6675 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6676 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6677 | act_ret = ACT_RET_YIELD; |
| 6678 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6679 | |
| 6680 | /* finished with error. */ |
| 6681 | case HLUA_E_ERRMSG: |
| 6682 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6683 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6684 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6685 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6686 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6687 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6688 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6689 | 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] | 6690 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6691 | |
| 6692 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6693 | 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] | 6694 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6695 | |
| 6696 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6697 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6698 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6699 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6700 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6701 | case HLUA_E_ERR: |
| 6702 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6703 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6704 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6705 | |
| 6706 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6707 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6708 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6709 | |
| 6710 | end: |
| 6711 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6712 | } |
| 6713 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6714 | 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] | 6715 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6716 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6717 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6718 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6719 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6720 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6721 | } |
| 6722 | |
| 6723 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6724 | { |
| 6725 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6726 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6727 | struct task *task; |
| 6728 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6729 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6730 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6731 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6732 | if (!hlua) { |
| 6733 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6734 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6735 | return 0; |
| 6736 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6737 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6738 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6739 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6740 | |
| 6741 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6742 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6743 | if (!task) { |
| 6744 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6745 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6746 | return 0; |
| 6747 | } |
| 6748 | task->nice = 0; |
| 6749 | task->context = ctx; |
| 6750 | task->process = hlua_applet_wakeup; |
| 6751 | ctx->ctx.hlua_apptcp.task = task; |
| 6752 | |
| 6753 | /* In the execution wrappers linked with a stream, the |
| 6754 | * Lua context can be not initialized. This behavior |
| 6755 | * permits to save performances because a systematic |
| 6756 | * Lua initialization cause 5% performances loss. |
| 6757 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6758 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6759 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6760 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6761 | return 0; |
| 6762 | } |
| 6763 | |
| 6764 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6765 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6766 | |
| 6767 | /* The following Lua calls can fail. */ |
| 6768 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6769 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6770 | error = lua_tostring(hlua->T, -1); |
| 6771 | else |
| 6772 | error = "critical error"; |
| 6773 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6774 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6775 | return 0; |
| 6776 | } |
| 6777 | |
| 6778 | /* Check stack available size. */ |
| 6779 | if (!lua_checkstack(hlua->T, 1)) { |
| 6780 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6781 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6782 | RESET_SAFE_LJMP(hlua->T); |
| 6783 | return 0; |
| 6784 | } |
| 6785 | |
| 6786 | /* Restore the function in the stack. */ |
| 6787 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6788 | |
| 6789 | /* Create and and push object stream in the stack. */ |
| 6790 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6791 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6792 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6793 | RESET_SAFE_LJMP(hlua->T); |
| 6794 | return 0; |
| 6795 | } |
| 6796 | hlua->nargs = 1; |
| 6797 | |
| 6798 | /* push keywords in the stack. */ |
| 6799 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6800 | if (!lua_checkstack(hlua->T, 1)) { |
| 6801 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6802 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6803 | RESET_SAFE_LJMP(hlua->T); |
| 6804 | return 0; |
| 6805 | } |
| 6806 | lua_pushstring(hlua->T, *arg); |
| 6807 | hlua->nargs++; |
| 6808 | } |
| 6809 | |
| 6810 | RESET_SAFE_LJMP(hlua->T); |
| 6811 | |
| 6812 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6813 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6814 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6815 | |
| 6816 | return 1; |
| 6817 | } |
| 6818 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6819 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6820 | { |
| 6821 | struct stream_interface *si = ctx->owner; |
| 6822 | struct stream *strm = si_strm(si); |
| 6823 | struct channel *res = si_ic(si); |
| 6824 | struct act_rule *rule = ctx->rule; |
| 6825 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6826 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6827 | |
| 6828 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6829 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6830 | /* eat the whole request */ |
| 6831 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6832 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6833 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6834 | |
| 6835 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6836 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6837 | return; |
| 6838 | |
| 6839 | /* Execute the function. */ |
| 6840 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6841 | /* finished. */ |
| 6842 | case HLUA_E_OK: |
| 6843 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6844 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6845 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6846 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6847 | res->flags |= CF_READ_NULL; |
| 6848 | si_shutr(si); |
| 6849 | return; |
| 6850 | |
| 6851 | /* yield. */ |
| 6852 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6853 | if (hlua->wake_time != TICK_ETERNITY) |
| 6854 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6855 | return; |
| 6856 | |
| 6857 | /* finished with error. */ |
| 6858 | case HLUA_E_ERRMSG: |
| 6859 | /* Display log. */ |
| 6860 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6861 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6862 | lua_pop(hlua->T, 1); |
| 6863 | goto error; |
| 6864 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6865 | case HLUA_E_ETMOUT: |
| 6866 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6867 | rule->arg.hlua_rule->fcn.name); |
| 6868 | goto error; |
| 6869 | |
| 6870 | case HLUA_E_NOMEM: |
| 6871 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6872 | rule->arg.hlua_rule->fcn.name); |
| 6873 | goto error; |
| 6874 | |
| 6875 | case HLUA_E_YIELD: /* unexpected */ |
| 6876 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6877 | rule->arg.hlua_rule->fcn.name); |
| 6878 | goto error; |
| 6879 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6880 | case HLUA_E_ERR: |
| 6881 | /* Display log. */ |
| 6882 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6883 | rule->arg.hlua_rule->fcn.name); |
| 6884 | goto error; |
| 6885 | |
| 6886 | default: |
| 6887 | goto error; |
| 6888 | } |
| 6889 | |
| 6890 | error: |
| 6891 | |
| 6892 | /* For all other cases, just close the stream. */ |
| 6893 | si_shutw(si); |
| 6894 | si_shutr(si); |
| 6895 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6896 | } |
| 6897 | |
| 6898 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6899 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6900 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6901 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6902 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6903 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6904 | } |
| 6905 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6906 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6907 | * an errors occurs and -1 if more data are required for initializing |
| 6908 | * the applet. |
| 6909 | */ |
| 6910 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6911 | { |
| 6912 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6913 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6914 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6915 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6916 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6917 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6918 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6919 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6920 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6921 | if (!hlua) { |
| 6922 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6923 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6924 | return 0; |
| 6925 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6926 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6927 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6928 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6929 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6930 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6931 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6932 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6933 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6934 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6935 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6936 | if (!task) { |
| 6937 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6938 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6939 | return 0; |
| 6940 | } |
| 6941 | task->nice = 0; |
| 6942 | task->context = ctx; |
| 6943 | task->process = hlua_applet_wakeup; |
| 6944 | ctx->ctx.hlua_apphttp.task = task; |
| 6945 | |
| 6946 | /* In the execution wrappers linked with a stream, the |
| 6947 | * Lua context can be not initialized. This behavior |
| 6948 | * permits to save performances because a systematic |
| 6949 | * Lua initialization cause 5% performances loss. |
| 6950 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6951 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6952 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6953 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6954 | return 0; |
| 6955 | } |
| 6956 | |
| 6957 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6958 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6959 | |
| 6960 | /* The following Lua calls can fail. */ |
| 6961 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6962 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6963 | error = lua_tostring(hlua->T, -1); |
| 6964 | else |
| 6965 | error = "critical error"; |
| 6966 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6967 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6968 | return 0; |
| 6969 | } |
| 6970 | |
| 6971 | /* Check stack available size. */ |
| 6972 | if (!lua_checkstack(hlua->T, 1)) { |
| 6973 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6974 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6975 | RESET_SAFE_LJMP(hlua->T); |
| 6976 | return 0; |
| 6977 | } |
| 6978 | |
| 6979 | /* Restore the function in the stack. */ |
| 6980 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6981 | |
| 6982 | /* Create and and push object stream in the stack. */ |
| 6983 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6984 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6985 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6986 | RESET_SAFE_LJMP(hlua->T); |
| 6987 | return 0; |
| 6988 | } |
| 6989 | hlua->nargs = 1; |
| 6990 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6991 | /* push keywords in the stack. */ |
| 6992 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6993 | if (!lua_checkstack(hlua->T, 1)) { |
| 6994 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6995 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6996 | RESET_SAFE_LJMP(hlua->T); |
| 6997 | return 0; |
| 6998 | } |
| 6999 | lua_pushstring(hlua->T, *arg); |
| 7000 | hlua->nargs++; |
| 7001 | } |
| 7002 | |
| 7003 | RESET_SAFE_LJMP(hlua->T); |
| 7004 | |
| 7005 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7006 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7007 | |
| 7008 | return 1; |
| 7009 | } |
| 7010 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7011 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7012 | { |
| 7013 | struct stream_interface *si = ctx->owner; |
| 7014 | struct stream *strm = si_strm(si); |
| 7015 | struct channel *req = si_oc(si); |
| 7016 | struct channel *res = si_ic(si); |
| 7017 | struct act_rule *rule = ctx->rule; |
| 7018 | struct proxy *px = strm->be; |
| 7019 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7020 | struct htx *req_htx, *res_htx; |
| 7021 | |
| 7022 | res_htx = htx_from_buf(&res->buf); |
| 7023 | |
| 7024 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7025 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7026 | goto out; |
| 7027 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7028 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7029 | if (!b_size(&res->buf)) { |
| 7030 | si_rx_room_blk(si); |
| 7031 | goto out; |
| 7032 | } |
| 7033 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7034 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7035 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7036 | |
| 7037 | /* Set the currently running flag. */ |
| 7038 | if (!HLUA_IS_RUNNING(hlua) && |
| 7039 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7040 | struct htx_blk *blk; |
| 7041 | size_t count = co_data(req); |
| 7042 | |
| 7043 | if (!count) { |
| 7044 | si_cant_get(si); |
| 7045 | goto out; |
| 7046 | } |
| 7047 | |
| 7048 | /* We need to flush the request header. This left the body for |
| 7049 | * the Lua. |
| 7050 | */ |
| 7051 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7052 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7053 | while (count && blk) { |
| 7054 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7055 | uint32_t sz = htx_get_blksz(blk); |
| 7056 | |
| 7057 | if (sz > count) { |
| 7058 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7059 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7060 | goto out; |
| 7061 | } |
| 7062 | |
| 7063 | count -= sz; |
| 7064 | co_set_data(req, co_data(req) - sz); |
| 7065 | blk = htx_remove_blk(req_htx, blk); |
| 7066 | |
| 7067 | if (type == HTX_BLK_EOH) |
| 7068 | break; |
| 7069 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7070 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7071 | } |
| 7072 | |
| 7073 | /* Executes The applet if it is not done. */ |
| 7074 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7075 | |
| 7076 | /* Execute the function. */ |
| 7077 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7078 | /* finished. */ |
| 7079 | case HLUA_E_OK: |
| 7080 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7081 | break; |
| 7082 | |
| 7083 | /* yield. */ |
| 7084 | case HLUA_E_AGAIN: |
| 7085 | if (hlua->wake_time != TICK_ETERNITY) |
| 7086 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7087 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7088 | |
| 7089 | /* finished with error. */ |
| 7090 | case HLUA_E_ERRMSG: |
| 7091 | /* Display log. */ |
| 7092 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7093 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7094 | lua_pop(hlua->T, 1); |
| 7095 | goto error; |
| 7096 | |
| 7097 | case HLUA_E_ETMOUT: |
| 7098 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7099 | rule->arg.hlua_rule->fcn.name); |
| 7100 | goto error; |
| 7101 | |
| 7102 | case HLUA_E_NOMEM: |
| 7103 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7104 | rule->arg.hlua_rule->fcn.name); |
| 7105 | goto error; |
| 7106 | |
| 7107 | case HLUA_E_YIELD: /* unexpected */ |
| 7108 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7109 | rule->arg.hlua_rule->fcn.name); |
| 7110 | goto error; |
| 7111 | |
| 7112 | case HLUA_E_ERR: |
| 7113 | /* Display log. */ |
| 7114 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7115 | rule->arg.hlua_rule->fcn.name); |
| 7116 | goto error; |
| 7117 | |
| 7118 | default: |
| 7119 | goto error; |
| 7120 | } |
| 7121 | } |
| 7122 | |
| 7123 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7124 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7125 | goto done; |
| 7126 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7127 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7128 | goto error; |
| 7129 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7130 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7131 | 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] | 7132 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7133 | si_rx_room_blk(si); |
| 7134 | goto out; |
| 7135 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7136 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7137 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7138 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | done: |
| 7142 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7143 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7144 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7145 | si_shutr(si); |
| 7146 | } |
| 7147 | |
| 7148 | /* eat the whole request */ |
| 7149 | if (co_data(req)) { |
| 7150 | req_htx = htx_from_buf(&req->buf); |
| 7151 | co_htx_skip(req, req_htx, co_data(req)); |
| 7152 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7153 | } |
| 7154 | } |
| 7155 | |
| 7156 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7157 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7158 | return; |
| 7159 | |
| 7160 | error: |
| 7161 | |
| 7162 | /* If we are in HTTP mode, and we are not send any |
| 7163 | * data, return a 500 server error in best effort: |
| 7164 | * if there is no room available in the buffer, |
| 7165 | * just close the connection. |
| 7166 | */ |
| 7167 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7168 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7169 | |
| 7170 | channel_erase(res); |
| 7171 | res->buf.data = b_data(err); |
| 7172 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7173 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7174 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7175 | } |
| 7176 | if (!(strm->flags & SF_ERR_MASK)) |
| 7177 | strm->flags |= SF_ERR_RESOURCE; |
| 7178 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7179 | goto done; |
| 7180 | } |
| 7181 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7182 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7183 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7184 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7185 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7186 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7187 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7188 | } |
| 7189 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7190 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7191 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7192 | * |
| 7193 | * This function can fail with an abort() due to an Lua critical error. |
| 7194 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7195 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7196 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7197 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7198 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7199 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7200 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7201 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7202 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7203 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7204 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7205 | if (!rule->arg.hlua_rule) { |
| 7206 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7207 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7208 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7209 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7210 | /* Memory for arguments. */ |
| 7211 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7212 | if (!rule->arg.hlua_rule->args) { |
| 7213 | memprintf(err, "out of memory error"); |
| 7214 | return ACT_RET_PRS_ERR; |
| 7215 | } |
| 7216 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7217 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7218 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7219 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7220 | /* Expect some arguments */ |
| 7221 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7222 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7223 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7224 | return ACT_RET_PRS_ERR; |
| 7225 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7226 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7227 | if (!rule->arg.hlua_rule->args[i]) { |
| 7228 | memprintf(err, "out of memory error"); |
| 7229 | return ACT_RET_PRS_ERR; |
| 7230 | } |
| 7231 | (*cur_arg)++; |
| 7232 | } |
| 7233 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7234 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7235 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7236 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7237 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7238 | } |
| 7239 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7240 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7241 | struct act_rule *rule, char **err) |
| 7242 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7243 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7244 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7245 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7246 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7247 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7248 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7249 | * the call of this analyzer. |
| 7250 | */ |
| 7251 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7252 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7253 | return ACT_RET_PRS_ERR; |
| 7254 | } |
| 7255 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7256 | /* Memory for the rule. */ |
| 7257 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7258 | if (!rule->arg.hlua_rule) { |
| 7259 | memprintf(err, "out of memory error"); |
| 7260 | return ACT_RET_PRS_ERR; |
| 7261 | } |
| 7262 | |
| 7263 | /* Reference the Lua function and store the reference. */ |
| 7264 | rule->arg.hlua_rule->fcn = *fcn; |
| 7265 | |
| 7266 | /* TODO: later accept arguments. */ |
| 7267 | rule->arg.hlua_rule->args = NULL; |
| 7268 | |
| 7269 | /* Add applet pointer in the rule. */ |
| 7270 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7271 | rule->applet.name = fcn->name; |
| 7272 | rule->applet.init = hlua_applet_http_init; |
| 7273 | rule->applet.fct = hlua_applet_http_fct; |
| 7274 | rule->applet.release = hlua_applet_http_release; |
| 7275 | rule->applet.timeout = hlua_timeout_applet; |
| 7276 | |
| 7277 | return ACT_RET_PRS_OK; |
| 7278 | } |
| 7279 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7280 | /* This function is an LUA binding used for registering |
| 7281 | * "sample-conv" functions. It expects a converter name used |
| 7282 | * in the haproxy configuration file, and an LUA function. |
| 7283 | */ |
| 7284 | __LJMP static int hlua_register_action(lua_State *L) |
| 7285 | { |
| 7286 | struct action_kw_list *akl; |
| 7287 | const char *name; |
| 7288 | int ref; |
| 7289 | int len; |
| 7290 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7291 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7292 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7293 | /* Initialise the number of expected arguments at 0. */ |
| 7294 | nargs = 0; |
| 7295 | |
| 7296 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7297 | 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] | 7298 | |
| 7299 | /* First argument : converter name. */ |
| 7300 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7301 | |
| 7302 | /* Second argument : environment. */ |
| 7303 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7304 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7305 | |
| 7306 | /* Third argument : lua function. */ |
| 7307 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7308 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7309 | /* 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] | 7310 | if (lua_gettop(L) >= 4) |
| 7311 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7312 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7313 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7314 | lua_pushnil(L); |
| 7315 | while (lua_next(L, 2) != 0) { |
| 7316 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7317 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7318 | |
| 7319 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7320 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7321 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7322 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7323 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7324 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7325 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7326 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7327 | |
| 7328 | /* Fill fcn. */ |
| 7329 | fcn->name = strdup(name); |
| 7330 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7331 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7332 | fcn->function_ref = ref; |
| 7333 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7334 | /* Set the expected number od arguments. */ |
| 7335 | fcn->nargs = nargs; |
| 7336 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7337 | /* List head */ |
| 7338 | akl->list.n = akl->list.p = NULL; |
| 7339 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7340 | /* action keyword. */ |
| 7341 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7342 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7343 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7344 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7345 | |
| 7346 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7347 | |
| 7348 | akl->kw[0].match_pfx = 0; |
| 7349 | akl->kw[0].private = fcn; |
| 7350 | akl->kw[0].parse = action_register_lua; |
| 7351 | |
| 7352 | /* select the action registering point. */ |
| 7353 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7354 | tcp_req_cont_keywords_register(akl); |
| 7355 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7356 | tcp_res_cont_keywords_register(akl); |
| 7357 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7358 | http_req_keywords_register(akl); |
| 7359 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7360 | http_res_keywords_register(akl); |
| 7361 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7362 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7363 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7364 | "are expected.", lua_tostring(L, -1))); |
| 7365 | |
| 7366 | /* pop the environment string. */ |
| 7367 | lua_pop(L, 1); |
| 7368 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7369 | return ACT_RET_PRS_OK; |
| 7370 | } |
| 7371 | |
| 7372 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7373 | struct act_rule *rule, char **err) |
| 7374 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7375 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7376 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7377 | if (px->mode == PR_MODE_HTTP) { |
| 7378 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7379 | return ACT_RET_PRS_ERR; |
| 7380 | } |
| 7381 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7382 | /* Memory for the rule. */ |
| 7383 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7384 | if (!rule->arg.hlua_rule) { |
| 7385 | memprintf(err, "out of memory error"); |
| 7386 | return ACT_RET_PRS_ERR; |
| 7387 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7388 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7389 | /* Reference the Lua function and store the reference. */ |
| 7390 | rule->arg.hlua_rule->fcn = *fcn; |
| 7391 | |
| 7392 | /* TODO: later accept arguments. */ |
| 7393 | rule->arg.hlua_rule->args = NULL; |
| 7394 | |
| 7395 | /* Add applet pointer in the rule. */ |
| 7396 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7397 | rule->applet.name = fcn->name; |
| 7398 | rule->applet.init = hlua_applet_tcp_init; |
| 7399 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7400 | rule->applet.release = hlua_applet_tcp_release; |
| 7401 | rule->applet.timeout = hlua_timeout_applet; |
| 7402 | |
| 7403 | return 0; |
| 7404 | } |
| 7405 | |
| 7406 | /* This function is an LUA binding used for registering |
| 7407 | * "sample-conv" functions. It expects a converter name used |
| 7408 | * in the haproxy configuration file, and an LUA function. |
| 7409 | */ |
| 7410 | __LJMP static int hlua_register_service(lua_State *L) |
| 7411 | { |
| 7412 | struct action_kw_list *akl; |
| 7413 | const char *name; |
| 7414 | const char *env; |
| 7415 | int ref; |
| 7416 | int len; |
| 7417 | struct hlua_function *fcn; |
| 7418 | |
| 7419 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7420 | |
| 7421 | /* First argument : converter name. */ |
| 7422 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7423 | |
| 7424 | /* Second argument : environment. */ |
| 7425 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7426 | |
| 7427 | /* Third argument : lua function. */ |
| 7428 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7429 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7430 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7431 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7432 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7433 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7434 | fcn = calloc(1, sizeof(*fcn)); |
| 7435 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7436 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7437 | |
| 7438 | /* Fill fcn. */ |
| 7439 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7440 | fcn->name = calloc(1, len); |
| 7441 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7442 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7443 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7444 | fcn->function_ref = ref; |
| 7445 | |
| 7446 | /* List head */ |
| 7447 | akl->list.n = akl->list.p = NULL; |
| 7448 | |
| 7449 | /* converter keyword. */ |
| 7450 | len = strlen("lua.") + strlen(name) + 1; |
| 7451 | akl->kw[0].kw = calloc(1, len); |
| 7452 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7453 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7454 | |
| 7455 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7456 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7457 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7458 | if (strcmp(env, "tcp") == 0) |
| 7459 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7460 | else if (strcmp(env, "http") == 0) |
| 7461 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7462 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7463 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7464 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7465 | |
| 7466 | akl->kw[0].match_pfx = 0; |
| 7467 | akl->kw[0].private = fcn; |
| 7468 | |
| 7469 | /* End of array. */ |
| 7470 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7471 | |
| 7472 | /* Register this new converter */ |
| 7473 | service_keywords_register(akl); |
| 7474 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7475 | return 0; |
| 7476 | } |
| 7477 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7478 | /* This function initialises Lua cli handler. It copies the |
| 7479 | * arguments in the Lua stack and create channel IO objects. |
| 7480 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7481 | 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] | 7482 | { |
| 7483 | struct hlua *hlua; |
| 7484 | struct hlua_function *fcn; |
| 7485 | int i; |
| 7486 | const char *error; |
| 7487 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7488 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7489 | appctx->ctx.hlua_cli.fcn = private; |
| 7490 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7491 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7492 | if (!hlua) { |
| 7493 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7494 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7495 | } |
| 7496 | HLUA_INIT(hlua); |
| 7497 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7498 | |
| 7499 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7500 | * 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] | 7501 | * applet_http. It is absolutely compatible. |
| 7502 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7503 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7504 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7505 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7506 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7507 | } |
| 7508 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7509 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7510 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7511 | |
| 7512 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7513 | 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] | 7514 | 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] | 7515 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7516 | } |
| 7517 | |
| 7518 | /* The following Lua calls can fail. */ |
| 7519 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7520 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7521 | error = lua_tostring(hlua->T, -1); |
| 7522 | else |
| 7523 | error = "critical error"; |
| 7524 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7525 | goto error; |
| 7526 | } |
| 7527 | |
| 7528 | /* Check stack available size. */ |
| 7529 | if (!lua_checkstack(hlua->T, 2)) { |
| 7530 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7531 | goto error; |
| 7532 | } |
| 7533 | |
| 7534 | /* Restore the function in the stack. */ |
| 7535 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7536 | |
| 7537 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7538 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7539 | */ |
| 7540 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7541 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7542 | goto error; |
| 7543 | } |
| 7544 | hlua->nargs = 1; |
| 7545 | |
| 7546 | /* push keywords in the stack. */ |
| 7547 | for (i = 0; *args[i]; i++) { |
| 7548 | /* Check stack available size. */ |
| 7549 | if (!lua_checkstack(hlua->T, 1)) { |
| 7550 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7551 | goto error; |
| 7552 | } |
| 7553 | lua_pushstring(hlua->T, args[i]); |
| 7554 | hlua->nargs++; |
| 7555 | } |
| 7556 | |
| 7557 | /* We must initialize the execution timeouts. */ |
| 7558 | hlua->max_time = hlua_timeout_session; |
| 7559 | |
| 7560 | /* At this point the execution is safe. */ |
| 7561 | RESET_SAFE_LJMP(hlua->T); |
| 7562 | |
| 7563 | /* It's ok */ |
| 7564 | return 0; |
| 7565 | |
| 7566 | /* It's not ok. */ |
| 7567 | error: |
| 7568 | RESET_SAFE_LJMP(hlua->T); |
| 7569 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7570 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7571 | return 1; |
| 7572 | } |
| 7573 | |
| 7574 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7575 | { |
| 7576 | struct hlua *hlua; |
| 7577 | struct stream_interface *si; |
| 7578 | struct hlua_function *fcn; |
| 7579 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7580 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7581 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7582 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7583 | |
| 7584 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7585 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7586 | return 1; |
| 7587 | |
| 7588 | /* Execute the function. */ |
| 7589 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7590 | |
| 7591 | /* finished. */ |
| 7592 | case HLUA_E_OK: |
| 7593 | return 1; |
| 7594 | |
| 7595 | /* yield. */ |
| 7596 | case HLUA_E_AGAIN: |
| 7597 | /* We want write. */ |
| 7598 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7599 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7600 | /* Set the timeout. */ |
| 7601 | if (hlua->wake_time != TICK_ETERNITY) |
| 7602 | task_schedule(hlua->task, hlua->wake_time); |
| 7603 | return 0; |
| 7604 | |
| 7605 | /* finished with error. */ |
| 7606 | case HLUA_E_ERRMSG: |
| 7607 | /* Display log. */ |
| 7608 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7609 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7610 | lua_pop(hlua->T, 1); |
| 7611 | return 1; |
| 7612 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7613 | case HLUA_E_ETMOUT: |
| 7614 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7615 | fcn->name); |
| 7616 | return 1; |
| 7617 | |
| 7618 | case HLUA_E_NOMEM: |
| 7619 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7620 | fcn->name); |
| 7621 | return 1; |
| 7622 | |
| 7623 | case HLUA_E_YIELD: /* unexpected */ |
| 7624 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7625 | fcn->name); |
| 7626 | return 1; |
| 7627 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7628 | case HLUA_E_ERR: |
| 7629 | /* Display log. */ |
| 7630 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7631 | fcn->name); |
| 7632 | return 1; |
| 7633 | |
| 7634 | default: |
| 7635 | return 1; |
| 7636 | } |
| 7637 | |
| 7638 | return 1; |
| 7639 | } |
| 7640 | |
| 7641 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7642 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7643 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7644 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7645 | } |
| 7646 | |
| 7647 | /* This function is an LUA binding used for registering |
| 7648 | * new keywords in the cli. It expects a list of keywords |
| 7649 | * which are the "path". It is limited to 5 keywords. A |
| 7650 | * description of the command, a function to be executed |
| 7651 | * for the parsing and a function for io handlers. |
| 7652 | */ |
| 7653 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7654 | { |
| 7655 | struct cli_kw_list *cli_kws; |
| 7656 | const char *message; |
| 7657 | int ref_io; |
| 7658 | int len; |
| 7659 | struct hlua_function *fcn; |
| 7660 | int index; |
| 7661 | int i; |
| 7662 | |
| 7663 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7664 | |
| 7665 | /* First argument : an array of maximum 5 keywords. */ |
| 7666 | if (!lua_istable(L, 1)) |
| 7667 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7668 | |
| 7669 | /* Second argument : string with contextual message. */ |
| 7670 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7671 | |
| 7672 | /* Third and fourth argument : lua function. */ |
| 7673 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7674 | |
| 7675 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7676 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7677 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7678 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7679 | fcn = calloc(1, sizeof(*fcn)); |
| 7680 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7681 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7682 | |
| 7683 | /* Fill path. */ |
| 7684 | index = 0; |
| 7685 | lua_pushnil(L); |
| 7686 | while(lua_next(L, 1) != 0) { |
| 7687 | if (index >= 5) |
| 7688 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7689 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7690 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7691 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7692 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7693 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7694 | index++; |
| 7695 | lua_pop(L, 1); |
| 7696 | } |
| 7697 | |
| 7698 | /* Copy help message. */ |
| 7699 | cli_kws->kw[0].usage = strdup(message); |
| 7700 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7701 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7702 | |
| 7703 | /* Fill fcn io handler. */ |
| 7704 | len = strlen("<lua.cli>") + 1; |
| 7705 | for (i = 0; i < index; i++) |
| 7706 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7707 | fcn->name = calloc(1, len); |
| 7708 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7709 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7710 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7711 | for (i = 0; i < index; i++) { |
| 7712 | strncat((char *)fcn->name, ".", len); |
| 7713 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7714 | } |
| 7715 | strncat((char *)fcn->name, ">", len); |
| 7716 | fcn->function_ref = ref_io; |
| 7717 | |
| 7718 | /* Fill last entries. */ |
| 7719 | cli_kws->kw[0].private = fcn; |
| 7720 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7721 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7722 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7723 | |
| 7724 | /* Register this new converter */ |
| 7725 | cli_register_kw(cli_kws); |
| 7726 | |
| 7727 | return 0; |
| 7728 | } |
| 7729 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7730 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7731 | struct proxy *defpx, const char *file, int line, |
| 7732 | char **err, unsigned int *timeout) |
| 7733 | { |
| 7734 | const char *error; |
| 7735 | |
| 7736 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7737 | if (error == PARSE_TIME_OVER) { |
| 7738 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7739 | args[1], args[0]); |
| 7740 | return -1; |
| 7741 | } |
| 7742 | else if (error == PARSE_TIME_UNDER) { |
| 7743 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7744 | args[1], args[0]); |
| 7745 | return -1; |
| 7746 | } |
| 7747 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7748 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7749 | return -1; |
| 7750 | } |
| 7751 | return 0; |
| 7752 | } |
| 7753 | |
| 7754 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7755 | struct proxy *defpx, const char *file, int line, |
| 7756 | char **err) |
| 7757 | { |
| 7758 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7759 | file, line, err, &hlua_timeout_session); |
| 7760 | } |
| 7761 | |
| 7762 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7763 | struct proxy *defpx, const char *file, int line, |
| 7764 | char **err) |
| 7765 | { |
| 7766 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7767 | file, line, err, &hlua_timeout_task); |
| 7768 | } |
| 7769 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7770 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7771 | struct proxy *defpx, const char *file, int line, |
| 7772 | char **err) |
| 7773 | { |
| 7774 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7775 | file, line, err, &hlua_timeout_applet); |
| 7776 | } |
| 7777 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7778 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7779 | struct proxy *defpx, const char *file, int line, |
| 7780 | char **err) |
| 7781 | { |
| 7782 | char *error; |
| 7783 | |
| 7784 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7785 | if (*error != '\0') { |
| 7786 | memprintf(err, "%s: invalid number", args[0]); |
| 7787 | return -1; |
| 7788 | } |
| 7789 | return 0; |
| 7790 | } |
| 7791 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7792 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7793 | struct proxy *defpx, const char *file, int line, |
| 7794 | char **err) |
| 7795 | { |
| 7796 | char *error; |
| 7797 | |
| 7798 | if (*(args[1]) == 0) { |
| 7799 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7800 | return -1; |
| 7801 | } |
| 7802 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7803 | if (*error != '\0') { |
| 7804 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7805 | return -1; |
| 7806 | } |
| 7807 | return 0; |
| 7808 | } |
| 7809 | |
| 7810 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7811 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7812 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7813 | * the main lua entry point. |
| 7814 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7815 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7816 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7817 | * |
| 7818 | * In some error case, LUA set an error message in top of the stack. This function |
| 7819 | * 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] | 7820 | * |
| 7821 | * This function can fail with an abort() due to an Lua critical error. |
| 7822 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7823 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7824 | */ |
| 7825 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7826 | struct proxy *defpx, const char *file, int line, |
| 7827 | char **err) |
| 7828 | { |
| 7829 | int error; |
| 7830 | |
| 7831 | /* Just load and compile the file. */ |
| 7832 | error = luaL_loadfile(gL.T, args[1]); |
| 7833 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7834 | 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] | 7835 | lua_pop(gL.T, 1); |
| 7836 | return -1; |
| 7837 | } |
| 7838 | |
| 7839 | /* If no syntax error where detected, execute the code. */ |
| 7840 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7841 | switch (error) { |
| 7842 | case LUA_OK: |
| 7843 | break; |
| 7844 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7845 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7846 | lua_pop(gL.T, 1); |
| 7847 | return -1; |
| 7848 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7849 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7850 | return -1; |
| 7851 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7852 | 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] | 7853 | lua_pop(gL.T, 1); |
| 7854 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 7855 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7856 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7857 | 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] | 7858 | lua_pop(gL.T, 1); |
| 7859 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 7860 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7861 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7862 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7863 | lua_pop(gL.T, 1); |
| 7864 | return -1; |
| 7865 | } |
| 7866 | |
| 7867 | return 0; |
| 7868 | } |
| 7869 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7870 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7871 | * in the given <ctx>. |
| 7872 | */ |
| 7873 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7874 | { |
| 7875 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7876 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7877 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7878 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7879 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7880 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7881 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7882 | |
| 7883 | return 0; |
| 7884 | } |
| 7885 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7886 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7887 | struct proxy *defpx, const char *file, int line, |
| 7888 | char **err) |
| 7889 | { |
| 7890 | char *path; |
| 7891 | char *type = "path"; |
| 7892 | if (too_many_args(2, args, err, NULL)) { |
| 7893 | return -1; |
| 7894 | } |
| 7895 | |
| 7896 | if (!(*args[1])) { |
| 7897 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7898 | return -1; |
| 7899 | } |
| 7900 | path = args[1]; |
| 7901 | |
| 7902 | if (*args[2]) { |
| 7903 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7904 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7905 | return -1; |
| 7906 | } |
| 7907 | type = args[2]; |
| 7908 | } |
| 7909 | |
| 7910 | return hlua_prepend_path(gL, type, path); |
| 7911 | } |
| 7912 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7913 | /* configuration keywords declaration */ |
| 7914 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7915 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7916 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7917 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7918 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7919 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7920 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7921 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7922 | { 0, NULL, NULL }, |
| 7923 | }}; |
| 7924 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7925 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7926 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7927 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7928 | /* This function can fail with an abort() due to an Lua critical error. |
| 7929 | * We are in the initialisation process of HAProxy, this abort() is |
| 7930 | * tolerated. |
| 7931 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7932 | int hlua_post_init() |
| 7933 | { |
| 7934 | struct hlua_init_function *init; |
| 7935 | const char *msg; |
| 7936 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7937 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7938 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7939 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7940 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7941 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7942 | error = lua_tostring(gL.T, -1); |
| 7943 | else |
| 7944 | error = "critical error"; |
| 7945 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7946 | exit(1); |
| 7947 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7948 | |
| 7949 | #if USE_OPENSSL |
| 7950 | /* Initialize SSL server. */ |
| 7951 | if (socket_ssl.xprt->prepare_srv) { |
| 7952 | int saved_used_backed = global.ssl_used_backend; |
| 7953 | // don't affect maxconn automatic computation |
| 7954 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7955 | global.ssl_used_backend = saved_used_backed; |
| 7956 | } |
| 7957 | #endif |
| 7958 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7959 | hlua_fcn_post_init(gL.T); |
| 7960 | RESET_SAFE_LJMP(gL.T); |
| 7961 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7962 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7963 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7964 | ret = hlua_ctx_resume(&gL, 0); |
| 7965 | switch (ret) { |
| 7966 | case HLUA_E_OK: |
| 7967 | lua_pop(gL.T, -1); |
| 7968 | return 1; |
| 7969 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7970 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7971 | return 0; |
| 7972 | case HLUA_E_ERRMSG: |
| 7973 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7974 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7975 | return 0; |
| 7976 | case HLUA_E_ERR: |
| 7977 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7978 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7979 | return 0; |
| 7980 | } |
| 7981 | } |
| 7982 | return 1; |
| 7983 | } |
| 7984 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7985 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7986 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7987 | * is the previously allocated size or the kind of object in case of a new |
| 7988 | * allocation. <nsize> is the requested new size. |
| 7989 | */ |
| 7990 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7991 | { |
| 7992 | struct hlua_mem_allocator *zone = ud; |
| 7993 | |
| 7994 | if (nsize == 0) { |
| 7995 | /* it's a free */ |
| 7996 | if (ptr) |
| 7997 | zone->allocated -= osize; |
| 7998 | free(ptr); |
| 7999 | return NULL; |
| 8000 | } |
| 8001 | |
| 8002 | if (!ptr) { |
| 8003 | /* it's a new allocation */ |
| 8004 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8005 | return NULL; |
| 8006 | |
| 8007 | ptr = malloc(nsize); |
| 8008 | if (ptr) |
| 8009 | zone->allocated += nsize; |
| 8010 | return ptr; |
| 8011 | } |
| 8012 | |
| 8013 | /* it's a realloc */ |
| 8014 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8015 | return NULL; |
| 8016 | |
| 8017 | ptr = realloc(ptr, nsize); |
| 8018 | if (ptr) |
| 8019 | zone->allocated += nsize - osize; |
| 8020 | return ptr; |
| 8021 | } |
| 8022 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8023 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8024 | * We are in the initialisation process of HAProxy, this abort() is |
| 8025 | * tolerated. |
| 8026 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8027 | void hlua_init(void) |
| 8028 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8029 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8030 | int idx; |
| 8031 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8032 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8033 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8034 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8035 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8036 | struct srv_kw *kw; |
| 8037 | int tmp_error; |
| 8038 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8039 | char *args[] = { /* SSL client configuration. */ |
| 8040 | "ssl", |
| 8041 | "verify", |
| 8042 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8043 | NULL |
| 8044 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8045 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8046 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8047 | /* Init main lua stack. */ |
| 8048 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8049 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8050 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8051 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8052 | hlua_sethlua(&gL); |
| 8053 | gL.Tref = LUA_REFNIL; |
| 8054 | gL.task = NULL; |
| 8055 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8056 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8057 | * the Lua function can fail with an abort. We are in the initialisation |
| 8058 | * process of HAProxy, this abort() is tolerated. |
| 8059 | */ |
| 8060 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8061 | /* Initialise lua. */ |
| 8062 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8063 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8064 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8065 | #ifdef HLUA_PREPEND_PATH |
| 8066 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8067 | #endif |
| 8068 | #ifdef HLUA_PREPEND_CPATH |
| 8069 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8070 | #endif |
| 8071 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8072 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8073 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8074 | /* Set safe environment for the initialisation. */ |
| 8075 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8076 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8077 | error_msg = lua_tostring(gL.T, -1); |
| 8078 | else |
| 8079 | error_msg = "critical error"; |
| 8080 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8081 | exit(1); |
| 8082 | } |
| 8083 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8084 | /* |
| 8085 | * |
| 8086 | * Create "core" object. |
| 8087 | * |
| 8088 | */ |
| 8089 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8090 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8091 | lua_newtable(gL.T); |
| 8092 | |
| 8093 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8094 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8095 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8096 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8097 | /* Register special functions. */ |
| 8098 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8099 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8100 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8101 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8102 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8103 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8104 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8105 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8106 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8107 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8108 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8109 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8110 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8111 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8112 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8113 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8114 | hlua_class_function(gL.T, "log", hlua_log); |
| 8115 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8116 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8117 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8118 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8119 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8120 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8121 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8122 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8123 | |
| 8124 | /* |
| 8125 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8126 | * Create "act" object. |
| 8127 | * |
| 8128 | */ |
| 8129 | |
| 8130 | /* This table entry is the object "act" base. */ |
| 8131 | lua_newtable(gL.T); |
| 8132 | |
| 8133 | /* push action return constants */ |
| 8134 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8135 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8136 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8137 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8138 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8139 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8140 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8141 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8142 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8143 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8144 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8145 | lua_setglobal(gL.T, "act"); |
| 8146 | |
| 8147 | /* |
| 8148 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8149 | * Register class Map |
| 8150 | * |
| 8151 | */ |
| 8152 | |
| 8153 | /* This table entry is the object "Map" base. */ |
| 8154 | lua_newtable(gL.T); |
| 8155 | |
| 8156 | /* register pattern types. */ |
| 8157 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8158 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8159 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8160 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8161 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8162 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8163 | |
| 8164 | /* register constructor. */ |
| 8165 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8166 | |
| 8167 | /* Create and fill the metatable. */ |
| 8168 | lua_newtable(gL.T); |
| 8169 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8170 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8171 | lua_pushstring(gL.T, "__index"); |
| 8172 | lua_newtable(gL.T); |
| 8173 | |
| 8174 | /* Register . */ |
| 8175 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8176 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8177 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8178 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8179 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8180 | /* Register previous table in the registry with reference and named entry. |
| 8181 | * The function hlua_register_metatable() pops the stack, so we |
| 8182 | * previously create a copy of the table. |
| 8183 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8184 | 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] | 8185 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8186 | |
| 8187 | /* Assign the metatable to the mai Map object. */ |
| 8188 | lua_setmetatable(gL.T, -2); |
| 8189 | |
| 8190 | /* Set a name to the table. */ |
| 8191 | lua_setglobal(gL.T, "Map"); |
| 8192 | |
| 8193 | /* |
| 8194 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8195 | * Register class Channel |
| 8196 | * |
| 8197 | */ |
| 8198 | |
| 8199 | /* Create and fill the metatable. */ |
| 8200 | lua_newtable(gL.T); |
| 8201 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8202 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8203 | lua_pushstring(gL.T, "__index"); |
| 8204 | lua_newtable(gL.T); |
| 8205 | |
| 8206 | /* Register . */ |
| 8207 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8208 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8209 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8210 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8211 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8212 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8213 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8214 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8215 | 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] | 8216 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8217 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8218 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8219 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8220 | |
| 8221 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8222 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8223 | |
| 8224 | /* |
| 8225 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8226 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8227 | * |
| 8228 | */ |
| 8229 | |
| 8230 | /* Create and fill the metatable. */ |
| 8231 | lua_newtable(gL.T); |
| 8232 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8233 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8234 | lua_pushstring(gL.T, "__index"); |
| 8235 | lua_newtable(gL.T); |
| 8236 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8237 | /* Browse existing fetches and create the associated |
| 8238 | * object method. |
| 8239 | */ |
| 8240 | sf = NULL; |
| 8241 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8242 | |
| 8243 | /* Dont register the keywork if the arguments check function are |
| 8244 | * not safe during the runtime. |
| 8245 | */ |
| 8246 | if ((sf->val_args != NULL) && |
| 8247 | (sf->val_args != val_payload_lv) && |
| 8248 | (sf->val_args != val_hdr)) |
| 8249 | continue; |
| 8250 | |
| 8251 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8252 | * by an underscore. |
| 8253 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8254 | strncpy(trash.area, sf->kw, trash.size); |
| 8255 | trash.area[trash.size - 1] = '\0'; |
| 8256 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8257 | if (*p == '.' || *p == '-' || *p == '+') |
| 8258 | *p = '_'; |
| 8259 | |
| 8260 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8261 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8262 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8263 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8264 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8265 | } |
| 8266 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8267 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8268 | |
| 8269 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8270 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8271 | |
| 8272 | /* |
| 8273 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8274 | * Register class Converters |
| 8275 | * |
| 8276 | */ |
| 8277 | |
| 8278 | /* Create and fill the metatable. */ |
| 8279 | lua_newtable(gL.T); |
| 8280 | |
| 8281 | /* Create and fill the __index entry. */ |
| 8282 | lua_pushstring(gL.T, "__index"); |
| 8283 | lua_newtable(gL.T); |
| 8284 | |
| 8285 | /* Browse existing converters and create the associated |
| 8286 | * object method. |
| 8287 | */ |
| 8288 | sc = NULL; |
| 8289 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8290 | /* Dont register the keywork if the arguments check function are |
| 8291 | * not safe during the runtime. |
| 8292 | */ |
| 8293 | if (sc->val_args != NULL) |
| 8294 | continue; |
| 8295 | |
| 8296 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8297 | * by an underscore. |
| 8298 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8299 | strncpy(trash.area, sc->kw, trash.size); |
| 8300 | trash.area[trash.size - 1] = '\0'; |
| 8301 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8302 | if (*p == '.' || *p == '-' || *p == '+') |
| 8303 | *p = '_'; |
| 8304 | |
| 8305 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8306 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8307 | lua_pushlightuserdata(gL.T, sc); |
| 8308 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8309 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8310 | } |
| 8311 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8312 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8313 | |
| 8314 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8315 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8316 | |
| 8317 | /* |
| 8318 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8319 | * Register class HTTP |
| 8320 | * |
| 8321 | */ |
| 8322 | |
| 8323 | /* Create and fill the metatable. */ |
| 8324 | lua_newtable(gL.T); |
| 8325 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8326 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8327 | lua_pushstring(gL.T, "__index"); |
| 8328 | lua_newtable(gL.T); |
| 8329 | |
| 8330 | /* Register Lua functions. */ |
| 8331 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8332 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8333 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8334 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8335 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8336 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8337 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8338 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8339 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8340 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8341 | |
| 8342 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8343 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8344 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8345 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8346 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8347 | 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] | 8348 | 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] | 8349 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8350 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8351 | |
| 8352 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8353 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8354 | |
| 8355 | /* |
| 8356 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8357 | * Register class AppletTCP |
| 8358 | * |
| 8359 | */ |
| 8360 | |
| 8361 | /* Create and fill the metatable. */ |
| 8362 | lua_newtable(gL.T); |
| 8363 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8364 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8365 | lua_pushstring(gL.T, "__index"); |
| 8366 | lua_newtable(gL.T); |
| 8367 | |
| 8368 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8369 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8370 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8371 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8372 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8373 | 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] | 8374 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8375 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8376 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8377 | |
| 8378 | lua_settable(gL.T, -3); |
| 8379 | |
| 8380 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8381 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8382 | |
| 8383 | /* |
| 8384 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8385 | * Register class AppletHTTP |
| 8386 | * |
| 8387 | */ |
| 8388 | |
| 8389 | /* Create and fill the metatable. */ |
| 8390 | lua_newtable(gL.T); |
| 8391 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8392 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8393 | lua_pushstring(gL.T, "__index"); |
| 8394 | lua_newtable(gL.T); |
| 8395 | |
| 8396 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8397 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8398 | 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] | 8399 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8400 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8401 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8402 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8403 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8404 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8405 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8406 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8407 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8408 | |
| 8409 | lua_settable(gL.T, -3); |
| 8410 | |
| 8411 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8412 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8413 | |
| 8414 | /* |
| 8415 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8416 | * Register class TXN |
| 8417 | * |
| 8418 | */ |
| 8419 | |
| 8420 | /* Create and fill the metatable. */ |
| 8421 | lua_newtable(gL.T); |
| 8422 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8423 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8424 | lua_pushstring(gL.T, "__index"); |
| 8425 | lua_newtable(gL.T); |
| 8426 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8427 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8428 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8429 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8430 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8431 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8432 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8433 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8434 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8435 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8436 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8437 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8438 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8439 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8440 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8441 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8442 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8443 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8444 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8445 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8446 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8447 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8448 | |
| 8449 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8450 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8451 | |
| 8452 | /* |
| 8453 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8454 | * Register class reply |
| 8455 | * |
| 8456 | */ |
| 8457 | lua_newtable(gL.T); |
| 8458 | lua_pushstring(gL.T, "__index"); |
| 8459 | lua_newtable(gL.T); |
| 8460 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8461 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8462 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8463 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8464 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8465 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8466 | |
| 8467 | |
| 8468 | /* |
| 8469 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8470 | * Register class Socket |
| 8471 | * |
| 8472 | */ |
| 8473 | |
| 8474 | /* Create and fill the metatable. */ |
| 8475 | lua_newtable(gL.T); |
| 8476 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8477 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8478 | lua_pushstring(gL.T, "__index"); |
| 8479 | lua_newtable(gL.T); |
| 8480 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8481 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8482 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8483 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8484 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8485 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8486 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8487 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8488 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8489 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8490 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8491 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8492 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8493 | 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] | 8494 | |
| 8495 | /* Register the garbage collector entry. */ |
| 8496 | lua_pushstring(gL.T, "__gc"); |
| 8497 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8498 | 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] | 8499 | |
| 8500 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8501 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8502 | |
| 8503 | /* Proxy and server configuration initialisation. */ |
| 8504 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8505 | init_new_proxy(&socket_proxy); |
| 8506 | socket_proxy.parent = NULL; |
| 8507 | socket_proxy.last_change = now.tv_sec; |
| 8508 | socket_proxy.id = "LUA-SOCKET"; |
| 8509 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8510 | socket_proxy.maxconn = 0; |
| 8511 | socket_proxy.accept = NULL; |
| 8512 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8513 | socket_proxy.srv = NULL; |
| 8514 | socket_proxy.conn_retries = 0; |
| 8515 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8516 | |
| 8517 | /* Init TCP server: unchanged parameters */ |
| 8518 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8519 | socket_tcp.next = NULL; |
| 8520 | socket_tcp.proxy = &socket_proxy; |
| 8521 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8522 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8523 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8524 | socket_tcp.idle_conns = NULL; |
| 8525 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8526 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8527 | socket_tcp.last_change = 0; |
| 8528 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8529 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8530 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8531 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8532 | |
| 8533 | /* XXX: Copy default parameter from default server, |
| 8534 | * but the default server is not initialized. |
| 8535 | */ |
| 8536 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8537 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8538 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8539 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8540 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8541 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8542 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8543 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8544 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8545 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8546 | |
| 8547 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8548 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8549 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8550 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8551 | socket_tcp.check.server = &socket_tcp; |
| 8552 | |
| 8553 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8554 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8555 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8556 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8557 | socket_tcp.agent.server = &socket_tcp; |
| 8558 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8559 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8560 | |
| 8561 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8562 | /* Init TCP server: unchanged parameters */ |
| 8563 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8564 | socket_ssl.next = NULL; |
| 8565 | socket_ssl.proxy = &socket_proxy; |
| 8566 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8567 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8568 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8569 | socket_ssl.idle_conns = NULL; |
| 8570 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8571 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8572 | socket_ssl.last_change = 0; |
| 8573 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8574 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8575 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8576 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8577 | |
| 8578 | /* XXX: Copy default parameter from default server, |
| 8579 | * but the default server is not initialized. |
| 8580 | */ |
| 8581 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8582 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8583 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8584 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8585 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8586 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8587 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8588 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8589 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8590 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8591 | |
| 8592 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8593 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8594 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8595 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8596 | socket_ssl.check.server = &socket_ssl; |
| 8597 | |
| 8598 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8599 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8600 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8601 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8602 | socket_ssl.agent.server = &socket_ssl; |
| 8603 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8604 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8605 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8606 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8607 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8608 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8609 | /* |
| 8610 | * |
| 8611 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8612 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8613 | * features like client certificates and ssl_verify. |
| 8614 | * |
| 8615 | */ |
| 8616 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8617 | if (tmp_error != 0) { |
| 8618 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8619 | abort(); /* This must be never arrives because the command line |
| 8620 | not editable by the user. */ |
| 8621 | } |
| 8622 | idx += kw->skip; |
| 8623 | } |
| 8624 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8625 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8626 | |
| 8627 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8628 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8629 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8630 | static void hlua_deinit() |
| 8631 | { |
| 8632 | lua_close(gL.T); |
| 8633 | } |
| 8634 | |
| 8635 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8636 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8637 | static void hlua_register_build_options(void) |
| 8638 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8639 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8640 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8641 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8642 | hap_register_build_opts(ptr, 1); |
| 8643 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8644 | |
| 8645 | INITCALL0(STG_REGISTER, hlua_register_build_options); |