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. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 421 | arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 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. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 564 | smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 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")); |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 686 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 687 | if (!argp[idx].data.prx) |
| 688 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 689 | argp[idx].type = ARGT_FE; |
| 690 | break; |
| 691 | |
| 692 | case ARGT_BE: |
| 693 | if (argp[idx].type != ARGT_STR) |
| 694 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 695 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 696 | if (!argp[idx].data.prx) |
| 697 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 698 | argp[idx].type = ARGT_BE; |
| 699 | break; |
| 700 | |
| 701 | case ARGT_TAB: |
| 702 | if (argp[idx].type != ARGT_STR) |
| 703 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 704 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 705 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 706 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 707 | argp[idx].type = ARGT_TAB; |
| 708 | break; |
| 709 | |
| 710 | case ARGT_SRV: |
| 711 | if (argp[idx].type != ARGT_STR) |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 713 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 714 | if (sname) { |
| 715 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 716 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 717 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 718 | if (!px) |
| 719 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 720 | } |
| 721 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 722 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 723 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 724 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 725 | argp[idx].data.srv = findserver(px, sname); |
| 726 | if (!argp[idx].data.srv) |
| 727 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 728 | argp[idx].type = ARGT_SRV; |
| 729 | break; |
| 730 | |
| 731 | case ARGT_IPV4: |
Christopher Faulet | 8e09ac8 | 2020-08-07 09:07:26 +0200 | [diff] [blame] | 732 | if (argp[idx].type != ARGT_STR) |
| 733 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 734 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 735 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 736 | argp[idx].type = ARGT_IPV4; |
| 737 | break; |
| 738 | |
| 739 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 740 | if (argp[idx].type == ARGT_SINT) |
| 741 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 742 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 743 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 744 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 745 | } |
| 746 | else |
| 747 | WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected")); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 748 | argp[idx].type = ARGT_MSK4; |
| 749 | break; |
| 750 | |
| 751 | case ARGT_IPV6: |
Christopher Faulet | 8e09ac8 | 2020-08-07 09:07:26 +0200 | [diff] [blame] | 752 | if (argp[idx].type != ARGT_STR) |
| 753 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 754 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 755 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 756 | argp[idx].type = ARGT_IPV6; |
| 757 | break; |
| 758 | |
| 759 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 760 | if (argp[idx].type == ARGT_SINT) |
| 761 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 762 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame^] | 763 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 764 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 765 | } |
| 766 | else |
| 767 | WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected")); |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 768 | argp[idx].type = ARGT_MSK6; |
| 769 | break; |
| 770 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 771 | case ARGT_MAP: |
| 772 | case ARGT_REG: |
| 773 | case ARGT_USR: |
| 774 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 775 | break; |
| 776 | } |
| 777 | |
| 778 | /* Check for type of argument. */ |
| 779 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 780 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 781 | arg_type_names[(mask & ARGT_MASK)], |
| 782 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 783 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 784 | } |
| 785 | |
| 786 | /* Next argument. */ |
| 787 | mask >>= ARGT_BITS; |
| 788 | idx++; |
| 789 | } |
| 790 | } |
| 791 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 792 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 793 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 794 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 795 | * |
| 796 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 797 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 798 | */ |
| 799 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 800 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 801 | struct hlua **hlua = lua_getextraspace(L); |
| 802 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 803 | } |
| 804 | static inline void hlua_sethlua(struct hlua *hlua) |
| 805 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 806 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 807 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 810 | /* This function is used to send logs. It try to send on screen (stderr) |
| 811 | * and on the default syslog server. |
| 812 | */ |
| 813 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 814 | { |
| 815 | struct tm tm; |
| 816 | char *p; |
| 817 | |
| 818 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 819 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 820 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 821 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 822 | /* Break the message if exceed the buffer size. */ |
| 823 | *(p-4) = ' '; |
| 824 | *(p-3) = '.'; |
| 825 | *(p-2) = '.'; |
| 826 | *(p-1) = '.'; |
| 827 | break; |
| 828 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 829 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 830 | *p = *msg; |
| 831 | else |
| 832 | *p = '.'; |
| 833 | } |
| 834 | *p = '\0'; |
| 835 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 836 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 837 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 838 | get_localtime(date.tv_sec, &tm); |
| 839 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 840 | 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] | 841 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 842 | fflush(stderr); |
| 843 | } |
| 844 | } |
| 845 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 846 | /* This function just ensure that the yield will be always |
| 847 | * returned with a timeout and permit to set some flags |
| 848 | */ |
| 849 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 850 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 851 | { |
| 852 | struct hlua *hlua = hlua_gethlua(L); |
| 853 | |
| 854 | /* Set the wake timeout. If timeout is required, we set |
| 855 | * the expiration time. |
| 856 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 857 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 858 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 859 | hlua->flags |= flags; |
| 860 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 861 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 862 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 865 | /* This function initialises the Lua environment stored in the stream. |
| 866 | * 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] | 867 | * 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] | 868 | * |
| 869 | * This function is particular. it initialises a new Lua thread. If the |
| 870 | * initialisation fails (example: out of memory error), the lua function |
| 871 | * throws an error (longjmp). |
| 872 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 873 | * 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] | 874 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 875 | * threads appear, the safe environment set a lock to ensure only one |
| 876 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 877 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 878 | * |
| 879 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 880 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 881 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 882 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 883 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 884 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 885 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 886 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 887 | 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] | 888 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 889 | if (!already_safe) { |
| 890 | if (!SET_SAFE_LJMP(gL.T)) { |
| 891 | lua->Tref = LUA_REFNIL; |
| 892 | return 0; |
| 893 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 894 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 895 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 896 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 897 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 898 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 899 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 900 | lua->T = lua_newthread(gL.T); |
| 901 | if (!lua->T) { |
| 902 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 903 | if (!already_safe) |
| 904 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 905 | return 0; |
| 906 | } |
| 907 | hlua_sethlua(lua); |
| 908 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 909 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 910 | if (!already_safe) |
| 911 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 912 | return 1; |
| 913 | } |
| 914 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 915 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 916 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 917 | * is not freed. |
| 918 | */ |
| 919 | void hlua_ctx_destroy(struct hlua *lua) |
| 920 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 921 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 922 | return; |
| 923 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 924 | if (!lua->T) |
| 925 | goto end; |
| 926 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 927 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 928 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 929 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 930 | if (!SET_SAFE_LJMP(lua->T)) |
| 931 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 932 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 933 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 934 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 935 | if (!SET_SAFE_LJMP(gL.T)) |
| 936 | return; |
| 937 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 938 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 939 | /* Forces a garbage collecting process. If the Lua program is finished |
| 940 | * without error, we run the GC on the thread pointer. Its freed all |
| 941 | * the unused memory. |
| 942 | * If the thread is finnish with an error or is currently yielded, |
| 943 | * it seems that the GC applied on the thread doesn't clean anything, |
| 944 | * so e run the GC on the main thread. |
| 945 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 946 | * the garbage collection. |
| 947 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 948 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 949 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 950 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 951 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 952 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 953 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 954 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 955 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 956 | |
| 957 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 958 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* This function is used to restore the Lua context when a coroutine |
| 962 | * fails. This function copy the common memory between old coroutine |
| 963 | * and the new coroutine. The old coroutine is destroyed, and its |
| 964 | * replaced by the new coroutine. |
| 965 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 966 | * as string error message and it is copied in the new stack. |
| 967 | */ |
| 968 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 969 | { |
| 970 | lua_State *T; |
| 971 | int new_ref; |
| 972 | |
| 973 | /* Renew the main LUA stack doesn't have sense. */ |
| 974 | if (lua == &gL) |
| 975 | return 0; |
| 976 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 977 | /* New Lua coroutine. */ |
| 978 | T = lua_newthread(gL.T); |
| 979 | if (!T) |
| 980 | return 0; |
| 981 | |
| 982 | /* Copy last error message. */ |
| 983 | if (keep_msg) |
| 984 | lua_xmove(lua->T, T, 1); |
| 985 | |
| 986 | /* Copy data between the coroutines. */ |
| 987 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 988 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 989 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 990 | |
| 991 | /* Destroy old data. */ |
| 992 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 993 | |
| 994 | /* The thread is garbage collected by Lua. */ |
| 995 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 996 | |
| 997 | /* Fill the struct with the new coroutine values. */ |
| 998 | lua->Mref = new_ref; |
| 999 | lua->T = T; |
| 1000 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1001 | |
| 1002 | /* Set context. */ |
| 1003 | hlua_sethlua(lua); |
| 1004 | |
| 1005 | return 1; |
| 1006 | } |
| 1007 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1008 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1009 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1010 | struct hlua *hlua = hlua_gethlua(L); |
| 1011 | |
| 1012 | /* Lua cannot yield when its returning from a function, |
| 1013 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1014 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1015 | */ |
| 1016 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1017 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1018 | return; |
| 1019 | } |
| 1020 | |
| 1021 | /* restore the interrupt condition. */ |
| 1022 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1023 | |
| 1024 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1025 | * If the state is not yieldable, trying yield causes an error. |
| 1026 | */ |
| 1027 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1028 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1029 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1030 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1031 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1032 | hlua->run_time += now_ms - hlua->start_time; |
| 1033 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1034 | lua_pushfstring(L, "execution timeout"); |
| 1035 | WILL_LJMP(lua_error(L)); |
| 1036 | } |
| 1037 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1038 | /* Update the start time. */ |
| 1039 | hlua->start_time = now_ms; |
| 1040 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1041 | /* Try to interrupt the process at the end of the current |
| 1042 | * unyieldable function. |
| 1043 | */ |
| 1044 | 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] | 1045 | } |
| 1046 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1047 | /* This function start or resumes the Lua stack execution. If the flag |
| 1048 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1049 | * The function return an error. |
| 1050 | * |
| 1051 | * The function can returns 4 values: |
| 1052 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1053 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1054 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1055 | * - 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] | 1056 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1057 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1058 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1059 | * 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] | 1060 | * LUA code. |
| 1061 | */ |
| 1062 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1063 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1064 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1065 | int nres; |
| 1066 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1067 | int ret; |
| 1068 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1069 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1071 | /* Initialise run time counter. */ |
| 1072 | if (!HLUA_IS_RUNNING(lua)) |
| 1073 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1074 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1075 | /* Lock the whole Lua execution. This lock must be before the |
| 1076 | * label "resume_execution". |
| 1077 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1078 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1079 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1080 | resume_execution: |
| 1081 | |
| 1082 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1083 | * instructions. it is used for preventing infinite loops. |
| 1084 | */ |
| 1085 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1086 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1087 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1088 | HLUA_SET_RUN(lua); |
| 1089 | HLUA_CLR_CTRLYIELD(lua); |
| 1090 | HLUA_CLR_WAKERESWR(lua); |
| 1091 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1092 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1093 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1094 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1095 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1096 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1097 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1098 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1099 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1100 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1101 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1102 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1103 | switch (ret) { |
| 1104 | |
| 1105 | case LUA_OK: |
| 1106 | ret = HLUA_E_OK; |
| 1107 | break; |
| 1108 | |
| 1109 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1110 | /* Check if the execution timeout is expired. It it is the case, we |
| 1111 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1112 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1113 | tv_update_date(0, 1); |
| 1114 | lua->run_time += now_ms - lua->start_time; |
| 1115 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1116 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1117 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1118 | break; |
| 1119 | } |
| 1120 | /* Process the forced yield. if the general yield is not allowed or |
| 1121 | * if no task were associated this the current Lua execution |
| 1122 | * coroutine, we resume the execution. Else we want to return in the |
| 1123 | * scheduler and we want to be waked up again, to continue the |
| 1124 | * current Lua execution. So we schedule our own task. |
| 1125 | */ |
| 1126 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1127 | if (!yield_allowed || !lua->task) |
| 1128 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1129 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1130 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1131 | if (!yield_allowed) { |
| 1132 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1133 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1134 | break; |
| 1135 | } |
| 1136 | ret = HLUA_E_AGAIN; |
| 1137 | break; |
| 1138 | |
| 1139 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1140 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1141 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1142 | * because the errors ares the only one mean to return immediately |
| 1143 | * from and lua execution. |
| 1144 | */ |
| 1145 | if (lua->flags & HLUA_EXIT) { |
| 1146 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1147 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1148 | break; |
| 1149 | } |
| 1150 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1151 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1152 | if (!lua_checkstack(lua->T, 1)) { |
| 1153 | ret = HLUA_E_ERR; |
| 1154 | break; |
| 1155 | } |
| 1156 | msg = lua_tostring(lua->T, -1); |
| 1157 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1158 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1159 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1161 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1163 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | ret = HLUA_E_ERRMSG; |
| 1165 | break; |
| 1166 | |
| 1167 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1168 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1169 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1170 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1171 | break; |
| 1172 | |
| 1173 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1174 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1175 | if (!lua_checkstack(lua->T, 1)) { |
| 1176 | ret = HLUA_E_ERR; |
| 1177 | break; |
| 1178 | } |
| 1179 | msg = lua_tostring(lua->T, -1); |
| 1180 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1181 | lua_pop(lua->T, 1); |
| 1182 | if (msg) |
| 1183 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1184 | else |
| 1185 | lua_pushfstring(lua->T, "message handler error"); |
| 1186 | ret = HLUA_E_ERRMSG; |
| 1187 | break; |
| 1188 | |
| 1189 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1190 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1191 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1192 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | switch (ret) { |
| 1197 | case HLUA_E_AGAIN: |
| 1198 | break; |
| 1199 | |
| 1200 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1201 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1202 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1203 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1204 | break; |
| 1205 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1206 | case HLUA_E_ETMOUT: |
| 1207 | case HLUA_E_NOMEM: |
| 1208 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1210 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1211 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | hlua_ctx_renew(lua, 0); |
| 1213 | break; |
| 1214 | |
| 1215 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1216 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1217 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | break; |
| 1219 | } |
| 1220 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1221 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1222 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1223 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1224 | return ret; |
| 1225 | } |
| 1226 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1227 | /* This function exit the current code. */ |
| 1228 | __LJMP static int hlua_done(lua_State *L) |
| 1229 | { |
| 1230 | struct hlua *hlua = hlua_gethlua(L); |
| 1231 | |
| 1232 | hlua->flags |= HLUA_EXIT; |
| 1233 | WILL_LJMP(lua_error(L)); |
| 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1238 | /* This function is an LUA binding. It provides a function |
| 1239 | * for deleting ACL from a referenced ACL file. |
| 1240 | */ |
| 1241 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1242 | { |
| 1243 | const char *name; |
| 1244 | const char *key; |
| 1245 | struct pat_ref *ref; |
| 1246 | |
| 1247 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1248 | |
| 1249 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1250 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1251 | |
| 1252 | ref = pat_ref_lookup(name); |
| 1253 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1254 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1255 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1256 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1257 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1258 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | /* This function is an LUA binding. It provides a function |
| 1263 | * for deleting map entry from a referenced map file. |
| 1264 | */ |
| 1265 | static int hlua_del_map(lua_State *L) |
| 1266 | { |
| 1267 | const char *name; |
| 1268 | const char *key; |
| 1269 | struct pat_ref *ref; |
| 1270 | |
| 1271 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1272 | |
| 1273 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1274 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1275 | |
| 1276 | ref = pat_ref_lookup(name); |
| 1277 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1278 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1279 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1280 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1281 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1282 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | /* This function is an LUA binding. It provides a function |
| 1287 | * for adding ACL pattern from a referenced ACL file. |
| 1288 | */ |
| 1289 | static int hlua_add_acl(lua_State *L) |
| 1290 | { |
| 1291 | const char *name; |
| 1292 | const char *key; |
| 1293 | struct pat_ref *ref; |
| 1294 | |
| 1295 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1296 | |
| 1297 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1298 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1299 | |
| 1300 | ref = pat_ref_lookup(name); |
| 1301 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1302 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1303 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1304 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1305 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1306 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1307 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | /* This function is an LUA binding. It provides a function |
| 1312 | * for setting map pattern and sample from a referenced map |
| 1313 | * file. |
| 1314 | */ |
| 1315 | static int hlua_set_map(lua_State *L) |
| 1316 | { |
| 1317 | const char *name; |
| 1318 | const char *key; |
| 1319 | const char *value; |
| 1320 | struct pat_ref *ref; |
| 1321 | |
| 1322 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1323 | |
| 1324 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1325 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1326 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1327 | |
| 1328 | ref = pat_ref_lookup(name); |
| 1329 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1330 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1331 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1332 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1333 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1334 | pat_ref_set(ref, key, value, NULL); |
| 1335 | else |
| 1336 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1337 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1338 | return 0; |
| 1339 | } |
| 1340 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1341 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1342 | * 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] | 1343 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1344 | * the name of the object (_G[<name>] = <metable> ). |
| 1345 | * |
| 1346 | * A metable is a table that modify the standard behavior of a standard |
| 1347 | * access to the associated data. The entries of this new metatable are |
| 1348 | * defined as is: |
| 1349 | * |
| 1350 | * http://lua-users.org/wiki/MetatableEvents |
| 1351 | * |
| 1352 | * __index |
| 1353 | * |
| 1354 | * we access an absent field in a table, the result is nil. This is |
| 1355 | * true, but it is not the whole truth. Actually, such access triggers |
| 1356 | * the interpreter to look for an __index metamethod: If there is no |
| 1357 | * such method, as usually happens, then the access results in nil; |
| 1358 | * otherwise, the metamethod will provide the result. |
| 1359 | * |
| 1360 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1361 | * the key does not appear in the table, but the metatable has an __index |
| 1362 | * property: |
| 1363 | * |
| 1364 | * - if the value is a function, the function is called, passing in the |
| 1365 | * table and the key; the return value of that function is returned as |
| 1366 | * the result. |
| 1367 | * |
| 1368 | * - if the value is another table, the value of the key in that table is |
| 1369 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1370 | * table's metatable has an __index property, then it continues on up) |
| 1371 | * |
| 1372 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1373 | * |
| 1374 | * http://www.lua.org/pil/13.4.1.html |
| 1375 | * |
| 1376 | * __newindex |
| 1377 | * |
| 1378 | * Like __index, but control property assignment. |
| 1379 | * |
| 1380 | * __mode - Control weak references. A string value with one or both |
| 1381 | * of the characters 'k' and 'v' which specifies that the the |
| 1382 | * keys and/or values in the table are weak references. |
| 1383 | * |
| 1384 | * __call - Treat a table like a function. When a table is followed by |
| 1385 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1386 | * a __call key pointing to a function, that function is invoked |
| 1387 | * (passing any specified arguments) and the return value is |
| 1388 | * returned. |
| 1389 | * |
| 1390 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1391 | * called, if the metatable for myTable has a __metatable |
| 1392 | * key, the value of that key is returned instead of the |
| 1393 | * actual metatable. |
| 1394 | * |
| 1395 | * __tostring - Control string representation. When the builtin |
| 1396 | * "tostring( myTable )" function is called, if the metatable |
| 1397 | * for myTable has a __tostring property set to a function, |
| 1398 | * that function is invoked (passing myTable to it) and the |
| 1399 | * return value is used as the string representation. |
| 1400 | * |
| 1401 | * __len - Control table length. When the table length is requested using |
| 1402 | * the length operator ( '#' ), if the metatable for myTable has |
| 1403 | * a __len key pointing to a function, that function is invoked |
| 1404 | * (passing myTable to it) and the return value used as the value |
| 1405 | * of "#myTable". |
| 1406 | * |
| 1407 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1408 | * collected, if the metatable has a __gc field pointing to a |
| 1409 | * function, that function is first invoked, passing the userdata |
| 1410 | * to it. The __gc metamethod is not called for tables. |
| 1411 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1412 | * |
| 1413 | * Special metamethods for redefining standard operators: |
| 1414 | * http://www.lua.org/pil/13.1.html |
| 1415 | * |
| 1416 | * __add "+" |
| 1417 | * __sub "-" |
| 1418 | * __mul "*" |
| 1419 | * __div "/" |
| 1420 | * __unm "!" |
| 1421 | * __pow "^" |
| 1422 | * __concat ".." |
| 1423 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1424 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1425 | * http://www.lua.org/pil/13.2.html |
| 1426 | * |
| 1427 | * __eq "==" |
| 1428 | * __lt "<" |
| 1429 | * __le "<=" |
| 1430 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1431 | |
| 1432 | /* |
| 1433 | * |
| 1434 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1435 | * Class Map |
| 1436 | * |
| 1437 | * |
| 1438 | */ |
| 1439 | |
| 1440 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1441 | * a class session, otherwise it throws an error. |
| 1442 | */ |
| 1443 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1444 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1445 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /* This function is the map constructor. It don't need |
| 1449 | * the class Map object. It creates and return a new Map |
| 1450 | * object. It must be called only during "body" or "init" |
| 1451 | * context because it process some filesystem accesses. |
| 1452 | */ |
| 1453 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1454 | { |
| 1455 | const char *fn; |
| 1456 | int match = PAT_MATCH_STR; |
| 1457 | struct sample_conv conv; |
| 1458 | const char *file = ""; |
| 1459 | int line = 0; |
| 1460 | lua_Debug ar; |
| 1461 | char *err = NULL; |
| 1462 | struct arg args[2]; |
| 1463 | |
| 1464 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1465 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1466 | |
| 1467 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1468 | |
| 1469 | if (lua_gettop(L) >= 2) { |
| 1470 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1471 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1472 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1473 | } |
| 1474 | |
| 1475 | /* Get Lua filename and line number. */ |
| 1476 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1477 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1478 | if (ar.currentline > 0) { /* is there info? */ |
| 1479 | file = ar.short_src; |
| 1480 | line = ar.currentline; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | /* fill fake sample_conv struct. */ |
| 1485 | conv.kw = ""; /* unused. */ |
| 1486 | conv.process = NULL; /* unused. */ |
| 1487 | conv.arg_mask = 0; /* unused. */ |
| 1488 | conv.val_args = NULL; /* unused. */ |
| 1489 | conv.out_type = SMP_T_STR; |
| 1490 | conv.private = (void *)(long)match; |
| 1491 | switch (match) { |
| 1492 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1493 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1494 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1495 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1496 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1497 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1498 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1499 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1500 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1501 | default: |
| 1502 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1503 | } |
| 1504 | |
| 1505 | /* fill fake args. */ |
| 1506 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1507 | args[0].data.str.area = strdup(fn); |
| 1508 | args[0].data.str.data = strlen(fn); |
| 1509 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1510 | args[1].type = ARGT_STOP; |
| 1511 | |
| 1512 | /* load the map. */ |
| 1513 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1514 | /* error case: we cant use luaL_error because we must |
| 1515 | * free the err variable. |
| 1516 | */ |
| 1517 | luaL_where(L, 1); |
| 1518 | lua_pushfstring(L, "'new': %s.", err); |
| 1519 | lua_concat(L, 2); |
| 1520 | free(err); |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1521 | free(args[0].data.str.area); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1522 | WILL_LJMP(lua_error(L)); |
| 1523 | } |
| 1524 | |
| 1525 | /* create the lua object. */ |
| 1526 | lua_newtable(L); |
| 1527 | lua_pushlightuserdata(L, args[0].data.map); |
| 1528 | lua_rawseti(L, -2, 0); |
| 1529 | |
| 1530 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1531 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1532 | lua_setmetatable(L, -2); |
| 1533 | |
| 1534 | |
| 1535 | return 1; |
| 1536 | } |
| 1537 | |
| 1538 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1539 | { |
| 1540 | struct map_descriptor *desc; |
| 1541 | struct pattern *pat; |
| 1542 | struct sample smp; |
| 1543 | |
| 1544 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1545 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1546 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1547 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1548 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1549 | } |
| 1550 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1551 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1552 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1553 | 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] | 1554 | } |
| 1555 | |
| 1556 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1557 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1558 | if (str) |
| 1559 | lua_pushstring(L, ""); |
| 1560 | else |
| 1561 | lua_pushnil(L); |
| 1562 | return 1; |
| 1563 | } |
| 1564 | |
| 1565 | /* 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] | 1566 | 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] | 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1571 | { |
| 1572 | return _hlua_map_lookup(L, 0); |
| 1573 | } |
| 1574 | |
| 1575 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1576 | { |
| 1577 | return _hlua_map_lookup(L, 1); |
| 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | * |
| 1582 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1583 | * Class Socket |
| 1584 | * |
| 1585 | * |
| 1586 | */ |
| 1587 | |
| 1588 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1589 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1590 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /* 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] | 1594 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1595 | * received. |
| 1596 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1597 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1598 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1599 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1601 | if (appctx->ctx.hlua_cosocket.die) { |
| 1602 | si_shutw(si); |
| 1603 | si_shutr(si); |
| 1604 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1605 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1606 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1607 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1608 | } |
| 1609 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1610 | /* If we cant write, wakeup the pending write signals. */ |
| 1611 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1612 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1613 | |
| 1614 | /* If we cant read, wakeup the pending read signals. */ |
| 1615 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1616 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1617 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1618 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1619 | * to be notified whenever the connection completes. |
| 1620 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1621 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1622 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1623 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1624 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1625 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1626 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1627 | |
| 1628 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1629 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1630 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1631 | /* 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] | 1632 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1633 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1634 | |
| 1635 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1636 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1637 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1638 | |
| 1639 | /* Some data were injected in the buffer, notify the stream |
| 1640 | * interface. |
| 1641 | */ |
| 1642 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1643 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1644 | |
| 1645 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1646 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1647 | */ |
| 1648 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1649 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1650 | } |
| 1651 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1652 | /* This function is called when the "struct stream" is destroyed. |
| 1653 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1654 | * Wake all the pending signals. |
| 1655 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1656 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1657 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1658 | struct xref *peer; |
| 1659 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1660 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1661 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1662 | if (peer) |
| 1663 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1664 | |
| 1665 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1666 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1667 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1671 | * uses this object. If the stream does not exists, just quit. |
| 1672 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1673 | * pending signal can rest in the read and write lists. destroy |
| 1674 | * it. |
| 1675 | */ |
| 1676 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1677 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1678 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1679 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1680 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1681 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1682 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1683 | |
| 1684 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1685 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1686 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1688 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1690 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1691 | appctx->ctx.hlua_cosocket.die = 1; |
| 1692 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1693 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1694 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1695 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1700 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1702 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1704 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1706 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1707 | struct hlua *hlua = hlua_gethlua(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 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1710 | |
| 1711 | /* Check if we run on the same thread than the xreator thread. |
| 1712 | * We cannot access to the socket if the thread is different. |
| 1713 | */ |
| 1714 | if (socket->tid != tid) |
| 1715 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1716 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1717 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1718 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1719 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1720 | |
| 1721 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1722 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1723 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1724 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1725 | appctx->ctx.hlua_cosocket.die = 1; |
| 1726 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1727 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1728 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1729 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1730 | return 0; |
| 1731 | } |
| 1732 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1733 | /* The close function calls close_helper. |
| 1734 | */ |
| 1735 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1736 | { |
| 1737 | MAY_LJMP(check_args(L, 1, "close")); |
| 1738 | return hlua_socket_close_helper(L); |
| 1739 | } |
| 1740 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1741 | /* This Lua function assumes that the stack contain three parameters. |
| 1742 | * 1 - USERDATA containing a struct socket |
| 1743 | * 2 - INTEGER with values of the macro defined below |
| 1744 | * If the integer is -1, we must read at most one line. |
| 1745 | * If the integer is -2, we ust read all the data until the |
| 1746 | * end of the stream. |
| 1747 | * If the integer is positive value, we must read a number of |
| 1748 | * bytes corresponding to this value. |
| 1749 | */ |
| 1750 | #define HLSR_READ_LINE (-1) |
| 1751 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1752 | __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] | 1753 | { |
| 1754 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1755 | int wanted = lua_tointeger(L, 2); |
| 1756 | struct hlua *hlua = hlua_gethlua(L); |
| 1757 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1758 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1759 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1760 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1761 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1762 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1763 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1764 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1765 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1766 | struct stream_interface *si; |
| 1767 | struct stream *s; |
| 1768 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1769 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1770 | |
| 1771 | /* Check if this lua stack is schedulable. */ |
| 1772 | if (!hlua || !hlua->task) |
| 1773 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1774 | "'frontend', 'backend' or 'task'")); |
| 1775 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1776 | /* Check if we run on the same thread than the xreator thread. |
| 1777 | * We cannot access to the socket if the thread is different. |
| 1778 | */ |
| 1779 | if (socket->tid != tid) |
| 1780 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1781 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1782 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1783 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1784 | if (!peer) |
| 1785 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1786 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1787 | si = appctx->owner; |
| 1788 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1790 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1792 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1793 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | if (nblk < 0) /* Connection close. */ |
| 1795 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1796 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1797 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1798 | |
| 1799 | /* remove final \r\n. */ |
| 1800 | if (nblk == 1) { |
| 1801 | if (blk1[len1-1] == '\n') { |
| 1802 | len1--; |
| 1803 | skip_at_end++; |
| 1804 | if (blk1[len1-1] == '\r') { |
| 1805 | len1--; |
| 1806 | skip_at_end++; |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | else { |
| 1811 | if (blk2[len2-1] == '\n') { |
| 1812 | len2--; |
| 1813 | skip_at_end++; |
| 1814 | if (blk2[len2-1] == '\r') { |
| 1815 | len2--; |
| 1816 | skip_at_end++; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1824 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | if (nblk < 0) /* Connection close. */ |
| 1826 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1827 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | goto connection_empty; |
| 1829 | } |
| 1830 | |
| 1831 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1833 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1834 | if (nblk < 0) /* Connection close. */ |
| 1835 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1836 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | goto connection_empty; |
| 1838 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1839 | missing_bytes = wanted - socket->b.n; |
| 1840 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1842 | len1 = missing_bytes; |
| 1843 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1844 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | len = len1; |
| 1848 | |
| 1849 | luaL_addlstring(&socket->b, blk1, len1); |
| 1850 | if (nblk == 2) { |
| 1851 | len += len2; |
| 1852 | luaL_addlstring(&socket->b, blk2, len2); |
| 1853 | } |
| 1854 | |
| 1855 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1856 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1857 | |
| 1858 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1859 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1860 | |
| 1861 | /* If the pattern reclaim to read all the data |
| 1862 | * in the connection, got out. |
| 1863 | */ |
| 1864 | if (wanted == HLSR_READ_ALL) |
| 1865 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1866 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1867 | goto connection_empty; |
| 1868 | |
| 1869 | /* Return result. */ |
| 1870 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1871 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | return 1; |
| 1873 | |
| 1874 | connection_closed: |
| 1875 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1876 | xref_unlock(&socket->xref, peer); |
| 1877 | |
| 1878 | no_peer: |
| 1879 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | /* If the buffer containds data. */ |
| 1881 | if (socket->b.n > 0) { |
| 1882 | luaL_pushresult(&socket->b); |
| 1883 | return 1; |
| 1884 | } |
| 1885 | lua_pushnil(L); |
| 1886 | lua_pushstring(L, "connection closed."); |
| 1887 | return 2; |
| 1888 | |
| 1889 | connection_empty: |
| 1890 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1891 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1892 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1894 | } |
| 1895 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1896 | 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] | 1897 | return 0; |
| 1898 | } |
| 1899 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1900 | /* This Lua function gets two parameters. The first one can be string |
| 1901 | * or a number. If the string is "*l", the user requires one line. If |
| 1902 | * 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] | 1903 | * If the value is a number, the user require a number of bytes equal |
| 1904 | * to the value. The default value is "*l" (a line). |
| 1905 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1906 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | * integer takes this values: |
| 1908 | * -1 : read a line |
| 1909 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1910 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1912 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1913 | * concatenated with the read data. |
| 1914 | */ |
| 1915 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1916 | { |
| 1917 | int wanted = HLSR_READ_LINE; |
| 1918 | const char *pattern; |
| 1919 | int type; |
| 1920 | char *error; |
| 1921 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1922 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | |
| 1924 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1925 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1926 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1927 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1929 | /* Check if we run on the same thread than the xreator thread. |
| 1930 | * We cannot access to the socket if the thread is different. |
| 1931 | */ |
| 1932 | if (socket->tid != tid) |
| 1933 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1934 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1935 | /* check for pattern. */ |
| 1936 | if (lua_gettop(L) >= 2) { |
| 1937 | type = lua_type(L, 2); |
| 1938 | if (type == LUA_TSTRING) { |
| 1939 | pattern = lua_tostring(L, 2); |
| 1940 | if (strcmp(pattern, "*a") == 0) |
| 1941 | wanted = HLSR_READ_ALL; |
| 1942 | else if (strcmp(pattern, "*l") == 0) |
| 1943 | wanted = HLSR_READ_LINE; |
| 1944 | else { |
| 1945 | wanted = strtoll(pattern, &error, 10); |
| 1946 | if (*error != '\0') |
| 1947 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1948 | } |
| 1949 | } |
| 1950 | else if (type == LUA_TNUMBER) { |
| 1951 | wanted = lua_tointeger(L, 2); |
| 1952 | if (wanted < 0) |
| 1953 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | /* Set pattern. */ |
| 1958 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1959 | |
| 1960 | /* Check if we would replace the top by itself. */ |
| 1961 | if (lua_gettop(L) != 2) |
| 1962 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1963 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1964 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | luaL_buffinit(L, &socket->b); |
| 1966 | |
| 1967 | /* Check prefix. */ |
| 1968 | if (lua_gettop(L) >= 3) { |
| 1969 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1970 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1971 | pattern = lua_tolstring(L, 3, &len); |
| 1972 | luaL_addlstring(&socket->b, pattern, len); |
| 1973 | } |
| 1974 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1975 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1979 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1981 | 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] | 1982 | { |
| 1983 | struct hlua_socket *socket; |
| 1984 | struct hlua *hlua = hlua_gethlua(L); |
| 1985 | struct appctx *appctx; |
| 1986 | size_t buf_len; |
| 1987 | const char *buf; |
| 1988 | int len; |
| 1989 | int send_len; |
| 1990 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1991 | struct xref *peer; |
| 1992 | struct stream_interface *si; |
| 1993 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1994 | |
| 1995 | /* Check if this lua stack is schedulable. */ |
| 1996 | if (!hlua || !hlua->task) |
| 1997 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1998 | "'frontend', 'backend' or 'task'")); |
| 1999 | |
| 2000 | /* Get object */ |
| 2001 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2002 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2003 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2004 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2005 | /* Check if we run on the same thread than the xreator thread. |
| 2006 | * We cannot access to the socket if the thread is different. |
| 2007 | */ |
| 2008 | if (socket->tid != tid) |
| 2009 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2010 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2011 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2012 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2013 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2014 | lua_pushinteger(L, -1); |
| 2015 | return 1; |
| 2016 | } |
| 2017 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2018 | si = appctx->owner; |
| 2019 | s = si_strm(si); |
| 2020 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2022 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2023 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | lua_pushinteger(L, -1); |
| 2025 | return 1; |
| 2026 | } |
| 2027 | |
| 2028 | /* Update the input buffer data. */ |
| 2029 | buf += sent; |
| 2030 | send_len = buf_len - sent; |
| 2031 | |
| 2032 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2033 | if (sent >= buf_len) { |
| 2034 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2035 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2036 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2038 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2039 | * the request buffer if its not required. |
| 2040 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2041 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2042 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2043 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2044 | } |
| 2045 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2046 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2047 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2048 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2050 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | |
| 2052 | /* send data */ |
| 2053 | if (len < send_len) |
| 2054 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2055 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2056 | |
| 2057 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2058 | * the data" (-2) are not expected because the available length |
| 2059 | * is tested. |
| 2060 | * Other unknown error are also not expected. |
| 2061 | */ |
| 2062 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2063 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2064 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2065 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2066 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2068 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2069 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | return 1; |
| 2071 | } |
| 2072 | |
| 2073 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2074 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2075 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2076 | s->req.rex = TICK_ETERNITY; |
| 2077 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | |
| 2079 | /* Update length sent. */ |
| 2080 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2081 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2082 | |
| 2083 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2084 | if (sent + len >= buf_len) { |
| 2085 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2087 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | |
| 2089 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2090 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2091 | xref_unlock(&socket->xref, peer); |
| 2092 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2093 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2094 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2095 | 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] | 2096 | return 0; |
| 2097 | } |
| 2098 | |
| 2099 | /* This function initiate the send of data. It just check the input |
| 2100 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2101 | * 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] | 2102 | * "hlua_socket_write_yield" that can yield. |
| 2103 | * |
| 2104 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2105 | * the associated object. The second is a string buffer. The third is |
| 2106 | * a facultative integer that represents where is the buffer position |
| 2107 | * of the start of the data that can send. The first byte is the |
| 2108 | * position "1". The default value is "1". The fourth argument is a |
| 2109 | * facultative integer that represents where is the buffer position |
| 2110 | * of the end of the data that can send. The default is the last byte. |
| 2111 | */ |
| 2112 | static int hlua_socket_send(struct lua_State *L) |
| 2113 | { |
| 2114 | int i; |
| 2115 | int j; |
| 2116 | const char *buf; |
| 2117 | size_t buf_len; |
| 2118 | |
| 2119 | /* Check number of arguments. */ |
| 2120 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2121 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2122 | |
| 2123 | /* Get the string. */ |
| 2124 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2125 | |
| 2126 | /* Get and check j. */ |
| 2127 | if (lua_gettop(L) == 4) { |
| 2128 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2129 | if (j < 0) |
| 2130 | j = buf_len + j + 1; |
| 2131 | if (j > buf_len) |
| 2132 | j = buf_len + 1; |
| 2133 | lua_pop(L, 1); |
| 2134 | } |
| 2135 | else |
| 2136 | j = buf_len; |
| 2137 | |
| 2138 | /* Get and check i. */ |
| 2139 | if (lua_gettop(L) == 3) { |
| 2140 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2141 | if (i < 0) |
| 2142 | i = buf_len + i + 1; |
| 2143 | if (i > buf_len) |
| 2144 | i = buf_len + 1; |
| 2145 | lua_pop(L, 1); |
| 2146 | } else |
| 2147 | i = 1; |
| 2148 | |
| 2149 | /* Check bth i and j. */ |
| 2150 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2151 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2152 | return 1; |
| 2153 | } |
| 2154 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2155 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2156 | return 1; |
| 2157 | } |
| 2158 | if (i == 0) |
| 2159 | i = 1; |
| 2160 | if (j == 0) |
| 2161 | j = 1; |
| 2162 | |
| 2163 | /* Pop the string. */ |
| 2164 | lua_pop(L, 1); |
| 2165 | |
| 2166 | /* Update the buffer length. */ |
| 2167 | buf += i - 1; |
| 2168 | buf_len = j - i + 1; |
| 2169 | lua_pushlstring(L, buf, buf_len); |
| 2170 | |
| 2171 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2172 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2174 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2177 | #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] | 2178 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2179 | { |
| 2180 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2181 | int ret; |
| 2182 | int len; |
| 2183 | char *p; |
| 2184 | |
| 2185 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2186 | if (ret <= 0) { |
| 2187 | lua_pushnil(L); |
| 2188 | return 1; |
| 2189 | } |
| 2190 | |
| 2191 | if (ret == AF_UNIX) { |
| 2192 | lua_pushstring(L, buffer+1); |
| 2193 | return 1; |
| 2194 | } |
| 2195 | else if (ret == AF_INET6) { |
| 2196 | buffer[0] = '['; |
| 2197 | len = strlen(buffer); |
| 2198 | buffer[len] = ']'; |
| 2199 | len++; |
| 2200 | buffer[len] = ':'; |
| 2201 | len++; |
| 2202 | p = buffer; |
| 2203 | } |
| 2204 | else if (ret == AF_INET) { |
| 2205 | p = buffer + 1; |
| 2206 | len = strlen(p); |
| 2207 | p[len] = ':'; |
| 2208 | len++; |
| 2209 | } |
| 2210 | else { |
| 2211 | lua_pushnil(L); |
| 2212 | return 1; |
| 2213 | } |
| 2214 | |
| 2215 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2216 | lua_pushnil(L); |
| 2217 | return 1; |
| 2218 | } |
| 2219 | |
| 2220 | lua_pushstring(L, p); |
| 2221 | return 1; |
| 2222 | } |
| 2223 | |
| 2224 | /* Returns information about the peer of the connection. */ |
| 2225 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2226 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2227 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2228 | struct xref *peer; |
| 2229 | struct appctx *appctx; |
| 2230 | struct stream_interface *si; |
| 2231 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2232 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2233 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2234 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2235 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2236 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2238 | /* Check if we run on the same thread than the xreator thread. |
| 2239 | * We cannot access to the socket if the thread is different. |
| 2240 | */ |
| 2241 | if (socket->tid != tid) |
| 2242 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2243 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2244 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2245 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2246 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | lua_pushnil(L); |
| 2248 | return 1; |
| 2249 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2250 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2251 | si = appctx->owner; |
| 2252 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2254 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2255 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2256 | lua_pushnil(L); |
| 2257 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | } |
| 2259 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2260 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2261 | xref_unlock(&socket->xref, peer); |
| 2262 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | /* Returns information about my connection side. */ |
| 2266 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2267 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2268 | struct hlua_socket *socket; |
| 2269 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2270 | struct appctx *appctx; |
| 2271 | struct xref *peer; |
| 2272 | struct stream_interface *si; |
| 2273 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2274 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2275 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2277 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2278 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2280 | /* Check if we run on the same thread than the xreator thread. |
| 2281 | * We cannot access to the socket if the thread is different. |
| 2282 | */ |
| 2283 | if (socket->tid != tid) |
| 2284 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2285 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2286 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2287 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2288 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | lua_pushnil(L); |
| 2290 | return 1; |
| 2291 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2292 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2293 | si = appctx->owner; |
| 2294 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2296 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2297 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | lua_pushnil(L); |
| 2300 | return 1; |
| 2301 | } |
| 2302 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2303 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2304 | xref_unlock(&socket->xref, peer); |
| 2305 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2309 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | .obj_type = OBJ_TYPE_APPLET, |
| 2311 | .name = "<LUA_TCP>", |
| 2312 | .fct = hlua_socket_handler, |
| 2313 | .release = hlua_socket_release, |
| 2314 | }; |
| 2315 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2316 | __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] | 2317 | { |
| 2318 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2319 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2320 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2322 | struct stream_interface *si; |
| 2323 | struct stream *s; |
| 2324 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2325 | /* Check if we run on the same thread than the xreator thread. |
| 2326 | * We cannot access to the socket if the thread is different. |
| 2327 | */ |
| 2328 | if (socket->tid != tid) |
| 2329 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2330 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2332 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2333 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2334 | lua_pushnil(L); |
| 2335 | lua_pushstring(L, "Can't connect"); |
| 2336 | return 2; |
| 2337 | } |
| 2338 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2339 | si = appctx->owner; |
| 2340 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2342 | /* Check if we run on the same thread than the xreator thread. |
| 2343 | * We cannot access to the socket if the thread is different. |
| 2344 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2345 | if (socket->tid != tid) { |
| 2346 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2347 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2348 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2349 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2351 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2352 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2353 | lua_pushnil(L); |
| 2354 | lua_pushstring(L, "Can't connect"); |
| 2355 | return 2; |
| 2356 | } |
| 2357 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2358 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | |
| 2360 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2361 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2362 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | lua_pushinteger(L, 1); |
| 2364 | return 1; |
| 2365 | } |
| 2366 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2367 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2368 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2369 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2370 | } |
| 2371 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2372 | 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] | 2373 | return 0; |
| 2374 | } |
| 2375 | |
| 2376 | /* This function fail or initite the connection. */ |
| 2377 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2378 | { |
| 2379 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2380 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2381 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2382 | struct hlua *hlua; |
| 2383 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2384 | int low, high; |
| 2385 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2386 | struct xref *peer; |
| 2387 | struct stream_interface *si; |
| 2388 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2390 | if (lua_gettop(L) < 2) |
| 2391 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2392 | |
| 2393 | /* Get args. */ |
| 2394 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2395 | |
| 2396 | /* Check if we run on the same thread than the xreator thread. |
| 2397 | * We cannot access to the socket if the thread is different. |
| 2398 | */ |
| 2399 | if (socket->tid != tid) |
| 2400 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2401 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2402 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2403 | if (lua_gettop(L) >= 3) { |
| 2404 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2405 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2407 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2408 | * that are not enclosed within square brackets. |
| 2409 | */ |
| 2410 | if (port > 0) { |
| 2411 | luaL_buffinit(L, &b); |
| 2412 | luaL_addstring(&b, ip); |
| 2413 | luaL_addchar(&b, ':'); |
| 2414 | luaL_pushresult(&b); |
| 2415 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2416 | } |
| 2417 | } |
| 2418 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2419 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2421 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2422 | lua_pushnil(L); |
| 2423 | return 1; |
| 2424 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2425 | |
| 2426 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2427 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2428 | if (!addr) { |
| 2429 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2430 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2431 | } |
| 2432 | if (low != high) { |
| 2433 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2434 | 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] | 2435 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2436 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2437 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2438 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2439 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2440 | if (port == -1) { |
| 2441 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2442 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2444 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2445 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | if (port == -1) { |
| 2447 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2448 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2449 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2450 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2451 | } |
| 2452 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2453 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2454 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2455 | si = appctx->owner; |
| 2456 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2457 | |
| 2458 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2459 | xref_unlock(&socket->xref, peer); |
| 2460 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2461 | } |
| 2462 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2463 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2464 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2465 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2466 | |
| 2467 | /* inform the stream that we want to be notified whenever the |
| 2468 | * connection completes. |
| 2469 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2470 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2471 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2472 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2473 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2474 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2475 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2476 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2477 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2478 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2479 | } |
| 2480 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2481 | |
| 2482 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2483 | /* Return yield waiting for connection. */ |
| 2484 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2485 | 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] | 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2490 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2491 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2492 | { |
| 2493 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2494 | struct xref *peer; |
| 2495 | struct appctx *appctx; |
| 2496 | struct stream_interface *si; |
| 2497 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2498 | |
| 2499 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2500 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2501 | |
| 2502 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2503 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2504 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2505 | lua_pushnil(L); |
| 2506 | return 1; |
| 2507 | } |
| 2508 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2509 | si = appctx->owner; |
| 2510 | s = si_strm(si); |
| 2511 | |
| 2512 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2513 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2515 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2516 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2517 | |
| 2518 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2519 | { |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
| 2523 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2524 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2525 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2526 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2527 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2528 | struct xref *peer; |
| 2529 | struct appctx *appctx; |
| 2530 | struct stream_interface *si; |
| 2531 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2532 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2534 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2535 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2536 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2537 | /* convert the timeout to millis */ |
| 2538 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2539 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2540 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2541 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2542 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2543 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2544 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2545 | 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] | 2546 | |
| 2547 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2548 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2549 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2550 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2551 | /* Check if we run on the same thread than the xreator thread. |
| 2552 | * We cannot access to the socket if the thread is different. |
| 2553 | */ |
| 2554 | if (socket->tid != tid) |
| 2555 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2556 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2557 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2558 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2559 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2560 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2561 | WILL_LJMP(lua_error(L)); |
| 2562 | return 0; |
| 2563 | } |
| 2564 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2565 | si = appctx->owner; |
| 2566 | s = si_strm(si); |
| 2567 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2568 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2569 | s->req.rto = tmout; |
| 2570 | s->req.wto = tmout; |
| 2571 | s->res.rto = tmout; |
| 2572 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2573 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2574 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2575 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2576 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2577 | |
| 2578 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2579 | task_queue(s->task); |
| 2580 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2581 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2582 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2583 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2584 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2588 | { |
| 2589 | struct hlua_socket *socket; |
| 2590 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2591 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2592 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | |
| 2594 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2595 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | hlua_pusherror(L, "socket: full stack"); |
| 2597 | goto out_fail_conf; |
| 2598 | } |
| 2599 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2600 | /* Create the object: obj[0] = userdata. */ |
| 2601 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2602 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2603 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2605 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2607 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2608 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2609 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2610 | goto out_fail_conf; |
| 2611 | } |
| 2612 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2613 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2615 | lua_setmetatable(L, -2); |
| 2616 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2617 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2618 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2619 | if (!appctx) { |
| 2620 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2621 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2622 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2624 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2625 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2626 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2627 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2628 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2629 | /* Now create a session, task and stream for this applet */ |
| 2630 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2631 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2633 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2634 | } |
| 2635 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2636 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2637 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2638 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2639 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | } |
| 2641 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2642 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2643 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | /* Configure "right" stream interface. this "si" is used to connect |
| 2646 | * and retrieve data from the server. The connection is initialized |
| 2647 | * with the "struct server". |
| 2648 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2649 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | |
| 2651 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2652 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2653 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2655 | return 1; |
| 2656 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2657 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2658 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2659 | out_fail_sess: |
| 2660 | appctx_free(appctx); |
| 2661 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | WILL_LJMP(lua_error(L)); |
| 2663 | return 0; |
| 2664 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2665 | |
| 2666 | /* |
| 2667 | * |
| 2668 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2669 | * Class Channel |
| 2670 | * |
| 2671 | * |
| 2672 | */ |
| 2673 | |
| 2674 | /* Returns the struct hlua_channel join to the class channel in the |
| 2675 | * stack entry "ud" or throws an argument error. |
| 2676 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2677 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2678 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2679 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | } |
| 2681 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2682 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2683 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2684 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2685 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2686 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2688 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2689 | return 0; |
| 2690 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2691 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2692 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2693 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2694 | |
| 2695 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2696 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2697 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2698 | return 1; |
| 2699 | } |
| 2700 | |
| 2701 | /* Duplicate all the data present in the input channel and put it |
| 2702 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2703 | * the stack if the channel is closed and all the data are consumed, |
| 2704 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2705 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2706 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2707 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2708 | { |
| 2709 | char *blk1; |
| 2710 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2711 | size_t len1; |
| 2712 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2713 | int ret; |
| 2714 | luaL_Buffer b; |
| 2715 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2716 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2717 | if (unlikely(ret == 0)) |
| 2718 | return 0; |
| 2719 | |
| 2720 | if (unlikely(ret < 0)) { |
| 2721 | lua_pushnil(L); |
| 2722 | return -1; |
| 2723 | } |
| 2724 | |
| 2725 | luaL_buffinit(L, &b); |
| 2726 | luaL_addlstring(&b, blk1, len1); |
| 2727 | if (unlikely(ret == 2)) |
| 2728 | luaL_addlstring(&b, blk2, len2); |
| 2729 | luaL_pushresult(&b); |
| 2730 | |
| 2731 | if (unlikely(ret == 2)) |
| 2732 | return len1 + len2; |
| 2733 | return len1; |
| 2734 | } |
| 2735 | |
| 2736 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2737 | * a yield. This function keep the data in the buffer. |
| 2738 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2739 | __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] | 2740 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2741 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2742 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2743 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2744 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2745 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2746 | WILL_LJMP(lua_error(L)); |
| 2747 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2748 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2749 | 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] | 2750 | return 1; |
| 2751 | } |
| 2752 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2753 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2754 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2755 | { |
| 2756 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2757 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2758 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2759 | } |
| 2760 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2762 | * a yield. This function consumes the data in the buffer. It returns |
| 2763 | * a string containing the data or a nil pointer if no data are available |
| 2764 | * and the channel is closed. |
| 2765 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2766 | __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] | 2767 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2768 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2769 | int ret; |
| 2770 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2771 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2772 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2773 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2774 | WILL_LJMP(lua_error(L)); |
| 2775 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2776 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2777 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2778 | 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] | 2779 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2780 | if (unlikely(ret == -1)) |
| 2781 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2783 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2784 | return 1; |
| 2785 | } |
| 2786 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2787 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2788 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2789 | { |
| 2790 | MAY_LJMP(check_args(L, 1, "get")); |
| 2791 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2792 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2793 | } |
| 2794 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2795 | /* This functions consumes and returns one line. If the channel is closed, |
| 2796 | * 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] | 2797 | * 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] | 2798 | * value. |
| 2799 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2800 | __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] | 2801 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2802 | char *blk1; |
| 2803 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2804 | size_t len1; |
| 2805 | size_t len2; |
| 2806 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2807 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2808 | int ret; |
| 2809 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2810 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2811 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2812 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2813 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2814 | WILL_LJMP(lua_error(L)); |
| 2815 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2816 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2817 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2818 | 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] | 2819 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2820 | if (ret == -1) { |
| 2821 | lua_pushnil(L); |
| 2822 | return 1; |
| 2823 | } |
| 2824 | |
| 2825 | luaL_buffinit(L, &b); |
| 2826 | luaL_addlstring(&b, blk1, len1); |
| 2827 | len = len1; |
| 2828 | if (unlikely(ret == 2)) { |
| 2829 | luaL_addlstring(&b, blk2, len2); |
| 2830 | len += len2; |
| 2831 | } |
| 2832 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2833 | 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] | 2834 | return 1; |
| 2835 | } |
| 2836 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2837 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2838 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2839 | { |
| 2840 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2841 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2842 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2843 | } |
| 2844 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | /* This function takes a string as input, and append it at the |
| 2846 | * input side of channel. If the data is too big, but a space |
| 2847 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2848 | * yields. If the data is bigger than the buffer, or if the |
| 2849 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2850 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2851 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2852 | __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] | 2853 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2854 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2855 | size_t len; |
| 2856 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2857 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2858 | int ret; |
| 2859 | int max; |
| 2860 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2861 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2862 | WILL_LJMP(lua_error(L)); |
| 2863 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2864 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2865 | * the request buffer if its not required. |
| 2866 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2867 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2868 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2869 | 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] | 2870 | } |
| 2871 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2872 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2873 | if (max > len - l) |
| 2874 | max = len - l; |
| 2875 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2876 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2877 | if (ret == -2 || ret == -3) { |
| 2878 | lua_pushinteger(L, -1); |
| 2879 | return 1; |
| 2880 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2881 | if (ret == -1) { |
| 2882 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2883 | 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] | 2884 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2885 | l += ret; |
| 2886 | lua_pop(L, 1); |
| 2887 | lua_pushinteger(L, l); |
| 2888 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2889 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2890 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2891 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2893 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | */ |
| 2895 | return 1; |
| 2896 | } |
| 2897 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2898 | 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] | 2899 | return 1; |
| 2900 | } |
| 2901 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2902 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2903 | * 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] | 2904 | * buffer size is too little for the data. |
| 2905 | */ |
| 2906 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2907 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2908 | size_t len; |
| 2909 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2910 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2911 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2912 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2913 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | lua_pushinteger(L, 0); |
| 2915 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2916 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2917 | } |
| 2918 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2919 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2921 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | * or -1 if the channel is closed or if the buffer size is too |
| 2923 | * little for the data. |
| 2924 | */ |
| 2925 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2926 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2927 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2928 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2929 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2930 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | lua_pushinteger(L, 0); |
| 2932 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2933 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2934 | WILL_LJMP(lua_error(L)); |
| 2935 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2936 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2938 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2939 | } |
| 2940 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2941 | /* Append data in the output side of the buffer. This data is immediately |
| 2942 | * sent. The function returns the amount of data written. If the buffer |
| 2943 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2944 | * if the channel is closed. |
| 2945 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2946 | __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] | 2947 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2948 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | size_t len; |
| 2950 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2951 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2952 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2953 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2954 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2955 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2956 | WILL_LJMP(lua_error(L)); |
| 2957 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2958 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2959 | lua_pushinteger(L, -1); |
| 2960 | return 1; |
| 2961 | } |
| 2962 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2963 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2964 | * the request buffer if its not required. |
| 2965 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2966 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2967 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2968 | 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] | 2969 | } |
| 2970 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2971 | /* The written data will be immediately sent, so we can check |
| 2972 | * the available space without taking in account the reserve. |
| 2973 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2974 | * data, because the buffer will be flushed. |
| 2975 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2976 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2977 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2978 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2979 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2980 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2981 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2982 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2983 | return 1; |
| 2984 | |
| 2985 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2986 | if (max > len - l) |
| 2987 | max = len - l; |
| 2988 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2989 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2990 | * detects a non contiguous buffer and realign it. |
| 2991 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 2992 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2993 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2994 | |
| 2995 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2996 | 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] | 2997 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | /* buffer replace considers that the input part is filled. |
| 2999 | * so, I must forward these new data in the output part. |
| 3000 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3001 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | |
| 3003 | l += max; |
| 3004 | lua_pop(L, 1); |
| 3005 | lua_pushinteger(L, l); |
| 3006 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3007 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3008 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3009 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3010 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3011 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3012 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3014 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3015 | if (l < len) { |
| 3016 | /* If we are waiting for space in the response buffer, we |
| 3017 | * must set the flag WAKERESWR. This flag required the task |
| 3018 | * wake up if any activity is detected on the response buffer. |
| 3019 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3020 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3021 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3022 | else |
| 3023 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3024 | 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] | 3025 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | |
| 3027 | return 1; |
| 3028 | } |
| 3029 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3030 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | * yield the LUA process, and resume it without checking the |
| 3032 | * input arguments. |
| 3033 | */ |
| 3034 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3035 | { |
| 3036 | MAY_LJMP(check_args(L, 2, "send")); |
| 3037 | lua_pushinteger(L, 0); |
| 3038 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3039 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | /* This function forward and amount of butes. The data pass from |
| 3043 | * the input side of the buffer to the output side, and can be |
| 3044 | * forwarded. This function never fails. |
| 3045 | * |
| 3046 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3047 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3048 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3049 | __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] | 3050 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3051 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3052 | int len; |
| 3053 | int l; |
| 3054 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3055 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3056 | |
| 3057 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3058 | |
| 3059 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3060 | WILL_LJMP(lua_error(L)); |
| 3061 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3063 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3064 | |
| 3065 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3066 | if (max > ci_data(chn)) |
| 3067 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3068 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | l += max; |
| 3070 | |
| 3071 | lua_pop(L, 1); |
| 3072 | lua_pushinteger(L, l); |
| 3073 | |
| 3074 | /* Check if it miss bytes to forward. */ |
| 3075 | if (l < len) { |
| 3076 | /* The the input channel or the output channel are closed, we |
| 3077 | * must return the amount of data forwarded. |
| 3078 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3079 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3080 | return 1; |
| 3081 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3082 | /* If we are waiting for space data in the response buffer, we |
| 3083 | * must set the flag WAKERESWR. This flag required the task |
| 3084 | * wake up if any activity is detected on the response buffer. |
| 3085 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3086 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3087 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3088 | else |
| 3089 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3090 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3091 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3092 | 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] | 3093 | } |
| 3094 | |
| 3095 | return 1; |
| 3096 | } |
| 3097 | |
| 3098 | /* Just check the input and prepare the stack for the previous |
| 3099 | * function "hlua_channel_forward_yield" |
| 3100 | */ |
| 3101 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3102 | { |
| 3103 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3104 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3105 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3106 | |
| 3107 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3108 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | /* Just returns the number of bytes available in the input |
| 3112 | * side of the buffer. This function never fails. |
| 3113 | */ |
| 3114 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3115 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3116 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3117 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3118 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3119 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3120 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3121 | struct htx *htx = htxbuf(&chn->buf); |
| 3122 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3123 | } |
| 3124 | else |
| 3125 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | return 1; |
| 3127 | } |
| 3128 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3129 | /* Returns true if the channel is full. */ |
| 3130 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3131 | { |
| 3132 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3133 | |
| 3134 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3135 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3136 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3137 | * applet). |
| 3138 | */ |
| 3139 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3140 | return 1; |
| 3141 | } |
| 3142 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3143 | /* Returns true if the channel is the response channel. */ |
| 3144 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3145 | { |
| 3146 | struct channel *chn; |
| 3147 | |
| 3148 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3149 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3150 | |
| 3151 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3152 | return 1; |
| 3153 | } |
| 3154 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3155 | /* Just returns the number of bytes available in the output |
| 3156 | * side of the buffer. This function never fails. |
| 3157 | */ |
| 3158 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3159 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3160 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3161 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3163 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3164 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3165 | return 1; |
| 3166 | } |
| 3167 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3168 | /* |
| 3169 | * |
| 3170 | * |
| 3171 | * Class Fetches |
| 3172 | * |
| 3173 | * |
| 3174 | */ |
| 3175 | |
| 3176 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3177 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3178 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3179 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3180 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3181 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | /* This function creates and push in the stack a fetch object according |
| 3185 | * with a current TXN. |
| 3186 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3187 | 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] | 3188 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3189 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3190 | |
| 3191 | /* Check stack size. */ |
| 3192 | if (!lua_checkstack(L, 3)) |
| 3193 | return 0; |
| 3194 | |
| 3195 | /* Create the object: obj[0] = userdata. |
| 3196 | * Note that the base of the Fetches object is the |
| 3197 | * transaction object. |
| 3198 | */ |
| 3199 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3200 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3201 | lua_rawseti(L, -2, 0); |
| 3202 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3203 | hsmp->s = txn->s; |
| 3204 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3205 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3206 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3207 | |
| 3208 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3209 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3210 | lua_setmetatable(L, -2); |
| 3211 | |
| 3212 | return 1; |
| 3213 | } |
| 3214 | |
| 3215 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3216 | * It uses closure argument to store the associated sample-fetch. It |
| 3217 | * returns only one argument or throws an error. An error is thrown |
| 3218 | * only if an error is encountered during the argument parsing. If |
| 3219 | * the "sample-fetch" function fails, nil is returned. |
| 3220 | */ |
| 3221 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3222 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3223 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3224 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3225 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3226 | int i; |
| 3227 | struct sample smp; |
| 3228 | |
| 3229 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3230 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3231 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3232 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3233 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3234 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3235 | /* Check execution authorization. */ |
| 3236 | if (f->use & SMP_USE_HTTP_ANY && |
| 3237 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3238 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3239 | "is not available in Lua services", f->kw); |
| 3240 | WILL_LJMP(lua_error(L)); |
| 3241 | } |
| 3242 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3243 | /* Get extra arguments. */ |
| 3244 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3245 | if (i >= ARGM_NBARGS) |
| 3246 | break; |
| 3247 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3248 | } |
| 3249 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3250 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3251 | |
| 3252 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3253 | 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] | 3254 | |
| 3255 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3256 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3257 | lua_pushfstring(L, "error in arguments"); |
| 3258 | WILL_LJMP(lua_error(L)); |
| 3259 | } |
| 3260 | |
| 3261 | /* Initialise the sample. */ |
| 3262 | memset(&smp, 0, sizeof(smp)); |
| 3263 | |
| 3264 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3265 | 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] | 3266 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3267 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3268 | lua_pushstring(L, ""); |
| 3269 | else |
| 3270 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3271 | return 1; |
| 3272 | } |
| 3273 | |
| 3274 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3275 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3276 | hlua_smp2lua_str(L, &smp); |
| 3277 | else |
| 3278 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3279 | return 1; |
| 3280 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3281 | |
| 3282 | /* |
| 3283 | * |
| 3284 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3285 | * Class Converters |
| 3286 | * |
| 3287 | * |
| 3288 | */ |
| 3289 | |
| 3290 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3291 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3292 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3293 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3294 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3295 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | /* This function creates and push in the stack a Converters object |
| 3299 | * according with a current TXN. |
| 3300 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3301 | 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] | 3302 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3303 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3304 | |
| 3305 | /* Check stack size. */ |
| 3306 | if (!lua_checkstack(L, 3)) |
| 3307 | return 0; |
| 3308 | |
| 3309 | /* Create the object: obj[0] = userdata. |
| 3310 | * Note that the base of the Converters object is the |
| 3311 | * same than the TXN object. |
| 3312 | */ |
| 3313 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3314 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3315 | lua_rawseti(L, -2, 0); |
| 3316 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3317 | hsmp->s = txn->s; |
| 3318 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3319 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3320 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3321 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3322 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3323 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3324 | lua_setmetatable(L, -2); |
| 3325 | |
| 3326 | return 1; |
| 3327 | } |
| 3328 | |
| 3329 | /* This function is an LUA binding. It is called with each converter. |
| 3330 | * It uses closure argument to store the associated converter. It |
| 3331 | * returns only one argument or throws an error. An error is thrown |
| 3332 | * only if an error is encountered during the argument parsing. If |
| 3333 | * the converter function function fails, nil is returned. |
| 3334 | */ |
| 3335 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3336 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3337 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3338 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3339 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3340 | int i; |
| 3341 | struct sample smp; |
| 3342 | |
| 3343 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3344 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3345 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3346 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3347 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3348 | |
| 3349 | /* Get extra arguments. */ |
| 3350 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3351 | if (i >= ARGM_NBARGS) |
| 3352 | break; |
| 3353 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3354 | } |
| 3355 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3356 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3357 | |
| 3358 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3359 | 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] | 3360 | |
| 3361 | /* Run the special args checker. */ |
| 3362 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3363 | hlua_pusherror(L, "error in arguments"); |
| 3364 | WILL_LJMP(lua_error(L)); |
| 3365 | } |
| 3366 | |
| 3367 | /* Initialise the sample. */ |
| 3368 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3369 | hlua_pusherror(L, "error in the input argument"); |
| 3370 | WILL_LJMP(lua_error(L)); |
| 3371 | } |
| 3372 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3373 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3374 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3375 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3376 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3377 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3378 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3379 | WILL_LJMP(lua_error(L)); |
| 3380 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3381 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3382 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3383 | hlua_pusherror(L, "error during the input argument casting"); |
| 3384 | WILL_LJMP(lua_error(L)); |
| 3385 | } |
| 3386 | |
| 3387 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3388 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3389 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3390 | lua_pushstring(L, ""); |
| 3391 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3392 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3393 | return 1; |
| 3394 | } |
| 3395 | |
| 3396 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3397 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3398 | hlua_smp2lua_str(L, &smp); |
| 3399 | else |
| 3400 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3401 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3402 | } |
| 3403 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3404 | /* |
| 3405 | * |
| 3406 | * |
| 3407 | * Class AppletTCP |
| 3408 | * |
| 3409 | * |
| 3410 | */ |
| 3411 | |
| 3412 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3413 | * a class stream, otherwise it throws an error. |
| 3414 | */ |
| 3415 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3416 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3417 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | /* This function creates and push in the stack an Applet object |
| 3421 | * according with a current TXN. |
| 3422 | */ |
| 3423 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3424 | { |
| 3425 | struct hlua_appctx *appctx; |
| 3426 | struct stream_interface *si = ctx->owner; |
| 3427 | struct stream *s = si_strm(si); |
| 3428 | struct proxy *p = s->be; |
| 3429 | |
| 3430 | /* Check stack size. */ |
| 3431 | if (!lua_checkstack(L, 3)) |
| 3432 | return 0; |
| 3433 | |
| 3434 | /* Create the object: obj[0] = userdata. |
| 3435 | * Note that the base of the Converters object is the |
| 3436 | * same than the TXN object. |
| 3437 | */ |
| 3438 | lua_newtable(L); |
| 3439 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3440 | lua_rawseti(L, -2, 0); |
| 3441 | appctx->appctx = ctx; |
| 3442 | appctx->htxn.s = s; |
| 3443 | appctx->htxn.p = p; |
| 3444 | |
| 3445 | /* Create the "f" field that contains a list of fetches. */ |
| 3446 | lua_pushstring(L, "f"); |
| 3447 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3448 | return 0; |
| 3449 | lua_settable(L, -3); |
| 3450 | |
| 3451 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3452 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3453 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3454 | return 0; |
| 3455 | lua_settable(L, -3); |
| 3456 | |
| 3457 | /* Create the "c" field that contains a list of converters. */ |
| 3458 | lua_pushstring(L, "c"); |
| 3459 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3460 | return 0; |
| 3461 | lua_settable(L, -3); |
| 3462 | |
| 3463 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3464 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3465 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3466 | return 0; |
| 3467 | lua_settable(L, -3); |
| 3468 | |
| 3469 | /* Pop a class stream metatable and affect it to the table. */ |
| 3470 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3471 | lua_setmetatable(L, -2); |
| 3472 | |
| 3473 | return 1; |
| 3474 | } |
| 3475 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3476 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3477 | { |
| 3478 | struct hlua_appctx *appctx; |
| 3479 | struct stream *s; |
| 3480 | const char *name; |
| 3481 | size_t len; |
| 3482 | struct sample smp; |
| 3483 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3484 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3485 | 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] | 3486 | |
| 3487 | /* It is useles to retrieve the stream, but this function |
| 3488 | * runs only in a stream context. |
| 3489 | */ |
| 3490 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3491 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3492 | s = appctx->htxn.s; |
| 3493 | |
| 3494 | /* Converts the third argument in a sample. */ |
| 3495 | hlua_lua2smp(L, 3, &smp); |
| 3496 | |
| 3497 | /* Store the sample in a variable. */ |
| 3498 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3499 | |
| 3500 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3501 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3502 | else |
| 3503 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3504 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3505 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3509 | { |
| 3510 | struct hlua_appctx *appctx; |
| 3511 | struct stream *s; |
| 3512 | const char *name; |
| 3513 | size_t len; |
| 3514 | struct sample smp; |
| 3515 | |
| 3516 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3517 | |
| 3518 | /* It is useles to retrieve the stream, but this function |
| 3519 | * runs only in a stream context. |
| 3520 | */ |
| 3521 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3522 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3523 | s = appctx->htxn.s; |
| 3524 | |
| 3525 | /* Unset the variable. */ |
| 3526 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3527 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3528 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3532 | { |
| 3533 | struct hlua_appctx *appctx; |
| 3534 | struct stream *s; |
| 3535 | const char *name; |
| 3536 | size_t len; |
| 3537 | struct sample smp; |
| 3538 | |
| 3539 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3540 | |
| 3541 | /* It is useles to retrieve the stream, but this function |
| 3542 | * runs only in a stream context. |
| 3543 | */ |
| 3544 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3545 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3546 | s = appctx->htxn.s; |
| 3547 | |
| 3548 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3549 | if (!vars_get_by_name(name, len, &smp)) { |
| 3550 | lua_pushnil(L); |
| 3551 | return 1; |
| 3552 | } |
| 3553 | |
| 3554 | return hlua_smp2lua(L, &smp); |
| 3555 | } |
| 3556 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3557 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3558 | { |
| 3559 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3560 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3561 | struct hlua *hlua; |
| 3562 | |
| 3563 | /* 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] | 3564 | if (!s->hlua) |
| 3565 | return 0; |
| 3566 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3567 | |
| 3568 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3569 | |
| 3570 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3571 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3572 | |
| 3573 | /* Get and store new value. */ |
| 3574 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3575 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3576 | |
| 3577 | return 0; |
| 3578 | } |
| 3579 | |
| 3580 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3581 | { |
| 3582 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3583 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3584 | struct hlua *hlua; |
| 3585 | |
| 3586 | /* 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] | 3587 | if (!s->hlua) { |
| 3588 | lua_pushnil(L); |
| 3589 | return 1; |
| 3590 | } |
| 3591 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3592 | |
| 3593 | /* Push configuration index in the stack. */ |
| 3594 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3595 | |
| 3596 | return 1; |
| 3597 | } |
| 3598 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3599 | /* If expected data not yet available, it returns a yield. This function |
| 3600 | * consumes the data in the buffer. It returns a string containing the |
| 3601 | * data. This string can be empty. |
| 3602 | */ |
| 3603 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3604 | { |
| 3605 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3606 | struct stream_interface *si = appctx->appctx->owner; |
| 3607 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3608 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3609 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3610 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3611 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3612 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3613 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3614 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3615 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3616 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3617 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3618 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3619 | 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] | 3620 | } |
| 3621 | |
| 3622 | /* End of data: commit the total strings and return. */ |
| 3623 | if (ret < 0) { |
| 3624 | luaL_pushresult(&appctx->b); |
| 3625 | return 1; |
| 3626 | } |
| 3627 | |
| 3628 | /* Ensure that the block 2 length is usable. */ |
| 3629 | if (ret == 1) |
| 3630 | len2 = 0; |
| 3631 | |
| 3632 | /* dont check the max length read and dont check. */ |
| 3633 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3634 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3635 | |
| 3636 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3637 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3638 | luaL_pushresult(&appctx->b); |
| 3639 | return 1; |
| 3640 | } |
| 3641 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3642 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3643 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3644 | { |
| 3645 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3646 | |
| 3647 | /* Initialise the string catenation. */ |
| 3648 | luaL_buffinit(L, &appctx->b); |
| 3649 | |
| 3650 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3651 | } |
| 3652 | |
| 3653 | /* If expected data not yet available, it returns a yield. This function |
| 3654 | * consumes the data in the buffer. It returns a string containing the |
| 3655 | * data. This string can be empty. |
| 3656 | */ |
| 3657 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3658 | { |
| 3659 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3660 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3661 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3662 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3663 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3664 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3665 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3666 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3667 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3668 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3669 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3670 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3671 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3672 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3673 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3674 | 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] | 3675 | } |
| 3676 | |
| 3677 | /* End of data: commit the total strings and return. */ |
| 3678 | if (ret < 0) { |
| 3679 | luaL_pushresult(&appctx->b); |
| 3680 | return 1; |
| 3681 | } |
| 3682 | |
| 3683 | /* Ensure that the block 2 length is usable. */ |
| 3684 | if (ret == 1) |
| 3685 | len2 = 0; |
| 3686 | |
| 3687 | if (len == -1) { |
| 3688 | |
| 3689 | /* If len == -1, catenate all the data avalaile and |
| 3690 | * yield because we want to get all the data until |
| 3691 | * the end of data stream. |
| 3692 | */ |
| 3693 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3694 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3695 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3696 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3697 | 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] | 3698 | |
| 3699 | } else { |
| 3700 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3701 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3702 | if (len1 > len) |
| 3703 | len1 = len; |
| 3704 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3705 | len -= len1; |
| 3706 | |
| 3707 | /* Copy the second block. */ |
| 3708 | if (len2 > len) |
| 3709 | len2 = len; |
| 3710 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3711 | len -= len2; |
| 3712 | |
| 3713 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3714 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3715 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3716 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3717 | if (len > 0) { |
| 3718 | lua_pushinteger(L, len); |
| 3719 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3720 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3721 | 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] | 3722 | } |
| 3723 | |
| 3724 | /* return the result. */ |
| 3725 | luaL_pushresult(&appctx->b); |
| 3726 | return 1; |
| 3727 | } |
| 3728 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3729 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3730 | hlua_pusherror(L, "Lua: internal error"); |
| 3731 | WILL_LJMP(lua_error(L)); |
| 3732 | return 0; |
| 3733 | } |
| 3734 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3735 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3736 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3737 | { |
| 3738 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3739 | int len = -1; |
| 3740 | |
| 3741 | if (lua_gettop(L) > 2) |
| 3742 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3743 | if (lua_gettop(L) >= 2) { |
| 3744 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3745 | lua_pop(L, 1); |
| 3746 | } |
| 3747 | |
| 3748 | /* Confirm or set the required length */ |
| 3749 | lua_pushinteger(L, len); |
| 3750 | |
| 3751 | /* Initialise the string catenation. */ |
| 3752 | luaL_buffinit(L, &appctx->b); |
| 3753 | |
| 3754 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3755 | } |
| 3756 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3757 | /* Append data in the output side of the buffer. This data is immediately |
| 3758 | * sent. The function returns the amount of data written. If the buffer |
| 3759 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3760 | * if the channel is closed. |
| 3761 | */ |
| 3762 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3763 | { |
| 3764 | size_t len; |
| 3765 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3766 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3767 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3768 | struct stream_interface *si = appctx->appctx->owner; |
| 3769 | struct channel *chn = si_ic(si); |
| 3770 | int max; |
| 3771 | |
| 3772 | /* Get the max amount of data which can write as input in the channel. */ |
| 3773 | max = channel_recv_max(chn); |
| 3774 | if (max > (len - l)) |
| 3775 | max = len - l; |
| 3776 | |
| 3777 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3778 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3779 | |
| 3780 | /* update counters. */ |
| 3781 | l += max; |
| 3782 | lua_pop(L, 1); |
| 3783 | lua_pushinteger(L, l); |
| 3784 | |
| 3785 | /* If some data is not send, declares the situation to the |
| 3786 | * applet, and returns a yield. |
| 3787 | */ |
| 3788 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3789 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3790 | 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] | 3791 | } |
| 3792 | |
| 3793 | return 1; |
| 3794 | } |
| 3795 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3796 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3797 | * yield the LUA process, and resume it without checking the |
| 3798 | * input arguments. |
| 3799 | */ |
| 3800 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3801 | { |
| 3802 | MAY_LJMP(check_args(L, 2, "send")); |
| 3803 | lua_pushinteger(L, 0); |
| 3804 | |
| 3805 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3806 | } |
| 3807 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3808 | /* |
| 3809 | * |
| 3810 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3811 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3812 | * |
| 3813 | * |
| 3814 | */ |
| 3815 | |
| 3816 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3817 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3818 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3819 | __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] | 3820 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3821 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3822 | } |
| 3823 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3824 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3825 | * according with a current TXN. |
| 3826 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3827 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3828 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3829 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3830 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3831 | struct stream_interface *si = ctx->owner; |
| 3832 | struct stream *s = si_strm(si); |
| 3833 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3834 | struct htx *htx; |
| 3835 | struct htx_blk *blk; |
| 3836 | struct htx_sl *sl; |
| 3837 | struct ist path; |
| 3838 | unsigned long long len = 0; |
| 3839 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3840 | |
| 3841 | /* Check stack size. */ |
| 3842 | if (!lua_checkstack(L, 3)) |
| 3843 | return 0; |
| 3844 | |
| 3845 | /* Create the object: obj[0] = userdata. |
| 3846 | * Note that the base of the Converters object is the |
| 3847 | * same than the TXN object. |
| 3848 | */ |
| 3849 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3850 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3851 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3852 | appctx->appctx = ctx; |
| 3853 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3854 | 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] | 3855 | appctx->htxn.s = s; |
| 3856 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3857 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3858 | /* Create the "f" field that contains a list of fetches. */ |
| 3859 | lua_pushstring(L, "f"); |
| 3860 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3861 | return 0; |
| 3862 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3863 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3864 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3865 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3866 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3867 | return 0; |
| 3868 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3869 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3870 | /* Create the "c" field that contains a list of converters. */ |
| 3871 | lua_pushstring(L, "c"); |
| 3872 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3873 | return 0; |
| 3874 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3875 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3876 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3877 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3878 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3879 | return 0; |
| 3880 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3881 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3882 | htx = htxbuf(&s->req.buf); |
| 3883 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3884 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3885 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3886 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3887 | /* Stores the request method. */ |
| 3888 | lua_pushstring(L, "method"); |
| 3889 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3890 | lua_settable(L, -3); |
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 http version. */ |
| 3893 | lua_pushstring(L, "version"); |
| 3894 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(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 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3898 | * the array on the top of the stack. |
| 3899 | */ |
| 3900 | lua_pushstring(L, "headers"); |
| 3901 | htxn.s = s; |
| 3902 | htxn.p = px; |
| 3903 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 3904 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3905 | return 0; |
| 3906 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3907 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3908 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3909 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3910 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3911 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3912 | p = path.ptr; |
| 3913 | end = path.ptr + path.len; |
| 3914 | q = p; |
| 3915 | while (q < end && *q != '?') |
| 3916 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3917 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3918 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3919 | lua_pushstring(L, "path"); |
| 3920 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3921 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3922 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3923 | /* Stores the query string. */ |
| 3924 | lua_pushstring(L, "qs"); |
| 3925 | if (*q == '?') |
| 3926 | q++; |
| 3927 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3928 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3929 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3930 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3931 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3932 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3933 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3934 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3935 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3936 | break; |
| 3937 | if (type == HTX_BLK_DATA) |
| 3938 | len += htx_get_blksz(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 (htx->extra != ULLONG_MAX) |
| 3941 | len += htx->extra; |
| 3942 | |
| 3943 | /* Stores the request path. */ |
| 3944 | lua_pushstring(L, "length"); |
| 3945 | lua_pushinteger(L, len); |
| 3946 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3947 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3948 | /* Create an empty array of HTTP request headers. */ |
| 3949 | lua_pushstring(L, "response"); |
| 3950 | lua_newtable(L); |
| 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 | /* Pop a class stream metatable and affect it to the table. */ |
| 3954 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3955 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3956 | |
| 3957 | return 1; |
| 3958 | } |
| 3959 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3960 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3961 | { |
| 3962 | struct hlua_appctx *appctx; |
| 3963 | struct stream *s; |
| 3964 | const char *name; |
| 3965 | size_t len; |
| 3966 | struct sample smp; |
| 3967 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3968 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3969 | 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] | 3970 | |
| 3971 | /* It is useles to retrieve the stream, but this function |
| 3972 | * runs only in a stream context. |
| 3973 | */ |
| 3974 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3975 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3976 | s = appctx->htxn.s; |
| 3977 | |
| 3978 | /* Converts the third argument in a sample. */ |
| 3979 | hlua_lua2smp(L, 3, &smp); |
| 3980 | |
| 3981 | /* Store the sample in a variable. */ |
| 3982 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3983 | |
| 3984 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3985 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3986 | else |
| 3987 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3988 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3989 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3993 | { |
| 3994 | struct hlua_appctx *appctx; |
| 3995 | struct stream *s; |
| 3996 | const char *name; |
| 3997 | size_t len; |
| 3998 | struct sample smp; |
| 3999 | |
| 4000 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4001 | |
| 4002 | /* It is useles to retrieve the stream, but this function |
| 4003 | * runs only in a stream context. |
| 4004 | */ |
| 4005 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4006 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4007 | s = appctx->htxn.s; |
| 4008 | |
| 4009 | /* Unset the variable. */ |
| 4010 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4011 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4012 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4016 | { |
| 4017 | struct hlua_appctx *appctx; |
| 4018 | struct stream *s; |
| 4019 | const char *name; |
| 4020 | size_t len; |
| 4021 | struct sample smp; |
| 4022 | |
| 4023 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4024 | |
| 4025 | /* It is useles to retrieve the stream, but this function |
| 4026 | * runs only in a stream context. |
| 4027 | */ |
| 4028 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4029 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4030 | s = appctx->htxn.s; |
| 4031 | |
| 4032 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4033 | if (!vars_get_by_name(name, len, &smp)) { |
| 4034 | lua_pushnil(L); |
| 4035 | return 1; |
| 4036 | } |
| 4037 | |
| 4038 | return hlua_smp2lua(L, &smp); |
| 4039 | } |
| 4040 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4041 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4042 | { |
| 4043 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4044 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4045 | struct hlua *hlua; |
| 4046 | |
| 4047 | /* 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] | 4048 | if (!s->hlua) |
| 4049 | return 0; |
| 4050 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4051 | |
| 4052 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4053 | |
| 4054 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4055 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4056 | |
| 4057 | /* Get and store new value. */ |
| 4058 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4059 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4060 | |
| 4061 | return 0; |
| 4062 | } |
| 4063 | |
| 4064 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4065 | { |
| 4066 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4067 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4068 | struct hlua *hlua; |
| 4069 | |
| 4070 | /* 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] | 4071 | if (!s->hlua) { |
| 4072 | lua_pushnil(L); |
| 4073 | return 1; |
| 4074 | } |
| 4075 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4076 | |
| 4077 | /* Push configuration index in the stack. */ |
| 4078 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4079 | |
| 4080 | return 1; |
| 4081 | } |
| 4082 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4083 | /* If expected data not yet available, it returns a yield. This function |
| 4084 | * consumes the data in the buffer. It returns a string containing the |
| 4085 | * data. This string can be empty. |
| 4086 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4087 | __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] | 4088 | { |
| 4089 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4090 | struct stream_interface *si = appctx->appctx->owner; |
| 4091 | struct channel *req = si_oc(si); |
| 4092 | struct htx *htx; |
| 4093 | struct htx_blk *blk; |
| 4094 | size_t count; |
| 4095 | int stop = 0; |
| 4096 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4097 | htx = htx_from_buf(&req->buf); |
| 4098 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4099 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4100 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4101 | while (count && !stop && blk) { |
| 4102 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4103 | uint32_t sz = htx_get_blksz(blk); |
| 4104 | struct ist v; |
| 4105 | uint32_t vlen; |
| 4106 | char *nl; |
| 4107 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4108 | if (type == HTX_BLK_EOM) { |
| 4109 | stop = 1; |
| 4110 | break; |
| 4111 | } |
| 4112 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4113 | vlen = sz; |
| 4114 | if (vlen > count) { |
| 4115 | if (type != HTX_BLK_DATA) |
| 4116 | break; |
| 4117 | vlen = count; |
| 4118 | } |
| 4119 | |
| 4120 | switch (type) { |
| 4121 | case HTX_BLK_UNUSED: |
| 4122 | break; |
| 4123 | |
| 4124 | case HTX_BLK_DATA: |
| 4125 | v = htx_get_blk_value(htx, blk); |
| 4126 | v.len = vlen; |
| 4127 | nl = istchr(v, '\n'); |
| 4128 | if (nl != NULL) { |
| 4129 | stop = 1; |
| 4130 | vlen = nl - v.ptr + 1; |
| 4131 | } |
| 4132 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4133 | break; |
| 4134 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4135 | case HTX_BLK_TLR: |
| 4136 | case HTX_BLK_EOM: |
| 4137 | stop = 1; |
| 4138 | break; |
| 4139 | |
| 4140 | default: |
| 4141 | break; |
| 4142 | } |
| 4143 | |
| 4144 | co_set_data(req, co_data(req) - vlen); |
| 4145 | count -= vlen; |
| 4146 | if (sz == vlen) |
| 4147 | blk = htx_remove_blk(htx, blk); |
| 4148 | else { |
| 4149 | htx_cut_data_blk(htx, blk, vlen); |
| 4150 | break; |
| 4151 | } |
| 4152 | } |
| 4153 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4154 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4155 | if (!stop) { |
| 4156 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4157 | 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] | 4158 | } |
| 4159 | |
| 4160 | /* return the result. */ |
| 4161 | luaL_pushresult(&appctx->b); |
| 4162 | return 1; |
| 4163 | } |
| 4164 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4165 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4166 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4167 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4168 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4169 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4170 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4171 | /* Initialise the string catenation. */ |
| 4172 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4173 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4174 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4175 | } |
| 4176 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4177 | /* If expected data not yet available, it returns a yield. This function |
| 4178 | * consumes the data in the buffer. It returns a string containing the |
| 4179 | * data. This string can be empty. |
| 4180 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4181 | __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] | 4182 | { |
| 4183 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4184 | struct stream_interface *si = appctx->appctx->owner; |
| 4185 | struct channel *req = si_oc(si); |
| 4186 | struct htx *htx; |
| 4187 | struct htx_blk *blk; |
| 4188 | size_t count; |
| 4189 | int len; |
| 4190 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4191 | htx = htx_from_buf(&req->buf); |
| 4192 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4193 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4194 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4195 | while (count && len && blk) { |
| 4196 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4197 | uint32_t sz = htx_get_blksz(blk); |
| 4198 | struct ist v; |
| 4199 | uint32_t vlen; |
| 4200 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4201 | if (type == HTX_BLK_EOM) { |
| 4202 | len = 0; |
| 4203 | break; |
| 4204 | } |
| 4205 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4206 | vlen = sz; |
| 4207 | if (len > 0 && vlen > len) |
| 4208 | vlen = len; |
| 4209 | if (vlen > count) { |
| 4210 | if (type != HTX_BLK_DATA) |
| 4211 | break; |
| 4212 | vlen = count; |
| 4213 | } |
| 4214 | |
| 4215 | switch (type) { |
| 4216 | case HTX_BLK_UNUSED: |
| 4217 | break; |
| 4218 | |
| 4219 | case HTX_BLK_DATA: |
| 4220 | v = htx_get_blk_value(htx, blk); |
| 4221 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4222 | break; |
| 4223 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4224 | case HTX_BLK_TLR: |
| 4225 | case HTX_BLK_EOM: |
| 4226 | len = 0; |
| 4227 | break; |
| 4228 | |
| 4229 | default: |
| 4230 | break; |
| 4231 | } |
| 4232 | |
| 4233 | co_set_data(req, co_data(req) - vlen); |
| 4234 | count -= vlen; |
| 4235 | if (len > 0) |
| 4236 | len -= vlen; |
| 4237 | if (sz == vlen) |
| 4238 | blk = htx_remove_blk(htx, blk); |
| 4239 | else { |
| 4240 | htx_cut_data_blk(htx, blk, vlen); |
| 4241 | break; |
| 4242 | } |
| 4243 | } |
| 4244 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4245 | htx_to_buf(htx, &req->buf); |
| 4246 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4247 | /* If we are no other data available, yield waiting for new data. */ |
| 4248 | if (len) { |
| 4249 | if (len > 0) { |
| 4250 | lua_pushinteger(L, len); |
| 4251 | lua_replace(L, 2); |
| 4252 | } |
| 4253 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4254 | 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] | 4255 | } |
| 4256 | |
| 4257 | /* return the result. */ |
| 4258 | luaL_pushresult(&appctx->b); |
| 4259 | return 1; |
| 4260 | } |
| 4261 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4262 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4263 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4264 | { |
| 4265 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4266 | int len = -1; |
| 4267 | |
| 4268 | /* Check arguments. */ |
| 4269 | if (lua_gettop(L) > 2) |
| 4270 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4271 | if (lua_gettop(L) >= 2) { |
| 4272 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4273 | lua_pop(L, 1); |
| 4274 | } |
| 4275 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4276 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4277 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4278 | /* Initialise the string catenation. */ |
| 4279 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4280 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4281 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | /* Append data in the output side of the buffer. This data is immediately |
| 4285 | * sent. The function returns the amount of data written. If the buffer |
| 4286 | * cannot contain the data, the function yields. The function returns -1 |
| 4287 | * if the channel is closed. |
| 4288 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4289 | __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] | 4290 | { |
| 4291 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4292 | struct stream_interface *si = appctx->appctx->owner; |
| 4293 | struct channel *res = si_ic(si); |
| 4294 | struct htx *htx = htx_from_buf(&res->buf); |
| 4295 | const char *data; |
| 4296 | size_t len; |
| 4297 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4298 | int max; |
| 4299 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4300 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4301 | if (!max) |
| 4302 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4303 | |
| 4304 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4305 | |
| 4306 | /* Get the max amount of data which can write as input in the channel. */ |
| 4307 | if (max > (len - l)) |
| 4308 | max = len - l; |
| 4309 | |
| 4310 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4311 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4312 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4313 | |
| 4314 | /* update counters. */ |
| 4315 | l += max; |
| 4316 | lua_pop(L, 1); |
| 4317 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4318 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4319 | /* If some data is not send, declares the situation to the |
| 4320 | * applet, and returns a yield. |
| 4321 | */ |
| 4322 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4323 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4324 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4325 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4326 | 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] | 4327 | } |
| 4328 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4329 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4330 | return 1; |
| 4331 | } |
| 4332 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4333 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4334 | * yield the LUA process, and resume it without checking the |
| 4335 | * input arguments. |
| 4336 | */ |
| 4337 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4338 | { |
| 4339 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4340 | |
| 4341 | /* We want to send some data. Headers must be sent. */ |
| 4342 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4343 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4344 | WILL_LJMP(lua_error(L)); |
| 4345 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4346 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4347 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4348 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4349 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4350 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4354 | { |
| 4355 | const char *name; |
| 4356 | int ret; |
| 4357 | |
| 4358 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4359 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4360 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4361 | |
| 4362 | /* Push in the stack the "response" entry. */ |
| 4363 | ret = lua_getfield(L, 1, "response"); |
| 4364 | if (ret != LUA_TTABLE) { |
| 4365 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4366 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4367 | WILL_LJMP(lua_error(L)); |
| 4368 | } |
| 4369 | |
| 4370 | /* check if the header is already registered if it is not |
| 4371 | * the case, register it. |
| 4372 | */ |
| 4373 | ret = lua_getfield(L, -1, name); |
| 4374 | if (ret == LUA_TNIL) { |
| 4375 | |
| 4376 | /* Entry not found. */ |
| 4377 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4378 | |
| 4379 | /* Insert the new header name in the array in the top of the stack. |
| 4380 | * It left the new array in the top of the stack. |
| 4381 | */ |
| 4382 | lua_newtable(L); |
| 4383 | lua_pushvalue(L, 2); |
| 4384 | lua_pushvalue(L, -2); |
| 4385 | lua_settable(L, -4); |
| 4386 | |
| 4387 | } else if (ret != LUA_TTABLE) { |
| 4388 | |
| 4389 | /* corruption error. */ |
| 4390 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4391 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4392 | WILL_LJMP(lua_error(L)); |
| 4393 | } |
| 4394 | |
| 4395 | /* Now the top od thestack is an array of values. We push |
| 4396 | * the header value as new entry. |
| 4397 | */ |
| 4398 | lua_pushvalue(L, 3); |
| 4399 | ret = lua_rawlen(L, -2); |
| 4400 | lua_rawseti(L, -2, ret + 1); |
| 4401 | lua_pushboolean(L, 1); |
| 4402 | return 1; |
| 4403 | } |
| 4404 | |
| 4405 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4406 | { |
| 4407 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4408 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4409 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4410 | |
| 4411 | if (status < 100 || status > 599) { |
| 4412 | lua_pushboolean(L, 0); |
| 4413 | return 1; |
| 4414 | } |
| 4415 | |
| 4416 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4417 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4418 | lua_pushboolean(L, 1); |
| 4419 | return 1; |
| 4420 | } |
| 4421 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4422 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4423 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4424 | { |
| 4425 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4426 | struct stream_interface *si = appctx->appctx->owner; |
| 4427 | struct channel *res = si_ic(si); |
| 4428 | struct htx *htx; |
| 4429 | struct htx_sl *sl; |
| 4430 | struct h1m h1m; |
| 4431 | const char *status, *reason; |
| 4432 | const char *name, *value; |
| 4433 | size_t nlen, vlen; |
| 4434 | unsigned int flags; |
| 4435 | |
| 4436 | /* Send the message at once. */ |
| 4437 | htx = htx_from_buf(&res->buf); |
| 4438 | h1m_init_res(&h1m); |
| 4439 | |
| 4440 | /* Use the same http version than the request. */ |
| 4441 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4442 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4443 | if (reason == NULL) |
| 4444 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4445 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4446 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4447 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4448 | } |
| 4449 | else { |
| 4450 | flags = HTX_SL_F_IS_RESP; |
| 4451 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4452 | } |
| 4453 | if (!sl) { |
| 4454 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4455 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4456 | WILL_LJMP(lua_error(L)); |
| 4457 | } |
| 4458 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4459 | |
| 4460 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4461 | lua_pushvalue(L, 0); |
| 4462 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4463 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4464 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4465 | WILL_LJMP(lua_error(L)); |
| 4466 | } |
| 4467 | |
| 4468 | /* Browse the list of headers. */ |
| 4469 | lua_pushnil(L); |
| 4470 | while(lua_next(L, -2) != 0) { |
| 4471 | /* We expect a string as -2. */ |
| 4472 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4473 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4474 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4475 | lua_typename(L, lua_type(L, -2))); |
| 4476 | WILL_LJMP(lua_error(L)); |
| 4477 | } |
| 4478 | name = lua_tolstring(L, -2, &nlen); |
| 4479 | |
| 4480 | /* We expect an array as -1. */ |
| 4481 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4482 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4483 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4484 | name, |
| 4485 | lua_typename(L, lua_type(L, -1))); |
| 4486 | WILL_LJMP(lua_error(L)); |
| 4487 | } |
| 4488 | |
| 4489 | /* Browse the table who is on the top of the stack. */ |
| 4490 | lua_pushnil(L); |
| 4491 | while(lua_next(L, -2) != 0) { |
| 4492 | int id; |
| 4493 | |
| 4494 | /* We expect a number as -2. */ |
| 4495 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4496 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4497 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4498 | name, |
| 4499 | lua_typename(L, lua_type(L, -2))); |
| 4500 | WILL_LJMP(lua_error(L)); |
| 4501 | } |
| 4502 | id = lua_tointeger(L, -2); |
| 4503 | |
| 4504 | /* We expect a string as -2. */ |
| 4505 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4506 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4507 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4508 | name, id, |
| 4509 | lua_typename(L, lua_type(L, -1))); |
| 4510 | WILL_LJMP(lua_error(L)); |
| 4511 | } |
| 4512 | value = lua_tolstring(L, -1, &vlen); |
| 4513 | |
| 4514 | /* Simple Protocol checks. */ |
| 4515 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4516 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4517 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4518 | struct ist v = ist2(value, vlen); |
| 4519 | int ret; |
| 4520 | |
| 4521 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4522 | if (ret < 0) { |
| 4523 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4524 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4525 | name); |
| 4526 | WILL_LJMP(lua_error(L)); |
| 4527 | } |
| 4528 | else if (ret == 0) |
| 4529 | goto next; /* Skip it */ |
| 4530 | } |
| 4531 | |
| 4532 | /* Add a new header */ |
| 4533 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4534 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4535 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4536 | name); |
| 4537 | WILL_LJMP(lua_error(L)); |
| 4538 | } |
| 4539 | next: |
| 4540 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4541 | lua_pop(L, 1); |
| 4542 | } |
| 4543 | |
| 4544 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4545 | lua_pop(L, 1); |
| 4546 | } |
| 4547 | |
| 4548 | if (h1m.flags & H1_MF_CHNK) |
| 4549 | h1m.flags &= ~H1_MF_CLEN; |
| 4550 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4551 | h1m.flags |= H1_MF_XFER_LEN; |
| 4552 | |
| 4553 | /* Uset HTX start-line flags */ |
| 4554 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4555 | flags |= HTX_SL_F_XFER_ENC; |
| 4556 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4557 | flags |= HTX_SL_F_XFER_LEN; |
| 4558 | if (h1m.flags & H1_MF_CHNK) |
| 4559 | flags |= HTX_SL_F_CHNK; |
| 4560 | else if (h1m.flags & H1_MF_CLEN) |
| 4561 | flags |= HTX_SL_F_CLEN; |
| 4562 | if (h1m.body_len == 0) |
| 4563 | flags |= HTX_SL_F_BODYLESS; |
| 4564 | } |
| 4565 | sl->flags |= flags; |
| 4566 | |
| 4567 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4568 | * and the status code implies the presence of a message body, we must |
| 4569 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4570 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4571 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4572 | */ |
| 4573 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4574 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4575 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4576 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4577 | /* Add a new header */ |
| 4578 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4579 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4580 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4581 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4582 | WILL_LJMP(lua_error(L)); |
| 4583 | } |
| 4584 | } |
| 4585 | |
| 4586 | /* Finalize headers. */ |
| 4587 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4588 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4589 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4590 | WILL_LJMP(lua_error(L)); |
| 4591 | } |
| 4592 | |
| 4593 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4594 | b_reset(&res->buf); |
| 4595 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4596 | WILL_LJMP(lua_error(L)); |
| 4597 | } |
| 4598 | |
| 4599 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4600 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4601 | |
| 4602 | /* Headers sent, set the flag. */ |
| 4603 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4604 | return 0; |
| 4605 | |
| 4606 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4607 | /* We will build the status line and the headers of the HTTP response. |
| 4608 | * We will try send at once if its not possible, we give back the hand |
| 4609 | * waiting for more room. |
| 4610 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4611 | __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] | 4612 | { |
| 4613 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4614 | struct stream_interface *si = appctx->appctx->owner; |
| 4615 | struct channel *res = si_ic(si); |
| 4616 | |
| 4617 | if (co_data(res)) { |
| 4618 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4619 | 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] | 4620 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4621 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4625 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4626 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4627 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4628 | } |
| 4629 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4630 | /* |
| 4631 | * |
| 4632 | * |
| 4633 | * Class HTTP |
| 4634 | * |
| 4635 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4636 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4637 | |
| 4638 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4639 | * a class stream, otherwise it throws an error. |
| 4640 | */ |
| 4641 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4642 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4643 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4644 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4645 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4646 | /* This function creates and push in the stack a HTTP object |
| 4647 | * according with a current TXN. |
| 4648 | */ |
| 4649 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4650 | { |
| 4651 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4652 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4653 | /* Check stack size. */ |
| 4654 | if (!lua_checkstack(L, 3)) |
| 4655 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4656 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4657 | /* Create the object: obj[0] = userdata. |
| 4658 | * Note that the base of the Converters object is the |
| 4659 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4660 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4661 | lua_newtable(L); |
| 4662 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4663 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4664 | |
| 4665 | htxn->s = txn->s; |
| 4666 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4667 | htxn->dir = txn->dir; |
| 4668 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4669 | |
| 4670 | /* Pop a class stream metatable and affect it to the table. */ |
| 4671 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4672 | lua_setmetatable(L, -2); |
| 4673 | |
| 4674 | return 1; |
| 4675 | } |
| 4676 | |
| 4677 | /* This function creates ans returns an array of HTTP headers. |
| 4678 | * This function does not fails. It is used as wrapper with the |
| 4679 | * 2 following functions. |
| 4680 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4681 | __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] | 4682 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4683 | struct htx *htx; |
| 4684 | int32_t pos; |
| 4685 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4686 | /* Create the table. */ |
| 4687 | lua_newtable(L); |
| 4688 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4689 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4690 | htx = htxbuf(&msg->chn->buf); |
| 4691 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4692 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4693 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4694 | struct ist n, v; |
| 4695 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4696 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4697 | if (type == HTX_BLK_HDR) { |
| 4698 | n = htx_get_blk_name(htx,blk); |
| 4699 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4700 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4701 | else if (type == HTX_BLK_EOH) |
| 4702 | break; |
| 4703 | else |
| 4704 | continue; |
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 | /* Check for existing entry: |
| 4707 | * assume that the table is on the top of the stack, and |
| 4708 | * push the key in the stack, the function lua_gettable() |
| 4709 | * perform the lookup. |
| 4710 | */ |
| 4711 | lua_pushlstring(L, n.ptr, n.len); |
| 4712 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4713 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4714 | switch (lua_type(L, -1)) { |
| 4715 | case LUA_TNIL: |
| 4716 | /* Table not found, create it. */ |
| 4717 | lua_pop(L, 1); /* remove the nil value. */ |
| 4718 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4719 | lua_newtable(L); /* create and push empty table. */ |
| 4720 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4721 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4722 | 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] | 4723 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4724 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4725 | case LUA_TTABLE: |
| 4726 | /* Entry found: push the value in the table. */ |
| 4727 | len = lua_rawlen(L, -1); |
| 4728 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4729 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4730 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4731 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4732 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4733 | default: |
| 4734 | /* Other cases are errors. */ |
| 4735 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4736 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4737 | } |
| 4738 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4739 | return 1; |
| 4740 | } |
| 4741 | |
| 4742 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4743 | { |
| 4744 | struct hlua_txn *htxn; |
| 4745 | |
| 4746 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4747 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4748 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4749 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4750 | WILL_LJMP(lua_error(L)); |
| 4751 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4752 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4756 | { |
| 4757 | struct hlua_txn *htxn; |
| 4758 | |
| 4759 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4760 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4761 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4762 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4763 | WILL_LJMP(lua_error(L)); |
| 4764 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4765 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | /* This function replace full header, or just a value in |
| 4769 | * the request or in the response. It is a wrapper fir the |
| 4770 | * 4 following functions. |
| 4771 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4772 | __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] | 4773 | { |
| 4774 | size_t name_len; |
| 4775 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4776 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4777 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4778 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4779 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4780 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4781 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4782 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4783 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4784 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4785 | 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] | 4786 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4787 | return 0; |
| 4788 | } |
| 4789 | |
| 4790 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4791 | { |
| 4792 | struct hlua_txn *htxn; |
| 4793 | |
| 4794 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4795 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4796 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4797 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4798 | WILL_LJMP(lua_error(L)); |
| 4799 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4800 | 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] | 4801 | } |
| 4802 | |
| 4803 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4804 | { |
| 4805 | struct hlua_txn *htxn; |
| 4806 | |
| 4807 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4808 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4809 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4810 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4811 | WILL_LJMP(lua_error(L)); |
| 4812 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4813 | 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] | 4814 | } |
| 4815 | |
| 4816 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4817 | { |
| 4818 | struct hlua_txn *htxn; |
| 4819 | |
| 4820 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4821 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4822 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4823 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4824 | WILL_LJMP(lua_error(L)); |
| 4825 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4826 | 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] | 4827 | } |
| 4828 | |
| 4829 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4830 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4831 | struct hlua_txn *htxn; |
| 4832 | |
| 4833 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4834 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4835 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4836 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4837 | WILL_LJMP(lua_error(L)); |
| 4838 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4839 | 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] | 4840 | } |
| 4841 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4842 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4843 | * It is a wrapper for the 2 following functions. |
| 4844 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4845 | __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] | 4846 | { |
| 4847 | size_t len; |
| 4848 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4849 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4850 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4851 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4852 | ctx.blk = NULL; |
| 4853 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4854 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4855 | return 0; |
| 4856 | } |
| 4857 | |
| 4858 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4859 | { |
| 4860 | struct hlua_txn *htxn; |
| 4861 | |
| 4862 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4863 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4864 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4865 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4866 | WILL_LJMP(lua_error(L)); |
| 4867 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4868 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4872 | { |
| 4873 | struct hlua_txn *htxn; |
| 4874 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4875 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4876 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4877 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4878 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4879 | WILL_LJMP(lua_error(L)); |
| 4880 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4881 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | /* This function adds an header. It is a wrapper used by |
| 4885 | * the 2 following functions. |
| 4886 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4887 | __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] | 4888 | { |
| 4889 | size_t name_len; |
| 4890 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4891 | size_t value_len; |
| 4892 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4893 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4894 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4895 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4896 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4897 | return 0; |
| 4898 | } |
| 4899 | |
| 4900 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4901 | { |
| 4902 | struct hlua_txn *htxn; |
| 4903 | |
| 4904 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4905 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4906 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4907 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4908 | WILL_LJMP(lua_error(L)); |
| 4909 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4910 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4914 | { |
| 4915 | struct hlua_txn *htxn; |
| 4916 | |
| 4917 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4918 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4919 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4920 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4921 | WILL_LJMP(lua_error(L)); |
| 4922 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4923 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4927 | { |
| 4928 | struct hlua_txn *htxn; |
| 4929 | |
| 4930 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4931 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4932 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4933 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4934 | WILL_LJMP(lua_error(L)); |
| 4935 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4936 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 4937 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4941 | { |
| 4942 | struct hlua_txn *htxn; |
| 4943 | |
| 4944 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4945 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4946 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4947 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4948 | WILL_LJMP(lua_error(L)); |
| 4949 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4950 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 4951 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | /* This function set the method. */ |
| 4955 | static int hlua_http_req_set_meth(lua_State *L) |
| 4956 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4957 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4958 | size_t name_len; |
| 4959 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4960 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4961 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4962 | WILL_LJMP(lua_error(L)); |
| 4963 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4964 | 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] | 4965 | return 1; |
| 4966 | } |
| 4967 | |
| 4968 | /* This function set the method. */ |
| 4969 | static int hlua_http_req_set_path(lua_State *L) |
| 4970 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4971 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4972 | size_t name_len; |
| 4973 | 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] | 4974 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4975 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4976 | WILL_LJMP(lua_error(L)); |
| 4977 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4978 | 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] | 4979 | return 1; |
| 4980 | } |
| 4981 | |
| 4982 | /* This function set the query-string. */ |
| 4983 | static int hlua_http_req_set_query(lua_State *L) |
| 4984 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4985 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4986 | size_t name_len; |
| 4987 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4988 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4989 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4990 | WILL_LJMP(lua_error(L)); |
| 4991 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4992 | /* Check length. */ |
| 4993 | if (name_len > trash.size - 1) { |
| 4994 | lua_pushboolean(L, 0); |
| 4995 | return 1; |
| 4996 | } |
| 4997 | |
| 4998 | /* Add the mark question as prefix. */ |
| 4999 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5000 | trash.area[trash.data++] = '?'; |
| 5001 | memcpy(trash.area + trash.data, name, name_len); |
| 5002 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5003 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5004 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5005 | 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] | 5006 | return 1; |
| 5007 | } |
| 5008 | |
| 5009 | /* This function set the uri. */ |
| 5010 | static int hlua_http_req_set_uri(lua_State *L) |
| 5011 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5012 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5013 | size_t name_len; |
| 5014 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5016 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5017 | WILL_LJMP(lua_error(L)); |
| 5018 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5019 | 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] | 5020 | return 1; |
| 5021 | } |
| 5022 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5023 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5024 | static int hlua_http_res_set_status(lua_State *L) |
| 5025 | { |
| 5026 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5027 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5028 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5029 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5030 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5031 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5032 | WILL_LJMP(lua_error(L)); |
| 5033 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5034 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5035 | return 0; |
| 5036 | } |
| 5037 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5038 | /* |
| 5039 | * |
| 5040 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5041 | * Class TXN |
| 5042 | * |
| 5043 | * |
| 5044 | */ |
| 5045 | |
| 5046 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5047 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5048 | */ |
| 5049 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5050 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5051 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5052 | } |
| 5053 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5054 | __LJMP static int hlua_set_var(lua_State *L) |
| 5055 | { |
| 5056 | struct hlua_txn *htxn; |
| 5057 | const char *name; |
| 5058 | size_t len; |
| 5059 | struct sample smp; |
| 5060 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5061 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5062 | 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] | 5063 | |
| 5064 | /* It is useles to retrieve the stream, but this function |
| 5065 | * runs only in a stream context. |
| 5066 | */ |
| 5067 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5068 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5069 | |
| 5070 | /* Converts the third argument in a sample. */ |
| 5071 | hlua_lua2smp(L, 3, &smp); |
| 5072 | |
| 5073 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5074 | 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] | 5075 | |
| 5076 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5077 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5078 | else |
| 5079 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5080 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5081 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5082 | } |
| 5083 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5084 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5085 | { |
| 5086 | struct hlua_txn *htxn; |
| 5087 | const char *name; |
| 5088 | size_t len; |
| 5089 | struct sample smp; |
| 5090 | |
| 5091 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5092 | |
| 5093 | /* It is useles to retrieve the stream, but this function |
| 5094 | * runs only in a stream context. |
| 5095 | */ |
| 5096 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5097 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5098 | |
| 5099 | /* Unset the variable. */ |
| 5100 | 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] | 5101 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5102 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5103 | } |
| 5104 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5105 | __LJMP static int hlua_get_var(lua_State *L) |
| 5106 | { |
| 5107 | struct hlua_txn *htxn; |
| 5108 | const char *name; |
| 5109 | size_t len; |
| 5110 | struct sample smp; |
| 5111 | |
| 5112 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5113 | |
| 5114 | /* It is useles to retrieve the stream, but this function |
| 5115 | * runs only in a stream context. |
| 5116 | */ |
| 5117 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5118 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5119 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5120 | 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] | 5121 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5122 | lua_pushnil(L); |
| 5123 | return 1; |
| 5124 | } |
| 5125 | |
| 5126 | return hlua_smp2lua(L, &smp); |
| 5127 | } |
| 5128 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5129 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5130 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5131 | struct hlua *hlua; |
| 5132 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5133 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5134 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5135 | /* It is useles to retrieve the stream, but this function |
| 5136 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5137 | */ |
| 5138 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5139 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5140 | |
| 5141 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5142 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5143 | |
| 5144 | /* Get and store new value. */ |
| 5145 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5146 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5147 | |
| 5148 | return 0; |
| 5149 | } |
| 5150 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5151 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5152 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5153 | struct hlua *hlua; |
| 5154 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5155 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5156 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5157 | /* It is useles to retrieve the stream, but this function |
| 5158 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5159 | */ |
| 5160 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5161 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5162 | |
| 5163 | /* Push configuration index in the stack. */ |
| 5164 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5165 | |
| 5166 | return 1; |
| 5167 | } |
| 5168 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5169 | /* Create stack entry containing a class TXN. This function |
| 5170 | * return 0 if the stack does not contains free slots, |
| 5171 | * otherwise it returns 1. |
| 5172 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5173 | 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] | 5174 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5175 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5176 | |
| 5177 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5178 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5179 | return 0; |
| 5180 | |
| 5181 | /* NOTE: The allocation never fails. The failure |
| 5182 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5183 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5184 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5185 | /* Create the object: obj[0] = userdata. */ |
| 5186 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5187 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5188 | lua_rawseti(L, -2, 0); |
| 5189 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5190 | htxn->s = s; |
| 5191 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5192 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5193 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5194 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5195 | /* Create the "f" field that contains a list of fetches. */ |
| 5196 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5197 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5198 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5199 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5200 | |
| 5201 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5202 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5203 | 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] | 5204 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5205 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5206 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5207 | /* Create the "c" field that contains a list of converters. */ |
| 5208 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5209 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5210 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5211 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5212 | |
| 5213 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5214 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5215 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5216 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5217 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5218 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5219 | /* Create the "req" field that contains the request channel object. */ |
| 5220 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5221 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5222 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5223 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5224 | |
| 5225 | /* Create the "res" field that contains the response channel object. */ |
| 5226 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5227 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5228 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5229 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5230 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5231 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5232 | lua_pushstring(L, "http"); |
| 5233 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5234 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5235 | return 0; |
| 5236 | } |
| 5237 | else |
| 5238 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5239 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5240 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5241 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5242 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5243 | lua_setmetatable(L, -2); |
| 5244 | |
| 5245 | return 1; |
| 5246 | } |
| 5247 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5248 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5249 | { |
| 5250 | const char *msg; |
| 5251 | struct hlua_txn *htxn; |
| 5252 | |
| 5253 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5254 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5255 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5256 | |
| 5257 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5258 | return 0; |
| 5259 | } |
| 5260 | |
| 5261 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5262 | { |
| 5263 | int level; |
| 5264 | const char *msg; |
| 5265 | struct hlua_txn *htxn; |
| 5266 | |
| 5267 | MAY_LJMP(check_args(L, 3, "log")); |
| 5268 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5269 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5270 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5271 | |
| 5272 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5273 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5274 | |
| 5275 | hlua_sendlog(htxn->s->be, level, msg); |
| 5276 | return 0; |
| 5277 | } |
| 5278 | |
| 5279 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5280 | { |
| 5281 | const char *msg; |
| 5282 | struct hlua_txn *htxn; |
| 5283 | |
| 5284 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5285 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5286 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5287 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5288 | return 0; |
| 5289 | } |
| 5290 | |
| 5291 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5292 | { |
| 5293 | const char *msg; |
| 5294 | struct hlua_txn *htxn; |
| 5295 | |
| 5296 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5297 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5298 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5299 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5300 | return 0; |
| 5301 | } |
| 5302 | |
| 5303 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5304 | { |
| 5305 | const char *msg; |
| 5306 | struct hlua_txn *htxn; |
| 5307 | |
| 5308 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5309 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5310 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5311 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5312 | return 0; |
| 5313 | } |
| 5314 | |
| 5315 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5316 | { |
| 5317 | const char *msg; |
| 5318 | struct hlua_txn *htxn; |
| 5319 | |
| 5320 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5321 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5322 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5323 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5324 | return 0; |
| 5325 | } |
| 5326 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5327 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5328 | { |
| 5329 | struct hlua_txn *htxn; |
| 5330 | int ll; |
| 5331 | |
| 5332 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5333 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5334 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5335 | |
| 5336 | if (ll < 0 || ll > 7) |
| 5337 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5338 | |
| 5339 | htxn->s->logs.level = ll; |
| 5340 | return 0; |
| 5341 | } |
| 5342 | |
| 5343 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5344 | { |
| 5345 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5346 | int tos; |
| 5347 | |
| 5348 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5349 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5350 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5351 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5352 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5353 | return 0; |
| 5354 | } |
| 5355 | |
| 5356 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5357 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5358 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5359 | int mark; |
| 5360 | |
| 5361 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5362 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5363 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5364 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5365 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5366 | return 0; |
| 5367 | } |
| 5368 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5369 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5370 | { |
| 5371 | struct hlua_txn *htxn; |
| 5372 | |
| 5373 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5374 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5375 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5376 | return 0; |
| 5377 | } |
| 5378 | |
| 5379 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5380 | { |
| 5381 | struct hlua_txn *htxn; |
| 5382 | |
| 5383 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5384 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5385 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5386 | return 0; |
| 5387 | } |
| 5388 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5389 | /* 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] | 5390 | * 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] | 5391 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5392 | * error. The Reply must be on top of the stack. |
| 5393 | */ |
| 5394 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5395 | { |
| 5396 | struct htx *htx; |
| 5397 | struct htx_sl *sl; |
| 5398 | struct h1m h1m; |
| 5399 | const char *status, *reason, *body; |
| 5400 | size_t status_len, reason_len, body_len; |
| 5401 | int ret, code, flags; |
| 5402 | |
| 5403 | code = 200; |
| 5404 | status = "200"; |
| 5405 | status_len = 3; |
| 5406 | ret = lua_getfield(L, -1, "status"); |
| 5407 | if (ret == LUA_TNUMBER) { |
| 5408 | code = lua_tointeger(L, -1); |
| 5409 | status = lua_tolstring(L, -1, &status_len); |
| 5410 | } |
| 5411 | lua_pop(L, 1); |
| 5412 | |
| 5413 | reason = http_get_reason(code); |
| 5414 | reason_len = strlen(reason); |
| 5415 | ret = lua_getfield(L, -1, "reason"); |
| 5416 | if (ret == LUA_TSTRING) |
| 5417 | reason = lua_tolstring(L, -1, &reason_len); |
| 5418 | lua_pop(L, 1); |
| 5419 | |
| 5420 | body = NULL; |
| 5421 | body_len = 0; |
| 5422 | ret = lua_getfield(L, -1, "body"); |
| 5423 | if (ret == LUA_TSTRING) |
| 5424 | body = lua_tolstring(L, -1, &body_len); |
| 5425 | lua_pop(L, 1); |
| 5426 | |
| 5427 | /* Prepare the response before inserting the headers */ |
| 5428 | h1m_init_res(&h1m); |
| 5429 | htx = htx_from_buf(&s->res.buf); |
| 5430 | channel_htx_truncate(&s->res, htx); |
| 5431 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5432 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5433 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5434 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5435 | } |
| 5436 | else { |
| 5437 | flags = HTX_SL_F_IS_RESP; |
| 5438 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5439 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5440 | } |
| 5441 | if (!sl) |
| 5442 | goto fail; |
| 5443 | sl->info.res.status = code; |
| 5444 | |
| 5445 | /* Push in the stack the "headers" entry. */ |
| 5446 | ret = lua_getfield(L, -1, "headers"); |
| 5447 | if (ret != LUA_TTABLE) |
| 5448 | goto skip_headers; |
| 5449 | |
| 5450 | lua_pushnil(L); |
| 5451 | while (lua_next(L, -2) != 0) { |
| 5452 | struct ist name, value; |
| 5453 | const char *n, *v; |
| 5454 | size_t nlen, vlen; |
| 5455 | |
| 5456 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5457 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5458 | goto next_hdr; |
| 5459 | } |
| 5460 | |
| 5461 | n = lua_tolstring(L, -2, &nlen); |
| 5462 | name = ist2(n, nlen); |
| 5463 | if (isteqi(name, ist("content-length"))) { |
| 5464 | /* Always skip content-length header. It will be added |
| 5465 | * later with the correct len |
| 5466 | */ |
| 5467 | goto next_hdr; |
| 5468 | } |
| 5469 | |
| 5470 | /* Loop on header's values */ |
| 5471 | lua_pushnil(L); |
| 5472 | while (lua_next(L, -2)) { |
| 5473 | if (!lua_isstring(L, -1)) { |
| 5474 | /* Skip the value if it is not a string */ |
| 5475 | goto next_value; |
| 5476 | } |
| 5477 | |
| 5478 | v = lua_tolstring(L, -1, &vlen); |
| 5479 | value = ist2(v, vlen); |
| 5480 | |
| 5481 | if (isteqi(name, ist("transfer-encoding"))) |
| 5482 | h1_parse_xfer_enc_header(&h1m, value); |
| 5483 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5484 | goto fail; |
| 5485 | |
| 5486 | next_value: |
| 5487 | lua_pop(L, 1); |
| 5488 | } |
| 5489 | |
| 5490 | next_hdr: |
| 5491 | lua_pop(L, 1); |
| 5492 | } |
| 5493 | skip_headers: |
| 5494 | lua_pop(L, 1); |
| 5495 | |
| 5496 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5497 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5498 | const char *clen = ultoa(body_len); |
| 5499 | |
| 5500 | h1m.flags |= H1_MF_CLEN; |
| 5501 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5502 | goto fail; |
| 5503 | } |
| 5504 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5505 | h1m.flags |= H1_MF_XFER_LEN; |
| 5506 | |
| 5507 | /* Update HTX start-line flags */ |
| 5508 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5509 | flags |= HTX_SL_F_XFER_ENC; |
| 5510 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5511 | flags |= HTX_SL_F_XFER_LEN; |
| 5512 | if (h1m.flags & H1_MF_CHNK) |
| 5513 | flags |= HTX_SL_F_CHNK; |
| 5514 | else if (h1m.flags & H1_MF_CLEN) |
| 5515 | flags |= HTX_SL_F_CLEN; |
| 5516 | if (h1m.body_len == 0) |
| 5517 | flags |= HTX_SL_F_BODYLESS; |
| 5518 | } |
| 5519 | sl->flags |= flags; |
| 5520 | |
| 5521 | |
| 5522 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5523 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5524 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5525 | goto fail; |
| 5526 | |
| 5527 | /* Now, forward the response and terminate the transaction */ |
| 5528 | s->txn->status = code; |
| 5529 | htx_to_buf(htx, &s->res.buf); |
| 5530 | if (!http_forward_proxy_resp(s, 1)) |
| 5531 | goto fail; |
| 5532 | |
| 5533 | return 1; |
| 5534 | |
| 5535 | fail: |
| 5536 | channel_htx_truncate(&s->res, htx); |
| 5537 | return 0; |
| 5538 | } |
| 5539 | |
| 5540 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5541 | * processing is just aborted. Nothing is returned to the client and all |
| 5542 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5543 | * is forwarded to the client before terminating the transaction. On success, |
| 5544 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5545 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5546 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5547 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5548 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5549 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5550 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5551 | struct stream *s; |
| 5552 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5553 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5554 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5555 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5556 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5557 | * just end the execution of the current lua code. */ |
| 5558 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5559 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5560 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5561 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5562 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5563 | struct channel *req = &s->req; |
| 5564 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5565 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5566 | channel_auto_read(req); |
| 5567 | channel_abort(req); |
| 5568 | channel_auto_close(req); |
| 5569 | channel_erase(req); |
| 5570 | |
| 5571 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5572 | channel_auto_read(res); |
| 5573 | channel_auto_close(res); |
| 5574 | channel_shutr_now(res); |
| 5575 | |
| 5576 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5577 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5578 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5579 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5580 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5581 | /* No reply or invalid reply */ |
| 5582 | s->txn->status = 0; |
| 5583 | http_reply_and_close(s, 0, NULL); |
| 5584 | } |
| 5585 | else { |
| 5586 | /* Remove extra args to have the reply on top of the stack */ |
| 5587 | if (lua_gettop(L) > 2) |
| 5588 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5589 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5590 | if (!hlua_txn_forward_reply(L, s)) { |
| 5591 | if (!(s->flags & SF_ERR_MASK)) |
| 5592 | s->flags |= SF_ERR_PRXCOND; |
| 5593 | lua_pushinteger(L, ACT_RET_ERR); |
| 5594 | WILL_LJMP(hlua_done(L)); |
| 5595 | return 0; /* Never reached */ |
| 5596 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5597 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5598 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5599 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5600 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5601 | /* let's log the request time */ |
| 5602 | s->logs.tv_request = now; |
| 5603 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5604 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5605 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5606 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5607 | done: |
| 5608 | if (!(s->flags & SF_ERR_MASK)) |
| 5609 | s->flags |= SF_ERR_LOCAL; |
| 5610 | if (!(s->flags & SF_FINST_MASK)) |
| 5611 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5612 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5613 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5614 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5615 | return 0; |
| 5616 | } |
| 5617 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5618 | /* |
| 5619 | * |
| 5620 | * |
| 5621 | * Class REPLY |
| 5622 | * |
| 5623 | * |
| 5624 | */ |
| 5625 | |
| 5626 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5627 | * free slots, the function fails and returns 0; |
| 5628 | */ |
| 5629 | static int hlua_txn_reply_new(lua_State *L) |
| 5630 | { |
| 5631 | struct hlua_txn *htxn; |
| 5632 | const char *reason, *body = NULL; |
| 5633 | int ret, status; |
| 5634 | |
| 5635 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5636 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5637 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5638 | WILL_LJMP(lua_error(L)); |
| 5639 | } |
| 5640 | |
| 5641 | /* Default value */ |
| 5642 | status = 200; |
| 5643 | reason = http_get_reason(status); |
| 5644 | |
| 5645 | if (lua_istable(L, 2)) { |
| 5646 | /* load status and reason from the table argument at index 2 */ |
| 5647 | ret = lua_getfield(L, 2, "status"); |
| 5648 | if (ret == LUA_TNIL) |
| 5649 | goto reason; |
| 5650 | else if (ret != LUA_TNUMBER) { |
| 5651 | /* invalid status: ignore the reason */ |
| 5652 | goto body; |
| 5653 | } |
| 5654 | status = lua_tointeger(L, -1); |
| 5655 | |
| 5656 | reason: |
| 5657 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5658 | ret = lua_getfield(L, 2, "reason"); |
| 5659 | if (ret == LUA_TSTRING) |
| 5660 | reason = lua_tostring(L, -1); |
| 5661 | |
| 5662 | body: |
| 5663 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5664 | ret = lua_getfield(L, 2, "body"); |
| 5665 | if (ret == LUA_TSTRING) |
| 5666 | body = lua_tostring(L, -1); |
| 5667 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5668 | } |
| 5669 | |
| 5670 | /* Create the Reply table */ |
| 5671 | lua_newtable(L); |
| 5672 | |
| 5673 | /* Add status element */ |
| 5674 | lua_pushstring(L, "status"); |
| 5675 | lua_pushinteger(L, status); |
| 5676 | lua_settable(L, -3); |
| 5677 | |
| 5678 | /* Add reason element */ |
| 5679 | reason = http_get_reason(status); |
| 5680 | lua_pushstring(L, "reason"); |
| 5681 | lua_pushstring(L, reason); |
| 5682 | lua_settable(L, -3); |
| 5683 | |
| 5684 | /* Add body element, nil if undefined */ |
| 5685 | lua_pushstring(L, "body"); |
| 5686 | if (body) |
| 5687 | lua_pushstring(L, body); |
| 5688 | else |
| 5689 | lua_pushnil(L); |
| 5690 | lua_settable(L, -3); |
| 5691 | |
| 5692 | /* Add headers element */ |
| 5693 | lua_pushstring(L, "headers"); |
| 5694 | lua_newtable(L); |
| 5695 | |
| 5696 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5697 | if (lua_istable(L, 2)) { |
| 5698 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5699 | ret = lua_getfield(L, 2, "headers"); |
| 5700 | if (ret == LUA_TTABLE) { |
| 5701 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5702 | lua_pushnil(L); |
| 5703 | while (lua_next(L, -2) != 0) { |
| 5704 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5705 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5706 | /* invalid value type, skip it */ |
| 5707 | lua_pop(L, 1); |
| 5708 | continue; |
| 5709 | } |
| 5710 | |
| 5711 | |
| 5712 | /* Duplicate the key and swap it with the value. */ |
| 5713 | lua_pushvalue(L, -2); |
| 5714 | lua_insert(L, -2); |
| 5715 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5716 | |
| 5717 | lua_newtable(L); |
| 5718 | lua_insert(L, -2); |
| 5719 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5720 | |
| 5721 | if (lua_isstring(L, -1)) { |
| 5722 | /* push the value in the inner table */ |
| 5723 | lua_rawseti(L, -2, 1); |
| 5724 | } |
| 5725 | else { /* table */ |
| 5726 | lua_pushnil(L); |
| 5727 | while (lua_next(L, -2) != 0) { |
| 5728 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5729 | if (!lua_isstring(L, -1)) { |
| 5730 | /* invalid value type, skip it*/ |
| 5731 | lua_pop(L, 1); |
| 5732 | continue; |
| 5733 | } |
| 5734 | /* push the value in the inner table */ |
| 5735 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5736 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5737 | } |
| 5738 | lua_pop(L, 1); |
| 5739 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5740 | } |
| 5741 | |
| 5742 | /* push (k,v) on the stack in the headers table: |
| 5743 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5744 | */ |
| 5745 | lua_settable(L, -5); |
| 5746 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5747 | } |
| 5748 | } |
| 5749 | lua_pop(L, 1); |
| 5750 | } |
| 5751 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5752 | lua_settable(L, -3); |
| 5753 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5754 | |
| 5755 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5756 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5757 | lua_setmetatable(L, -2); |
| 5758 | return 1; |
| 5759 | } |
| 5760 | |
| 5761 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5762 | * provided, the default one corresponding to the status code is used. |
| 5763 | */ |
| 5764 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5765 | { |
| 5766 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5767 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5768 | |
| 5769 | /* First argument (self) must be a table */ |
| 5770 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5771 | |
| 5772 | if (status < 100 || status > 599) { |
| 5773 | lua_pushboolean(L, 0); |
| 5774 | return 1; |
| 5775 | } |
| 5776 | if (!reason) |
| 5777 | reason = http_get_reason(status); |
| 5778 | |
| 5779 | lua_pushinteger(L, status); |
| 5780 | lua_setfield(L, 1, "status"); |
| 5781 | |
| 5782 | lua_pushstring(L, reason); |
| 5783 | lua_setfield(L, 1, "reason"); |
| 5784 | |
| 5785 | lua_pushboolean(L, 1); |
| 5786 | return 1; |
| 5787 | } |
| 5788 | |
| 5789 | /* Add a header into the reply object. Each header name is associated to an |
| 5790 | * array of values in the "headers" table. If the header name is not found, a |
| 5791 | * new entry is created. |
| 5792 | */ |
| 5793 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5794 | { |
| 5795 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5796 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5797 | int ret; |
| 5798 | |
| 5799 | /* First argument (self) must be a table */ |
| 5800 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5801 | |
| 5802 | /* Push in the stack the "headers" entry. */ |
| 5803 | ret = lua_getfield(L, 1, "headers"); |
| 5804 | if (ret != LUA_TTABLE) { |
| 5805 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5806 | WILL_LJMP(lua_error(L)); |
| 5807 | } |
| 5808 | |
| 5809 | /* check if the header is already registered. If not, register it. */ |
| 5810 | ret = lua_getfield(L, -1, name); |
| 5811 | if (ret == LUA_TNIL) { |
| 5812 | /* Entry not found. */ |
| 5813 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5814 | |
| 5815 | /* Insert the new header name in the array in the top of the stack. |
| 5816 | * It left the new array in the top of the stack. |
| 5817 | */ |
| 5818 | lua_newtable(L); |
| 5819 | lua_pushstring(L, name); |
| 5820 | lua_pushvalue(L, -2); |
| 5821 | lua_settable(L, -4); |
| 5822 | } |
| 5823 | else if (ret != LUA_TTABLE) { |
| 5824 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5825 | WILL_LJMP(lua_error(L)); |
| 5826 | } |
| 5827 | |
| 5828 | /* Now the top od thestack is an array of values. We push |
| 5829 | * the header value as new entry. |
| 5830 | */ |
| 5831 | lua_pushstring(L, value); |
| 5832 | ret = lua_rawlen(L, -2); |
| 5833 | lua_rawseti(L, -2, ret + 1); |
| 5834 | |
| 5835 | lua_pushboolean(L, 1); |
| 5836 | return 1; |
| 5837 | } |
| 5838 | |
| 5839 | /* Remove all occurrences of a given header name. */ |
| 5840 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5841 | { |
| 5842 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5843 | int ret; |
| 5844 | |
| 5845 | /* First argument (self) must be a table */ |
| 5846 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5847 | |
| 5848 | /* Push in the stack the "headers" entry. */ |
| 5849 | ret = lua_getfield(L, 1, "headers"); |
| 5850 | if (ret != LUA_TTABLE) { |
| 5851 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 5852 | WILL_LJMP(lua_error(L)); |
| 5853 | } |
| 5854 | |
| 5855 | lua_pushstring(L, name); |
| 5856 | lua_pushnil(L); |
| 5857 | lua_settable(L, -3); |
| 5858 | |
| 5859 | lua_pushboolean(L, 1); |
| 5860 | return 1; |
| 5861 | } |
| 5862 | |
| 5863 | /* Set the reply's body. Overwrite any existing entry. */ |
| 5864 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 5865 | { |
| 5866 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5867 | |
| 5868 | /* First argument (self) must be a table */ |
| 5869 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5870 | |
| 5871 | lua_pushstring(L, payload); |
| 5872 | lua_setfield(L, 1, "body"); |
| 5873 | |
| 5874 | lua_pushboolean(L, 1); |
| 5875 | return 1; |
| 5876 | } |
| 5877 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5878 | __LJMP static int hlua_log(lua_State *L) |
| 5879 | { |
| 5880 | int level; |
| 5881 | const char *msg; |
| 5882 | |
| 5883 | MAY_LJMP(check_args(L, 2, "log")); |
| 5884 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5885 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5886 | |
| 5887 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5888 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5889 | |
| 5890 | hlua_sendlog(NULL, level, msg); |
| 5891 | return 0; |
| 5892 | } |
| 5893 | |
| 5894 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5895 | { |
| 5896 | const char *msg; |
| 5897 | |
| 5898 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5899 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5900 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5901 | return 0; |
| 5902 | } |
| 5903 | |
| 5904 | __LJMP static int hlua_log_info(lua_State *L) |
| 5905 | { |
| 5906 | const char *msg; |
| 5907 | |
| 5908 | MAY_LJMP(check_args(L, 1, "info")); |
| 5909 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5910 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5911 | return 0; |
| 5912 | } |
| 5913 | |
| 5914 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5915 | { |
| 5916 | const char *msg; |
| 5917 | |
| 5918 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5919 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5920 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5921 | return 0; |
| 5922 | } |
| 5923 | |
| 5924 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5925 | { |
| 5926 | const char *msg; |
| 5927 | |
| 5928 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5929 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5930 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5931 | return 0; |
| 5932 | } |
| 5933 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5934 | __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] | 5935 | { |
| 5936 | int wakeup_ms = lua_tointeger(L, -1); |
| 5937 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5938 | 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] | 5939 | return 0; |
| 5940 | } |
| 5941 | |
| 5942 | __LJMP static int hlua_sleep(lua_State *L) |
| 5943 | { |
| 5944 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5945 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5946 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5947 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5948 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5949 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5950 | wakeup_ms = tick_add(now_ms, delay); |
| 5951 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5952 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5953 | 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] | 5954 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5955 | } |
| 5956 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5957 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5958 | { |
| 5959 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5960 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5961 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5962 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5963 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5964 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5965 | wakeup_ms = tick_add(now_ms, delay); |
| 5966 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5967 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5968 | 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] | 5969 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5970 | } |
| 5971 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5972 | /* This functionis an LUA binding. it permits to give back |
| 5973 | * the hand at the HAProxy scheduler. It is used when the |
| 5974 | * LUA processing consumes a lot of time. |
| 5975 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5976 | __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] | 5977 | { |
| 5978 | return 0; |
| 5979 | } |
| 5980 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5981 | __LJMP static int hlua_yield(lua_State *L) |
| 5982 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5983 | 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] | 5984 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5987 | /* This function change the nice of the currently executed |
| 5988 | * task. It is used set low or high priority at the current |
| 5989 | * task. |
| 5990 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5991 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5992 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5993 | struct hlua *hlua; |
| 5994 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5995 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5996 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5997 | hlua = hlua_gethlua(L); |
| 5998 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5999 | |
| 6000 | /* If he task is not set, I'm in a start mode. */ |
| 6001 | if (!hlua || !hlua->task) |
| 6002 | return 0; |
| 6003 | |
| 6004 | if (nice < -1024) |
| 6005 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6006 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6007 | nice = 1024; |
| 6008 | |
| 6009 | hlua->task->nice = nice; |
| 6010 | return 0; |
| 6011 | } |
| 6012 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6013 | /* 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] | 6014 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6015 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6016 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6017 | * |
| 6018 | * Task wrapper are longjmp safe because the only one Lua code |
| 6019 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6020 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6021 | 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] | 6022 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6023 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6024 | enum hlua_exec status; |
| 6025 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6026 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6027 | task_set_affinity(task, tid_bit); |
| 6028 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6029 | /* If it is the first call to the task, we must initialize the |
| 6030 | * execution timeouts. |
| 6031 | */ |
| 6032 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6033 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6034 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6035 | /* Execute the Lua code. */ |
| 6036 | status = hlua_ctx_resume(hlua, 1); |
| 6037 | |
| 6038 | switch (status) { |
| 6039 | /* finished or yield */ |
| 6040 | case HLUA_E_OK: |
| 6041 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6042 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6043 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6044 | break; |
| 6045 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6046 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6047 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6048 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6049 | break; |
| 6050 | |
| 6051 | /* finished with error. */ |
| 6052 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6053 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6054 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6055 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6056 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6057 | break; |
| 6058 | |
| 6059 | case HLUA_E_ERR: |
| 6060 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6061 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6062 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6063 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6064 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6065 | break; |
| 6066 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6067 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6068 | } |
| 6069 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6070 | /* This function is an LUA binding that register LUA function to be |
| 6071 | * executed after the HAProxy configuration parsing and before the |
| 6072 | * HAProxy scheduler starts. This function expect only one LUA |
| 6073 | * argument that is a function. This function returns nothing, but |
| 6074 | * throws if an error is encountered. |
| 6075 | */ |
| 6076 | __LJMP static int hlua_register_init(lua_State *L) |
| 6077 | { |
| 6078 | struct hlua_init_function *init; |
| 6079 | int ref; |
| 6080 | |
| 6081 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6082 | |
| 6083 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6084 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6085 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6086 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6087 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6088 | |
| 6089 | init->function_ref = ref; |
| 6090 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6091 | return 0; |
| 6092 | } |
| 6093 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6094 | /* This functio is an LUA binding. It permits to register a task |
| 6095 | * executed in parallel of the main HAroxy activity. The task is |
| 6096 | * created and it is set in the HAProxy scheduler. It can be called |
| 6097 | * from the "init" section, "post init" or during the runtime. |
| 6098 | * |
| 6099 | * Lua prototype: |
| 6100 | * |
| 6101 | * <none> core.register_task(<function>) |
| 6102 | */ |
| 6103 | static int hlua_register_task(lua_State *L) |
| 6104 | { |
| 6105 | struct hlua *hlua; |
| 6106 | struct task *task; |
| 6107 | int ref; |
| 6108 | |
| 6109 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6110 | |
| 6111 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6112 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6113 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6114 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6115 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6116 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6117 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6118 | if (!task) |
| 6119 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6120 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6121 | task->context = hlua; |
| 6122 | task->process = hlua_process_task; |
| 6123 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6124 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6125 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6126 | |
| 6127 | /* Restore the function in the stack. */ |
| 6128 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6129 | hlua->nargs = 0; |
| 6130 | |
| 6131 | /* Schedule task. */ |
| 6132 | task_schedule(task, now_ms); |
| 6133 | |
| 6134 | return 0; |
| 6135 | } |
| 6136 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6137 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6138 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6139 | * resume converters. |
| 6140 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6141 | 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] | 6142 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6143 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6144 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6145 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6146 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6147 | if (!stream) |
| 6148 | return 0; |
| 6149 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6150 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6151 | * Lua context can be not initialized. This behavior |
| 6152 | * permits to save performances because a systematic |
| 6153 | * Lua initialization cause 5% performances loss. |
| 6154 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6155 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6156 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6157 | if (!stream->hlua) { |
| 6158 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6159 | return 0; |
| 6160 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6161 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6162 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6163 | return 0; |
| 6164 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6165 | } |
| 6166 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6167 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6168 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6169 | |
| 6170 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6171 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6172 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6173 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6174 | else |
| 6175 | error = "critical error"; |
| 6176 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6177 | return 0; |
| 6178 | } |
| 6179 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6180 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6181 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6182 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6183 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6184 | return 0; |
| 6185 | } |
| 6186 | |
| 6187 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6188 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6189 | |
| 6190 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6191 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6192 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6193 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6194 | return 0; |
| 6195 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6196 | hlua_smp2lua(stream->hlua->T, smp); |
| 6197 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6198 | |
| 6199 | /* push keywords in the stack. */ |
| 6200 | if (arg_p) { |
| 6201 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6202 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6203 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6204 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6205 | return 0; |
| 6206 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6207 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6208 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6209 | } |
| 6210 | } |
| 6211 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6212 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6213 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6214 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6215 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6216 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6217 | } |
| 6218 | |
| 6219 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6220 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6221 | /* finished. */ |
| 6222 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6223 | /* If the stack is empty, the function fails. */ |
| 6224 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6225 | return 0; |
| 6226 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6227 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6228 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6229 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6230 | return 1; |
| 6231 | |
| 6232 | /* yield. */ |
| 6233 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6234 | 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] | 6235 | return 0; |
| 6236 | |
| 6237 | /* finished with error. */ |
| 6238 | case HLUA_E_ERRMSG: |
| 6239 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6240 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6241 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6242 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6243 | return 0; |
| 6244 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6245 | case HLUA_E_ETMOUT: |
| 6246 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6247 | return 0; |
| 6248 | |
| 6249 | case HLUA_E_NOMEM: |
| 6250 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6251 | return 0; |
| 6252 | |
| 6253 | case HLUA_E_YIELD: |
| 6254 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6255 | return 0; |
| 6256 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6257 | case HLUA_E_ERR: |
| 6258 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6259 | 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] | 6260 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6261 | |
| 6262 | default: |
| 6263 | return 0; |
| 6264 | } |
| 6265 | } |
| 6266 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6267 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6268 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6269 | * resume sample-fetches. This function will be called by the sample |
| 6270 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6271 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6272 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6273 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6274 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6275 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6276 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6277 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6278 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6279 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6280 | if (!stream) |
| 6281 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6282 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6283 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6284 | * Lua context can be not initialized. This behavior |
| 6285 | * permits to save performances because a systematic |
| 6286 | * Lua initialization cause 5% performances loss. |
| 6287 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6288 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6289 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6290 | if (!stream->hlua) { |
| 6291 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6292 | return 0; |
| 6293 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6294 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6295 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6296 | return 0; |
| 6297 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6298 | } |
| 6299 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6300 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6301 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6302 | |
| 6303 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6304 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6305 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6306 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6307 | else |
| 6308 | error = "critical error"; |
| 6309 | 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] | 6310 | return 0; |
| 6311 | } |
| 6312 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6313 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6314 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6315 | 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] | 6316 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6317 | return 0; |
| 6318 | } |
| 6319 | |
| 6320 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6321 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6322 | |
| 6323 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6324 | 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] | 6325 | 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] | 6326 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6327 | return 0; |
| 6328 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6329 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6330 | |
| 6331 | /* push keywords in the stack. */ |
| 6332 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6333 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6334 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6335 | 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] | 6336 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6337 | return 0; |
| 6338 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6339 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6340 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6341 | } |
| 6342 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6343 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6344 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6345 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6346 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6347 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6348 | } |
| 6349 | |
| 6350 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6351 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6352 | /* finished. */ |
| 6353 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6354 | /* If the stack is empty, the function fails. */ |
| 6355 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6356 | return 0; |
| 6357 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6358 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6359 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6360 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6361 | |
| 6362 | /* Set the end of execution flag. */ |
| 6363 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6364 | return 1; |
| 6365 | |
| 6366 | /* yield. */ |
| 6367 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6368 | 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] | 6369 | return 0; |
| 6370 | |
| 6371 | /* finished with error. */ |
| 6372 | case HLUA_E_ERRMSG: |
| 6373 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6374 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6375 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6376 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6377 | return 0; |
| 6378 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6379 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6380 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6381 | return 0; |
| 6382 | |
| 6383 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6384 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6385 | return 0; |
| 6386 | |
| 6387 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6388 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6389 | return 0; |
| 6390 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6391 | case HLUA_E_ERR: |
| 6392 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6393 | 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] | 6394 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6395 | |
| 6396 | default: |
| 6397 | return 0; |
| 6398 | } |
| 6399 | } |
| 6400 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6401 | /* This function is an LUA binding used for registering |
| 6402 | * "sample-conv" functions. It expects a converter name used |
| 6403 | * in the haproxy configuration file, and an LUA function. |
| 6404 | */ |
| 6405 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6406 | { |
| 6407 | struct sample_conv_kw_list *sck; |
| 6408 | const char *name; |
| 6409 | int ref; |
| 6410 | int len; |
| 6411 | struct hlua_function *fcn; |
| 6412 | |
| 6413 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6414 | |
| 6415 | /* First argument : converter name. */ |
| 6416 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6417 | |
| 6418 | /* Second argument : lua function. */ |
| 6419 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6420 | |
| 6421 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6422 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6423 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6424 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6425 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6426 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6427 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6428 | |
| 6429 | /* Fill fcn. */ |
| 6430 | fcn->name = strdup(name); |
| 6431 | if (!fcn->name) |
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 | fcn->function_ref = ref; |
| 6434 | |
| 6435 | /* List head */ |
| 6436 | sck->list.n = sck->list.p = NULL; |
| 6437 | |
| 6438 | /* converter keyword. */ |
| 6439 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6440 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6441 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6442 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6443 | |
| 6444 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6445 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6446 | 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] | 6447 | sck->kw[0].val_args = NULL; |
| 6448 | sck->kw[0].in_type = SMP_T_STR; |
| 6449 | sck->kw[0].out_type = SMP_T_STR; |
| 6450 | sck->kw[0].private = fcn; |
| 6451 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6452 | /* Register this new converter */ |
| 6453 | sample_register_convs(sck); |
| 6454 | |
| 6455 | return 0; |
| 6456 | } |
| 6457 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6458 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6459 | * "sample-fetch" functions. It expects a converter name used |
| 6460 | * in the haproxy configuration file, and an LUA function. |
| 6461 | */ |
| 6462 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6463 | { |
| 6464 | const char *name; |
| 6465 | int ref; |
| 6466 | int len; |
| 6467 | struct sample_fetch_kw_list *sfk; |
| 6468 | struct hlua_function *fcn; |
| 6469 | |
| 6470 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6471 | |
| 6472 | /* First argument : sample-fetch name. */ |
| 6473 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6474 | |
| 6475 | /* Second argument : lua function. */ |
| 6476 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6477 | |
| 6478 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6479 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6480 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6481 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6482 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6483 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6484 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6485 | |
| 6486 | /* Fill fcn. */ |
| 6487 | fcn->name = strdup(name); |
| 6488 | if (!fcn->name) |
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 | fcn->function_ref = ref; |
| 6491 | |
| 6492 | /* List head */ |
| 6493 | sfk->list.n = sfk->list.p = NULL; |
| 6494 | |
| 6495 | /* sample-fetch keyword. */ |
| 6496 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6497 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6498 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6499 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6500 | |
| 6501 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6502 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6503 | 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] | 6504 | sfk->kw[0].val_args = NULL; |
| 6505 | sfk->kw[0].out_type = SMP_T_STR; |
| 6506 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6507 | sfk->kw[0].val = 0; |
| 6508 | sfk->kw[0].private = fcn; |
| 6509 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6510 | /* Register this new fetch. */ |
| 6511 | sample_register_fetches(sfk); |
| 6512 | |
| 6513 | return 0; |
| 6514 | } |
| 6515 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6516 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6517 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6518 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6519 | { |
| 6520 | struct hlua *hlua = hlua_gethlua(L); |
| 6521 | unsigned int delay; |
| 6522 | unsigned int wakeup_ms; |
| 6523 | |
| 6524 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6525 | |
| 6526 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6527 | wakeup_ms = tick_add(now_ms, delay); |
| 6528 | hlua->wake_time = wakeup_ms; |
| 6529 | return 0; |
| 6530 | } |
| 6531 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6532 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6533 | * wrapper during the initialisation period. This function may return any |
| 6534 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6535 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6536 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6537 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6538 | 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] | 6539 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6540 | { |
| 6541 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6542 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6543 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6544 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6545 | |
| 6546 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6547 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6548 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6549 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6550 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6551 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6552 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6553 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6554 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6555 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6556 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6557 | * Lua context can be not initialized. This behavior |
| 6558 | * permits to save performances because a systematic |
| 6559 | * Lua initialization cause 5% performances loss. |
| 6560 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6561 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6562 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6563 | if (!s->hlua) { |
| 6564 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6565 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6566 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6567 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6568 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6569 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6570 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6571 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6572 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6573 | } |
| 6574 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6575 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6576 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6577 | |
| 6578 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6579 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6580 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6581 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6582 | else |
| 6583 | error = "critical error"; |
| 6584 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6585 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6586 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6587 | } |
| 6588 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6589 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6590 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6591 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6592 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6593 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6594 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6595 | } |
| 6596 | |
| 6597 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6598 | 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] | 6599 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6600 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6601 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6602 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6603 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6604 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6605 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6606 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6607 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6608 | |
| 6609 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6610 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6611 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6612 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6613 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6614 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6615 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6616 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6617 | lua_pushstring(s->hlua->T, *arg); |
| 6618 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6619 | } |
| 6620 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6621 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6622 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6623 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6624 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6625 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6629 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6630 | /* finished. */ |
| 6631 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6632 | /* Catch the return value */ |
| 6633 | if (lua_gettop(s->hlua->T) > 0) |
| 6634 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6635 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6636 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6637 | if (act_ret == ACT_RET_YIELD) { |
| 6638 | if (flags & ACT_OPT_FINAL) |
| 6639 | goto err_yield; |
| 6640 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6641 | if (dir == SMP_OPT_DIR_REQ) |
| 6642 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6643 | s->hlua->wake_time); |
| 6644 | else |
| 6645 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6646 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6647 | } |
| 6648 | goto end; |
| 6649 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6650 | /* yield. */ |
| 6651 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6652 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6653 | if (dir == SMP_OPT_DIR_REQ) |
| 6654 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6655 | s->hlua->wake_time); |
| 6656 | else |
| 6657 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6658 | s->hlua->wake_time); |
| 6659 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6660 | /* Some actions can be wake up when a "write" event |
| 6661 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6662 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6663 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6664 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6665 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6666 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6667 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6668 | act_ret = ACT_RET_YIELD; |
| 6669 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6670 | |
| 6671 | /* finished with error. */ |
| 6672 | case HLUA_E_ERRMSG: |
| 6673 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6674 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6675 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6676 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6677 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6678 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6679 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6680 | 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] | 6681 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6682 | |
| 6683 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6684 | 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] | 6685 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6686 | |
| 6687 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6688 | err_yield: |
| 6689 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6690 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6691 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6692 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6693 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6694 | case HLUA_E_ERR: |
| 6695 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6696 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6697 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6698 | |
| 6699 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6700 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6701 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6702 | |
| 6703 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 6704 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6705 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6706 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6707 | } |
| 6708 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6709 | 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] | 6710 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6711 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6712 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6713 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6714 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6715 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6716 | } |
| 6717 | |
| 6718 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6719 | { |
| 6720 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6721 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6722 | struct task *task; |
| 6723 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6724 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6725 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6726 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6727 | if (!hlua) { |
| 6728 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6729 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6730 | return 0; |
| 6731 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6732 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6733 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6734 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6735 | |
| 6736 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6737 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6738 | if (!task) { |
| 6739 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6740 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6741 | return 0; |
| 6742 | } |
| 6743 | task->nice = 0; |
| 6744 | task->context = ctx; |
| 6745 | task->process = hlua_applet_wakeup; |
| 6746 | ctx->ctx.hlua_apptcp.task = task; |
| 6747 | |
| 6748 | /* In the execution wrappers linked with a stream, the |
| 6749 | * Lua context can be not initialized. This behavior |
| 6750 | * permits to save performances because a systematic |
| 6751 | * Lua initialization cause 5% performances loss. |
| 6752 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6753 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6754 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6755 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6756 | return 0; |
| 6757 | } |
| 6758 | |
| 6759 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6760 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6761 | |
| 6762 | /* The following Lua calls can fail. */ |
| 6763 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6764 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6765 | error = lua_tostring(hlua->T, -1); |
| 6766 | else |
| 6767 | error = "critical error"; |
| 6768 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6769 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6770 | return 0; |
| 6771 | } |
| 6772 | |
| 6773 | /* Check stack available size. */ |
| 6774 | if (!lua_checkstack(hlua->T, 1)) { |
| 6775 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6776 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6777 | RESET_SAFE_LJMP(hlua->T); |
| 6778 | return 0; |
| 6779 | } |
| 6780 | |
| 6781 | /* Restore the function in the stack. */ |
| 6782 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6783 | |
| 6784 | /* Create and and push object stream in the stack. */ |
| 6785 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6786 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6787 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6788 | RESET_SAFE_LJMP(hlua->T); |
| 6789 | return 0; |
| 6790 | } |
| 6791 | hlua->nargs = 1; |
| 6792 | |
| 6793 | /* push keywords in the stack. */ |
| 6794 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6795 | if (!lua_checkstack(hlua->T, 1)) { |
| 6796 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6797 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6798 | RESET_SAFE_LJMP(hlua->T); |
| 6799 | return 0; |
| 6800 | } |
| 6801 | lua_pushstring(hlua->T, *arg); |
| 6802 | hlua->nargs++; |
| 6803 | } |
| 6804 | |
| 6805 | RESET_SAFE_LJMP(hlua->T); |
| 6806 | |
| 6807 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6808 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6809 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6810 | |
| 6811 | return 1; |
| 6812 | } |
| 6813 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6814 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6815 | { |
| 6816 | struct stream_interface *si = ctx->owner; |
| 6817 | struct stream *strm = si_strm(si); |
| 6818 | struct channel *res = si_ic(si); |
| 6819 | struct act_rule *rule = ctx->rule; |
| 6820 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6821 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6822 | |
| 6823 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6824 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6825 | /* eat the whole request */ |
| 6826 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6827 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6828 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6829 | |
| 6830 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6831 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6832 | return; |
| 6833 | |
| 6834 | /* Execute the function. */ |
| 6835 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6836 | /* finished. */ |
| 6837 | case HLUA_E_OK: |
| 6838 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6839 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6840 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6841 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6842 | res->flags |= CF_READ_NULL; |
| 6843 | si_shutr(si); |
| 6844 | return; |
| 6845 | |
| 6846 | /* yield. */ |
| 6847 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6848 | if (hlua->wake_time != TICK_ETERNITY) |
| 6849 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6850 | return; |
| 6851 | |
| 6852 | /* finished with error. */ |
| 6853 | case HLUA_E_ERRMSG: |
| 6854 | /* Display log. */ |
| 6855 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6856 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6857 | lua_pop(hlua->T, 1); |
| 6858 | goto error; |
| 6859 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6860 | case HLUA_E_ETMOUT: |
| 6861 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6862 | rule->arg.hlua_rule->fcn.name); |
| 6863 | goto error; |
| 6864 | |
| 6865 | case HLUA_E_NOMEM: |
| 6866 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6867 | rule->arg.hlua_rule->fcn.name); |
| 6868 | goto error; |
| 6869 | |
| 6870 | case HLUA_E_YIELD: /* unexpected */ |
| 6871 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6872 | rule->arg.hlua_rule->fcn.name); |
| 6873 | goto error; |
| 6874 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6875 | case HLUA_E_ERR: |
| 6876 | /* Display log. */ |
| 6877 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6878 | rule->arg.hlua_rule->fcn.name); |
| 6879 | goto error; |
| 6880 | |
| 6881 | default: |
| 6882 | goto error; |
| 6883 | } |
| 6884 | |
| 6885 | error: |
| 6886 | |
| 6887 | /* For all other cases, just close the stream. */ |
| 6888 | si_shutw(si); |
| 6889 | si_shutr(si); |
| 6890 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6891 | } |
| 6892 | |
| 6893 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6894 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6895 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6896 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6897 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6898 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6899 | } |
| 6900 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6901 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6902 | * an errors occurs and -1 if more data are required for initializing |
| 6903 | * the applet. |
| 6904 | */ |
| 6905 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6906 | { |
| 6907 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6908 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6909 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6910 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6911 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6912 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6913 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6914 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6915 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6916 | if (!hlua) { |
| 6917 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6918 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6919 | return 0; |
| 6920 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6921 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6922 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6923 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6924 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6925 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6926 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6927 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6928 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6929 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6930 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6931 | if (!task) { |
| 6932 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6933 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6934 | return 0; |
| 6935 | } |
| 6936 | task->nice = 0; |
| 6937 | task->context = ctx; |
| 6938 | task->process = hlua_applet_wakeup; |
| 6939 | ctx->ctx.hlua_apphttp.task = task; |
| 6940 | |
| 6941 | /* In the execution wrappers linked with a stream, the |
| 6942 | * Lua context can be not initialized. This behavior |
| 6943 | * permits to save performances because a systematic |
| 6944 | * Lua initialization cause 5% performances loss. |
| 6945 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6946 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6947 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6948 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6949 | return 0; |
| 6950 | } |
| 6951 | |
| 6952 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6953 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6954 | |
| 6955 | /* The following Lua calls can fail. */ |
| 6956 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6957 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6958 | error = lua_tostring(hlua->T, -1); |
| 6959 | else |
| 6960 | error = "critical error"; |
| 6961 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6962 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6963 | return 0; |
| 6964 | } |
| 6965 | |
| 6966 | /* Check stack available size. */ |
| 6967 | if (!lua_checkstack(hlua->T, 1)) { |
| 6968 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6969 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6970 | RESET_SAFE_LJMP(hlua->T); |
| 6971 | return 0; |
| 6972 | } |
| 6973 | |
| 6974 | /* Restore the function in the stack. */ |
| 6975 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6976 | |
| 6977 | /* Create and and push object stream in the stack. */ |
| 6978 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6979 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6980 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6981 | RESET_SAFE_LJMP(hlua->T); |
| 6982 | return 0; |
| 6983 | } |
| 6984 | hlua->nargs = 1; |
| 6985 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6986 | /* push keywords in the stack. */ |
| 6987 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6988 | if (!lua_checkstack(hlua->T, 1)) { |
| 6989 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6990 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6991 | RESET_SAFE_LJMP(hlua->T); |
| 6992 | return 0; |
| 6993 | } |
| 6994 | lua_pushstring(hlua->T, *arg); |
| 6995 | hlua->nargs++; |
| 6996 | } |
| 6997 | |
| 6998 | RESET_SAFE_LJMP(hlua->T); |
| 6999 | |
| 7000 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7001 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7002 | |
| 7003 | return 1; |
| 7004 | } |
| 7005 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7006 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7007 | { |
| 7008 | struct stream_interface *si = ctx->owner; |
| 7009 | struct stream *strm = si_strm(si); |
| 7010 | struct channel *req = si_oc(si); |
| 7011 | struct channel *res = si_ic(si); |
| 7012 | struct act_rule *rule = ctx->rule; |
| 7013 | struct proxy *px = strm->be; |
| 7014 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7015 | struct htx *req_htx, *res_htx; |
| 7016 | |
| 7017 | res_htx = htx_from_buf(&res->buf); |
| 7018 | |
| 7019 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7020 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7021 | goto out; |
| 7022 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7023 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7024 | if (!b_size(&res->buf)) { |
| 7025 | si_rx_room_blk(si); |
| 7026 | goto out; |
| 7027 | } |
| 7028 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7029 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7030 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7031 | |
| 7032 | /* Set the currently running flag. */ |
| 7033 | if (!HLUA_IS_RUNNING(hlua) && |
| 7034 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7035 | struct htx_blk *blk; |
| 7036 | size_t count = co_data(req); |
| 7037 | |
| 7038 | if (!count) { |
| 7039 | si_cant_get(si); |
| 7040 | goto out; |
| 7041 | } |
| 7042 | |
| 7043 | /* We need to flush the request header. This left the body for |
| 7044 | * the Lua. |
| 7045 | */ |
| 7046 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7047 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7048 | while (count && blk) { |
| 7049 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7050 | uint32_t sz = htx_get_blksz(blk); |
| 7051 | |
| 7052 | if (sz > count) { |
| 7053 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7054 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7055 | goto out; |
| 7056 | } |
| 7057 | |
| 7058 | count -= sz; |
| 7059 | co_set_data(req, co_data(req) - sz); |
| 7060 | blk = htx_remove_blk(req_htx, blk); |
| 7061 | |
| 7062 | if (type == HTX_BLK_EOH) |
| 7063 | break; |
| 7064 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7065 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7066 | } |
| 7067 | |
| 7068 | /* Executes The applet if it is not done. */ |
| 7069 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7070 | |
| 7071 | /* Execute the function. */ |
| 7072 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7073 | /* finished. */ |
| 7074 | case HLUA_E_OK: |
| 7075 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7076 | break; |
| 7077 | |
| 7078 | /* yield. */ |
| 7079 | case HLUA_E_AGAIN: |
| 7080 | if (hlua->wake_time != TICK_ETERNITY) |
| 7081 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7082 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7083 | |
| 7084 | /* finished with error. */ |
| 7085 | case HLUA_E_ERRMSG: |
| 7086 | /* Display log. */ |
| 7087 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7088 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7089 | lua_pop(hlua->T, 1); |
| 7090 | goto error; |
| 7091 | |
| 7092 | case HLUA_E_ETMOUT: |
| 7093 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7094 | rule->arg.hlua_rule->fcn.name); |
| 7095 | goto error; |
| 7096 | |
| 7097 | case HLUA_E_NOMEM: |
| 7098 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7099 | rule->arg.hlua_rule->fcn.name); |
| 7100 | goto error; |
| 7101 | |
| 7102 | case HLUA_E_YIELD: /* unexpected */ |
| 7103 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7104 | rule->arg.hlua_rule->fcn.name); |
| 7105 | goto error; |
| 7106 | |
| 7107 | case HLUA_E_ERR: |
| 7108 | /* Display log. */ |
| 7109 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7110 | rule->arg.hlua_rule->fcn.name); |
| 7111 | goto error; |
| 7112 | |
| 7113 | default: |
| 7114 | goto error; |
| 7115 | } |
| 7116 | } |
| 7117 | |
| 7118 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7119 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7120 | goto done; |
| 7121 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7122 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7123 | goto error; |
| 7124 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7125 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7126 | 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] | 7127 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7128 | si_rx_room_blk(si); |
| 7129 | goto out; |
| 7130 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7131 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7132 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7133 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7134 | } |
| 7135 | |
| 7136 | done: |
| 7137 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7138 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7139 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7140 | si_shutr(si); |
| 7141 | } |
| 7142 | |
| 7143 | /* eat the whole request */ |
| 7144 | if (co_data(req)) { |
| 7145 | req_htx = htx_from_buf(&req->buf); |
| 7146 | co_htx_skip(req, req_htx, co_data(req)); |
| 7147 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7148 | } |
| 7149 | } |
| 7150 | |
| 7151 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7152 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7153 | return; |
| 7154 | |
| 7155 | error: |
| 7156 | |
| 7157 | /* If we are in HTTP mode, and we are not send any |
| 7158 | * data, return a 500 server error in best effort: |
| 7159 | * if there is no room available in the buffer, |
| 7160 | * just close the connection. |
| 7161 | */ |
| 7162 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7163 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7164 | |
| 7165 | channel_erase(res); |
| 7166 | res->buf.data = b_data(err); |
| 7167 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7168 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7169 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7170 | } |
| 7171 | if (!(strm->flags & SF_ERR_MASK)) |
| 7172 | strm->flags |= SF_ERR_RESOURCE; |
| 7173 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7174 | goto done; |
| 7175 | } |
| 7176 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7177 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7178 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7179 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7180 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7181 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7182 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7183 | } |
| 7184 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7185 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7186 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7187 | * |
| 7188 | * This function can fail with an abort() due to an Lua critical error. |
| 7189 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7190 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7191 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7192 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7193 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7194 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7195 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7196 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7197 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7198 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7199 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7200 | if (!rule->arg.hlua_rule) { |
| 7201 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7202 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7203 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7204 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7205 | /* Memory for arguments. */ |
| 7206 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7207 | if (!rule->arg.hlua_rule->args) { |
| 7208 | memprintf(err, "out of memory error"); |
| 7209 | return ACT_RET_PRS_ERR; |
| 7210 | } |
| 7211 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7212 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7213 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7214 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7215 | /* Expect some arguments */ |
| 7216 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7217 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7218 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7219 | return ACT_RET_PRS_ERR; |
| 7220 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7221 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7222 | if (!rule->arg.hlua_rule->args[i]) { |
| 7223 | memprintf(err, "out of memory error"); |
| 7224 | return ACT_RET_PRS_ERR; |
| 7225 | } |
| 7226 | (*cur_arg)++; |
| 7227 | } |
| 7228 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7229 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7230 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7231 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7232 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7233 | } |
| 7234 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7235 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7236 | struct act_rule *rule, char **err) |
| 7237 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7238 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7239 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7240 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7241 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7242 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7243 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7244 | * the call of this analyzer. |
| 7245 | */ |
| 7246 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7247 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7248 | return ACT_RET_PRS_ERR; |
| 7249 | } |
| 7250 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7251 | /* Memory for the rule. */ |
| 7252 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7253 | if (!rule->arg.hlua_rule) { |
| 7254 | memprintf(err, "out of memory error"); |
| 7255 | return ACT_RET_PRS_ERR; |
| 7256 | } |
| 7257 | |
| 7258 | /* Reference the Lua function and store the reference. */ |
| 7259 | rule->arg.hlua_rule->fcn = *fcn; |
| 7260 | |
| 7261 | /* TODO: later accept arguments. */ |
| 7262 | rule->arg.hlua_rule->args = NULL; |
| 7263 | |
| 7264 | /* Add applet pointer in the rule. */ |
| 7265 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7266 | rule->applet.name = fcn->name; |
| 7267 | rule->applet.init = hlua_applet_http_init; |
| 7268 | rule->applet.fct = hlua_applet_http_fct; |
| 7269 | rule->applet.release = hlua_applet_http_release; |
| 7270 | rule->applet.timeout = hlua_timeout_applet; |
| 7271 | |
| 7272 | return ACT_RET_PRS_OK; |
| 7273 | } |
| 7274 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7275 | /* This function is an LUA binding used for registering |
| 7276 | * "sample-conv" functions. It expects a converter name used |
| 7277 | * in the haproxy configuration file, and an LUA function. |
| 7278 | */ |
| 7279 | __LJMP static int hlua_register_action(lua_State *L) |
| 7280 | { |
| 7281 | struct action_kw_list *akl; |
| 7282 | const char *name; |
| 7283 | int ref; |
| 7284 | int len; |
| 7285 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7286 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7287 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7288 | /* Initialise the number of expected arguments at 0. */ |
| 7289 | nargs = 0; |
| 7290 | |
| 7291 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7292 | 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] | 7293 | |
| 7294 | /* First argument : converter name. */ |
| 7295 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7296 | |
| 7297 | /* Second argument : environment. */ |
| 7298 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7299 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7300 | |
| 7301 | /* Third argument : lua function. */ |
| 7302 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7303 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7304 | /* 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] | 7305 | if (lua_gettop(L) >= 4) |
| 7306 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7307 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7308 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7309 | lua_pushnil(L); |
| 7310 | while (lua_next(L, 2) != 0) { |
| 7311 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7312 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7313 | |
| 7314 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7315 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7316 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7317 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7318 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7319 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7320 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7321 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7322 | |
| 7323 | /* Fill fcn. */ |
| 7324 | fcn->name = strdup(name); |
| 7325 | if (!fcn->name) |
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 | fcn->function_ref = ref; |
| 7328 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7329 | /* Set the expected number od arguments. */ |
| 7330 | fcn->nargs = nargs; |
| 7331 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7332 | /* List head */ |
| 7333 | akl->list.n = akl->list.p = NULL; |
| 7334 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7335 | /* action keyword. */ |
| 7336 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7337 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7338 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7339 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7340 | |
| 7341 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7342 | |
| 7343 | akl->kw[0].match_pfx = 0; |
| 7344 | akl->kw[0].private = fcn; |
| 7345 | akl->kw[0].parse = action_register_lua; |
| 7346 | |
| 7347 | /* select the action registering point. */ |
| 7348 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7349 | tcp_req_cont_keywords_register(akl); |
| 7350 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7351 | tcp_res_cont_keywords_register(akl); |
| 7352 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7353 | http_req_keywords_register(akl); |
| 7354 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7355 | http_res_keywords_register(akl); |
| 7356 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7357 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7358 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7359 | "are expected.", lua_tostring(L, -1))); |
| 7360 | |
| 7361 | /* pop the environment string. */ |
| 7362 | lua_pop(L, 1); |
| 7363 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7364 | return ACT_RET_PRS_OK; |
| 7365 | } |
| 7366 | |
| 7367 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7368 | struct act_rule *rule, char **err) |
| 7369 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7370 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7371 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7372 | if (px->mode == PR_MODE_HTTP) { |
| 7373 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7374 | return ACT_RET_PRS_ERR; |
| 7375 | } |
| 7376 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7377 | /* Memory for the rule. */ |
| 7378 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7379 | if (!rule->arg.hlua_rule) { |
| 7380 | memprintf(err, "out of memory error"); |
| 7381 | return ACT_RET_PRS_ERR; |
| 7382 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7383 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7384 | /* Reference the Lua function and store the reference. */ |
| 7385 | rule->arg.hlua_rule->fcn = *fcn; |
| 7386 | |
| 7387 | /* TODO: later accept arguments. */ |
| 7388 | rule->arg.hlua_rule->args = NULL; |
| 7389 | |
| 7390 | /* Add applet pointer in the rule. */ |
| 7391 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7392 | rule->applet.name = fcn->name; |
| 7393 | rule->applet.init = hlua_applet_tcp_init; |
| 7394 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7395 | rule->applet.release = hlua_applet_tcp_release; |
| 7396 | rule->applet.timeout = hlua_timeout_applet; |
| 7397 | |
| 7398 | return 0; |
| 7399 | } |
| 7400 | |
| 7401 | /* This function is an LUA binding used for registering |
| 7402 | * "sample-conv" functions. It expects a converter name used |
| 7403 | * in the haproxy configuration file, and an LUA function. |
| 7404 | */ |
| 7405 | __LJMP static int hlua_register_service(lua_State *L) |
| 7406 | { |
| 7407 | struct action_kw_list *akl; |
| 7408 | const char *name; |
| 7409 | const char *env; |
| 7410 | int ref; |
| 7411 | int len; |
| 7412 | struct hlua_function *fcn; |
| 7413 | |
| 7414 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7415 | |
| 7416 | /* First argument : converter name. */ |
| 7417 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7418 | |
| 7419 | /* Second argument : environment. */ |
| 7420 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7421 | |
| 7422 | /* Third argument : lua function. */ |
| 7423 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7424 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7425 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7426 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7427 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7428 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7429 | fcn = calloc(1, sizeof(*fcn)); |
| 7430 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7431 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7432 | |
| 7433 | /* Fill fcn. */ |
| 7434 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7435 | fcn->name = calloc(1, len); |
| 7436 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7437 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7438 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7439 | fcn->function_ref = ref; |
| 7440 | |
| 7441 | /* List head */ |
| 7442 | akl->list.n = akl->list.p = NULL; |
| 7443 | |
| 7444 | /* converter keyword. */ |
| 7445 | len = strlen("lua.") + strlen(name) + 1; |
| 7446 | akl->kw[0].kw = calloc(1, len); |
| 7447 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7448 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7449 | |
| 7450 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7451 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7452 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7453 | if (strcmp(env, "tcp") == 0) |
| 7454 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7455 | else if (strcmp(env, "http") == 0) |
| 7456 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7457 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7458 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7459 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7460 | |
| 7461 | akl->kw[0].match_pfx = 0; |
| 7462 | akl->kw[0].private = fcn; |
| 7463 | |
| 7464 | /* End of array. */ |
| 7465 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7466 | |
| 7467 | /* Register this new converter */ |
| 7468 | service_keywords_register(akl); |
| 7469 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7470 | return 0; |
| 7471 | } |
| 7472 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7473 | /* This function initialises Lua cli handler. It copies the |
| 7474 | * arguments in the Lua stack and create channel IO objects. |
| 7475 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7476 | 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] | 7477 | { |
| 7478 | struct hlua *hlua; |
| 7479 | struct hlua_function *fcn; |
| 7480 | int i; |
| 7481 | const char *error; |
| 7482 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7483 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7484 | appctx->ctx.hlua_cli.fcn = private; |
| 7485 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7486 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7487 | if (!hlua) { |
| 7488 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7489 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7490 | } |
| 7491 | HLUA_INIT(hlua); |
| 7492 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7493 | |
| 7494 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7495 | * 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] | 7496 | * applet_http. It is absolutely compatible. |
| 7497 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7498 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7499 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7500 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7501 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7502 | } |
| 7503 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7504 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7505 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7506 | |
| 7507 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7508 | 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] | 7509 | 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] | 7510 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7511 | } |
| 7512 | |
| 7513 | /* The following Lua calls can fail. */ |
| 7514 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7515 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7516 | error = lua_tostring(hlua->T, -1); |
| 7517 | else |
| 7518 | error = "critical error"; |
| 7519 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7520 | goto error; |
| 7521 | } |
| 7522 | |
| 7523 | /* Check stack available size. */ |
| 7524 | if (!lua_checkstack(hlua->T, 2)) { |
| 7525 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7526 | goto error; |
| 7527 | } |
| 7528 | |
| 7529 | /* Restore the function in the stack. */ |
| 7530 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7531 | |
| 7532 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7533 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7534 | */ |
| 7535 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7536 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7537 | goto error; |
| 7538 | } |
| 7539 | hlua->nargs = 1; |
| 7540 | |
| 7541 | /* push keywords in the stack. */ |
| 7542 | for (i = 0; *args[i]; i++) { |
| 7543 | /* Check stack available size. */ |
| 7544 | if (!lua_checkstack(hlua->T, 1)) { |
| 7545 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7546 | goto error; |
| 7547 | } |
| 7548 | lua_pushstring(hlua->T, args[i]); |
| 7549 | hlua->nargs++; |
| 7550 | } |
| 7551 | |
| 7552 | /* We must initialize the execution timeouts. */ |
| 7553 | hlua->max_time = hlua_timeout_session; |
| 7554 | |
| 7555 | /* At this point the execution is safe. */ |
| 7556 | RESET_SAFE_LJMP(hlua->T); |
| 7557 | |
| 7558 | /* It's ok */ |
| 7559 | return 0; |
| 7560 | |
| 7561 | /* It's not ok. */ |
| 7562 | error: |
| 7563 | RESET_SAFE_LJMP(hlua->T); |
| 7564 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7565 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7566 | return 1; |
| 7567 | } |
| 7568 | |
| 7569 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7570 | { |
| 7571 | struct hlua *hlua; |
| 7572 | struct stream_interface *si; |
| 7573 | struct hlua_function *fcn; |
| 7574 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7575 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7576 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7577 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7578 | |
| 7579 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7580 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7581 | return 1; |
| 7582 | |
| 7583 | /* Execute the function. */ |
| 7584 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7585 | |
| 7586 | /* finished. */ |
| 7587 | case HLUA_E_OK: |
| 7588 | return 1; |
| 7589 | |
| 7590 | /* yield. */ |
| 7591 | case HLUA_E_AGAIN: |
| 7592 | /* We want write. */ |
| 7593 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7594 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7595 | /* Set the timeout. */ |
| 7596 | if (hlua->wake_time != TICK_ETERNITY) |
| 7597 | task_schedule(hlua->task, hlua->wake_time); |
| 7598 | return 0; |
| 7599 | |
| 7600 | /* finished with error. */ |
| 7601 | case HLUA_E_ERRMSG: |
| 7602 | /* Display log. */ |
| 7603 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7604 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7605 | lua_pop(hlua->T, 1); |
| 7606 | return 1; |
| 7607 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7608 | case HLUA_E_ETMOUT: |
| 7609 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7610 | fcn->name); |
| 7611 | return 1; |
| 7612 | |
| 7613 | case HLUA_E_NOMEM: |
| 7614 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7615 | fcn->name); |
| 7616 | return 1; |
| 7617 | |
| 7618 | case HLUA_E_YIELD: /* unexpected */ |
| 7619 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7620 | fcn->name); |
| 7621 | return 1; |
| 7622 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7623 | case HLUA_E_ERR: |
| 7624 | /* Display log. */ |
| 7625 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7626 | fcn->name); |
| 7627 | return 1; |
| 7628 | |
| 7629 | default: |
| 7630 | return 1; |
| 7631 | } |
| 7632 | |
| 7633 | return 1; |
| 7634 | } |
| 7635 | |
| 7636 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7637 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7638 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7639 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7640 | } |
| 7641 | |
| 7642 | /* This function is an LUA binding used for registering |
| 7643 | * new keywords in the cli. It expects a list of keywords |
| 7644 | * which are the "path". It is limited to 5 keywords. A |
| 7645 | * description of the command, a function to be executed |
| 7646 | * for the parsing and a function for io handlers. |
| 7647 | */ |
| 7648 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7649 | { |
| 7650 | struct cli_kw_list *cli_kws; |
| 7651 | const char *message; |
| 7652 | int ref_io; |
| 7653 | int len; |
| 7654 | struct hlua_function *fcn; |
| 7655 | int index; |
| 7656 | int i; |
| 7657 | |
| 7658 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7659 | |
| 7660 | /* First argument : an array of maximum 5 keywords. */ |
| 7661 | if (!lua_istable(L, 1)) |
| 7662 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7663 | |
| 7664 | /* Second argument : string with contextual message. */ |
| 7665 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7666 | |
| 7667 | /* Third and fourth argument : lua function. */ |
| 7668 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7669 | |
| 7670 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7671 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7672 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7673 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7674 | fcn = calloc(1, sizeof(*fcn)); |
| 7675 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7676 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7677 | |
| 7678 | /* Fill path. */ |
| 7679 | index = 0; |
| 7680 | lua_pushnil(L); |
| 7681 | while(lua_next(L, 1) != 0) { |
| 7682 | if (index >= 5) |
| 7683 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7684 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7685 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7686 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7687 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7688 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7689 | index++; |
| 7690 | lua_pop(L, 1); |
| 7691 | } |
| 7692 | |
| 7693 | /* Copy help message. */ |
| 7694 | cli_kws->kw[0].usage = strdup(message); |
| 7695 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7696 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7697 | |
| 7698 | /* Fill fcn io handler. */ |
| 7699 | len = strlen("<lua.cli>") + 1; |
| 7700 | for (i = 0; i < index; i++) |
| 7701 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7702 | fcn->name = calloc(1, len); |
| 7703 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7704 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7705 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7706 | for (i = 0; i < index; i++) { |
| 7707 | strncat((char *)fcn->name, ".", len); |
| 7708 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7709 | } |
| 7710 | strncat((char *)fcn->name, ">", len); |
| 7711 | fcn->function_ref = ref_io; |
| 7712 | |
| 7713 | /* Fill last entries. */ |
| 7714 | cli_kws->kw[0].private = fcn; |
| 7715 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7716 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7717 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7718 | |
| 7719 | /* Register this new converter */ |
| 7720 | cli_register_kw(cli_kws); |
| 7721 | |
| 7722 | return 0; |
| 7723 | } |
| 7724 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7725 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7726 | struct proxy *defpx, const char *file, int line, |
| 7727 | char **err, unsigned int *timeout) |
| 7728 | { |
| 7729 | const char *error; |
| 7730 | |
| 7731 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7732 | if (error == PARSE_TIME_OVER) { |
| 7733 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7734 | args[1], args[0]); |
| 7735 | return -1; |
| 7736 | } |
| 7737 | else if (error == PARSE_TIME_UNDER) { |
| 7738 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7739 | args[1], args[0]); |
| 7740 | return -1; |
| 7741 | } |
| 7742 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7743 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7744 | return -1; |
| 7745 | } |
| 7746 | return 0; |
| 7747 | } |
| 7748 | |
| 7749 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7750 | struct proxy *defpx, const char *file, int line, |
| 7751 | char **err) |
| 7752 | { |
| 7753 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7754 | file, line, err, &hlua_timeout_session); |
| 7755 | } |
| 7756 | |
| 7757 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7758 | struct proxy *defpx, const char *file, int line, |
| 7759 | char **err) |
| 7760 | { |
| 7761 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7762 | file, line, err, &hlua_timeout_task); |
| 7763 | } |
| 7764 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7765 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7766 | struct proxy *defpx, const char *file, int line, |
| 7767 | char **err) |
| 7768 | { |
| 7769 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7770 | file, line, err, &hlua_timeout_applet); |
| 7771 | } |
| 7772 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7773 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7774 | struct proxy *defpx, const char *file, int line, |
| 7775 | char **err) |
| 7776 | { |
| 7777 | char *error; |
| 7778 | |
| 7779 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7780 | if (*error != '\0') { |
| 7781 | memprintf(err, "%s: invalid number", args[0]); |
| 7782 | return -1; |
| 7783 | } |
| 7784 | return 0; |
| 7785 | } |
| 7786 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7787 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7788 | struct proxy *defpx, const char *file, int line, |
| 7789 | char **err) |
| 7790 | { |
| 7791 | char *error; |
| 7792 | |
| 7793 | if (*(args[1]) == 0) { |
| 7794 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7795 | return -1; |
| 7796 | } |
| 7797 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7798 | if (*error != '\0') { |
| 7799 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7800 | return -1; |
| 7801 | } |
| 7802 | return 0; |
| 7803 | } |
| 7804 | |
| 7805 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7806 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7807 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7808 | * the main lua entry point. |
| 7809 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7810 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7811 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7812 | * |
| 7813 | * In some error case, LUA set an error message in top of the stack. This function |
| 7814 | * 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] | 7815 | * |
| 7816 | * This function can fail with an abort() due to an Lua critical error. |
| 7817 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7818 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7819 | */ |
| 7820 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7821 | struct proxy *defpx, const char *file, int line, |
| 7822 | char **err) |
| 7823 | { |
| 7824 | int error; |
| 7825 | |
| 7826 | /* Just load and compile the file. */ |
| 7827 | error = luaL_loadfile(gL.T, args[1]); |
| 7828 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7829 | 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] | 7830 | lua_pop(gL.T, 1); |
| 7831 | return -1; |
| 7832 | } |
| 7833 | |
| 7834 | /* If no syntax error where detected, execute the code. */ |
| 7835 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7836 | switch (error) { |
| 7837 | case LUA_OK: |
| 7838 | break; |
| 7839 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7840 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7841 | lua_pop(gL.T, 1); |
| 7842 | return -1; |
| 7843 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7844 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7845 | return -1; |
| 7846 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7847 | 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] | 7848 | lua_pop(gL.T, 1); |
| 7849 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 7850 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7851 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7852 | 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] | 7853 | lua_pop(gL.T, 1); |
| 7854 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 7855 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7856 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7857 | memprintf(err, "Lua unknown 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; |
| 7860 | } |
| 7861 | |
| 7862 | return 0; |
| 7863 | } |
| 7864 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7865 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7866 | * in the given <ctx>. |
| 7867 | */ |
| 7868 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7869 | { |
| 7870 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7871 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7872 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7873 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7874 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7875 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7876 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7877 | |
| 7878 | return 0; |
| 7879 | } |
| 7880 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7881 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7882 | struct proxy *defpx, const char *file, int line, |
| 7883 | char **err) |
| 7884 | { |
| 7885 | char *path; |
| 7886 | char *type = "path"; |
| 7887 | if (too_many_args(2, args, err, NULL)) { |
| 7888 | return -1; |
| 7889 | } |
| 7890 | |
| 7891 | if (!(*args[1])) { |
| 7892 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7893 | return -1; |
| 7894 | } |
| 7895 | path = args[1]; |
| 7896 | |
| 7897 | if (*args[2]) { |
| 7898 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7899 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7900 | return -1; |
| 7901 | } |
| 7902 | type = args[2]; |
| 7903 | } |
| 7904 | |
| 7905 | return hlua_prepend_path(gL, type, path); |
| 7906 | } |
| 7907 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7908 | /* configuration keywords declaration */ |
| 7909 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7910 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7911 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7912 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7913 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7914 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7915 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7916 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7917 | { 0, NULL, NULL }, |
| 7918 | }}; |
| 7919 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7920 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7921 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7922 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7923 | /* This function can fail with an abort() due to an Lua critical error. |
| 7924 | * We are in the initialisation process of HAProxy, this abort() is |
| 7925 | * tolerated. |
| 7926 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7927 | int hlua_post_init() |
| 7928 | { |
| 7929 | struct hlua_init_function *init; |
| 7930 | const char *msg; |
| 7931 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7932 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7933 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7934 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7935 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7936 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7937 | error = lua_tostring(gL.T, -1); |
| 7938 | else |
| 7939 | error = "critical error"; |
| 7940 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7941 | exit(1); |
| 7942 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7943 | |
| 7944 | #if USE_OPENSSL |
| 7945 | /* Initialize SSL server. */ |
| 7946 | if (socket_ssl.xprt->prepare_srv) { |
| 7947 | int saved_used_backed = global.ssl_used_backend; |
| 7948 | // don't affect maxconn automatic computation |
| 7949 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7950 | global.ssl_used_backend = saved_used_backed; |
| 7951 | } |
| 7952 | #endif |
| 7953 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7954 | hlua_fcn_post_init(gL.T); |
| 7955 | RESET_SAFE_LJMP(gL.T); |
| 7956 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7957 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7958 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7959 | ret = hlua_ctx_resume(&gL, 0); |
| 7960 | switch (ret) { |
| 7961 | case HLUA_E_OK: |
| 7962 | lua_pop(gL.T, -1); |
| 7963 | return 1; |
| 7964 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7965 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7966 | return 0; |
| 7967 | case HLUA_E_ERRMSG: |
| 7968 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7969 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7970 | return 0; |
| 7971 | case HLUA_E_ERR: |
| 7972 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7973 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7974 | return 0; |
| 7975 | } |
| 7976 | } |
| 7977 | return 1; |
| 7978 | } |
| 7979 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7980 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7981 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7982 | * is the previously allocated size or the kind of object in case of a new |
| 7983 | * allocation. <nsize> is the requested new size. |
| 7984 | */ |
| 7985 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7986 | { |
| 7987 | struct hlua_mem_allocator *zone = ud; |
| 7988 | |
| 7989 | if (nsize == 0) { |
| 7990 | /* it's a free */ |
| 7991 | if (ptr) |
| 7992 | zone->allocated -= osize; |
| 7993 | free(ptr); |
| 7994 | return NULL; |
| 7995 | } |
| 7996 | |
| 7997 | if (!ptr) { |
| 7998 | /* it's a new allocation */ |
| 7999 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8000 | return NULL; |
| 8001 | |
| 8002 | ptr = malloc(nsize); |
| 8003 | if (ptr) |
| 8004 | zone->allocated += nsize; |
| 8005 | return ptr; |
| 8006 | } |
| 8007 | |
| 8008 | /* it's a realloc */ |
| 8009 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8010 | return NULL; |
| 8011 | |
| 8012 | ptr = realloc(ptr, nsize); |
| 8013 | if (ptr) |
| 8014 | zone->allocated += nsize - osize; |
| 8015 | return ptr; |
| 8016 | } |
| 8017 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8018 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8019 | * We are in the initialisation process of HAProxy, this abort() is |
| 8020 | * tolerated. |
| 8021 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8022 | void hlua_init(void) |
| 8023 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8024 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8025 | int idx; |
| 8026 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8027 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8028 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8029 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8030 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8031 | struct srv_kw *kw; |
| 8032 | int tmp_error; |
| 8033 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8034 | char *args[] = { /* SSL client configuration. */ |
| 8035 | "ssl", |
| 8036 | "verify", |
| 8037 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8038 | NULL |
| 8039 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8040 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8041 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8042 | /* Init main lua stack. */ |
| 8043 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8044 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8045 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8046 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8047 | hlua_sethlua(&gL); |
| 8048 | gL.Tref = LUA_REFNIL; |
| 8049 | gL.task = NULL; |
| 8050 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8051 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8052 | * the Lua function can fail with an abort. We are in the initialisation |
| 8053 | * process of HAProxy, this abort() is tolerated. |
| 8054 | */ |
| 8055 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8056 | /* Initialise lua. */ |
| 8057 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8058 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8059 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8060 | #ifdef HLUA_PREPEND_PATH |
| 8061 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8062 | #endif |
| 8063 | #ifdef HLUA_PREPEND_CPATH |
| 8064 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8065 | #endif |
| 8066 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8067 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8068 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8069 | /* Set safe environment for the initialisation. */ |
| 8070 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8071 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8072 | error_msg = lua_tostring(gL.T, -1); |
| 8073 | else |
| 8074 | error_msg = "critical error"; |
| 8075 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8076 | exit(1); |
| 8077 | } |
| 8078 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8079 | /* |
| 8080 | * |
| 8081 | * Create "core" object. |
| 8082 | * |
| 8083 | */ |
| 8084 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8085 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8086 | lua_newtable(gL.T); |
| 8087 | |
| 8088 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8089 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8090 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8091 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8092 | /* Register special functions. */ |
| 8093 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8094 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8095 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8096 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8097 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8098 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8099 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8100 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8101 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8102 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8103 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8104 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8105 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8106 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8107 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8108 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8109 | hlua_class_function(gL.T, "log", hlua_log); |
| 8110 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8111 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8112 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8113 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8114 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8115 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8116 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8117 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8118 | |
| 8119 | /* |
| 8120 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8121 | * Create "act" object. |
| 8122 | * |
| 8123 | */ |
| 8124 | |
| 8125 | /* This table entry is the object "act" base. */ |
| 8126 | lua_newtable(gL.T); |
| 8127 | |
| 8128 | /* push action return constants */ |
| 8129 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8130 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8131 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8132 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8133 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8134 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8135 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8136 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8137 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8138 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8139 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8140 | lua_setglobal(gL.T, "act"); |
| 8141 | |
| 8142 | /* |
| 8143 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8144 | * Register class Map |
| 8145 | * |
| 8146 | */ |
| 8147 | |
| 8148 | /* This table entry is the object "Map" base. */ |
| 8149 | lua_newtable(gL.T); |
| 8150 | |
| 8151 | /* register pattern types. */ |
| 8152 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8153 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8154 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8155 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8156 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8157 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8158 | |
| 8159 | /* register constructor. */ |
| 8160 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8161 | |
| 8162 | /* Create and fill the metatable. */ |
| 8163 | lua_newtable(gL.T); |
| 8164 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8165 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8166 | lua_pushstring(gL.T, "__index"); |
| 8167 | lua_newtable(gL.T); |
| 8168 | |
| 8169 | /* Register . */ |
| 8170 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8171 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8172 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8173 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8174 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8175 | /* Register previous table in the registry with reference and named entry. |
| 8176 | * The function hlua_register_metatable() pops the stack, so we |
| 8177 | * previously create a copy of the table. |
| 8178 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8179 | 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] | 8180 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8181 | |
| 8182 | /* Assign the metatable to the mai Map object. */ |
| 8183 | lua_setmetatable(gL.T, -2); |
| 8184 | |
| 8185 | /* Set a name to the table. */ |
| 8186 | lua_setglobal(gL.T, "Map"); |
| 8187 | |
| 8188 | /* |
| 8189 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8190 | * Register class Channel |
| 8191 | * |
| 8192 | */ |
| 8193 | |
| 8194 | /* Create and fill the metatable. */ |
| 8195 | lua_newtable(gL.T); |
| 8196 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8197 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8198 | lua_pushstring(gL.T, "__index"); |
| 8199 | lua_newtable(gL.T); |
| 8200 | |
| 8201 | /* Register . */ |
| 8202 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8203 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8204 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8205 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8206 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8207 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8208 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8209 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8210 | 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] | 8211 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8212 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8213 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8214 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8215 | |
| 8216 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8217 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8218 | |
| 8219 | /* |
| 8220 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8221 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8222 | * |
| 8223 | */ |
| 8224 | |
| 8225 | /* Create and fill the metatable. */ |
| 8226 | lua_newtable(gL.T); |
| 8227 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8228 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8229 | lua_pushstring(gL.T, "__index"); |
| 8230 | lua_newtable(gL.T); |
| 8231 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8232 | /* Browse existing fetches and create the associated |
| 8233 | * object method. |
| 8234 | */ |
| 8235 | sf = NULL; |
| 8236 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8237 | |
| 8238 | /* Dont register the keywork if the arguments check function are |
| 8239 | * not safe during the runtime. |
| 8240 | */ |
| 8241 | if ((sf->val_args != NULL) && |
| 8242 | (sf->val_args != val_payload_lv) && |
| 8243 | (sf->val_args != val_hdr)) |
| 8244 | continue; |
| 8245 | |
| 8246 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8247 | * by an underscore. |
| 8248 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8249 | strncpy(trash.area, sf->kw, trash.size); |
| 8250 | trash.area[trash.size - 1] = '\0'; |
| 8251 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8252 | if (*p == '.' || *p == '-' || *p == '+') |
| 8253 | *p = '_'; |
| 8254 | |
| 8255 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8256 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8257 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8258 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8259 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8260 | } |
| 8261 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8262 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8263 | |
| 8264 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8265 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8266 | |
| 8267 | /* |
| 8268 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8269 | * Register class Converters |
| 8270 | * |
| 8271 | */ |
| 8272 | |
| 8273 | /* Create and fill the metatable. */ |
| 8274 | lua_newtable(gL.T); |
| 8275 | |
| 8276 | /* Create and fill the __index entry. */ |
| 8277 | lua_pushstring(gL.T, "__index"); |
| 8278 | lua_newtable(gL.T); |
| 8279 | |
| 8280 | /* Browse existing converters and create the associated |
| 8281 | * object method. |
| 8282 | */ |
| 8283 | sc = NULL; |
| 8284 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8285 | /* Dont register the keywork if the arguments check function are |
| 8286 | * not safe during the runtime. |
| 8287 | */ |
| 8288 | if (sc->val_args != NULL) |
| 8289 | continue; |
| 8290 | |
| 8291 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8292 | * by an underscore. |
| 8293 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8294 | strncpy(trash.area, sc->kw, trash.size); |
| 8295 | trash.area[trash.size - 1] = '\0'; |
| 8296 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8297 | if (*p == '.' || *p == '-' || *p == '+') |
| 8298 | *p = '_'; |
| 8299 | |
| 8300 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8301 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8302 | lua_pushlightuserdata(gL.T, sc); |
| 8303 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8304 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8305 | } |
| 8306 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8307 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8308 | |
| 8309 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8310 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8311 | |
| 8312 | /* |
| 8313 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8314 | * Register class HTTP |
| 8315 | * |
| 8316 | */ |
| 8317 | |
| 8318 | /* Create and fill the metatable. */ |
| 8319 | lua_newtable(gL.T); |
| 8320 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8321 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8322 | lua_pushstring(gL.T, "__index"); |
| 8323 | lua_newtable(gL.T); |
| 8324 | |
| 8325 | /* Register Lua functions. */ |
| 8326 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8327 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8328 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8329 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8330 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8331 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8332 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8333 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8334 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8335 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8336 | |
| 8337 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8338 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8339 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8340 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8341 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8342 | 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] | 8343 | 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] | 8344 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8345 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8346 | |
| 8347 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8348 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8349 | |
| 8350 | /* |
| 8351 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8352 | * Register class AppletTCP |
| 8353 | * |
| 8354 | */ |
| 8355 | |
| 8356 | /* Create and fill the metatable. */ |
| 8357 | lua_newtable(gL.T); |
| 8358 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8359 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8360 | lua_pushstring(gL.T, "__index"); |
| 8361 | lua_newtable(gL.T); |
| 8362 | |
| 8363 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8364 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8365 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8366 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8367 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8368 | 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] | 8369 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8370 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8371 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8372 | |
| 8373 | lua_settable(gL.T, -3); |
| 8374 | |
| 8375 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8376 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8377 | |
| 8378 | /* |
| 8379 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8380 | * Register class AppletHTTP |
| 8381 | * |
| 8382 | */ |
| 8383 | |
| 8384 | /* Create and fill the metatable. */ |
| 8385 | lua_newtable(gL.T); |
| 8386 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8387 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8388 | lua_pushstring(gL.T, "__index"); |
| 8389 | lua_newtable(gL.T); |
| 8390 | |
| 8391 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8392 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8393 | 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] | 8394 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8395 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8396 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8397 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8398 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8399 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8400 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8401 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8402 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8403 | |
| 8404 | lua_settable(gL.T, -3); |
| 8405 | |
| 8406 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8407 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8408 | |
| 8409 | /* |
| 8410 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8411 | * Register class TXN |
| 8412 | * |
| 8413 | */ |
| 8414 | |
| 8415 | /* Create and fill the metatable. */ |
| 8416 | lua_newtable(gL.T); |
| 8417 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8418 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8419 | lua_pushstring(gL.T, "__index"); |
| 8420 | lua_newtable(gL.T); |
| 8421 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8422 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8423 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8424 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8425 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8426 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8427 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8428 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8429 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8430 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8431 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8432 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8433 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8434 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8435 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8436 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8437 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8438 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8439 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8440 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8441 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8442 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8443 | |
| 8444 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8445 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8446 | |
| 8447 | /* |
| 8448 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8449 | * Register class reply |
| 8450 | * |
| 8451 | */ |
| 8452 | lua_newtable(gL.T); |
| 8453 | lua_pushstring(gL.T, "__index"); |
| 8454 | lua_newtable(gL.T); |
| 8455 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8456 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8457 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8458 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8459 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8460 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8461 | |
| 8462 | |
| 8463 | /* |
| 8464 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8465 | * Register class Socket |
| 8466 | * |
| 8467 | */ |
| 8468 | |
| 8469 | /* Create and fill the metatable. */ |
| 8470 | lua_newtable(gL.T); |
| 8471 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8472 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8473 | lua_pushstring(gL.T, "__index"); |
| 8474 | lua_newtable(gL.T); |
| 8475 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8476 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8477 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8478 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8479 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8480 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8481 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8482 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8483 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8484 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8485 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8486 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8487 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8488 | 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] | 8489 | |
| 8490 | /* Register the garbage collector entry. */ |
| 8491 | lua_pushstring(gL.T, "__gc"); |
| 8492 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
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 previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8496 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8497 | |
| 8498 | /* Proxy and server configuration initialisation. */ |
| 8499 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8500 | init_new_proxy(&socket_proxy); |
| 8501 | socket_proxy.parent = NULL; |
| 8502 | socket_proxy.last_change = now.tv_sec; |
| 8503 | socket_proxy.id = "LUA-SOCKET"; |
| 8504 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8505 | socket_proxy.maxconn = 0; |
| 8506 | socket_proxy.accept = NULL; |
| 8507 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8508 | socket_proxy.srv = NULL; |
| 8509 | socket_proxy.conn_retries = 0; |
| 8510 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8511 | |
| 8512 | /* Init TCP server: unchanged parameters */ |
| 8513 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8514 | socket_tcp.next = NULL; |
| 8515 | socket_tcp.proxy = &socket_proxy; |
| 8516 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8517 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8518 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8519 | socket_tcp.idle_conns = NULL; |
| 8520 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8521 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8522 | socket_tcp.last_change = 0; |
| 8523 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8524 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8525 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8526 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8527 | |
| 8528 | /* XXX: Copy default parameter from default server, |
| 8529 | * but the default server is not initialized. |
| 8530 | */ |
| 8531 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8532 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8533 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8534 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8535 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8536 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8537 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8538 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8539 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8540 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8541 | |
| 8542 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8543 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8544 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8545 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8546 | socket_tcp.check.server = &socket_tcp; |
| 8547 | |
| 8548 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8549 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8550 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8551 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8552 | socket_tcp.agent.server = &socket_tcp; |
| 8553 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8554 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8555 | |
| 8556 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8557 | /* Init TCP server: unchanged parameters */ |
| 8558 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8559 | socket_ssl.next = NULL; |
| 8560 | socket_ssl.proxy = &socket_proxy; |
| 8561 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8562 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8563 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8564 | socket_ssl.idle_conns = NULL; |
| 8565 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8566 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8567 | socket_ssl.last_change = 0; |
| 8568 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8569 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8570 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8571 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8572 | |
| 8573 | /* XXX: Copy default parameter from default server, |
| 8574 | * but the default server is not initialized. |
| 8575 | */ |
| 8576 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8577 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8578 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8579 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8580 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8581 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8582 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8583 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8584 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8585 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8586 | |
| 8587 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8588 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8589 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8590 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8591 | socket_ssl.check.server = &socket_ssl; |
| 8592 | |
| 8593 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8594 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8595 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8596 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8597 | socket_ssl.agent.server = &socket_ssl; |
| 8598 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8599 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8600 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8601 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8602 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8603 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8604 | /* |
| 8605 | * |
| 8606 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8607 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8608 | * features like client certificates and ssl_verify. |
| 8609 | * |
| 8610 | */ |
| 8611 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8612 | if (tmp_error != 0) { |
| 8613 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8614 | abort(); /* This must be never arrives because the command line |
| 8615 | not editable by the user. */ |
| 8616 | } |
| 8617 | idx += kw->skip; |
| 8618 | } |
| 8619 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8620 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8621 | |
| 8622 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8623 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8624 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 8625 | static void hlua_deinit() |
| 8626 | { |
| 8627 | lua_close(gL.T); |
| 8628 | } |
| 8629 | |
| 8630 | REGISTER_POST_DEINIT(hlua_deinit); |
| 8631 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8632 | static void hlua_register_build_options(void) |
| 8633 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8634 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8635 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8636 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8637 | hap_register_build_opts(ptr, 1); |
| 8638 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8639 | |
| 8640 | INITCALL0(STG_REGISTER, hlua_register_build_options); |