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 | 3f0f82e | 2020-06-04 19:42:41 +0200 | [diff] [blame] | 24 | #include <haproxy/applet.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 25 | #include <haproxy/api.h> |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | 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 | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 33 | #include <haproxy/http_htx.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 34 | #include <haproxy/thread.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 35 | #include <haproxy/regex.h> |
Willy Tarreau | 374b442 | 2020-06-02 17:46:16 +0200 | [diff] [blame] | 36 | #include <haproxy/xref.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 37 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 40 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 41 | #include <haproxy/http_fetch.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 42 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 43 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 44 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 45 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 46 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 47 | #include <haproxy/payload.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 48 | #include <haproxy/proxy-t.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 49 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 50 | #include <haproxy/server-t.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 51 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 52 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 53 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 54 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 55 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 56 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 57 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 58 | #include <haproxy/vars.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 59 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 60 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 61 | * macro is used only for identifying the function that can |
| 62 | * not return because a longjmp is executed. |
| 63 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 64 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 65 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 66 | */ |
| 67 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 68 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 69 | #define MAY_LJMP(func) func |
| 70 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 71 | /* This couple of function executes securely some Lua calls outside of |
| 72 | * the lua runtime environment. Each Lua call can return a longjmp |
| 73 | * if it encounter a memory error. |
| 74 | * |
| 75 | * Lua documentation extract: |
| 76 | * |
| 77 | * If an error happens outside any protected environment, Lua calls |
| 78 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 79 | * exiting the host application. Your panic function can avoid this |
| 80 | * exit by never returning (e.g., doing a long jump to your own |
| 81 | * recovery point outside Lua). |
| 82 | * |
| 83 | * The panic function runs as if it were a message handler (see |
| 84 | * §2.3); in particular, the error message is at the top of the |
| 85 | * stack. However, there is no guarantee about stack space. To push |
| 86 | * anything on the stack, the panic function must first check the |
| 87 | * available space (see §4.2). |
| 88 | * |
| 89 | * We must check all the Lua entry point. This includes: |
| 90 | * - The include/proto/hlua.h exported functions |
| 91 | * - the task wrapper function |
| 92 | * - The action wrapper function |
| 93 | * - The converters wrapper function |
| 94 | * - The sample-fetch wrapper functions |
| 95 | * |
| 96 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 97 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 98 | * |
| 99 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 100 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 101 | * because they must be exists in the program stack when the longjmp |
| 102 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 103 | * |
| 104 | * Note that the Lua processing is not really thread safe. It provides |
| 105 | * heavy system which consists to add our own lock function in the Lua |
| 106 | * code and recompile the library. This system will probably not accepted |
| 107 | * by maintainers of various distribs. |
| 108 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 109 | * 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] | 110 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 111 | * of function and a lua_unlock() at the end of the function. So I |
| 112 | * conclude that the Lua thread safe mode just perform a mutex around |
| 113 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 114 | * easier for distro maintainers. |
| 115 | * |
| 116 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 117 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 118 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 119 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 120 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 121 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 122 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 123 | 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] | 124 | |
| 125 | #define SET_SAFE_LJMP(__L) \ |
| 126 | ({ \ |
| 127 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 128 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 130 | lua_atpanic(__L, hlua_panic_safe); \ |
| 131 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 132 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 133 | } else { \ |
| 134 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 135 | ret = 1; \ |
| 136 | } \ |
| 137 | ret; \ |
| 138 | }) |
| 139 | |
| 140 | /* If we are the last function catching Lua errors, we |
| 141 | * must reset the panic function. |
| 142 | */ |
| 143 | #define RESET_SAFE_LJMP(__L) \ |
| 144 | do { \ |
| 145 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 146 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 147 | } while(0) |
| 148 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 149 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 150 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 151 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 152 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 153 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 154 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 155 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 156 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 157 | /* The main Lua execution context. */ |
| 158 | struct hlua gL; |
| 159 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 160 | /* This is the memory pool containing struct lua for applets |
| 161 | * (including cli). |
| 162 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 163 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 164 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 165 | /* Used for Socket connection. */ |
| 166 | static struct proxy socket_proxy; |
| 167 | static struct server socket_tcp; |
| 168 | #ifdef USE_OPENSSL |
| 169 | static struct server socket_ssl; |
| 170 | #endif |
| 171 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 172 | /* List head of the function called at the initialisation time. */ |
| 173 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 174 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 175 | /* The following variables contains the reference of the different |
| 176 | * Lua classes. These references are useful for identify metadata |
| 177 | * associated with an object. |
| 178 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 179 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 180 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 181 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 182 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 183 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 184 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 185 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 186 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 187 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 188 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 189 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 190 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 191 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 192 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 193 | * because a task may remain alive during all the haproxy execution. |
| 194 | */ |
| 195 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 196 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 197 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 198 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 199 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 200 | * it is used for preventing infinite loops. |
| 201 | * |
| 202 | * I test the scheer with an infinite loop containing one incrementation |
| 203 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 204 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 205 | * to one interrupt each 10 000 instructions. |
| 206 | * |
| 207 | * configured | Number of |
| 208 | * instructions | loops executed |
| 209 | * between two | in milions |
| 210 | * forced yields | |
| 211 | * ---------------+--------------- |
| 212 | * 10 | 160 |
| 213 | * 500 | 670 |
| 214 | * 1000 | 680 |
| 215 | * 5000 | 700 |
| 216 | * 7000 | 700 |
| 217 | * 8000 | 700 |
| 218 | * 9000 | 710 <- ceil |
| 219 | * 10000 | 710 |
| 220 | * 100000 | 710 |
| 221 | * 1000000 | 710 |
| 222 | * |
| 223 | */ |
| 224 | static unsigned int hlua_nb_instruction = 10000; |
| 225 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 226 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 227 | * be enforced on any memory allocation. |
| 228 | */ |
| 229 | struct hlua_mem_allocator { |
| 230 | size_t allocated; |
| 231 | size_t limit; |
| 232 | }; |
| 233 | |
| 234 | static struct hlua_mem_allocator hlua_global_allocator; |
| 235 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 236 | /* These functions converts types between HAProxy internal args or |
| 237 | * sample and LUA types. Another function permits to check if the |
| 238 | * LUA stack contains arguments according with an required ARG_T |
| 239 | * format. |
| 240 | */ |
| 241 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 242 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 243 | __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] | 244 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 245 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 246 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 247 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 248 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 249 | __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] | 250 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 251 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 252 | do { \ |
| 253 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 254 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 255 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 256 | } while (0) |
| 257 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 258 | /* Used to check an Lua function type in the stack. It creates and |
| 259 | * returns a reference of the function. This function throws an |
| 260 | * error if the rgument is not a "function". |
| 261 | */ |
| 262 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 263 | { |
| 264 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 265 | 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] | 266 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 267 | } |
| 268 | lua_pushvalue(L, argno); |
| 269 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 270 | } |
| 271 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 272 | /* Return the string that is of the top of the stack. */ |
| 273 | const char *hlua_get_top_error_string(lua_State *L) |
| 274 | { |
| 275 | if (lua_gettop(L) < 1) |
| 276 | return "unknown error"; |
| 277 | if (lua_type(L, -1) != LUA_TSTRING) |
| 278 | return "unknown error"; |
| 279 | return lua_tostring(L, -1); |
| 280 | } |
| 281 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 282 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 283 | { |
| 284 | lua_Debug ar; |
| 285 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 286 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 287 | int filled = 0; |
| 288 | |
| 289 | while (lua_getstack(L, level++, &ar)) { |
| 290 | |
| 291 | /* Add separator */ |
| 292 | if (filled) |
| 293 | chunk_appendf(msg, ", "); |
| 294 | filled = 1; |
| 295 | |
| 296 | /* Fill fields: |
| 297 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 298 | * 'l': fills in the field currentline; |
| 299 | * 'n': fills in the field name and namewhat; |
| 300 | * 't': fills in the field istailcall; |
| 301 | */ |
| 302 | lua_getinfo(L, "Slnt", &ar); |
| 303 | |
| 304 | /* Append code localisation */ |
| 305 | if (ar.currentline > 0) |
| 306 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 307 | else |
| 308 | chunk_appendf(msg, "%s ", ar.short_src); |
| 309 | |
| 310 | /* |
| 311 | * Get function name |
| 312 | * |
| 313 | * if namewhat is no empty, name is defined. |
| 314 | * what contains "Lua" for Lua function, "C" for C function, |
| 315 | * or "main" for main code. |
| 316 | */ |
| 317 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 318 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 319 | |
| 320 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 321 | chunk_appendf(msg, "main chunk"); |
| 322 | |
| 323 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 324 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 325 | |
| 326 | else /* nothing left... */ |
| 327 | chunk_appendf(msg, "?"); |
| 328 | |
| 329 | |
| 330 | /* Display tailed call */ |
| 331 | if (ar.istailcall) |
| 332 | chunk_appendf(msg, " ..."); |
| 333 | } |
| 334 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 335 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 339 | /* This function check the number of arguments available in the |
| 340 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 341 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 342 | */ |
| 343 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 344 | { |
| 345 | if (lua_gettop(L) == nb) |
| 346 | return; |
| 347 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 348 | } |
| 349 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 350 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 351 | * and the line number where the error is encountered. |
| 352 | */ |
| 353 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 354 | { |
| 355 | va_list argp; |
| 356 | va_start(argp, fmt); |
| 357 | luaL_where(L, 1); |
| 358 | lua_pushvfstring(L, fmt, argp); |
| 359 | va_end(argp); |
| 360 | lua_concat(L, 2); |
| 361 | return 1; |
| 362 | } |
| 363 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 364 | /* This functions is used with sample fetch and converters. It |
| 365 | * converts the HAProxy configuration argument in a lua stack |
| 366 | * values. |
| 367 | * |
| 368 | * It takes an array of "arg", and each entry of the array is |
| 369 | * converted and pushed in the LUA stack. |
| 370 | */ |
| 371 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 372 | { |
| 373 | switch (arg->type) { |
| 374 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 375 | case ARGT_TIME: |
| 376 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 377 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 381 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case ARGT_IPV4: |
| 385 | case ARGT_IPV6: |
| 386 | case ARGT_MSK4: |
| 387 | case ARGT_MSK6: |
| 388 | case ARGT_FE: |
| 389 | case ARGT_BE: |
| 390 | case ARGT_TAB: |
| 391 | case ARGT_SRV: |
| 392 | case ARGT_USR: |
| 393 | case ARGT_MAP: |
| 394 | default: |
| 395 | lua_pushnil(L); |
| 396 | break; |
| 397 | } |
| 398 | return 1; |
| 399 | } |
| 400 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 401 | /* 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] | 402 | * 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] | 403 | * with sample fetch wrappers. The input arguments are given to the |
| 404 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 405 | */ |
| 406 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 407 | { |
| 408 | switch (lua_type(L, ud)) { |
| 409 | |
| 410 | case LUA_TNUMBER: |
| 411 | case LUA_TBOOLEAN: |
| 412 | arg->type = ARGT_SINT; |
| 413 | arg->data.sint = lua_tointeger(L, ud); |
| 414 | break; |
| 415 | |
| 416 | case LUA_TSTRING: |
| 417 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 418 | 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] | 419 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 420 | arg->data.str.size = arg->data.str.data; |
| 421 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 422 | break; |
| 423 | |
| 424 | case LUA_TUSERDATA: |
| 425 | case LUA_TNIL: |
| 426 | case LUA_TTABLE: |
| 427 | case LUA_TFUNCTION: |
| 428 | case LUA_TTHREAD: |
| 429 | case LUA_TLIGHTUSERDATA: |
| 430 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 431 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | return 1; |
| 435 | } |
| 436 | |
| 437 | /* the following functions are used to convert a struct sample |
| 438 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 439 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 440 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 441 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 442 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 443 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 444 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 445 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 446 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 447 | break; |
| 448 | |
| 449 | case SMP_T_BIN: |
| 450 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 451 | 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] | 452 | break; |
| 453 | |
| 454 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 455 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 457 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 458 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 459 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 460 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 461 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 462 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 463 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 464 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 465 | 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] | 466 | break; |
| 467 | default: |
| 468 | lua_pushnil(L); |
| 469 | break; |
| 470 | } |
| 471 | break; |
| 472 | |
| 473 | case SMP_T_IPV4: |
| 474 | case SMP_T_IPV6: |
| 475 | 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] | 476 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 477 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 478 | 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] | 479 | else |
| 480 | lua_pushnil(L); |
| 481 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 482 | default: |
| 483 | lua_pushnil(L); |
| 484 | break; |
| 485 | } |
| 486 | return 1; |
| 487 | } |
| 488 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 489 | /* the following functions are used to convert a struct sample |
| 490 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 491 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 492 | */ |
| 493 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 494 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 495 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 496 | |
| 497 | case SMP_T_BIN: |
| 498 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 499 | 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] | 500 | break; |
| 501 | |
| 502 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 503 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 504 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 505 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 506 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 507 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 508 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 509 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 510 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 511 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 512 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 513 | 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] | 514 | break; |
| 515 | default: |
| 516 | lua_pushstring(L, ""); |
| 517 | break; |
| 518 | } |
| 519 | break; |
| 520 | |
| 521 | case SMP_T_SINT: |
| 522 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 523 | case SMP_T_IPV4: |
| 524 | case SMP_T_IPV6: |
| 525 | 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] | 526 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 527 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 528 | 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] | 529 | else |
| 530 | lua_pushstring(L, ""); |
| 531 | break; |
| 532 | default: |
| 533 | lua_pushstring(L, ""); |
| 534 | break; |
| 535 | } |
| 536 | return 1; |
| 537 | } |
| 538 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 539 | /* the following functions are used to convert an Lua type in a |
| 540 | * struct sample. This is useful to provide data from a converter |
| 541 | * to the LUA code. |
| 542 | */ |
| 543 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 544 | { |
| 545 | switch (lua_type(L, ud)) { |
| 546 | |
| 547 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 548 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 549 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 550 | break; |
| 551 | |
| 552 | |
| 553 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 554 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 555 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 556 | break; |
| 557 | |
| 558 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 559 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 560 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 561 | 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] | 562 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 563 | smp->data.u.str.size = smp->data.u.str.data; |
| 564 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 565 | break; |
| 566 | |
| 567 | case LUA_TUSERDATA: |
| 568 | case LUA_TNIL: |
| 569 | case LUA_TTABLE: |
| 570 | case LUA_TFUNCTION: |
| 571 | case LUA_TTHREAD: |
| 572 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 573 | case LUA_TNONE: |
| 574 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 575 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 576 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 577 | break; |
| 578 | } |
| 579 | return 1; |
| 580 | } |
| 581 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 582 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 583 | * 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] | 584 | * 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] | 585 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 586 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 587 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 588 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 589 | __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] | 590 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 591 | { |
| 592 | int min_arg; |
| 593 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 594 | struct proxy *px; |
| 595 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 596 | |
| 597 | idx = 0; |
| 598 | min_arg = ARGM(mask); |
| 599 | mask >>= ARGM_BITS; |
| 600 | |
| 601 | while (1) { |
| 602 | |
| 603 | /* Check oversize. */ |
| 604 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 605 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* Check for mandatory arguments. */ |
| 609 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 610 | if (idx < min_arg) { |
| 611 | |
| 612 | /* If miss other argument than the first one, we return an error. */ |
| 613 | if (idx > 0) |
| 614 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 615 | |
| 616 | /* If first argument have a certain type, some default values |
| 617 | * may be used. See the function smp_resolve_args(). |
| 618 | */ |
| 619 | switch (mask & ARGT_MASK) { |
| 620 | |
| 621 | case ARGT_FE: |
| 622 | if (!(p->cap & PR_CAP_FE)) |
| 623 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 624 | argp[idx].data.prx = p; |
| 625 | argp[idx].type = ARGT_FE; |
| 626 | argp[idx+1].type = ARGT_STOP; |
| 627 | break; |
| 628 | |
| 629 | case ARGT_BE: |
| 630 | if (!(p->cap & PR_CAP_BE)) |
| 631 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 632 | argp[idx].data.prx = p; |
| 633 | argp[idx].type = ARGT_BE; |
| 634 | argp[idx+1].type = ARGT_STOP; |
| 635 | break; |
| 636 | |
| 637 | case ARGT_TAB: |
| 638 | argp[idx].data.prx = p; |
| 639 | argp[idx].type = ARGT_TAB; |
| 640 | argp[idx+1].type = ARGT_STOP; |
| 641 | break; |
| 642 | |
| 643 | default: |
| 644 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 645 | break; |
| 646 | } |
| 647 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 651 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 652 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 653 | argp[idx].type != ARGT_STOP) { |
| 654 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 655 | } |
| 656 | |
| 657 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 658 | argp[idx].type == ARGT_STOP) { |
| 659 | return 0; |
| 660 | } |
| 661 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 662 | /* Convert some argument types. */ |
| 663 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 664 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 665 | if (argp[idx].type != ARGT_SINT) |
| 666 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 667 | argp[idx].type = ARGT_SINT; |
| 668 | break; |
| 669 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 670 | case ARGT_TIME: |
| 671 | if (argp[idx].type != ARGT_SINT) |
| 672 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 673 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 674 | break; |
| 675 | |
| 676 | case ARGT_SIZE: |
| 677 | if (argp[idx].type != ARGT_SINT) |
| 678 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 679 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 680 | break; |
| 681 | |
| 682 | case ARGT_FE: |
| 683 | if (argp[idx].type != ARGT_STR) |
| 684 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 685 | memcpy(trash.area, argp[idx].data.str.area, |
| 686 | argp[idx].data.str.data); |
| 687 | trash.area[argp[idx].data.str.data] = 0; |
| 688 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 689 | if (!argp[idx].data.prx) |
| 690 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 691 | argp[idx].type = ARGT_FE; |
| 692 | break; |
| 693 | |
| 694 | case ARGT_BE: |
| 695 | if (argp[idx].type != ARGT_STR) |
| 696 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 697 | memcpy(trash.area, argp[idx].data.str.area, |
| 698 | argp[idx].data.str.data); |
| 699 | trash.area[argp[idx].data.str.data] = 0; |
| 700 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 701 | if (!argp[idx].data.prx) |
| 702 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 703 | argp[idx].type = ARGT_BE; |
| 704 | break; |
| 705 | |
| 706 | case ARGT_TAB: |
| 707 | if (argp[idx].type != ARGT_STR) |
| 708 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 709 | memcpy(trash.area, argp[idx].data.str.area, |
| 710 | argp[idx].data.str.data); |
| 711 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 712 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 713 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 714 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 715 | argp[idx].type = ARGT_TAB; |
| 716 | break; |
| 717 | |
| 718 | case ARGT_SRV: |
| 719 | if (argp[idx].type != ARGT_STR) |
| 720 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 721 | memcpy(trash.area, argp[idx].data.str.area, |
| 722 | argp[idx].data.str.data); |
| 723 | trash.area[argp[idx].data.str.data] = 0; |
| 724 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 725 | if (sname) { |
| 726 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 727 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 728 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 729 | if (!px) |
| 730 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 731 | } |
| 732 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 733 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 734 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 735 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 736 | argp[idx].data.srv = findserver(px, sname); |
| 737 | if (!argp[idx].data.srv) |
| 738 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 739 | argp[idx].type = ARGT_SRV; |
| 740 | break; |
| 741 | |
| 742 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 743 | memcpy(trash.area, argp[idx].data.str.area, |
| 744 | argp[idx].data.str.data); |
| 745 | trash.area[argp[idx].data.str.data] = 0; |
| 746 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 747 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 748 | argp[idx].type = ARGT_IPV4; |
| 749 | break; |
| 750 | |
| 751 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 752 | memcpy(trash.area, argp[idx].data.str.area, |
| 753 | argp[idx].data.str.data); |
| 754 | trash.area[argp[idx].data.str.data] = 0; |
| 755 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 756 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 757 | argp[idx].type = ARGT_MSK4; |
| 758 | break; |
| 759 | |
| 760 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 761 | memcpy(trash.area, argp[idx].data.str.area, |
| 762 | argp[idx].data.str.data); |
| 763 | trash.area[argp[idx].data.str.data] = 0; |
| 764 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 765 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 766 | argp[idx].type = ARGT_IPV6; |
| 767 | break; |
| 768 | |
| 769 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 770 | memcpy(trash.area, argp[idx].data.str.area, |
| 771 | argp[idx].data.str.data); |
| 772 | trash.area[argp[idx].data.str.data] = 0; |
| 773 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 774 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 775 | argp[idx].type = ARGT_MSK6; |
| 776 | break; |
| 777 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 778 | case ARGT_MAP: |
| 779 | case ARGT_REG: |
| 780 | case ARGT_USR: |
| 781 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 782 | break; |
| 783 | } |
| 784 | |
| 785 | /* Check for type of argument. */ |
| 786 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 787 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 788 | arg_type_names[(mask & ARGT_MASK)], |
| 789 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 790 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 791 | } |
| 792 | |
| 793 | /* Next argument. */ |
| 794 | mask >>= ARGT_BITS; |
| 795 | idx++; |
| 796 | } |
| 797 | } |
| 798 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 799 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 800 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 801 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 802 | * |
| 803 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 804 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 805 | */ |
| 806 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 807 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 808 | struct hlua **hlua = lua_getextraspace(L); |
| 809 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 810 | } |
| 811 | static inline void hlua_sethlua(struct hlua *hlua) |
| 812 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 813 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 814 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 815 | } |
| 816 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 817 | /* This function is used to send logs. It try to send on screen (stderr) |
| 818 | * and on the default syslog server. |
| 819 | */ |
| 820 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 821 | { |
| 822 | struct tm tm; |
| 823 | char *p; |
| 824 | |
| 825 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 826 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 827 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 828 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 829 | /* Break the message if exceed the buffer size. */ |
| 830 | *(p-4) = ' '; |
| 831 | *(p-3) = '.'; |
| 832 | *(p-2) = '.'; |
| 833 | *(p-1) = '.'; |
| 834 | break; |
| 835 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 836 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 837 | *p = *msg; |
| 838 | else |
| 839 | *p = '.'; |
| 840 | } |
| 841 | *p = '\0'; |
| 842 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 843 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 844 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 845 | get_localtime(date.tv_sec, &tm); |
| 846 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 847 | 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] | 848 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 849 | fflush(stderr); |
| 850 | } |
| 851 | } |
| 852 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 853 | /* This function just ensure that the yield will be always |
| 854 | * returned with a timeout and permit to set some flags |
| 855 | */ |
| 856 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 857 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 858 | { |
| 859 | struct hlua *hlua = hlua_gethlua(L); |
| 860 | |
| 861 | /* Set the wake timeout. If timeout is required, we set |
| 862 | * the expiration time. |
| 863 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 864 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 865 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 866 | hlua->flags |= flags; |
| 867 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 868 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 869 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 872 | /* This function initialises the Lua environment stored in the stream. |
| 873 | * 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] | 874 | * 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] | 875 | * |
| 876 | * This function is particular. it initialises a new Lua thread. If the |
| 877 | * initialisation fails (example: out of memory error), the lua function |
| 878 | * throws an error (longjmp). |
| 879 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 880 | * 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] | 881 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 882 | * threads appear, the safe environment set a lock to ensure only one |
| 883 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 884 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 885 | * |
| 886 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 887 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 888 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 889 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 890 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 891 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 892 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 893 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 894 | 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] | 895 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 896 | if (!already_safe) { |
| 897 | if (!SET_SAFE_LJMP(gL.T)) { |
| 898 | lua->Tref = LUA_REFNIL; |
| 899 | return 0; |
| 900 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 901 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 902 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 903 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 904 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 905 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 906 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | lua->T = lua_newthread(gL.T); |
| 908 | if (!lua->T) { |
| 909 | lua->Tref = LUA_REFNIL; |
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 0; |
| 913 | } |
| 914 | hlua_sethlua(lua); |
| 915 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 916 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 917 | if (!already_safe) |
| 918 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 919 | return 1; |
| 920 | } |
| 921 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 922 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 923 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 924 | * is not freed. |
| 925 | */ |
| 926 | void hlua_ctx_destroy(struct hlua *lua) |
| 927 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 928 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 929 | return; |
| 930 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 931 | if (!lua->T) |
| 932 | goto end; |
| 933 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 934 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 935 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 936 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 937 | if (!SET_SAFE_LJMP(lua->T)) |
| 938 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 939 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 940 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 941 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 942 | if (!SET_SAFE_LJMP(gL.T)) |
| 943 | return; |
| 944 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 945 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 946 | /* Forces a garbage collecting process. If the Lua program is finished |
| 947 | * without error, we run the GC on the thread pointer. Its freed all |
| 948 | * the unused memory. |
| 949 | * If the thread is finnish with an error or is currently yielded, |
| 950 | * it seems that the GC applied on the thread doesn't clean anything, |
| 951 | * so e run the GC on the main thread. |
| 952 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 953 | * the garbage collection. |
| 954 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 955 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 956 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 957 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 958 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 959 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 960 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 961 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 962 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 963 | |
| 964 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 965 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* This function is used to restore the Lua context when a coroutine |
| 969 | * fails. This function copy the common memory between old coroutine |
| 970 | * and the new coroutine. The old coroutine is destroyed, and its |
| 971 | * replaced by the new coroutine. |
| 972 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 973 | * as string error message and it is copied in the new stack. |
| 974 | */ |
| 975 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 976 | { |
| 977 | lua_State *T; |
| 978 | int new_ref; |
| 979 | |
| 980 | /* Renew the main LUA stack doesn't have sense. */ |
| 981 | if (lua == &gL) |
| 982 | return 0; |
| 983 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 984 | /* New Lua coroutine. */ |
| 985 | T = lua_newthread(gL.T); |
| 986 | if (!T) |
| 987 | return 0; |
| 988 | |
| 989 | /* Copy last error message. */ |
| 990 | if (keep_msg) |
| 991 | lua_xmove(lua->T, T, 1); |
| 992 | |
| 993 | /* Copy data between the coroutines. */ |
| 994 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 995 | lua_xmove(lua->T, T, 1); |
| 996 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 997 | |
| 998 | /* Destroy old data. */ |
| 999 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1000 | |
| 1001 | /* The thread is garbage collected by Lua. */ |
| 1002 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1003 | |
| 1004 | /* Fill the struct with the new coroutine values. */ |
| 1005 | lua->Mref = new_ref; |
| 1006 | lua->T = T; |
| 1007 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1008 | |
| 1009 | /* Set context. */ |
| 1010 | hlua_sethlua(lua); |
| 1011 | |
| 1012 | return 1; |
| 1013 | } |
| 1014 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1015 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1016 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1017 | struct hlua *hlua = hlua_gethlua(L); |
| 1018 | |
| 1019 | /* Lua cannot yield when its returning from a function, |
| 1020 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1021 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1022 | */ |
| 1023 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1024 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1025 | return; |
| 1026 | } |
| 1027 | |
| 1028 | /* restore the interrupt condition. */ |
| 1029 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1030 | |
| 1031 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1032 | * If the state is not yieldable, trying yield causes an error. |
| 1033 | */ |
| 1034 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1035 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1036 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1037 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1038 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1039 | hlua->run_time += now_ms - hlua->start_time; |
| 1040 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1041 | lua_pushfstring(L, "execution timeout"); |
| 1042 | WILL_LJMP(lua_error(L)); |
| 1043 | } |
| 1044 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1045 | /* Update the start time. */ |
| 1046 | hlua->start_time = now_ms; |
| 1047 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1048 | /* Try to interrupt the process at the end of the current |
| 1049 | * unyieldable function. |
| 1050 | */ |
| 1051 | 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] | 1052 | } |
| 1053 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1054 | /* This function start or resumes the Lua stack execution. If the flag |
| 1055 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1056 | * The function return an error. |
| 1057 | * |
| 1058 | * The function can returns 4 values: |
| 1059 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1060 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1061 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1062 | * - 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] | 1063 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1064 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1065 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1066 | * 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] | 1067 | * LUA code. |
| 1068 | */ |
| 1069 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1070 | { |
| 1071 | int ret; |
| 1072 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1073 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1074 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1075 | /* Initialise run time counter. */ |
| 1076 | if (!HLUA_IS_RUNNING(lua)) |
| 1077 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1078 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1079 | /* Lock the whole Lua execution. This lock must be before the |
| 1080 | * label "resume_execution". |
| 1081 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1082 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1083 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1084 | resume_execution: |
| 1085 | |
| 1086 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1087 | * instructions. it is used for preventing infinite loops. |
| 1088 | */ |
| 1089 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1090 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1091 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1092 | HLUA_SET_RUN(lua); |
| 1093 | HLUA_CLR_CTRLYIELD(lua); |
| 1094 | HLUA_CLR_WAKERESWR(lua); |
| 1095 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1096 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1097 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1098 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1099 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1100 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1101 | /* Call the function. */ |
| 1102 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 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; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1507 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1508 | args[1].type = ARGT_STOP; |
| 1509 | |
| 1510 | /* load the map. */ |
| 1511 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1512 | /* error case: we cant use luaL_error because we must |
| 1513 | * free the err variable. |
| 1514 | */ |
| 1515 | luaL_where(L, 1); |
| 1516 | lua_pushfstring(L, "'new': %s.", err); |
| 1517 | lua_concat(L, 2); |
| 1518 | free(err); |
| 1519 | WILL_LJMP(lua_error(L)); |
| 1520 | } |
| 1521 | |
| 1522 | /* create the lua object. */ |
| 1523 | lua_newtable(L); |
| 1524 | lua_pushlightuserdata(L, args[0].data.map); |
| 1525 | lua_rawseti(L, -2, 0); |
| 1526 | |
| 1527 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1528 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1529 | lua_setmetatable(L, -2); |
| 1530 | |
| 1531 | |
| 1532 | return 1; |
| 1533 | } |
| 1534 | |
| 1535 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1536 | { |
| 1537 | struct map_descriptor *desc; |
| 1538 | struct pattern *pat; |
| 1539 | struct sample smp; |
| 1540 | |
| 1541 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1542 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1543 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1544 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1545 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1546 | } |
| 1547 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1548 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1549 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1550 | 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] | 1551 | } |
| 1552 | |
| 1553 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1554 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1555 | if (str) |
| 1556 | lua_pushstring(L, ""); |
| 1557 | else |
| 1558 | lua_pushnil(L); |
| 1559 | return 1; |
| 1560 | } |
| 1561 | |
| 1562 | /* 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] | 1563 | 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] | 1564 | return 1; |
| 1565 | } |
| 1566 | |
| 1567 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1568 | { |
| 1569 | return _hlua_map_lookup(L, 0); |
| 1570 | } |
| 1571 | |
| 1572 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1573 | { |
| 1574 | return _hlua_map_lookup(L, 1); |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * |
| 1579 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1580 | * Class Socket |
| 1581 | * |
| 1582 | * |
| 1583 | */ |
| 1584 | |
| 1585 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1586 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1587 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /* 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] | 1591 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1592 | * received. |
| 1593 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1594 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1595 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1596 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1598 | if (appctx->ctx.hlua_cosocket.die) { |
| 1599 | si_shutw(si); |
| 1600 | si_shutr(si); |
| 1601 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1602 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1603 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1604 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1605 | } |
| 1606 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1607 | /* If we cant write, wakeup the pending write signals. */ |
| 1608 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1609 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1610 | |
| 1611 | /* If we cant read, wakeup the pending read signals. */ |
| 1612 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1613 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1614 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1615 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1616 | * to be notified whenever the connection completes. |
| 1617 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1618 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1619 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1620 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1621 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1622 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1623 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1624 | |
| 1625 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1626 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1627 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1628 | /* 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] | 1629 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1630 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1631 | |
| 1632 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1633 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1634 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1635 | |
| 1636 | /* Some data were injected in the buffer, notify the stream |
| 1637 | * interface. |
| 1638 | */ |
| 1639 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1640 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1641 | |
| 1642 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1643 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1644 | */ |
| 1645 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1646 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1647 | } |
| 1648 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1649 | /* This function is called when the "struct stream" is destroyed. |
| 1650 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | * Wake all the pending signals. |
| 1652 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1653 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1654 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1655 | struct xref *peer; |
| 1656 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1657 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1658 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1659 | if (peer) |
| 1660 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1661 | |
| 1662 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1663 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1664 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1668 | * uses this object. If the stream does not exists, just quit. |
| 1669 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1670 | * pending signal can rest in the read and write lists. destroy |
| 1671 | * it. |
| 1672 | */ |
| 1673 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1674 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1675 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1676 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1677 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1679 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1680 | |
| 1681 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1682 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1683 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1685 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1686 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1687 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1688 | appctx->ctx.hlua_cosocket.die = 1; |
| 1689 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1690 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1691 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1692 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1693 | return 0; |
| 1694 | } |
| 1695 | |
| 1696 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1697 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1698 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1699 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1700 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1701 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1702 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1703 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1704 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1706 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1707 | |
| 1708 | /* Check if we run on the same thread than the xreator thread. |
| 1709 | * We cannot access to the socket if the thread is different. |
| 1710 | */ |
| 1711 | if (socket->tid != tid) |
| 1712 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1713 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1714 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1715 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1717 | |
| 1718 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1719 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1720 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1721 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1722 | appctx->ctx.hlua_cosocket.die = 1; |
| 1723 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1724 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1725 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1726 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1727 | return 0; |
| 1728 | } |
| 1729 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1730 | /* The close function calls close_helper. |
| 1731 | */ |
| 1732 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1733 | { |
| 1734 | MAY_LJMP(check_args(L, 1, "close")); |
| 1735 | return hlua_socket_close_helper(L); |
| 1736 | } |
| 1737 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1738 | /* This Lua function assumes that the stack contain three parameters. |
| 1739 | * 1 - USERDATA containing a struct socket |
| 1740 | * 2 - INTEGER with values of the macro defined below |
| 1741 | * If the integer is -1, we must read at most one line. |
| 1742 | * If the integer is -2, we ust read all the data until the |
| 1743 | * end of the stream. |
| 1744 | * If the integer is positive value, we must read a number of |
| 1745 | * bytes corresponding to this value. |
| 1746 | */ |
| 1747 | #define HLSR_READ_LINE (-1) |
| 1748 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1749 | __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] | 1750 | { |
| 1751 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1752 | int wanted = lua_tointeger(L, 2); |
| 1753 | struct hlua *hlua = hlua_gethlua(L); |
| 1754 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1755 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1756 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1757 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1758 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1759 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1760 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1761 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1762 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1763 | struct stream_interface *si; |
| 1764 | struct stream *s; |
| 1765 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1766 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1767 | |
| 1768 | /* Check if this lua stack is schedulable. */ |
| 1769 | if (!hlua || !hlua->task) |
| 1770 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1771 | "'frontend', 'backend' or 'task'")); |
| 1772 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1773 | /* Check if we run on the same thread than the xreator thread. |
| 1774 | * We cannot access to the socket if the thread is different. |
| 1775 | */ |
| 1776 | if (socket->tid != tid) |
| 1777 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1778 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1779 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1780 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1781 | if (!peer) |
| 1782 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1783 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1784 | si = appctx->owner; |
| 1785 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1787 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1788 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1790 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | if (nblk < 0) /* Connection close. */ |
| 1792 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1793 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1795 | |
| 1796 | /* remove final \r\n. */ |
| 1797 | if (nblk == 1) { |
| 1798 | if (blk1[len1-1] == '\n') { |
| 1799 | len1--; |
| 1800 | skip_at_end++; |
| 1801 | if (blk1[len1-1] == '\r') { |
| 1802 | len1--; |
| 1803 | skip_at_end++; |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | else { |
| 1808 | if (blk2[len2-1] == '\n') { |
| 1809 | len2--; |
| 1810 | skip_at_end++; |
| 1811 | if (blk2[len2-1] == '\r') { |
| 1812 | len2--; |
| 1813 | skip_at_end++; |
| 1814 | } |
| 1815 | } |
| 1816 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1821 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | if (nblk < 0) /* Connection close. */ |
| 1823 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1824 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | goto connection_empty; |
| 1826 | } |
| 1827 | |
| 1828 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1830 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | if (nblk < 0) /* Connection close. */ |
| 1832 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1833 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1834 | goto connection_empty; |
| 1835 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1836 | missing_bytes = wanted - socket->b.n; |
| 1837 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1838 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1839 | len1 = missing_bytes; |
| 1840 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1841 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | len = len1; |
| 1845 | |
| 1846 | luaL_addlstring(&socket->b, blk1, len1); |
| 1847 | if (nblk == 2) { |
| 1848 | len += len2; |
| 1849 | luaL_addlstring(&socket->b, blk2, len2); |
| 1850 | } |
| 1851 | |
| 1852 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1853 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1854 | |
| 1855 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1856 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1857 | |
| 1858 | /* If the pattern reclaim to read all the data |
| 1859 | * in the connection, got out. |
| 1860 | */ |
| 1861 | if (wanted == HLSR_READ_ALL) |
| 1862 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1863 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1864 | goto connection_empty; |
| 1865 | |
| 1866 | /* Return result. */ |
| 1867 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1868 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | return 1; |
| 1870 | |
| 1871 | connection_closed: |
| 1872 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1873 | xref_unlock(&socket->xref, peer); |
| 1874 | |
| 1875 | no_peer: |
| 1876 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | /* If the buffer containds data. */ |
| 1878 | if (socket->b.n > 0) { |
| 1879 | luaL_pushresult(&socket->b); |
| 1880 | return 1; |
| 1881 | } |
| 1882 | lua_pushnil(L); |
| 1883 | lua_pushstring(L, "connection closed."); |
| 1884 | return 2; |
| 1885 | |
| 1886 | connection_empty: |
| 1887 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1888 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1889 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1890 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1891 | } |
| 1892 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1893 | 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] | 1894 | return 0; |
| 1895 | } |
| 1896 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1897 | /* This Lua function gets two parameters. The first one can be string |
| 1898 | * or a number. If the string is "*l", the user requires one line. If |
| 1899 | * 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] | 1900 | * If the value is a number, the user require a number of bytes equal |
| 1901 | * to the value. The default value is "*l" (a line). |
| 1902 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1903 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | * integer takes this values: |
| 1905 | * -1 : read a line |
| 1906 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1907 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1909 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | * concatenated with the read data. |
| 1911 | */ |
| 1912 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1913 | { |
| 1914 | int wanted = HLSR_READ_LINE; |
| 1915 | const char *pattern; |
| 1916 | int type; |
| 1917 | char *error; |
| 1918 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1919 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1920 | |
| 1921 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1922 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1923 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1924 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1926 | /* Check if we run on the same thread than the xreator thread. |
| 1927 | * We cannot access to the socket if the thread is different. |
| 1928 | */ |
| 1929 | if (socket->tid != tid) |
| 1930 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1931 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1932 | /* check for pattern. */ |
| 1933 | if (lua_gettop(L) >= 2) { |
| 1934 | type = lua_type(L, 2); |
| 1935 | if (type == LUA_TSTRING) { |
| 1936 | pattern = lua_tostring(L, 2); |
| 1937 | if (strcmp(pattern, "*a") == 0) |
| 1938 | wanted = HLSR_READ_ALL; |
| 1939 | else if (strcmp(pattern, "*l") == 0) |
| 1940 | wanted = HLSR_READ_LINE; |
| 1941 | else { |
| 1942 | wanted = strtoll(pattern, &error, 10); |
| 1943 | if (*error != '\0') |
| 1944 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1945 | } |
| 1946 | } |
| 1947 | else if (type == LUA_TNUMBER) { |
| 1948 | wanted = lua_tointeger(L, 2); |
| 1949 | if (wanted < 0) |
| 1950 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | /* Set pattern. */ |
| 1955 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1956 | |
| 1957 | /* Check if we would replace the top by itself. */ |
| 1958 | if (lua_gettop(L) != 2) |
| 1959 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1961 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | luaL_buffinit(L, &socket->b); |
| 1963 | |
| 1964 | /* Check prefix. */ |
| 1965 | if (lua_gettop(L) >= 3) { |
| 1966 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1967 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1968 | pattern = lua_tolstring(L, 3, &len); |
| 1969 | luaL_addlstring(&socket->b, pattern, len); |
| 1970 | } |
| 1971 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1972 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1976 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1978 | 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] | 1979 | { |
| 1980 | struct hlua_socket *socket; |
| 1981 | struct hlua *hlua = hlua_gethlua(L); |
| 1982 | struct appctx *appctx; |
| 1983 | size_t buf_len; |
| 1984 | const char *buf; |
| 1985 | int len; |
| 1986 | int send_len; |
| 1987 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1988 | struct xref *peer; |
| 1989 | struct stream_interface *si; |
| 1990 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1991 | |
| 1992 | /* Check if this lua stack is schedulable. */ |
| 1993 | if (!hlua || !hlua->task) |
| 1994 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1995 | "'frontend', 'backend' or 'task'")); |
| 1996 | |
| 1997 | /* Get object */ |
| 1998 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1999 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2000 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2002 | /* Check if we run on the same thread than the xreator thread. |
| 2003 | * We cannot access to the socket if the thread is different. |
| 2004 | */ |
| 2005 | if (socket->tid != tid) |
| 2006 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2007 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2008 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2009 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2010 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2011 | lua_pushinteger(L, -1); |
| 2012 | return 1; |
| 2013 | } |
| 2014 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2015 | si = appctx->owner; |
| 2016 | s = si_strm(si); |
| 2017 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2018 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2019 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2020 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | lua_pushinteger(L, -1); |
| 2022 | return 1; |
| 2023 | } |
| 2024 | |
| 2025 | /* Update the input buffer data. */ |
| 2026 | buf += sent; |
| 2027 | send_len = buf_len - sent; |
| 2028 | |
| 2029 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2030 | if (sent >= buf_len) { |
| 2031 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2033 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2034 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2035 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2036 | * the request buffer if its not required. |
| 2037 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2038 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2039 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2040 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2043 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2044 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2045 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2047 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | |
| 2049 | /* send data */ |
| 2050 | if (len < send_len) |
| 2051 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2052 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2053 | |
| 2054 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2055 | * the data" (-2) are not expected because the available length |
| 2056 | * is tested. |
| 2057 | * Other unknown error are also not expected. |
| 2058 | */ |
| 2059 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2060 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2061 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2062 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2063 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2065 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2066 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | return 1; |
| 2068 | } |
| 2069 | |
| 2070 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2071 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2072 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2073 | s->req.rex = TICK_ETERNITY; |
| 2074 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | |
| 2076 | /* Update length sent. */ |
| 2077 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2078 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | |
| 2080 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2081 | if (sent + len >= buf_len) { |
| 2082 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2084 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2085 | |
| 2086 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2087 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2088 | xref_unlock(&socket->xref, peer); |
| 2089 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2090 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2091 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2092 | 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] | 2093 | return 0; |
| 2094 | } |
| 2095 | |
| 2096 | /* This function initiate the send of data. It just check the input |
| 2097 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2098 | * 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] | 2099 | * "hlua_socket_write_yield" that can yield. |
| 2100 | * |
| 2101 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2102 | * the associated object. The second is a string buffer. The third is |
| 2103 | * a facultative integer that represents where is the buffer position |
| 2104 | * of the start of the data that can send. The first byte is the |
| 2105 | * position "1". The default value is "1". The fourth argument is a |
| 2106 | * facultative integer that represents where is the buffer position |
| 2107 | * of the end of the data that can send. The default is the last byte. |
| 2108 | */ |
| 2109 | static int hlua_socket_send(struct lua_State *L) |
| 2110 | { |
| 2111 | int i; |
| 2112 | int j; |
| 2113 | const char *buf; |
| 2114 | size_t buf_len; |
| 2115 | |
| 2116 | /* Check number of arguments. */ |
| 2117 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2118 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2119 | |
| 2120 | /* Get the string. */ |
| 2121 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2122 | |
| 2123 | /* Get and check j. */ |
| 2124 | if (lua_gettop(L) == 4) { |
| 2125 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2126 | if (j < 0) |
| 2127 | j = buf_len + j + 1; |
| 2128 | if (j > buf_len) |
| 2129 | j = buf_len + 1; |
| 2130 | lua_pop(L, 1); |
| 2131 | } |
| 2132 | else |
| 2133 | j = buf_len; |
| 2134 | |
| 2135 | /* Get and check i. */ |
| 2136 | if (lua_gettop(L) == 3) { |
| 2137 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2138 | if (i < 0) |
| 2139 | i = buf_len + i + 1; |
| 2140 | if (i > buf_len) |
| 2141 | i = buf_len + 1; |
| 2142 | lua_pop(L, 1); |
| 2143 | } else |
| 2144 | i = 1; |
| 2145 | |
| 2146 | /* Check bth i and j. */ |
| 2147 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2148 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | return 1; |
| 2150 | } |
| 2151 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2152 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | return 1; |
| 2154 | } |
| 2155 | if (i == 0) |
| 2156 | i = 1; |
| 2157 | if (j == 0) |
| 2158 | j = 1; |
| 2159 | |
| 2160 | /* Pop the string. */ |
| 2161 | lua_pop(L, 1); |
| 2162 | |
| 2163 | /* Update the buffer length. */ |
| 2164 | buf += i - 1; |
| 2165 | buf_len = j - i + 1; |
| 2166 | lua_pushlstring(L, buf, buf_len); |
| 2167 | |
| 2168 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2169 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2171 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | } |
| 2173 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2174 | #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] | 2175 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2176 | { |
| 2177 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2178 | int ret; |
| 2179 | int len; |
| 2180 | char *p; |
| 2181 | |
| 2182 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2183 | if (ret <= 0) { |
| 2184 | lua_pushnil(L); |
| 2185 | return 1; |
| 2186 | } |
| 2187 | |
| 2188 | if (ret == AF_UNIX) { |
| 2189 | lua_pushstring(L, buffer+1); |
| 2190 | return 1; |
| 2191 | } |
| 2192 | else if (ret == AF_INET6) { |
| 2193 | buffer[0] = '['; |
| 2194 | len = strlen(buffer); |
| 2195 | buffer[len] = ']'; |
| 2196 | len++; |
| 2197 | buffer[len] = ':'; |
| 2198 | len++; |
| 2199 | p = buffer; |
| 2200 | } |
| 2201 | else if (ret == AF_INET) { |
| 2202 | p = buffer + 1; |
| 2203 | len = strlen(p); |
| 2204 | p[len] = ':'; |
| 2205 | len++; |
| 2206 | } |
| 2207 | else { |
| 2208 | lua_pushnil(L); |
| 2209 | return 1; |
| 2210 | } |
| 2211 | |
| 2212 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2213 | lua_pushnil(L); |
| 2214 | return 1; |
| 2215 | } |
| 2216 | |
| 2217 | lua_pushstring(L, p); |
| 2218 | return 1; |
| 2219 | } |
| 2220 | |
| 2221 | /* Returns information about the peer of the connection. */ |
| 2222 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2223 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2224 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2225 | struct xref *peer; |
| 2226 | struct appctx *appctx; |
| 2227 | struct stream_interface *si; |
| 2228 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2229 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2230 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2232 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2233 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2234 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2235 | /* Check if we run on the same thread than the xreator thread. |
| 2236 | * We cannot access to the socket if the thread is different. |
| 2237 | */ |
| 2238 | if (socket->tid != tid) |
| 2239 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2240 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2241 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2242 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2243 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | lua_pushnil(L); |
| 2245 | return 1; |
| 2246 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2247 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2248 | si = appctx->owner; |
| 2249 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2250 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2251 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2252 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2253 | lua_pushnil(L); |
| 2254 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2257 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2258 | xref_unlock(&socket->xref, peer); |
| 2259 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | /* Returns information about my connection side. */ |
| 2263 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2264 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2265 | struct hlua_socket *socket; |
| 2266 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2267 | struct appctx *appctx; |
| 2268 | struct xref *peer; |
| 2269 | struct stream_interface *si; |
| 2270 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2271 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2272 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2273 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2274 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2275 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2277 | /* Check if we run on the same thread than the xreator thread. |
| 2278 | * We cannot access to the socket if the thread is different. |
| 2279 | */ |
| 2280 | if (socket->tid != tid) |
| 2281 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2282 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2283 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2284 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2285 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | lua_pushnil(L); |
| 2287 | return 1; |
| 2288 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2289 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2290 | si = appctx->owner; |
| 2291 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2293 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2294 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2295 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2296 | lua_pushnil(L); |
| 2297 | return 1; |
| 2298 | } |
| 2299 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2300 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2301 | xref_unlock(&socket->xref, peer); |
| 2302 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2306 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | .obj_type = OBJ_TYPE_APPLET, |
| 2308 | .name = "<LUA_TCP>", |
| 2309 | .fct = hlua_socket_handler, |
| 2310 | .release = hlua_socket_release, |
| 2311 | }; |
| 2312 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2313 | __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] | 2314 | { |
| 2315 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2316 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2317 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2318 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2319 | struct stream_interface *si; |
| 2320 | struct stream *s; |
| 2321 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2322 | /* Check if we run on the same thread than the xreator thread. |
| 2323 | * We cannot access to the socket if the thread is different. |
| 2324 | */ |
| 2325 | if (socket->tid != tid) |
| 2326 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2327 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2328 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2329 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2330 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | lua_pushnil(L); |
| 2332 | lua_pushstring(L, "Can't connect"); |
| 2333 | return 2; |
| 2334 | } |
| 2335 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2336 | si = appctx->owner; |
| 2337 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2339 | /* Check if we run on the same thread than the xreator thread. |
| 2340 | * We cannot access to the socket if the thread is different. |
| 2341 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2342 | if (socket->tid != tid) { |
| 2343 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2344 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2345 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2346 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2348 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2349 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | lua_pushnil(L); |
| 2351 | lua_pushstring(L, "Can't connect"); |
| 2352 | return 2; |
| 2353 | } |
| 2354 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2355 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | |
| 2357 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2358 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | lua_pushinteger(L, 1); |
| 2361 | return 1; |
| 2362 | } |
| 2363 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2364 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2365 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2367 | } |
| 2368 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2369 | 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] | 2370 | return 0; |
| 2371 | } |
| 2372 | |
| 2373 | /* This function fail or initite the connection. */ |
| 2374 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2375 | { |
| 2376 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2377 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2379 | struct hlua *hlua; |
| 2380 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2381 | int low, high; |
| 2382 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2383 | struct xref *peer; |
| 2384 | struct stream_interface *si; |
| 2385 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2387 | if (lua_gettop(L) < 2) |
| 2388 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | |
| 2390 | /* Get args. */ |
| 2391 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2392 | |
| 2393 | /* Check if we run on the same thread than the xreator thread. |
| 2394 | * We cannot access to the socket if the thread is different. |
| 2395 | */ |
| 2396 | if (socket->tid != tid) |
| 2397 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2398 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2400 | if (lua_gettop(L) >= 3) { |
| 2401 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2402 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2404 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2405 | * that are not enclosed within square brackets. |
| 2406 | */ |
| 2407 | if (port > 0) { |
| 2408 | luaL_buffinit(L, &b); |
| 2409 | luaL_addstring(&b, ip); |
| 2410 | luaL_addchar(&b, ':'); |
| 2411 | luaL_pushresult(&b); |
| 2412 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2413 | } |
| 2414 | } |
| 2415 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2416 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2417 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2418 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2419 | lua_pushnil(L); |
| 2420 | return 1; |
| 2421 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2422 | |
| 2423 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2424 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2425 | if (!addr) { |
| 2426 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2427 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2428 | } |
| 2429 | if (low != high) { |
| 2430 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2431 | 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] | 2432 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2433 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2435 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2436 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2437 | if (port == -1) { |
| 2438 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2439 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2440 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2441 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2442 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | if (port == -1) { |
| 2444 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2445 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2447 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2448 | } |
| 2449 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2450 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2451 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2452 | si = appctx->owner; |
| 2453 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2454 | |
| 2455 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2456 | xref_unlock(&socket->xref, peer); |
| 2457 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2458 | } |
| 2459 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2460 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2462 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2463 | |
| 2464 | /* inform the stream that we want to be notified whenever the |
| 2465 | * connection completes. |
| 2466 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2467 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2468 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2469 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2470 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2471 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2472 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2473 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2474 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2475 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2476 | } |
| 2477 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2478 | |
| 2479 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2480 | /* Return yield waiting for connection. */ |
| 2481 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2482 | 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] | 2483 | |
| 2484 | return 0; |
| 2485 | } |
| 2486 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2487 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2489 | { |
| 2490 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2491 | struct xref *peer; |
| 2492 | struct appctx *appctx; |
| 2493 | struct stream_interface *si; |
| 2494 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2495 | |
| 2496 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2497 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2498 | |
| 2499 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2500 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2501 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2502 | lua_pushnil(L); |
| 2503 | return 1; |
| 2504 | } |
| 2505 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2506 | si = appctx->owner; |
| 2507 | s = si_strm(si); |
| 2508 | |
| 2509 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2510 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2511 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2512 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2513 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | |
| 2515 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2516 | { |
| 2517 | return 0; |
| 2518 | } |
| 2519 | |
| 2520 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2521 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2522 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2523 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2524 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2525 | struct xref *peer; |
| 2526 | struct appctx *appctx; |
| 2527 | struct stream_interface *si; |
| 2528 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2529 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2531 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2532 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2533 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2534 | /* convert the timeout to millis */ |
| 2535 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2536 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2537 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2538 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2539 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2540 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2541 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2542 | 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] | 2543 | |
| 2544 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2545 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2546 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2547 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2548 | /* Check if we run on the same thread than the xreator thread. |
| 2549 | * We cannot access to the socket if the thread is different. |
| 2550 | */ |
| 2551 | if (socket->tid != tid) |
| 2552 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2553 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2554 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2555 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2556 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2557 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2558 | WILL_LJMP(lua_error(L)); |
| 2559 | return 0; |
| 2560 | } |
| 2561 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2562 | si = appctx->owner; |
| 2563 | s = si_strm(si); |
| 2564 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2565 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2566 | s->req.rto = tmout; |
| 2567 | s->req.wto = tmout; |
| 2568 | s->res.rto = tmout; |
| 2569 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2570 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2571 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2572 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2573 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2574 | |
| 2575 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2576 | task_queue(s->task); |
| 2577 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2578 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2579 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2580 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2581 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2585 | { |
| 2586 | struct hlua_socket *socket; |
| 2587 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2588 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2589 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | |
| 2591 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2592 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | hlua_pusherror(L, "socket: full stack"); |
| 2594 | goto out_fail_conf; |
| 2595 | } |
| 2596 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2597 | /* Create the object: obj[0] = userdata. */ |
| 2598 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2599 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2600 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2602 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2604 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2605 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2606 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2607 | goto out_fail_conf; |
| 2608 | } |
| 2609 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2610 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2611 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2612 | lua_setmetatable(L, -2); |
| 2613 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2614 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2615 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2616 | if (!appctx) { |
| 2617 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2618 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2619 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2621 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2622 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2623 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2624 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2625 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2626 | /* Now create a session, task and stream for this applet */ |
| 2627 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2628 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2630 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2633 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2634 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2636 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | } |
| 2638 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2640 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2641 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | /* Configure "right" stream interface. this "si" is used to connect |
| 2643 | * and retrieve data from the server. The connection is initialized |
| 2644 | * with the "struct server". |
| 2645 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2646 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | |
| 2648 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2649 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2650 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2651 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | return 1; |
| 2653 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2654 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2655 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2656 | out_fail_sess: |
| 2657 | appctx_free(appctx); |
| 2658 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2659 | WILL_LJMP(lua_error(L)); |
| 2660 | return 0; |
| 2661 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2662 | |
| 2663 | /* |
| 2664 | * |
| 2665 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2666 | * Class Channel |
| 2667 | * |
| 2668 | * |
| 2669 | */ |
| 2670 | |
| 2671 | /* Returns the struct hlua_channel join to the class channel in the |
| 2672 | * stack entry "ud" or throws an argument error. |
| 2673 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2674 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2675 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2676 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2679 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2680 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2681 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2682 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2683 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2684 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2685 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2686 | return 0; |
| 2687 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2688 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2689 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2690 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2691 | |
| 2692 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2693 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2694 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2695 | return 1; |
| 2696 | } |
| 2697 | |
| 2698 | /* Duplicate all the data present in the input channel and put it |
| 2699 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2700 | * the stack if the channel is closed and all the data are consumed, |
| 2701 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2702 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2703 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2704 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2705 | { |
| 2706 | char *blk1; |
| 2707 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2708 | size_t len1; |
| 2709 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2710 | int ret; |
| 2711 | luaL_Buffer b; |
| 2712 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2713 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2714 | if (unlikely(ret == 0)) |
| 2715 | return 0; |
| 2716 | |
| 2717 | if (unlikely(ret < 0)) { |
| 2718 | lua_pushnil(L); |
| 2719 | return -1; |
| 2720 | } |
| 2721 | |
| 2722 | luaL_buffinit(L, &b); |
| 2723 | luaL_addlstring(&b, blk1, len1); |
| 2724 | if (unlikely(ret == 2)) |
| 2725 | luaL_addlstring(&b, blk2, len2); |
| 2726 | luaL_pushresult(&b); |
| 2727 | |
| 2728 | if (unlikely(ret == 2)) |
| 2729 | return len1 + len2; |
| 2730 | return len1; |
| 2731 | } |
| 2732 | |
| 2733 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2734 | * a yield. This function keep the data in the buffer. |
| 2735 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2736 | __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] | 2737 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2738 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2739 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2740 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2741 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2742 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2743 | WILL_LJMP(lua_error(L)); |
| 2744 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2745 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2746 | 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] | 2747 | return 1; |
| 2748 | } |
| 2749 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2750 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2751 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2752 | { |
| 2753 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2754 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2755 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2756 | } |
| 2757 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2758 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2759 | * a yield. This function consumes the data in the buffer. It returns |
| 2760 | * a string containing the data or a nil pointer if no data are available |
| 2761 | * and the channel is closed. |
| 2762 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2763 | __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] | 2764 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2765 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2766 | int ret; |
| 2767 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2768 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2769 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2770 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2771 | WILL_LJMP(lua_error(L)); |
| 2772 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2773 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2774 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2775 | 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] | 2776 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2777 | if (unlikely(ret == -1)) |
| 2778 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2779 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2780 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2781 | return 1; |
| 2782 | } |
| 2783 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2784 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2785 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2786 | { |
| 2787 | MAY_LJMP(check_args(L, 1, "get")); |
| 2788 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2789 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2790 | } |
| 2791 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | /* This functions consumes and returns one line. If the channel is closed, |
| 2793 | * 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] | 2794 | * 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] | 2795 | * value. |
| 2796 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2797 | __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] | 2798 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2799 | char *blk1; |
| 2800 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2801 | size_t len1; |
| 2802 | size_t len2; |
| 2803 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2804 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2805 | int ret; |
| 2806 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2807 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2808 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2809 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2810 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2811 | WILL_LJMP(lua_error(L)); |
| 2812 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2813 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2814 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2815 | 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] | 2816 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2817 | if (ret == -1) { |
| 2818 | lua_pushnil(L); |
| 2819 | return 1; |
| 2820 | } |
| 2821 | |
| 2822 | luaL_buffinit(L, &b); |
| 2823 | luaL_addlstring(&b, blk1, len1); |
| 2824 | len = len1; |
| 2825 | if (unlikely(ret == 2)) { |
| 2826 | luaL_addlstring(&b, blk2, len2); |
| 2827 | len += len2; |
| 2828 | } |
| 2829 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2830 | 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] | 2831 | return 1; |
| 2832 | } |
| 2833 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2834 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2835 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2836 | { |
| 2837 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2838 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2839 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2840 | } |
| 2841 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2842 | /* This function takes a string as input, and append it at the |
| 2843 | * input side of channel. If the data is too big, but a space |
| 2844 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2845 | * yields. If the data is bigger than the buffer, or if the |
| 2846 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2847 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2848 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2849 | __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] | 2850 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2851 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2852 | size_t len; |
| 2853 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2854 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2855 | int ret; |
| 2856 | int max; |
| 2857 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2858 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2859 | WILL_LJMP(lua_error(L)); |
| 2860 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2861 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2862 | * the request buffer if its not required. |
| 2863 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2864 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2865 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2866 | 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] | 2867 | } |
| 2868 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2869 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2870 | if (max > len - l) |
| 2871 | max = len - l; |
| 2872 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2873 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2874 | if (ret == -2 || ret == -3) { |
| 2875 | lua_pushinteger(L, -1); |
| 2876 | return 1; |
| 2877 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2878 | if (ret == -1) { |
| 2879 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2880 | 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] | 2881 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2882 | l += ret; |
| 2883 | lua_pop(L, 1); |
| 2884 | lua_pushinteger(L, l); |
| 2885 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2886 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2887 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2888 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2889 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2890 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2891 | */ |
| 2892 | return 1; |
| 2893 | } |
| 2894 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2895 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2896 | return 1; |
| 2897 | } |
| 2898 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2899 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2900 | * 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] | 2901 | * buffer size is too little for the data. |
| 2902 | */ |
| 2903 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2904 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2905 | size_t len; |
| 2906 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2907 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2908 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2909 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2910 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2911 | lua_pushinteger(L, 0); |
| 2912 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2913 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | } |
| 2915 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2916 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2917 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2918 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2919 | * or -1 if the channel is closed or if the buffer size is too |
| 2920 | * little for the data. |
| 2921 | */ |
| 2922 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2923 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2924 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2925 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2926 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2927 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | lua_pushinteger(L, 0); |
| 2929 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2930 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2931 | WILL_LJMP(lua_error(L)); |
| 2932 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2933 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2935 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2936 | } |
| 2937 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2938 | /* Append data in the output side of the buffer. This data is immediately |
| 2939 | * sent. The function returns the amount of data written. If the buffer |
| 2940 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2941 | * if the channel is closed. |
| 2942 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2943 | __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] | 2944 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2945 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2946 | size_t len; |
| 2947 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2948 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2949 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2950 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2951 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2952 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2953 | WILL_LJMP(lua_error(L)); |
| 2954 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2955 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2956 | lua_pushinteger(L, -1); |
| 2957 | return 1; |
| 2958 | } |
| 2959 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2960 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2961 | * the request buffer if its not required. |
| 2962 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2963 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2964 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2965 | 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] | 2966 | } |
| 2967 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2968 | /* The written data will be immediately sent, so we can check |
| 2969 | * the available space without taking in account the reserve. |
| 2970 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2971 | * data, because the buffer will be flushed. |
| 2972 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2973 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2974 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2975 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2976 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2977 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2978 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2979 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2980 | return 1; |
| 2981 | |
| 2982 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2983 | if (max > len - l) |
| 2984 | max = len - l; |
| 2985 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2986 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2987 | * detects a non contiguous buffer and realign it. |
| 2988 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 2989 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2990 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2991 | |
| 2992 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2993 | 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] | 2994 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | /* buffer replace considers that the input part is filled. |
| 2996 | * so, I must forward these new data in the output part. |
| 2997 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 2998 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | |
| 3000 | l += max; |
| 3001 | lua_pop(L, 1); |
| 3002 | lua_pushinteger(L, l); |
| 3003 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3004 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3005 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3006 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3007 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3008 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3009 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3011 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3012 | if (l < len) { |
| 3013 | /* If we are waiting for space in the response buffer, we |
| 3014 | * must set the flag WAKERESWR. This flag required the task |
| 3015 | * wake up if any activity is detected on the response buffer. |
| 3016 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3017 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3018 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3019 | else |
| 3020 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3021 | 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] | 3022 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3023 | |
| 3024 | return 1; |
| 3025 | } |
| 3026 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3027 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3028 | * yield the LUA process, and resume it without checking the |
| 3029 | * input arguments. |
| 3030 | */ |
| 3031 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3032 | { |
| 3033 | MAY_LJMP(check_args(L, 2, "send")); |
| 3034 | lua_pushinteger(L, 0); |
| 3035 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3036 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | /* This function forward and amount of butes. The data pass from |
| 3040 | * the input side of the buffer to the output side, and can be |
| 3041 | * forwarded. This function never fails. |
| 3042 | * |
| 3043 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3044 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3045 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3046 | __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] | 3047 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3048 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3049 | int len; |
| 3050 | int l; |
| 3051 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3052 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | |
| 3054 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3055 | |
| 3056 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3057 | WILL_LJMP(lua_error(L)); |
| 3058 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3059 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3060 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3061 | |
| 3062 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3063 | if (max > ci_data(chn)) |
| 3064 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3065 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3066 | l += max; |
| 3067 | |
| 3068 | lua_pop(L, 1); |
| 3069 | lua_pushinteger(L, l); |
| 3070 | |
| 3071 | /* Check if it miss bytes to forward. */ |
| 3072 | if (l < len) { |
| 3073 | /* The the input channel or the output channel are closed, we |
| 3074 | * must return the amount of data forwarded. |
| 3075 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3076 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3077 | return 1; |
| 3078 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3079 | /* If we are waiting for space data in the response buffer, we |
| 3080 | * must set the flag WAKERESWR. This flag required the task |
| 3081 | * wake up if any activity is detected on the response buffer. |
| 3082 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3083 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3084 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3085 | else |
| 3086 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3087 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3088 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3089 | 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] | 3090 | } |
| 3091 | |
| 3092 | return 1; |
| 3093 | } |
| 3094 | |
| 3095 | /* Just check the input and prepare the stack for the previous |
| 3096 | * function "hlua_channel_forward_yield" |
| 3097 | */ |
| 3098 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3099 | { |
| 3100 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3101 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3102 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3103 | |
| 3104 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3105 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /* Just returns the number of bytes available in the input |
| 3109 | * side of the buffer. This function never fails. |
| 3110 | */ |
| 3111 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3112 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3113 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3114 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3115 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3116 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3117 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3118 | struct htx *htx = htxbuf(&chn->buf); |
| 3119 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3120 | } |
| 3121 | else |
| 3122 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | return 1; |
| 3124 | } |
| 3125 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3126 | /* Returns true if the channel is full. */ |
| 3127 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3128 | { |
| 3129 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3130 | |
| 3131 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3132 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3133 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3134 | * applet). |
| 3135 | */ |
| 3136 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3137 | return 1; |
| 3138 | } |
| 3139 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3140 | /* Returns true if the channel is the response channel. */ |
| 3141 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3142 | { |
| 3143 | struct channel *chn; |
| 3144 | |
| 3145 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3146 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3147 | |
| 3148 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3149 | return 1; |
| 3150 | } |
| 3151 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3152 | /* Just returns the number of bytes available in the output |
| 3153 | * side of the buffer. This function never fails. |
| 3154 | */ |
| 3155 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3156 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3157 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3158 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3160 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3161 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | return 1; |
| 3163 | } |
| 3164 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3165 | /* |
| 3166 | * |
| 3167 | * |
| 3168 | * Class Fetches |
| 3169 | * |
| 3170 | * |
| 3171 | */ |
| 3172 | |
| 3173 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3174 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3175 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3176 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3177 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3178 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | /* This function creates and push in the stack a fetch object according |
| 3182 | * with a current TXN. |
| 3183 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3184 | 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] | 3185 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3186 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3187 | |
| 3188 | /* Check stack size. */ |
| 3189 | if (!lua_checkstack(L, 3)) |
| 3190 | return 0; |
| 3191 | |
| 3192 | /* Create the object: obj[0] = userdata. |
| 3193 | * Note that the base of the Fetches object is the |
| 3194 | * transaction object. |
| 3195 | */ |
| 3196 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3197 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3198 | lua_rawseti(L, -2, 0); |
| 3199 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3200 | hsmp->s = txn->s; |
| 3201 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3202 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3203 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3204 | |
| 3205 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3206 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3207 | lua_setmetatable(L, -2); |
| 3208 | |
| 3209 | return 1; |
| 3210 | } |
| 3211 | |
| 3212 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3213 | * It uses closure argument to store the associated sample-fetch. It |
| 3214 | * returns only one argument or throws an error. An error is thrown |
| 3215 | * only if an error is encountered during the argument parsing. If |
| 3216 | * the "sample-fetch" function fails, nil is returned. |
| 3217 | */ |
| 3218 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3219 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3220 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3221 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3222 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3223 | int i; |
| 3224 | struct sample smp; |
| 3225 | |
| 3226 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3227 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3228 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3229 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3230 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3231 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3232 | /* Check execution authorization. */ |
| 3233 | if (f->use & SMP_USE_HTTP_ANY && |
| 3234 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3235 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3236 | "is not available in Lua services", f->kw); |
| 3237 | WILL_LJMP(lua_error(L)); |
| 3238 | } |
| 3239 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3240 | /* Get extra arguments. */ |
| 3241 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3242 | if (i >= ARGM_NBARGS) |
| 3243 | break; |
| 3244 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3245 | } |
| 3246 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3247 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3248 | |
| 3249 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3250 | 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] | 3251 | |
| 3252 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3253 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3254 | lua_pushfstring(L, "error in arguments"); |
| 3255 | WILL_LJMP(lua_error(L)); |
| 3256 | } |
| 3257 | |
| 3258 | /* Initialise the sample. */ |
| 3259 | memset(&smp, 0, sizeof(smp)); |
| 3260 | |
| 3261 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3262 | 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] | 3263 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3264 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3265 | lua_pushstring(L, ""); |
| 3266 | else |
| 3267 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3268 | return 1; |
| 3269 | } |
| 3270 | |
| 3271 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3272 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3273 | hlua_smp2lua_str(L, &smp); |
| 3274 | else |
| 3275 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3276 | return 1; |
| 3277 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3278 | |
| 3279 | /* |
| 3280 | * |
| 3281 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3282 | * Class Converters |
| 3283 | * |
| 3284 | * |
| 3285 | */ |
| 3286 | |
| 3287 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3288 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3289 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3290 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3291 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3292 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | /* This function creates and push in the stack a Converters object |
| 3296 | * according with a current TXN. |
| 3297 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3298 | 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] | 3299 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3300 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3301 | |
| 3302 | /* Check stack size. */ |
| 3303 | if (!lua_checkstack(L, 3)) |
| 3304 | return 0; |
| 3305 | |
| 3306 | /* Create the object: obj[0] = userdata. |
| 3307 | * Note that the base of the Converters object is the |
| 3308 | * same than the TXN object. |
| 3309 | */ |
| 3310 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3311 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3312 | lua_rawseti(L, -2, 0); |
| 3313 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3314 | hsmp->s = txn->s; |
| 3315 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3316 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3317 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3318 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3319 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3320 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3321 | lua_setmetatable(L, -2); |
| 3322 | |
| 3323 | return 1; |
| 3324 | } |
| 3325 | |
| 3326 | /* This function is an LUA binding. It is called with each converter. |
| 3327 | * It uses closure argument to store the associated converter. It |
| 3328 | * returns only one argument or throws an error. An error is thrown |
| 3329 | * only if an error is encountered during the argument parsing. If |
| 3330 | * the converter function function fails, nil is returned. |
| 3331 | */ |
| 3332 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3333 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3334 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3335 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3336 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3337 | int i; |
| 3338 | struct sample smp; |
| 3339 | |
| 3340 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3341 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3342 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3343 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3344 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3345 | |
| 3346 | /* Get extra arguments. */ |
| 3347 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3348 | if (i >= ARGM_NBARGS) |
| 3349 | break; |
| 3350 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3351 | } |
| 3352 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3353 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3354 | |
| 3355 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3356 | 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] | 3357 | |
| 3358 | /* Run the special args checker. */ |
| 3359 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3360 | hlua_pusherror(L, "error in arguments"); |
| 3361 | WILL_LJMP(lua_error(L)); |
| 3362 | } |
| 3363 | |
| 3364 | /* Initialise the sample. */ |
| 3365 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3366 | hlua_pusherror(L, "error in the input argument"); |
| 3367 | WILL_LJMP(lua_error(L)); |
| 3368 | } |
| 3369 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3370 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3371 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3372 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3373 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3374 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3375 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3376 | WILL_LJMP(lua_error(L)); |
| 3377 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3378 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3379 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3380 | hlua_pusherror(L, "error during the input argument casting"); |
| 3381 | WILL_LJMP(lua_error(L)); |
| 3382 | } |
| 3383 | |
| 3384 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3385 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3386 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3387 | lua_pushstring(L, ""); |
| 3388 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3389 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3390 | return 1; |
| 3391 | } |
| 3392 | |
| 3393 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3394 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3395 | hlua_smp2lua_str(L, &smp); |
| 3396 | else |
| 3397 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3398 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3399 | } |
| 3400 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3401 | /* |
| 3402 | * |
| 3403 | * |
| 3404 | * Class AppletTCP |
| 3405 | * |
| 3406 | * |
| 3407 | */ |
| 3408 | |
| 3409 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3410 | * a class stream, otherwise it throws an error. |
| 3411 | */ |
| 3412 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3413 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3414 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | /* This function creates and push in the stack an Applet object |
| 3418 | * according with a current TXN. |
| 3419 | */ |
| 3420 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3421 | { |
| 3422 | struct hlua_appctx *appctx; |
| 3423 | struct stream_interface *si = ctx->owner; |
| 3424 | struct stream *s = si_strm(si); |
| 3425 | struct proxy *p = s->be; |
| 3426 | |
| 3427 | /* Check stack size. */ |
| 3428 | if (!lua_checkstack(L, 3)) |
| 3429 | return 0; |
| 3430 | |
| 3431 | /* Create the object: obj[0] = userdata. |
| 3432 | * Note that the base of the Converters object is the |
| 3433 | * same than the TXN object. |
| 3434 | */ |
| 3435 | lua_newtable(L); |
| 3436 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3437 | lua_rawseti(L, -2, 0); |
| 3438 | appctx->appctx = ctx; |
| 3439 | appctx->htxn.s = s; |
| 3440 | appctx->htxn.p = p; |
| 3441 | |
| 3442 | /* Create the "f" field that contains a list of fetches. */ |
| 3443 | lua_pushstring(L, "f"); |
| 3444 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3445 | return 0; |
| 3446 | lua_settable(L, -3); |
| 3447 | |
| 3448 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3449 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3450 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3451 | return 0; |
| 3452 | lua_settable(L, -3); |
| 3453 | |
| 3454 | /* Create the "c" field that contains a list of converters. */ |
| 3455 | lua_pushstring(L, "c"); |
| 3456 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3457 | return 0; |
| 3458 | lua_settable(L, -3); |
| 3459 | |
| 3460 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3461 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3462 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3463 | return 0; |
| 3464 | lua_settable(L, -3); |
| 3465 | |
| 3466 | /* Pop a class stream metatable and affect it to the table. */ |
| 3467 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3468 | lua_setmetatable(L, -2); |
| 3469 | |
| 3470 | return 1; |
| 3471 | } |
| 3472 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3473 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3474 | { |
| 3475 | struct hlua_appctx *appctx; |
| 3476 | struct stream *s; |
| 3477 | const char *name; |
| 3478 | size_t len; |
| 3479 | struct sample smp; |
| 3480 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3481 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3482 | 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] | 3483 | |
| 3484 | /* It is useles to retrieve the stream, but this function |
| 3485 | * runs only in a stream context. |
| 3486 | */ |
| 3487 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3488 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3489 | s = appctx->htxn.s; |
| 3490 | |
| 3491 | /* Converts the third argument in a sample. */ |
| 3492 | hlua_lua2smp(L, 3, &smp); |
| 3493 | |
| 3494 | /* Store the sample in a variable. */ |
| 3495 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3496 | |
| 3497 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3498 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3499 | else |
| 3500 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3501 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3502 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3506 | { |
| 3507 | struct hlua_appctx *appctx; |
| 3508 | struct stream *s; |
| 3509 | const char *name; |
| 3510 | size_t len; |
| 3511 | struct sample smp; |
| 3512 | |
| 3513 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3514 | |
| 3515 | /* It is useles to retrieve the stream, but this function |
| 3516 | * runs only in a stream context. |
| 3517 | */ |
| 3518 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3519 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3520 | s = appctx->htxn.s; |
| 3521 | |
| 3522 | /* Unset the variable. */ |
| 3523 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3524 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3525 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3529 | { |
| 3530 | struct hlua_appctx *appctx; |
| 3531 | struct stream *s; |
| 3532 | const char *name; |
| 3533 | size_t len; |
| 3534 | struct sample smp; |
| 3535 | |
| 3536 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3537 | |
| 3538 | /* It is useles to retrieve the stream, but this function |
| 3539 | * runs only in a stream context. |
| 3540 | */ |
| 3541 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3542 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3543 | s = appctx->htxn.s; |
| 3544 | |
| 3545 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3546 | if (!vars_get_by_name(name, len, &smp)) { |
| 3547 | lua_pushnil(L); |
| 3548 | return 1; |
| 3549 | } |
| 3550 | |
| 3551 | return hlua_smp2lua(L, &smp); |
| 3552 | } |
| 3553 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3554 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3555 | { |
| 3556 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3557 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3558 | struct hlua *hlua; |
| 3559 | |
| 3560 | /* 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] | 3561 | if (!s->hlua) |
| 3562 | return 0; |
| 3563 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3564 | |
| 3565 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3566 | |
| 3567 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3568 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3569 | |
| 3570 | /* Get and store new value. */ |
| 3571 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3572 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3573 | |
| 3574 | return 0; |
| 3575 | } |
| 3576 | |
| 3577 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3578 | { |
| 3579 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3580 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3581 | struct hlua *hlua; |
| 3582 | |
| 3583 | /* 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] | 3584 | if (!s->hlua) { |
| 3585 | lua_pushnil(L); |
| 3586 | return 1; |
| 3587 | } |
| 3588 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3589 | |
| 3590 | /* Push configuration index in the stack. */ |
| 3591 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3592 | |
| 3593 | return 1; |
| 3594 | } |
| 3595 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3596 | /* If expected data not yet available, it returns a yield. This function |
| 3597 | * consumes the data in the buffer. It returns a string containing the |
| 3598 | * data. This string can be empty. |
| 3599 | */ |
| 3600 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3601 | { |
| 3602 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3603 | struct stream_interface *si = appctx->appctx->owner; |
| 3604 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3605 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3606 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3607 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3608 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3609 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3610 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3611 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &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 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3614 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3615 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3616 | 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] | 3617 | } |
| 3618 | |
| 3619 | /* End of data: commit the total strings and return. */ |
| 3620 | if (ret < 0) { |
| 3621 | luaL_pushresult(&appctx->b); |
| 3622 | return 1; |
| 3623 | } |
| 3624 | |
| 3625 | /* Ensure that the block 2 length is usable. */ |
| 3626 | if (ret == 1) |
| 3627 | len2 = 0; |
| 3628 | |
| 3629 | /* dont check the max length read and dont check. */ |
| 3630 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3631 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3632 | |
| 3633 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3634 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3635 | luaL_pushresult(&appctx->b); |
| 3636 | return 1; |
| 3637 | } |
| 3638 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3639 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3640 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3641 | { |
| 3642 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3643 | |
| 3644 | /* Initialise the string catenation. */ |
| 3645 | luaL_buffinit(L, &appctx->b); |
| 3646 | |
| 3647 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3648 | } |
| 3649 | |
| 3650 | /* If expected data not yet available, it returns a yield. This function |
| 3651 | * consumes the data in the buffer. It returns a string containing the |
| 3652 | * data. This string can be empty. |
| 3653 | */ |
| 3654 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3655 | { |
| 3656 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3657 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3658 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3659 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3660 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3661 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3662 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3663 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3664 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3665 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3666 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &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 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3669 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3670 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3671 | 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] | 3672 | } |
| 3673 | |
| 3674 | /* End of data: commit the total strings and return. */ |
| 3675 | if (ret < 0) { |
| 3676 | luaL_pushresult(&appctx->b); |
| 3677 | return 1; |
| 3678 | } |
| 3679 | |
| 3680 | /* Ensure that the block 2 length is usable. */ |
| 3681 | if (ret == 1) |
| 3682 | len2 = 0; |
| 3683 | |
| 3684 | if (len == -1) { |
| 3685 | |
| 3686 | /* If len == -1, catenate all the data avalaile and |
| 3687 | * yield because we want to get all the data until |
| 3688 | * the end of data stream. |
| 3689 | */ |
| 3690 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3691 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3692 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3693 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3694 | 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] | 3695 | |
| 3696 | } else { |
| 3697 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3698 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3699 | if (len1 > len) |
| 3700 | len1 = len; |
| 3701 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3702 | len -= len1; |
| 3703 | |
| 3704 | /* Copy the second block. */ |
| 3705 | if (len2 > len) |
| 3706 | len2 = len; |
| 3707 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3708 | len -= len2; |
| 3709 | |
| 3710 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3711 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3712 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3713 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3714 | if (len > 0) { |
| 3715 | lua_pushinteger(L, len); |
| 3716 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3717 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3718 | 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] | 3719 | } |
| 3720 | |
| 3721 | /* return the result. */ |
| 3722 | luaL_pushresult(&appctx->b); |
| 3723 | return 1; |
| 3724 | } |
| 3725 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3726 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3727 | hlua_pusherror(L, "Lua: internal error"); |
| 3728 | WILL_LJMP(lua_error(L)); |
| 3729 | return 0; |
| 3730 | } |
| 3731 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3732 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3733 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3734 | { |
| 3735 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3736 | int len = -1; |
| 3737 | |
| 3738 | if (lua_gettop(L) > 2) |
| 3739 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3740 | if (lua_gettop(L) >= 2) { |
| 3741 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3742 | lua_pop(L, 1); |
| 3743 | } |
| 3744 | |
| 3745 | /* Confirm or set the required length */ |
| 3746 | lua_pushinteger(L, len); |
| 3747 | |
| 3748 | /* Initialise the string catenation. */ |
| 3749 | luaL_buffinit(L, &appctx->b); |
| 3750 | |
| 3751 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3752 | } |
| 3753 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3754 | /* Append data in the output side of the buffer. This data is immediately |
| 3755 | * sent. The function returns the amount of data written. If the buffer |
| 3756 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3757 | * if the channel is closed. |
| 3758 | */ |
| 3759 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3760 | { |
| 3761 | size_t len; |
| 3762 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3763 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3764 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3765 | struct stream_interface *si = appctx->appctx->owner; |
| 3766 | struct channel *chn = si_ic(si); |
| 3767 | int max; |
| 3768 | |
| 3769 | /* Get the max amount of data which can write as input in the channel. */ |
| 3770 | max = channel_recv_max(chn); |
| 3771 | if (max > (len - l)) |
| 3772 | max = len - l; |
| 3773 | |
| 3774 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3775 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3776 | |
| 3777 | /* update counters. */ |
| 3778 | l += max; |
| 3779 | lua_pop(L, 1); |
| 3780 | lua_pushinteger(L, l); |
| 3781 | |
| 3782 | /* If some data is not send, declares the situation to the |
| 3783 | * applet, and returns a yield. |
| 3784 | */ |
| 3785 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3786 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3787 | 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] | 3788 | } |
| 3789 | |
| 3790 | return 1; |
| 3791 | } |
| 3792 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3793 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3794 | * yield the LUA process, and resume it without checking the |
| 3795 | * input arguments. |
| 3796 | */ |
| 3797 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3798 | { |
| 3799 | MAY_LJMP(check_args(L, 2, "send")); |
| 3800 | lua_pushinteger(L, 0); |
| 3801 | |
| 3802 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3803 | } |
| 3804 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3805 | /* |
| 3806 | * |
| 3807 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3808 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3809 | * |
| 3810 | * |
| 3811 | */ |
| 3812 | |
| 3813 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3814 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3815 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3816 | __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] | 3817 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3818 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3819 | } |
| 3820 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3821 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3822 | * according with a current TXN. |
| 3823 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3824 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3825 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3826 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3827 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3828 | struct stream_interface *si = ctx->owner; |
| 3829 | struct stream *s = si_strm(si); |
| 3830 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3831 | struct htx *htx; |
| 3832 | struct htx_blk *blk; |
| 3833 | struct htx_sl *sl; |
| 3834 | struct ist path; |
| 3835 | unsigned long long len = 0; |
| 3836 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3837 | |
| 3838 | /* Check stack size. */ |
| 3839 | if (!lua_checkstack(L, 3)) |
| 3840 | return 0; |
| 3841 | |
| 3842 | /* Create the object: obj[0] = userdata. |
| 3843 | * Note that the base of the Converters object is the |
| 3844 | * same than the TXN object. |
| 3845 | */ |
| 3846 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3847 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3848 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3849 | appctx->appctx = ctx; |
| 3850 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3851 | 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] | 3852 | appctx->htxn.s = s; |
| 3853 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3854 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3855 | /* Create the "f" field that contains a list of fetches. */ |
| 3856 | lua_pushstring(L, "f"); |
| 3857 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3858 | return 0; |
| 3859 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3860 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3861 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3862 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3863 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3864 | return 0; |
| 3865 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3866 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3867 | /* Create the "c" field that contains a list of converters. */ |
| 3868 | lua_pushstring(L, "c"); |
| 3869 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3870 | return 0; |
| 3871 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3872 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3873 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3874 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3875 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3876 | return 0; |
| 3877 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3878 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3879 | htx = htxbuf(&s->req.buf); |
| 3880 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3881 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3882 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3883 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3884 | /* Stores the request method. */ |
| 3885 | lua_pushstring(L, "method"); |
| 3886 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3887 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3888 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3889 | /* Stores the http version. */ |
| 3890 | lua_pushstring(L, "version"); |
| 3891 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3892 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3893 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3894 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3895 | * the array on the top of the stack. |
| 3896 | */ |
| 3897 | lua_pushstring(L, "headers"); |
| 3898 | htxn.s = s; |
| 3899 | htxn.p = px; |
| 3900 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 3901 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3902 | return 0; |
| 3903 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3904 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3905 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3906 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3907 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3908 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3909 | p = path.ptr; |
| 3910 | end = path.ptr + path.len; |
| 3911 | q = p; |
| 3912 | while (q < end && *q != '?') |
| 3913 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3914 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3915 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3916 | lua_pushstring(L, "path"); |
| 3917 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3918 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3919 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3920 | /* Stores the query string. */ |
| 3921 | lua_pushstring(L, "qs"); |
| 3922 | if (*q == '?') |
| 3923 | q++; |
| 3924 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3925 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3926 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3927 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3928 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3929 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3930 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3931 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3932 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3933 | break; |
| 3934 | if (type == HTX_BLK_DATA) |
| 3935 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3936 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3937 | if (htx->extra != ULLONG_MAX) |
| 3938 | len += htx->extra; |
| 3939 | |
| 3940 | /* Stores the request path. */ |
| 3941 | lua_pushstring(L, "length"); |
| 3942 | lua_pushinteger(L, len); |
| 3943 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3944 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3945 | /* Create an empty array of HTTP request headers. */ |
| 3946 | lua_pushstring(L, "response"); |
| 3947 | lua_newtable(L); |
| 3948 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3949 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3950 | /* Pop a class stream metatable and affect it to the table. */ |
| 3951 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3952 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3953 | |
| 3954 | return 1; |
| 3955 | } |
| 3956 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3957 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3958 | { |
| 3959 | struct hlua_appctx *appctx; |
| 3960 | struct stream *s; |
| 3961 | const char *name; |
| 3962 | size_t len; |
| 3963 | struct sample smp; |
| 3964 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3965 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3966 | 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] | 3967 | |
| 3968 | /* It is useles to retrieve the stream, but this function |
| 3969 | * runs only in a stream context. |
| 3970 | */ |
| 3971 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3972 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3973 | s = appctx->htxn.s; |
| 3974 | |
| 3975 | /* Converts the third argument in a sample. */ |
| 3976 | hlua_lua2smp(L, 3, &smp); |
| 3977 | |
| 3978 | /* Store the sample in a variable. */ |
| 3979 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3980 | |
| 3981 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3982 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3983 | else |
| 3984 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3985 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3986 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3990 | { |
| 3991 | struct hlua_appctx *appctx; |
| 3992 | struct stream *s; |
| 3993 | const char *name; |
| 3994 | size_t len; |
| 3995 | struct sample smp; |
| 3996 | |
| 3997 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3998 | |
| 3999 | /* It is useles to retrieve the stream, but this function |
| 4000 | * runs only in a stream context. |
| 4001 | */ |
| 4002 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4003 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4004 | s = appctx->htxn.s; |
| 4005 | |
| 4006 | /* Unset the variable. */ |
| 4007 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4008 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4009 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4013 | { |
| 4014 | struct hlua_appctx *appctx; |
| 4015 | struct stream *s; |
| 4016 | const char *name; |
| 4017 | size_t len; |
| 4018 | struct sample smp; |
| 4019 | |
| 4020 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4021 | |
| 4022 | /* It is useles to retrieve the stream, but this function |
| 4023 | * runs only in a stream context. |
| 4024 | */ |
| 4025 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4026 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4027 | s = appctx->htxn.s; |
| 4028 | |
| 4029 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4030 | if (!vars_get_by_name(name, len, &smp)) { |
| 4031 | lua_pushnil(L); |
| 4032 | return 1; |
| 4033 | } |
| 4034 | |
| 4035 | return hlua_smp2lua(L, &smp); |
| 4036 | } |
| 4037 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4038 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4039 | { |
| 4040 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4041 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4042 | struct hlua *hlua; |
| 4043 | |
| 4044 | /* 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] | 4045 | if (!s->hlua) |
| 4046 | return 0; |
| 4047 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4048 | |
| 4049 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4050 | |
| 4051 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4052 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4053 | |
| 4054 | /* Get and store new value. */ |
| 4055 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4056 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4057 | |
| 4058 | return 0; |
| 4059 | } |
| 4060 | |
| 4061 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4062 | { |
| 4063 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4064 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4065 | struct hlua *hlua; |
| 4066 | |
| 4067 | /* 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] | 4068 | if (!s->hlua) { |
| 4069 | lua_pushnil(L); |
| 4070 | return 1; |
| 4071 | } |
| 4072 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4073 | |
| 4074 | /* Push configuration index in the stack. */ |
| 4075 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4076 | |
| 4077 | return 1; |
| 4078 | } |
| 4079 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4080 | /* If expected data not yet available, it returns a yield. This function |
| 4081 | * consumes the data in the buffer. It returns a string containing the |
| 4082 | * data. This string can be empty. |
| 4083 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4084 | __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] | 4085 | { |
| 4086 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4087 | struct stream_interface *si = appctx->appctx->owner; |
| 4088 | struct channel *req = si_oc(si); |
| 4089 | struct htx *htx; |
| 4090 | struct htx_blk *blk; |
| 4091 | size_t count; |
| 4092 | int stop = 0; |
| 4093 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4094 | htx = htx_from_buf(&req->buf); |
| 4095 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4096 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4097 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4098 | while (count && !stop && blk) { |
| 4099 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4100 | uint32_t sz = htx_get_blksz(blk); |
| 4101 | struct ist v; |
| 4102 | uint32_t vlen; |
| 4103 | char *nl; |
| 4104 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4105 | if (type == HTX_BLK_EOM) { |
| 4106 | stop = 1; |
| 4107 | break; |
| 4108 | } |
| 4109 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4110 | vlen = sz; |
| 4111 | if (vlen > count) { |
| 4112 | if (type != HTX_BLK_DATA) |
| 4113 | break; |
| 4114 | vlen = count; |
| 4115 | } |
| 4116 | |
| 4117 | switch (type) { |
| 4118 | case HTX_BLK_UNUSED: |
| 4119 | break; |
| 4120 | |
| 4121 | case HTX_BLK_DATA: |
| 4122 | v = htx_get_blk_value(htx, blk); |
| 4123 | v.len = vlen; |
| 4124 | nl = istchr(v, '\n'); |
| 4125 | if (nl != NULL) { |
| 4126 | stop = 1; |
| 4127 | vlen = nl - v.ptr + 1; |
| 4128 | } |
| 4129 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4130 | break; |
| 4131 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4132 | case HTX_BLK_TLR: |
| 4133 | case HTX_BLK_EOM: |
| 4134 | stop = 1; |
| 4135 | break; |
| 4136 | |
| 4137 | default: |
| 4138 | break; |
| 4139 | } |
| 4140 | |
| 4141 | co_set_data(req, co_data(req) - vlen); |
| 4142 | count -= vlen; |
| 4143 | if (sz == vlen) |
| 4144 | blk = htx_remove_blk(htx, blk); |
| 4145 | else { |
| 4146 | htx_cut_data_blk(htx, blk, vlen); |
| 4147 | break; |
| 4148 | } |
| 4149 | } |
| 4150 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4151 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4152 | if (!stop) { |
| 4153 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4154 | 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] | 4155 | } |
| 4156 | |
| 4157 | /* return the result. */ |
| 4158 | luaL_pushresult(&appctx->b); |
| 4159 | return 1; |
| 4160 | } |
| 4161 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4162 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4163 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4164 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4165 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4166 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4167 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4168 | /* Initialise the string catenation. */ |
| 4169 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4170 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4171 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4172 | } |
| 4173 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4174 | /* If expected data not yet available, it returns a yield. This function |
| 4175 | * consumes the data in the buffer. It returns a string containing the |
| 4176 | * data. This string can be empty. |
| 4177 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4178 | __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] | 4179 | { |
| 4180 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4181 | struct stream_interface *si = appctx->appctx->owner; |
| 4182 | struct channel *req = si_oc(si); |
| 4183 | struct htx *htx; |
| 4184 | struct htx_blk *blk; |
| 4185 | size_t count; |
| 4186 | int len; |
| 4187 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4188 | htx = htx_from_buf(&req->buf); |
| 4189 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4190 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4191 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4192 | while (count && len && blk) { |
| 4193 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4194 | uint32_t sz = htx_get_blksz(blk); |
| 4195 | struct ist v; |
| 4196 | uint32_t vlen; |
| 4197 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4198 | if (type == HTX_BLK_EOM) { |
| 4199 | len = 0; |
| 4200 | break; |
| 4201 | } |
| 4202 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4203 | vlen = sz; |
| 4204 | if (len > 0 && vlen > len) |
| 4205 | vlen = len; |
| 4206 | if (vlen > count) { |
| 4207 | if (type != HTX_BLK_DATA) |
| 4208 | break; |
| 4209 | vlen = count; |
| 4210 | } |
| 4211 | |
| 4212 | switch (type) { |
| 4213 | case HTX_BLK_UNUSED: |
| 4214 | break; |
| 4215 | |
| 4216 | case HTX_BLK_DATA: |
| 4217 | v = htx_get_blk_value(htx, blk); |
| 4218 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4219 | break; |
| 4220 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4221 | case HTX_BLK_TLR: |
| 4222 | case HTX_BLK_EOM: |
| 4223 | len = 0; |
| 4224 | break; |
| 4225 | |
| 4226 | default: |
| 4227 | break; |
| 4228 | } |
| 4229 | |
| 4230 | co_set_data(req, co_data(req) - vlen); |
| 4231 | count -= vlen; |
| 4232 | if (len > 0) |
| 4233 | len -= vlen; |
| 4234 | if (sz == vlen) |
| 4235 | blk = htx_remove_blk(htx, blk); |
| 4236 | else { |
| 4237 | htx_cut_data_blk(htx, blk, vlen); |
| 4238 | break; |
| 4239 | } |
| 4240 | } |
| 4241 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4242 | htx_to_buf(htx, &req->buf); |
| 4243 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4244 | /* If we are no other data available, yield waiting for new data. */ |
| 4245 | if (len) { |
| 4246 | if (len > 0) { |
| 4247 | lua_pushinteger(L, len); |
| 4248 | lua_replace(L, 2); |
| 4249 | } |
| 4250 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4251 | 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] | 4252 | } |
| 4253 | |
| 4254 | /* return the result. */ |
| 4255 | luaL_pushresult(&appctx->b); |
| 4256 | return 1; |
| 4257 | } |
| 4258 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4259 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4260 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4261 | { |
| 4262 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4263 | int len = -1; |
| 4264 | |
| 4265 | /* Check arguments. */ |
| 4266 | if (lua_gettop(L) > 2) |
| 4267 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4268 | if (lua_gettop(L) >= 2) { |
| 4269 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4270 | lua_pop(L, 1); |
| 4271 | } |
| 4272 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4273 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4274 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4275 | /* Initialise the string catenation. */ |
| 4276 | luaL_buffinit(L, &appctx->b); |
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 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | /* Append data in the output side of the buffer. This data is immediately |
| 4282 | * sent. The function returns the amount of data written. If the buffer |
| 4283 | * cannot contain the data, the function yields. The function returns -1 |
| 4284 | * if the channel is closed. |
| 4285 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4286 | __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] | 4287 | { |
| 4288 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4289 | struct stream_interface *si = appctx->appctx->owner; |
| 4290 | struct channel *res = si_ic(si); |
| 4291 | struct htx *htx = htx_from_buf(&res->buf); |
| 4292 | const char *data; |
| 4293 | size_t len; |
| 4294 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4295 | int max; |
| 4296 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4297 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4298 | if (!max) |
| 4299 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4300 | |
| 4301 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4302 | |
| 4303 | /* Get the max amount of data which can write as input in the channel. */ |
| 4304 | if (max > (len - l)) |
| 4305 | max = len - l; |
| 4306 | |
| 4307 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4308 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4309 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4310 | |
| 4311 | /* update counters. */ |
| 4312 | l += max; |
| 4313 | lua_pop(L, 1); |
| 4314 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4315 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4316 | /* If some data is not send, declares the situation to the |
| 4317 | * applet, and returns a yield. |
| 4318 | */ |
| 4319 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4320 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4321 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4322 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4323 | 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] | 4324 | } |
| 4325 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4326 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4327 | return 1; |
| 4328 | } |
| 4329 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4330 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4331 | * yield the LUA process, and resume it without checking the |
| 4332 | * input arguments. |
| 4333 | */ |
| 4334 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4335 | { |
| 4336 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4337 | |
| 4338 | /* We want to send some data. Headers must be sent. */ |
| 4339 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4340 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4341 | WILL_LJMP(lua_error(L)); |
| 4342 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4343 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4344 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4345 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4346 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4347 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4351 | { |
| 4352 | const char *name; |
| 4353 | int ret; |
| 4354 | |
| 4355 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4356 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4357 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4358 | |
| 4359 | /* Push in the stack the "response" entry. */ |
| 4360 | ret = lua_getfield(L, 1, "response"); |
| 4361 | if (ret != LUA_TTABLE) { |
| 4362 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4363 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4364 | WILL_LJMP(lua_error(L)); |
| 4365 | } |
| 4366 | |
| 4367 | /* check if the header is already registered if it is not |
| 4368 | * the case, register it. |
| 4369 | */ |
| 4370 | ret = lua_getfield(L, -1, name); |
| 4371 | if (ret == LUA_TNIL) { |
| 4372 | |
| 4373 | /* Entry not found. */ |
| 4374 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4375 | |
| 4376 | /* Insert the new header name in the array in the top of the stack. |
| 4377 | * It left the new array in the top of the stack. |
| 4378 | */ |
| 4379 | lua_newtable(L); |
| 4380 | lua_pushvalue(L, 2); |
| 4381 | lua_pushvalue(L, -2); |
| 4382 | lua_settable(L, -4); |
| 4383 | |
| 4384 | } else if (ret != LUA_TTABLE) { |
| 4385 | |
| 4386 | /* corruption error. */ |
| 4387 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4388 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4389 | WILL_LJMP(lua_error(L)); |
| 4390 | } |
| 4391 | |
| 4392 | /* Now the top od thestack is an array of values. We push |
| 4393 | * the header value as new entry. |
| 4394 | */ |
| 4395 | lua_pushvalue(L, 3); |
| 4396 | ret = lua_rawlen(L, -2); |
| 4397 | lua_rawseti(L, -2, ret + 1); |
| 4398 | lua_pushboolean(L, 1); |
| 4399 | return 1; |
| 4400 | } |
| 4401 | |
| 4402 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4403 | { |
| 4404 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4405 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4406 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4407 | |
| 4408 | if (status < 100 || status > 599) { |
| 4409 | lua_pushboolean(L, 0); |
| 4410 | return 1; |
| 4411 | } |
| 4412 | |
| 4413 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4414 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4415 | lua_pushboolean(L, 1); |
| 4416 | return 1; |
| 4417 | } |
| 4418 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4419 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4420 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4421 | { |
| 4422 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4423 | struct stream_interface *si = appctx->appctx->owner; |
| 4424 | struct channel *res = si_ic(si); |
| 4425 | struct htx *htx; |
| 4426 | struct htx_sl *sl; |
| 4427 | struct h1m h1m; |
| 4428 | const char *status, *reason; |
| 4429 | const char *name, *value; |
| 4430 | size_t nlen, vlen; |
| 4431 | unsigned int flags; |
| 4432 | |
| 4433 | /* Send the message at once. */ |
| 4434 | htx = htx_from_buf(&res->buf); |
| 4435 | h1m_init_res(&h1m); |
| 4436 | |
| 4437 | /* Use the same http version than the request. */ |
| 4438 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4439 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4440 | if (reason == NULL) |
| 4441 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4442 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4443 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4444 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4445 | } |
| 4446 | else { |
| 4447 | flags = HTX_SL_F_IS_RESP; |
| 4448 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4449 | } |
| 4450 | if (!sl) { |
| 4451 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4452 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4453 | WILL_LJMP(lua_error(L)); |
| 4454 | } |
| 4455 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4456 | |
| 4457 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4458 | lua_pushvalue(L, 0); |
| 4459 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4460 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4461 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4462 | WILL_LJMP(lua_error(L)); |
| 4463 | } |
| 4464 | |
| 4465 | /* Browse the list of headers. */ |
| 4466 | lua_pushnil(L); |
| 4467 | while(lua_next(L, -2) != 0) { |
| 4468 | /* We expect a string as -2. */ |
| 4469 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4470 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4471 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4472 | lua_typename(L, lua_type(L, -2))); |
| 4473 | WILL_LJMP(lua_error(L)); |
| 4474 | } |
| 4475 | name = lua_tolstring(L, -2, &nlen); |
| 4476 | |
| 4477 | /* We expect an array as -1. */ |
| 4478 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4479 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4480 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4481 | name, |
| 4482 | lua_typename(L, lua_type(L, -1))); |
| 4483 | WILL_LJMP(lua_error(L)); |
| 4484 | } |
| 4485 | |
| 4486 | /* Browse the table who is on the top of the stack. */ |
| 4487 | lua_pushnil(L); |
| 4488 | while(lua_next(L, -2) != 0) { |
| 4489 | int id; |
| 4490 | |
| 4491 | /* We expect a number as -2. */ |
| 4492 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4493 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4494 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4495 | name, |
| 4496 | lua_typename(L, lua_type(L, -2))); |
| 4497 | WILL_LJMP(lua_error(L)); |
| 4498 | } |
| 4499 | id = lua_tointeger(L, -2); |
| 4500 | |
| 4501 | /* We expect a string as -2. */ |
| 4502 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4503 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4504 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4505 | name, id, |
| 4506 | lua_typename(L, lua_type(L, -1))); |
| 4507 | WILL_LJMP(lua_error(L)); |
| 4508 | } |
| 4509 | value = lua_tolstring(L, -1, &vlen); |
| 4510 | |
| 4511 | /* Simple Protocol checks. */ |
| 4512 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4513 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4514 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4515 | struct ist v = ist2(value, vlen); |
| 4516 | int ret; |
| 4517 | |
| 4518 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4519 | if (ret < 0) { |
| 4520 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4521 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4522 | name); |
| 4523 | WILL_LJMP(lua_error(L)); |
| 4524 | } |
| 4525 | else if (ret == 0) |
| 4526 | goto next; /* Skip it */ |
| 4527 | } |
| 4528 | |
| 4529 | /* Add a new header */ |
| 4530 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4531 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4532 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4533 | name); |
| 4534 | WILL_LJMP(lua_error(L)); |
| 4535 | } |
| 4536 | next: |
| 4537 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4538 | lua_pop(L, 1); |
| 4539 | } |
| 4540 | |
| 4541 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4542 | lua_pop(L, 1); |
| 4543 | } |
| 4544 | |
| 4545 | if (h1m.flags & H1_MF_CHNK) |
| 4546 | h1m.flags &= ~H1_MF_CLEN; |
| 4547 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4548 | h1m.flags |= H1_MF_XFER_LEN; |
| 4549 | |
| 4550 | /* Uset HTX start-line flags */ |
| 4551 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4552 | flags |= HTX_SL_F_XFER_ENC; |
| 4553 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4554 | flags |= HTX_SL_F_XFER_LEN; |
| 4555 | if (h1m.flags & H1_MF_CHNK) |
| 4556 | flags |= HTX_SL_F_CHNK; |
| 4557 | else if (h1m.flags & H1_MF_CLEN) |
| 4558 | flags |= HTX_SL_F_CLEN; |
| 4559 | if (h1m.body_len == 0) |
| 4560 | flags |= HTX_SL_F_BODYLESS; |
| 4561 | } |
| 4562 | sl->flags |= flags; |
| 4563 | |
| 4564 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4565 | * and the status code implies the presence of a message body, we must |
| 4566 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4567 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4568 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4569 | */ |
| 4570 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4571 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4572 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4573 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4574 | /* Add a new header */ |
| 4575 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4576 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4577 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4578 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4579 | WILL_LJMP(lua_error(L)); |
| 4580 | } |
| 4581 | } |
| 4582 | |
| 4583 | /* Finalize headers. */ |
| 4584 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4585 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4586 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4587 | WILL_LJMP(lua_error(L)); |
| 4588 | } |
| 4589 | |
| 4590 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4591 | b_reset(&res->buf); |
| 4592 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4593 | WILL_LJMP(lua_error(L)); |
| 4594 | } |
| 4595 | |
| 4596 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4597 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4598 | |
| 4599 | /* Headers sent, set the flag. */ |
| 4600 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4601 | return 0; |
| 4602 | |
| 4603 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4604 | /* We will build the status line and the headers of the HTTP response. |
| 4605 | * We will try send at once if its not possible, we give back the hand |
| 4606 | * waiting for more room. |
| 4607 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4608 | __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] | 4609 | { |
| 4610 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4611 | struct stream_interface *si = appctx->appctx->owner; |
| 4612 | struct channel *res = si_ic(si); |
| 4613 | |
| 4614 | if (co_data(res)) { |
| 4615 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4616 | 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] | 4617 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4618 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4622 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4623 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4624 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4625 | } |
| 4626 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4627 | /* |
| 4628 | * |
| 4629 | * |
| 4630 | * Class HTTP |
| 4631 | * |
| 4632 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4633 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4634 | |
| 4635 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4636 | * a class stream, otherwise it throws an error. |
| 4637 | */ |
| 4638 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4639 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4640 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4641 | } |
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 | /* This function creates and push in the stack a HTTP object |
| 4644 | * according with a current TXN. |
| 4645 | */ |
| 4646 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4647 | { |
| 4648 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4649 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4650 | /* Check stack size. */ |
| 4651 | if (!lua_checkstack(L, 3)) |
| 4652 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4653 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4654 | /* Create the object: obj[0] = userdata. |
| 4655 | * Note that the base of the Converters object is the |
| 4656 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4657 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4658 | lua_newtable(L); |
| 4659 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4660 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4661 | |
| 4662 | htxn->s = txn->s; |
| 4663 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4664 | htxn->dir = txn->dir; |
| 4665 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4666 | |
| 4667 | /* Pop a class stream metatable and affect it to the table. */ |
| 4668 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4669 | lua_setmetatable(L, -2); |
| 4670 | |
| 4671 | return 1; |
| 4672 | } |
| 4673 | |
| 4674 | /* This function creates ans returns an array of HTTP headers. |
| 4675 | * This function does not fails. It is used as wrapper with the |
| 4676 | * 2 following functions. |
| 4677 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4678 | __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] | 4679 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4680 | struct htx *htx; |
| 4681 | int32_t pos; |
| 4682 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4683 | /* Create the table. */ |
| 4684 | lua_newtable(L); |
| 4685 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4686 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4687 | htx = htxbuf(&msg->chn->buf); |
| 4688 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4689 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4690 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4691 | struct ist n, v; |
| 4692 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4693 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4694 | if (type == HTX_BLK_HDR) { |
| 4695 | n = htx_get_blk_name(htx,blk); |
| 4696 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4697 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4698 | else if (type == HTX_BLK_EOH) |
| 4699 | break; |
| 4700 | else |
| 4701 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4702 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4703 | /* Check for existing entry: |
| 4704 | * assume that the table is on the top of the stack, and |
| 4705 | * push the key in the stack, the function lua_gettable() |
| 4706 | * perform the lookup. |
| 4707 | */ |
| 4708 | lua_pushlstring(L, n.ptr, n.len); |
| 4709 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4710 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4711 | switch (lua_type(L, -1)) { |
| 4712 | case LUA_TNIL: |
| 4713 | /* Table not found, create it. */ |
| 4714 | lua_pop(L, 1); /* remove the nil value. */ |
| 4715 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4716 | lua_newtable(L); /* create and push empty table. */ |
| 4717 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4718 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4719 | 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] | 4720 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4721 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4722 | case LUA_TTABLE: |
| 4723 | /* Entry found: push the value in the table. */ |
| 4724 | len = lua_rawlen(L, -1); |
| 4725 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4726 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4727 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4728 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4729 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4730 | default: |
| 4731 | /* Other cases are errors. */ |
| 4732 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4733 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4734 | } |
| 4735 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4736 | return 1; |
| 4737 | } |
| 4738 | |
| 4739 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4740 | { |
| 4741 | struct hlua_txn *htxn; |
| 4742 | |
| 4743 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4744 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4745 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4746 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4747 | WILL_LJMP(lua_error(L)); |
| 4748 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4749 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4753 | { |
| 4754 | struct hlua_txn *htxn; |
| 4755 | |
| 4756 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4757 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4758 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4759 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4760 | WILL_LJMP(lua_error(L)); |
| 4761 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4762 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | /* This function replace full header, or just a value in |
| 4766 | * the request or in the response. It is a wrapper fir the |
| 4767 | * 4 following functions. |
| 4768 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4769 | __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] | 4770 | { |
| 4771 | size_t name_len; |
| 4772 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4773 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4774 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4775 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4776 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4777 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4778 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4779 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4780 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4781 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4782 | 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] | 4783 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4784 | return 0; |
| 4785 | } |
| 4786 | |
| 4787 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4788 | { |
| 4789 | struct hlua_txn *htxn; |
| 4790 | |
| 4791 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4792 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4793 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4794 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4795 | WILL_LJMP(lua_error(L)); |
| 4796 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4797 | 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] | 4798 | } |
| 4799 | |
| 4800 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4801 | { |
| 4802 | struct hlua_txn *htxn; |
| 4803 | |
| 4804 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4805 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4806 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4807 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4808 | WILL_LJMP(lua_error(L)); |
| 4809 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4810 | 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] | 4811 | } |
| 4812 | |
| 4813 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4814 | { |
| 4815 | struct hlua_txn *htxn; |
| 4816 | |
| 4817 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4818 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4819 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4820 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4821 | WILL_LJMP(lua_error(L)); |
| 4822 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4823 | 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] | 4824 | } |
| 4825 | |
| 4826 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4827 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4828 | struct hlua_txn *htxn; |
| 4829 | |
| 4830 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4831 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4832 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4833 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4834 | WILL_LJMP(lua_error(L)); |
| 4835 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4836 | 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] | 4837 | } |
| 4838 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4839 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4840 | * It is a wrapper for the 2 following functions. |
| 4841 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4842 | __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] | 4843 | { |
| 4844 | size_t len; |
| 4845 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4846 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4847 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4848 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4849 | ctx.blk = NULL; |
| 4850 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4851 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4852 | return 0; |
| 4853 | } |
| 4854 | |
| 4855 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4856 | { |
| 4857 | struct hlua_txn *htxn; |
| 4858 | |
| 4859 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4860 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4861 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4862 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4863 | WILL_LJMP(lua_error(L)); |
| 4864 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4865 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4869 | { |
| 4870 | struct hlua_txn *htxn; |
| 4871 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4872 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4873 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4874 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4875 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4876 | WILL_LJMP(lua_error(L)); |
| 4877 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4878 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | /* This function adds an header. It is a wrapper used by |
| 4882 | * the 2 following functions. |
| 4883 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4884 | __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] | 4885 | { |
| 4886 | size_t name_len; |
| 4887 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4888 | size_t value_len; |
| 4889 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4890 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4891 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4892 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4893 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4894 | return 0; |
| 4895 | } |
| 4896 | |
| 4897 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4898 | { |
| 4899 | struct hlua_txn *htxn; |
| 4900 | |
| 4901 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4902 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4903 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4904 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4905 | WILL_LJMP(lua_error(L)); |
| 4906 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4907 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4911 | { |
| 4912 | struct hlua_txn *htxn; |
| 4913 | |
| 4914 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4915 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4916 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4917 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4918 | WILL_LJMP(lua_error(L)); |
| 4919 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4920 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4924 | { |
| 4925 | struct hlua_txn *htxn; |
| 4926 | |
| 4927 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4928 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4929 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4930 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4931 | WILL_LJMP(lua_error(L)); |
| 4932 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4933 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 4934 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4938 | { |
| 4939 | struct hlua_txn *htxn; |
| 4940 | |
| 4941 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4942 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4943 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4944 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4945 | WILL_LJMP(lua_error(L)); |
| 4946 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4947 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 4948 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | /* This function set the method. */ |
| 4952 | static int hlua_http_req_set_meth(lua_State *L) |
| 4953 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4954 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4955 | size_t name_len; |
| 4956 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4957 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4958 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4959 | WILL_LJMP(lua_error(L)); |
| 4960 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4961 | 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] | 4962 | return 1; |
| 4963 | } |
| 4964 | |
| 4965 | /* This function set the method. */ |
| 4966 | static int hlua_http_req_set_path(lua_State *L) |
| 4967 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4968 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4969 | size_t name_len; |
| 4970 | 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] | 4971 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4972 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4973 | WILL_LJMP(lua_error(L)); |
| 4974 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4975 | 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] | 4976 | return 1; |
| 4977 | } |
| 4978 | |
| 4979 | /* This function set the query-string. */ |
| 4980 | static int hlua_http_req_set_query(lua_State *L) |
| 4981 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4982 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4983 | size_t name_len; |
| 4984 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4985 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4986 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4987 | WILL_LJMP(lua_error(L)); |
| 4988 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4989 | /* Check length. */ |
| 4990 | if (name_len > trash.size - 1) { |
| 4991 | lua_pushboolean(L, 0); |
| 4992 | return 1; |
| 4993 | } |
| 4994 | |
| 4995 | /* Add the mark question as prefix. */ |
| 4996 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4997 | trash.area[trash.data++] = '?'; |
| 4998 | memcpy(trash.area + trash.data, name, name_len); |
| 4999 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5000 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5001 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5002 | 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] | 5003 | return 1; |
| 5004 | } |
| 5005 | |
| 5006 | /* This function set the uri. */ |
| 5007 | static int hlua_http_req_set_uri(lua_State *L) |
| 5008 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5009 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5010 | size_t name_len; |
| 5011 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5012 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5013 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5014 | WILL_LJMP(lua_error(L)); |
| 5015 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5016 | 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] | 5017 | return 1; |
| 5018 | } |
| 5019 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5020 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5021 | static int hlua_http_res_set_status(lua_State *L) |
| 5022 | { |
| 5023 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5024 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5025 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5026 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5027 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5028 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5029 | WILL_LJMP(lua_error(L)); |
| 5030 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5031 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5032 | return 0; |
| 5033 | } |
| 5034 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5035 | /* |
| 5036 | * |
| 5037 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5038 | * Class TXN |
| 5039 | * |
| 5040 | * |
| 5041 | */ |
| 5042 | |
| 5043 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5044 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5045 | */ |
| 5046 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5047 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5048 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5049 | } |
| 5050 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5051 | __LJMP static int hlua_set_var(lua_State *L) |
| 5052 | { |
| 5053 | struct hlua_txn *htxn; |
| 5054 | const char *name; |
| 5055 | size_t len; |
| 5056 | struct sample smp; |
| 5057 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5058 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5059 | 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] | 5060 | |
| 5061 | /* It is useles to retrieve the stream, but this function |
| 5062 | * runs only in a stream context. |
| 5063 | */ |
| 5064 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5065 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5066 | |
| 5067 | /* Converts the third argument in a sample. */ |
| 5068 | hlua_lua2smp(L, 3, &smp); |
| 5069 | |
| 5070 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5071 | 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] | 5072 | |
| 5073 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5074 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5075 | else |
| 5076 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5077 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5078 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5079 | } |
| 5080 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5081 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5082 | { |
| 5083 | struct hlua_txn *htxn; |
| 5084 | const char *name; |
| 5085 | size_t len; |
| 5086 | struct sample smp; |
| 5087 | |
| 5088 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5089 | |
| 5090 | /* It is useles to retrieve the stream, but this function |
| 5091 | * runs only in a stream context. |
| 5092 | */ |
| 5093 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5094 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5095 | |
| 5096 | /* Unset the variable. */ |
| 5097 | 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] | 5098 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5099 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5100 | } |
| 5101 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5102 | __LJMP static int hlua_get_var(lua_State *L) |
| 5103 | { |
| 5104 | struct hlua_txn *htxn; |
| 5105 | const char *name; |
| 5106 | size_t len; |
| 5107 | struct sample smp; |
| 5108 | |
| 5109 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5110 | |
| 5111 | /* It is useles to retrieve the stream, but this function |
| 5112 | * runs only in a stream context. |
| 5113 | */ |
| 5114 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5115 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5116 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5117 | 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] | 5118 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5119 | lua_pushnil(L); |
| 5120 | return 1; |
| 5121 | } |
| 5122 | |
| 5123 | return hlua_smp2lua(L, &smp); |
| 5124 | } |
| 5125 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5126 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5127 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5128 | struct hlua *hlua; |
| 5129 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5130 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5131 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5132 | /* It is useles to retrieve the stream, but this function |
| 5133 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5134 | */ |
| 5135 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5136 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5137 | |
| 5138 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5139 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5140 | |
| 5141 | /* Get and store new value. */ |
| 5142 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5143 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5144 | |
| 5145 | return 0; |
| 5146 | } |
| 5147 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5148 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5149 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5150 | struct hlua *hlua; |
| 5151 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5152 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5153 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5154 | /* It is useles to retrieve the stream, but this function |
| 5155 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5156 | */ |
| 5157 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5158 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5159 | |
| 5160 | /* Push configuration index in the stack. */ |
| 5161 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5162 | |
| 5163 | return 1; |
| 5164 | } |
| 5165 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5166 | /* Create stack entry containing a class TXN. This function |
| 5167 | * return 0 if the stack does not contains free slots, |
| 5168 | * otherwise it returns 1. |
| 5169 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5170 | 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] | 5171 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5172 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5173 | |
| 5174 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5175 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5176 | return 0; |
| 5177 | |
| 5178 | /* NOTE: The allocation never fails. The failure |
| 5179 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5180 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5181 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5182 | /* Create the object: obj[0] = userdata. */ |
| 5183 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5184 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5185 | lua_rawseti(L, -2, 0); |
| 5186 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5187 | htxn->s = s; |
| 5188 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5189 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5190 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5191 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5192 | /* Create the "f" field that contains a list of fetches. */ |
| 5193 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5194 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5195 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5196 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5197 | |
| 5198 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5199 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5200 | 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] | 5201 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5202 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5203 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5204 | /* Create the "c" field that contains a list of converters. */ |
| 5205 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5206 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5207 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5208 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5209 | |
| 5210 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5211 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5212 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5213 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5214 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5215 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5216 | /* Create the "req" field that contains the request channel object. */ |
| 5217 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5218 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5219 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5220 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5221 | |
| 5222 | /* Create the "res" field that contains the response channel object. */ |
| 5223 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5224 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5225 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5226 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5227 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5228 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5229 | lua_pushstring(L, "http"); |
| 5230 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5231 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5232 | return 0; |
| 5233 | } |
| 5234 | else |
| 5235 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5236 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5237 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5238 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5239 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5240 | lua_setmetatable(L, -2); |
| 5241 | |
| 5242 | return 1; |
| 5243 | } |
| 5244 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5245 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5246 | { |
| 5247 | const char *msg; |
| 5248 | struct hlua_txn *htxn; |
| 5249 | |
| 5250 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5251 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5252 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5253 | |
| 5254 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5255 | return 0; |
| 5256 | } |
| 5257 | |
| 5258 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5259 | { |
| 5260 | int level; |
| 5261 | const char *msg; |
| 5262 | struct hlua_txn *htxn; |
| 5263 | |
| 5264 | MAY_LJMP(check_args(L, 3, "log")); |
| 5265 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5266 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5267 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5268 | |
| 5269 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5270 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5271 | |
| 5272 | hlua_sendlog(htxn->s->be, level, msg); |
| 5273 | return 0; |
| 5274 | } |
| 5275 | |
| 5276 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5277 | { |
| 5278 | const char *msg; |
| 5279 | struct hlua_txn *htxn; |
| 5280 | |
| 5281 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5282 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5283 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5284 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5285 | return 0; |
| 5286 | } |
| 5287 | |
| 5288 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5289 | { |
| 5290 | const char *msg; |
| 5291 | struct hlua_txn *htxn; |
| 5292 | |
| 5293 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5294 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5295 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5296 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5297 | return 0; |
| 5298 | } |
| 5299 | |
| 5300 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5301 | { |
| 5302 | const char *msg; |
| 5303 | struct hlua_txn *htxn; |
| 5304 | |
| 5305 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5306 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5307 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5308 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5309 | return 0; |
| 5310 | } |
| 5311 | |
| 5312 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5313 | { |
| 5314 | const char *msg; |
| 5315 | struct hlua_txn *htxn; |
| 5316 | |
| 5317 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5318 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5319 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5320 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5321 | return 0; |
| 5322 | } |
| 5323 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5324 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5325 | { |
| 5326 | struct hlua_txn *htxn; |
| 5327 | int ll; |
| 5328 | |
| 5329 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5330 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5331 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5332 | |
| 5333 | if (ll < 0 || ll > 7) |
| 5334 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5335 | |
| 5336 | htxn->s->logs.level = ll; |
| 5337 | return 0; |
| 5338 | } |
| 5339 | |
| 5340 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5341 | { |
| 5342 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5343 | int tos; |
| 5344 | |
| 5345 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5346 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5347 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5348 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5349 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5350 | return 0; |
| 5351 | } |
| 5352 | |
| 5353 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5354 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5355 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5356 | int mark; |
| 5357 | |
| 5358 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5359 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5360 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5361 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5362 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5363 | return 0; |
| 5364 | } |
| 5365 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5366 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5367 | { |
| 5368 | struct hlua_txn *htxn; |
| 5369 | |
| 5370 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5371 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5372 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5373 | return 0; |
| 5374 | } |
| 5375 | |
| 5376 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5377 | { |
| 5378 | struct hlua_txn *htxn; |
| 5379 | |
| 5380 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5381 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5382 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5383 | return 0; |
| 5384 | } |
| 5385 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5386 | /* 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] | 5387 | * 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] | 5388 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5389 | * error. The Reply must be on top of the stack. |
| 5390 | */ |
| 5391 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5392 | { |
| 5393 | struct htx *htx; |
| 5394 | struct htx_sl *sl; |
| 5395 | struct h1m h1m; |
| 5396 | const char *status, *reason, *body; |
| 5397 | size_t status_len, reason_len, body_len; |
| 5398 | int ret, code, flags; |
| 5399 | |
| 5400 | code = 200; |
| 5401 | status = "200"; |
| 5402 | status_len = 3; |
| 5403 | ret = lua_getfield(L, -1, "status"); |
| 5404 | if (ret == LUA_TNUMBER) { |
| 5405 | code = lua_tointeger(L, -1); |
| 5406 | status = lua_tolstring(L, -1, &status_len); |
| 5407 | } |
| 5408 | lua_pop(L, 1); |
| 5409 | |
| 5410 | reason = http_get_reason(code); |
| 5411 | reason_len = strlen(reason); |
| 5412 | ret = lua_getfield(L, -1, "reason"); |
| 5413 | if (ret == LUA_TSTRING) |
| 5414 | reason = lua_tolstring(L, -1, &reason_len); |
| 5415 | lua_pop(L, 1); |
| 5416 | |
| 5417 | body = NULL; |
| 5418 | body_len = 0; |
| 5419 | ret = lua_getfield(L, -1, "body"); |
| 5420 | if (ret == LUA_TSTRING) |
| 5421 | body = lua_tolstring(L, -1, &body_len); |
| 5422 | lua_pop(L, 1); |
| 5423 | |
| 5424 | /* Prepare the response before inserting the headers */ |
| 5425 | h1m_init_res(&h1m); |
| 5426 | htx = htx_from_buf(&s->res.buf); |
| 5427 | channel_htx_truncate(&s->res, htx); |
| 5428 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5429 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5430 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5431 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5432 | } |
| 5433 | else { |
| 5434 | flags = HTX_SL_F_IS_RESP; |
| 5435 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5436 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5437 | } |
| 5438 | if (!sl) |
| 5439 | goto fail; |
| 5440 | sl->info.res.status = code; |
| 5441 | |
| 5442 | /* Push in the stack the "headers" entry. */ |
| 5443 | ret = lua_getfield(L, -1, "headers"); |
| 5444 | if (ret != LUA_TTABLE) |
| 5445 | goto skip_headers; |
| 5446 | |
| 5447 | lua_pushnil(L); |
| 5448 | while (lua_next(L, -2) != 0) { |
| 5449 | struct ist name, value; |
| 5450 | const char *n, *v; |
| 5451 | size_t nlen, vlen; |
| 5452 | |
| 5453 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5454 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5455 | goto next_hdr; |
| 5456 | } |
| 5457 | |
| 5458 | n = lua_tolstring(L, -2, &nlen); |
| 5459 | name = ist2(n, nlen); |
| 5460 | if (isteqi(name, ist("content-length"))) { |
| 5461 | /* Always skip content-length header. It will be added |
| 5462 | * later with the correct len |
| 5463 | */ |
| 5464 | goto next_hdr; |
| 5465 | } |
| 5466 | |
| 5467 | /* Loop on header's values */ |
| 5468 | lua_pushnil(L); |
| 5469 | while (lua_next(L, -2)) { |
| 5470 | if (!lua_isstring(L, -1)) { |
| 5471 | /* Skip the value if it is not a string */ |
| 5472 | goto next_value; |
| 5473 | } |
| 5474 | |
| 5475 | v = lua_tolstring(L, -1, &vlen); |
| 5476 | value = ist2(v, vlen); |
| 5477 | |
| 5478 | if (isteqi(name, ist("transfer-encoding"))) |
| 5479 | h1_parse_xfer_enc_header(&h1m, value); |
| 5480 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5481 | goto fail; |
| 5482 | |
| 5483 | next_value: |
| 5484 | lua_pop(L, 1); |
| 5485 | } |
| 5486 | |
| 5487 | next_hdr: |
| 5488 | lua_pop(L, 1); |
| 5489 | } |
| 5490 | skip_headers: |
| 5491 | lua_pop(L, 1); |
| 5492 | |
| 5493 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5494 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5495 | const char *clen = ultoa(body_len); |
| 5496 | |
| 5497 | h1m.flags |= H1_MF_CLEN; |
| 5498 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5499 | goto fail; |
| 5500 | } |
| 5501 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5502 | h1m.flags |= H1_MF_XFER_LEN; |
| 5503 | |
| 5504 | /* Update HTX start-line flags */ |
| 5505 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5506 | flags |= HTX_SL_F_XFER_ENC; |
| 5507 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5508 | flags |= HTX_SL_F_XFER_LEN; |
| 5509 | if (h1m.flags & H1_MF_CHNK) |
| 5510 | flags |= HTX_SL_F_CHNK; |
| 5511 | else if (h1m.flags & H1_MF_CLEN) |
| 5512 | flags |= HTX_SL_F_CLEN; |
| 5513 | if (h1m.body_len == 0) |
| 5514 | flags |= HTX_SL_F_BODYLESS; |
| 5515 | } |
| 5516 | sl->flags |= flags; |
| 5517 | |
| 5518 | |
| 5519 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5520 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5521 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5522 | goto fail; |
| 5523 | |
| 5524 | /* Now, forward the response and terminate the transaction */ |
| 5525 | s->txn->status = code; |
| 5526 | htx_to_buf(htx, &s->res.buf); |
| 5527 | if (!http_forward_proxy_resp(s, 1)) |
| 5528 | goto fail; |
| 5529 | |
| 5530 | return 1; |
| 5531 | |
| 5532 | fail: |
| 5533 | channel_htx_truncate(&s->res, htx); |
| 5534 | return 0; |
| 5535 | } |
| 5536 | |
| 5537 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5538 | * processing is just aborted. Nothing is returned to the client and all |
| 5539 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5540 | * is forwarded to the client before terminating the transaction. On success, |
| 5541 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5542 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5543 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5544 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5545 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5546 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5547 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5548 | struct stream *s; |
| 5549 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5550 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5551 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5552 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5553 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5554 | * just end the execution of the current lua code. */ |
| 5555 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5556 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5557 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5558 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5559 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5560 | struct channel *req = &s->req; |
| 5561 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5562 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5563 | channel_auto_read(req); |
| 5564 | channel_abort(req); |
| 5565 | channel_auto_close(req); |
| 5566 | channel_erase(req); |
| 5567 | |
| 5568 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5569 | channel_auto_read(res); |
| 5570 | channel_auto_close(res); |
| 5571 | channel_shutr_now(res); |
| 5572 | |
| 5573 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5574 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5575 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5576 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5577 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5578 | /* No reply or invalid reply */ |
| 5579 | s->txn->status = 0; |
| 5580 | http_reply_and_close(s, 0, NULL); |
| 5581 | } |
| 5582 | else { |
| 5583 | /* Remove extra args to have the reply on top of the stack */ |
| 5584 | if (lua_gettop(L) > 2) |
| 5585 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5586 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5587 | if (!hlua_txn_forward_reply(L, s)) { |
| 5588 | if (!(s->flags & SF_ERR_MASK)) |
| 5589 | s->flags |= SF_ERR_PRXCOND; |
| 5590 | lua_pushinteger(L, ACT_RET_ERR); |
| 5591 | WILL_LJMP(hlua_done(L)); |
| 5592 | return 0; /* Never reached */ |
| 5593 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5594 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5595 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5596 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5597 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5598 | /* let's log the request time */ |
| 5599 | s->logs.tv_request = now; |
| 5600 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5601 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5602 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5603 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5604 | done: |
| 5605 | if (!(s->flags & SF_ERR_MASK)) |
| 5606 | s->flags |= SF_ERR_LOCAL; |
| 5607 | if (!(s->flags & SF_FINST_MASK)) |
| 5608 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5609 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5610 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5611 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5612 | return 0; |
| 5613 | } |
| 5614 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5615 | /* |
| 5616 | * |
| 5617 | * |
| 5618 | * Class REPLY |
| 5619 | * |
| 5620 | * |
| 5621 | */ |
| 5622 | |
| 5623 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5624 | * free slots, the function fails and returns 0; |
| 5625 | */ |
| 5626 | static int hlua_txn_reply_new(lua_State *L) |
| 5627 | { |
| 5628 | struct hlua_txn *htxn; |
| 5629 | const char *reason, *body = NULL; |
| 5630 | int ret, status; |
| 5631 | |
| 5632 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5633 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5634 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5635 | WILL_LJMP(lua_error(L)); |
| 5636 | } |
| 5637 | |
| 5638 | /* Default value */ |
| 5639 | status = 200; |
| 5640 | reason = http_get_reason(status); |
| 5641 | |
| 5642 | if (lua_istable(L, 2)) { |
| 5643 | /* load status and reason from the table argument at index 2 */ |
| 5644 | ret = lua_getfield(L, 2, "status"); |
| 5645 | if (ret == LUA_TNIL) |
| 5646 | goto reason; |
| 5647 | else if (ret != LUA_TNUMBER) { |
| 5648 | /* invalid status: ignore the reason */ |
| 5649 | goto body; |
| 5650 | } |
| 5651 | status = lua_tointeger(L, -1); |
| 5652 | |
| 5653 | reason: |
| 5654 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5655 | ret = lua_getfield(L, 2, "reason"); |
| 5656 | if (ret == LUA_TSTRING) |
| 5657 | reason = lua_tostring(L, -1); |
| 5658 | |
| 5659 | body: |
| 5660 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5661 | ret = lua_getfield(L, 2, "body"); |
| 5662 | if (ret == LUA_TSTRING) |
| 5663 | body = lua_tostring(L, -1); |
| 5664 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5665 | } |
| 5666 | |
| 5667 | /* Create the Reply table */ |
| 5668 | lua_newtable(L); |
| 5669 | |
| 5670 | /* Add status element */ |
| 5671 | lua_pushstring(L, "status"); |
| 5672 | lua_pushinteger(L, status); |
| 5673 | lua_settable(L, -3); |
| 5674 | |
| 5675 | /* Add reason element */ |
| 5676 | reason = http_get_reason(status); |
| 5677 | lua_pushstring(L, "reason"); |
| 5678 | lua_pushstring(L, reason); |
| 5679 | lua_settable(L, -3); |
| 5680 | |
| 5681 | /* Add body element, nil if undefined */ |
| 5682 | lua_pushstring(L, "body"); |
| 5683 | if (body) |
| 5684 | lua_pushstring(L, body); |
| 5685 | else |
| 5686 | lua_pushnil(L); |
| 5687 | lua_settable(L, -3); |
| 5688 | |
| 5689 | /* Add headers element */ |
| 5690 | lua_pushstring(L, "headers"); |
| 5691 | lua_newtable(L); |
| 5692 | |
| 5693 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5694 | if (lua_istable(L, 2)) { |
| 5695 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5696 | ret = lua_getfield(L, 2, "headers"); |
| 5697 | if (ret == LUA_TTABLE) { |
| 5698 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5699 | lua_pushnil(L); |
| 5700 | while (lua_next(L, -2) != 0) { |
| 5701 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5702 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5703 | /* invalid value type, skip it */ |
| 5704 | lua_pop(L, 1); |
| 5705 | continue; |
| 5706 | } |
| 5707 | |
| 5708 | |
| 5709 | /* Duplicate the key and swap it with the value. */ |
| 5710 | lua_pushvalue(L, -2); |
| 5711 | lua_insert(L, -2); |
| 5712 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5713 | |
| 5714 | lua_newtable(L); |
| 5715 | lua_insert(L, -2); |
| 5716 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5717 | |
| 5718 | if (lua_isstring(L, -1)) { |
| 5719 | /* push the value in the inner table */ |
| 5720 | lua_rawseti(L, -2, 1); |
| 5721 | } |
| 5722 | else { /* table */ |
| 5723 | lua_pushnil(L); |
| 5724 | while (lua_next(L, -2) != 0) { |
| 5725 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5726 | if (!lua_isstring(L, -1)) { |
| 5727 | /* invalid value type, skip it*/ |
| 5728 | lua_pop(L, 1); |
| 5729 | continue; |
| 5730 | } |
| 5731 | /* push the value in the inner table */ |
| 5732 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5733 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5734 | } |
| 5735 | lua_pop(L, 1); |
| 5736 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5737 | } |
| 5738 | |
| 5739 | /* push (k,v) on the stack in the headers table: |
| 5740 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5741 | */ |
| 5742 | lua_settable(L, -5); |
| 5743 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5744 | } |
| 5745 | } |
| 5746 | lua_pop(L, 1); |
| 5747 | } |
| 5748 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5749 | lua_settable(L, -3); |
| 5750 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5751 | |
| 5752 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5753 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5754 | lua_setmetatable(L, -2); |
| 5755 | return 1; |
| 5756 | } |
| 5757 | |
| 5758 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5759 | * provided, the default one corresponding to the status code is used. |
| 5760 | */ |
| 5761 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5762 | { |
| 5763 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5764 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5765 | |
| 5766 | /* First argument (self) must be a table */ |
| 5767 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5768 | |
| 5769 | if (status < 100 || status > 599) { |
| 5770 | lua_pushboolean(L, 0); |
| 5771 | return 1; |
| 5772 | } |
| 5773 | if (!reason) |
| 5774 | reason = http_get_reason(status); |
| 5775 | |
| 5776 | lua_pushinteger(L, status); |
| 5777 | lua_setfield(L, 1, "status"); |
| 5778 | |
| 5779 | lua_pushstring(L, reason); |
| 5780 | lua_setfield(L, 1, "reason"); |
| 5781 | |
| 5782 | lua_pushboolean(L, 1); |
| 5783 | return 1; |
| 5784 | } |
| 5785 | |
| 5786 | /* Add a header into the reply object. Each header name is associated to an |
| 5787 | * array of values in the "headers" table. If the header name is not found, a |
| 5788 | * new entry is created. |
| 5789 | */ |
| 5790 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5791 | { |
| 5792 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5793 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5794 | int ret; |
| 5795 | |
| 5796 | /* First argument (self) must be a table */ |
| 5797 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5798 | |
| 5799 | /* Push in the stack the "headers" entry. */ |
| 5800 | ret = lua_getfield(L, 1, "headers"); |
| 5801 | if (ret != LUA_TTABLE) { |
| 5802 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5803 | WILL_LJMP(lua_error(L)); |
| 5804 | } |
| 5805 | |
| 5806 | /* check if the header is already registered. If not, register it. */ |
| 5807 | ret = lua_getfield(L, -1, name); |
| 5808 | if (ret == LUA_TNIL) { |
| 5809 | /* Entry not found. */ |
| 5810 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5811 | |
| 5812 | /* Insert the new header name in the array in the top of the stack. |
| 5813 | * It left the new array in the top of the stack. |
| 5814 | */ |
| 5815 | lua_newtable(L); |
| 5816 | lua_pushstring(L, name); |
| 5817 | lua_pushvalue(L, -2); |
| 5818 | lua_settable(L, -4); |
| 5819 | } |
| 5820 | else if (ret != LUA_TTABLE) { |
| 5821 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5822 | WILL_LJMP(lua_error(L)); |
| 5823 | } |
| 5824 | |
| 5825 | /* Now the top od thestack is an array of values. We push |
| 5826 | * the header value as new entry. |
| 5827 | */ |
| 5828 | lua_pushstring(L, value); |
| 5829 | ret = lua_rawlen(L, -2); |
| 5830 | lua_rawseti(L, -2, ret + 1); |
| 5831 | |
| 5832 | lua_pushboolean(L, 1); |
| 5833 | return 1; |
| 5834 | } |
| 5835 | |
| 5836 | /* Remove all occurrences of a given header name. */ |
| 5837 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5838 | { |
| 5839 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5840 | int ret; |
| 5841 | |
| 5842 | /* First argument (self) must be a table */ |
| 5843 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5844 | |
| 5845 | /* Push in the stack the "headers" entry. */ |
| 5846 | ret = lua_getfield(L, 1, "headers"); |
| 5847 | if (ret != LUA_TTABLE) { |
| 5848 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 5849 | WILL_LJMP(lua_error(L)); |
| 5850 | } |
| 5851 | |
| 5852 | lua_pushstring(L, name); |
| 5853 | lua_pushnil(L); |
| 5854 | lua_settable(L, -3); |
| 5855 | |
| 5856 | lua_pushboolean(L, 1); |
| 5857 | return 1; |
| 5858 | } |
| 5859 | |
| 5860 | /* Set the reply's body. Overwrite any existing entry. */ |
| 5861 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 5862 | { |
| 5863 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5864 | |
| 5865 | /* First argument (self) must be a table */ |
| 5866 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5867 | |
| 5868 | lua_pushstring(L, payload); |
| 5869 | lua_setfield(L, 1, "body"); |
| 5870 | |
| 5871 | lua_pushboolean(L, 1); |
| 5872 | return 1; |
| 5873 | } |
| 5874 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5875 | __LJMP static int hlua_log(lua_State *L) |
| 5876 | { |
| 5877 | int level; |
| 5878 | const char *msg; |
| 5879 | |
| 5880 | MAY_LJMP(check_args(L, 2, "log")); |
| 5881 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5882 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5883 | |
| 5884 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5885 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5886 | |
| 5887 | hlua_sendlog(NULL, level, msg); |
| 5888 | return 0; |
| 5889 | } |
| 5890 | |
| 5891 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5892 | { |
| 5893 | const char *msg; |
| 5894 | |
| 5895 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5896 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5897 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5898 | return 0; |
| 5899 | } |
| 5900 | |
| 5901 | __LJMP static int hlua_log_info(lua_State *L) |
| 5902 | { |
| 5903 | const char *msg; |
| 5904 | |
| 5905 | MAY_LJMP(check_args(L, 1, "info")); |
| 5906 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5907 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5908 | return 0; |
| 5909 | } |
| 5910 | |
| 5911 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5912 | { |
| 5913 | const char *msg; |
| 5914 | |
| 5915 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5916 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5917 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5918 | return 0; |
| 5919 | } |
| 5920 | |
| 5921 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5922 | { |
| 5923 | const char *msg; |
| 5924 | |
| 5925 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5926 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5927 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5928 | return 0; |
| 5929 | } |
| 5930 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5931 | __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] | 5932 | { |
| 5933 | int wakeup_ms = lua_tointeger(L, -1); |
| 5934 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5935 | 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] | 5936 | return 0; |
| 5937 | } |
| 5938 | |
| 5939 | __LJMP static int hlua_sleep(lua_State *L) |
| 5940 | { |
| 5941 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5942 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5943 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5944 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5945 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5946 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5947 | wakeup_ms = tick_add(now_ms, delay); |
| 5948 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5949 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5950 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5951 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5952 | } |
| 5953 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5954 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5955 | { |
| 5956 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5957 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5958 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5959 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5960 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5961 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5962 | wakeup_ms = tick_add(now_ms, delay); |
| 5963 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5964 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5965 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5966 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5967 | } |
| 5968 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5969 | /* This functionis an LUA binding. it permits to give back |
| 5970 | * the hand at the HAProxy scheduler. It is used when the |
| 5971 | * LUA processing consumes a lot of time. |
| 5972 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5973 | __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] | 5974 | { |
| 5975 | return 0; |
| 5976 | } |
| 5977 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5978 | __LJMP static int hlua_yield(lua_State *L) |
| 5979 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5980 | 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] | 5981 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5982 | } |
| 5983 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5984 | /* This function change the nice of the currently executed |
| 5985 | * task. It is used set low or high priority at the current |
| 5986 | * task. |
| 5987 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5988 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5989 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5990 | struct hlua *hlua; |
| 5991 | int nice; |
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 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5994 | hlua = hlua_gethlua(L); |
| 5995 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5996 | |
| 5997 | /* If he task is not set, I'm in a start mode. */ |
| 5998 | if (!hlua || !hlua->task) |
| 5999 | return 0; |
| 6000 | |
| 6001 | if (nice < -1024) |
| 6002 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6003 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6004 | nice = 1024; |
| 6005 | |
| 6006 | hlua->task->nice = nice; |
| 6007 | return 0; |
| 6008 | } |
| 6009 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6010 | /* 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] | 6011 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6012 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6013 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6014 | * |
| 6015 | * Task wrapper are longjmp safe because the only one Lua code |
| 6016 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6017 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6018 | 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] | 6019 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6020 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6021 | enum hlua_exec status; |
| 6022 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6023 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6024 | task_set_affinity(task, tid_bit); |
| 6025 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6026 | /* If it is the first call to the task, we must initialize the |
| 6027 | * execution timeouts. |
| 6028 | */ |
| 6029 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6030 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6031 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6032 | /* Execute the Lua code. */ |
| 6033 | status = hlua_ctx_resume(hlua, 1); |
| 6034 | |
| 6035 | switch (status) { |
| 6036 | /* finished or yield */ |
| 6037 | case HLUA_E_OK: |
| 6038 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6039 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6040 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6041 | break; |
| 6042 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6043 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6044 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6045 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6046 | break; |
| 6047 | |
| 6048 | /* finished with error. */ |
| 6049 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6050 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6051 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6052 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6053 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6054 | break; |
| 6055 | |
| 6056 | case HLUA_E_ERR: |
| 6057 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6058 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6059 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6060 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6061 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6062 | break; |
| 6063 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6064 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6065 | } |
| 6066 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6067 | /* This function is an LUA binding that register LUA function to be |
| 6068 | * executed after the HAProxy configuration parsing and before the |
| 6069 | * HAProxy scheduler starts. This function expect only one LUA |
| 6070 | * argument that is a function. This function returns nothing, but |
| 6071 | * throws if an error is encountered. |
| 6072 | */ |
| 6073 | __LJMP static int hlua_register_init(lua_State *L) |
| 6074 | { |
| 6075 | struct hlua_init_function *init; |
| 6076 | int ref; |
| 6077 | |
| 6078 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6079 | |
| 6080 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6081 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6082 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6083 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6084 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6085 | |
| 6086 | init->function_ref = ref; |
| 6087 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6088 | return 0; |
| 6089 | } |
| 6090 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6091 | /* This functio is an LUA binding. It permits to register a task |
| 6092 | * executed in parallel of the main HAroxy activity. The task is |
| 6093 | * created and it is set in the HAProxy scheduler. It can be called |
| 6094 | * from the "init" section, "post init" or during the runtime. |
| 6095 | * |
| 6096 | * Lua prototype: |
| 6097 | * |
| 6098 | * <none> core.register_task(<function>) |
| 6099 | */ |
| 6100 | static int hlua_register_task(lua_State *L) |
| 6101 | { |
| 6102 | struct hlua *hlua; |
| 6103 | struct task *task; |
| 6104 | int ref; |
| 6105 | |
| 6106 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6107 | |
| 6108 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6109 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6110 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6111 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6112 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6113 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6114 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6115 | if (!task) |
| 6116 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6117 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6118 | task->context = hlua; |
| 6119 | task->process = hlua_process_task; |
| 6120 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6121 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6122 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6123 | |
| 6124 | /* Restore the function in the stack. */ |
| 6125 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6126 | hlua->nargs = 0; |
| 6127 | |
| 6128 | /* Schedule task. */ |
| 6129 | task_schedule(task, now_ms); |
| 6130 | |
| 6131 | return 0; |
| 6132 | } |
| 6133 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6134 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6135 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6136 | * resume converters. |
| 6137 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6138 | 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] | 6139 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6140 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6141 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6142 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6143 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6144 | if (!stream) |
| 6145 | return 0; |
| 6146 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6147 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6148 | * Lua context can be not initialized. This behavior |
| 6149 | * permits to save performances because a systematic |
| 6150 | * Lua initialization cause 5% performances loss. |
| 6151 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6152 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6153 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6154 | if (!stream->hlua) { |
| 6155 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6156 | return 0; |
| 6157 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6158 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6159 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6160 | return 0; |
| 6161 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6162 | } |
| 6163 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6164 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6165 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6166 | |
| 6167 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6168 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6169 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6170 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6171 | else |
| 6172 | error = "critical error"; |
| 6173 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6174 | return 0; |
| 6175 | } |
| 6176 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6177 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6178 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6179 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6180 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6181 | return 0; |
| 6182 | } |
| 6183 | |
| 6184 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6185 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6186 | |
| 6187 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6188 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6189 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6190 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6191 | return 0; |
| 6192 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6193 | hlua_smp2lua(stream->hlua->T, smp); |
| 6194 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6195 | |
| 6196 | /* push keywords in the stack. */ |
| 6197 | if (arg_p) { |
| 6198 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6199 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6200 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6201 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6202 | return 0; |
| 6203 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6204 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6205 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6206 | } |
| 6207 | } |
| 6208 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6209 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6210 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6211 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6212 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6213 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6214 | } |
| 6215 | |
| 6216 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6217 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6218 | /* finished. */ |
| 6219 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6220 | /* If the stack is empty, the function fails. */ |
| 6221 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6222 | return 0; |
| 6223 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6224 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6225 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6226 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6227 | return 1; |
| 6228 | |
| 6229 | /* yield. */ |
| 6230 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6231 | 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] | 6232 | return 0; |
| 6233 | |
| 6234 | /* finished with error. */ |
| 6235 | case HLUA_E_ERRMSG: |
| 6236 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6237 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6238 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6239 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6240 | return 0; |
| 6241 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6242 | case HLUA_E_ETMOUT: |
| 6243 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6244 | return 0; |
| 6245 | |
| 6246 | case HLUA_E_NOMEM: |
| 6247 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6248 | return 0; |
| 6249 | |
| 6250 | case HLUA_E_YIELD: |
| 6251 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6252 | return 0; |
| 6253 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6254 | case HLUA_E_ERR: |
| 6255 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6256 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6257 | |
| 6258 | default: |
| 6259 | return 0; |
| 6260 | } |
| 6261 | } |
| 6262 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6263 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6264 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6265 | * resume sample-fetches. This function will be called by the sample |
| 6266 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6267 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6268 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6269 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6270 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6271 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6272 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6273 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6274 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6275 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6276 | if (!stream) |
| 6277 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6278 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6279 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6280 | * Lua context can be not initialized. This behavior |
| 6281 | * permits to save performances because a systematic |
| 6282 | * Lua initialization cause 5% performances loss. |
| 6283 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6284 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6285 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6286 | if (!stream->hlua) { |
| 6287 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6288 | return 0; |
| 6289 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6290 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6291 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6292 | return 0; |
| 6293 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6294 | } |
| 6295 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6296 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6297 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6298 | |
| 6299 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6300 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6301 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6302 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6303 | else |
| 6304 | error = "critical error"; |
| 6305 | 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] | 6306 | return 0; |
| 6307 | } |
| 6308 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6309 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6310 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6311 | 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] | 6312 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6313 | return 0; |
| 6314 | } |
| 6315 | |
| 6316 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6317 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6318 | |
| 6319 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6320 | 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] | 6321 | 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] | 6322 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6323 | return 0; |
| 6324 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6325 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6326 | |
| 6327 | /* push keywords in the stack. */ |
| 6328 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6329 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6330 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6331 | 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] | 6332 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6333 | return 0; |
| 6334 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6335 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6336 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6337 | } |
| 6338 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6339 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6340 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6341 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6342 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6343 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6347 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6348 | /* finished. */ |
| 6349 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6350 | /* If the stack is empty, the function fails. */ |
| 6351 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6352 | return 0; |
| 6353 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6354 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6355 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6356 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6357 | |
| 6358 | /* Set the end of execution flag. */ |
| 6359 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6360 | return 1; |
| 6361 | |
| 6362 | /* yield. */ |
| 6363 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6364 | 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] | 6365 | return 0; |
| 6366 | |
| 6367 | /* finished with error. */ |
| 6368 | case HLUA_E_ERRMSG: |
| 6369 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6370 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6371 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6372 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6373 | return 0; |
| 6374 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6375 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6376 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6377 | return 0; |
| 6378 | |
| 6379 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6380 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6381 | return 0; |
| 6382 | |
| 6383 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6384 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6385 | return 0; |
| 6386 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6387 | case HLUA_E_ERR: |
| 6388 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6389 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6390 | |
| 6391 | default: |
| 6392 | return 0; |
| 6393 | } |
| 6394 | } |
| 6395 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6396 | /* This function is an LUA binding used for registering |
| 6397 | * "sample-conv" functions. It expects a converter name used |
| 6398 | * in the haproxy configuration file, and an LUA function. |
| 6399 | */ |
| 6400 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6401 | { |
| 6402 | struct sample_conv_kw_list *sck; |
| 6403 | const char *name; |
| 6404 | int ref; |
| 6405 | int len; |
| 6406 | struct hlua_function *fcn; |
| 6407 | |
| 6408 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6409 | |
| 6410 | /* First argument : converter name. */ |
| 6411 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6412 | |
| 6413 | /* Second argument : lua function. */ |
| 6414 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6415 | |
| 6416 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6417 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6418 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6419 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6420 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6421 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6422 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6423 | |
| 6424 | /* Fill fcn. */ |
| 6425 | fcn->name = strdup(name); |
| 6426 | if (!fcn->name) |
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 | fcn->function_ref = ref; |
| 6429 | |
| 6430 | /* List head */ |
| 6431 | sck->list.n = sck->list.p = NULL; |
| 6432 | |
| 6433 | /* converter keyword. */ |
| 6434 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6435 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6436 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6437 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6438 | |
| 6439 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6440 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6441 | 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] | 6442 | sck->kw[0].val_args = NULL; |
| 6443 | sck->kw[0].in_type = SMP_T_STR; |
| 6444 | sck->kw[0].out_type = SMP_T_STR; |
| 6445 | sck->kw[0].private = fcn; |
| 6446 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6447 | /* Register this new converter */ |
| 6448 | sample_register_convs(sck); |
| 6449 | |
| 6450 | return 0; |
| 6451 | } |
| 6452 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6453 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6454 | * "sample-fetch" functions. It expects a converter name used |
| 6455 | * in the haproxy configuration file, and an LUA function. |
| 6456 | */ |
| 6457 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6458 | { |
| 6459 | const char *name; |
| 6460 | int ref; |
| 6461 | int len; |
| 6462 | struct sample_fetch_kw_list *sfk; |
| 6463 | struct hlua_function *fcn; |
| 6464 | |
| 6465 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6466 | |
| 6467 | /* First argument : sample-fetch name. */ |
| 6468 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6469 | |
| 6470 | /* Second argument : lua function. */ |
| 6471 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6472 | |
| 6473 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6474 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6475 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6476 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6477 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6478 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6479 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6480 | |
| 6481 | /* Fill fcn. */ |
| 6482 | fcn->name = strdup(name); |
| 6483 | if (!fcn->name) |
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 | fcn->function_ref = ref; |
| 6486 | |
| 6487 | /* List head */ |
| 6488 | sfk->list.n = sfk->list.p = NULL; |
| 6489 | |
| 6490 | /* sample-fetch keyword. */ |
| 6491 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6492 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6493 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6494 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6495 | |
| 6496 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6497 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6498 | 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] | 6499 | sfk->kw[0].val_args = NULL; |
| 6500 | sfk->kw[0].out_type = SMP_T_STR; |
| 6501 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6502 | sfk->kw[0].val = 0; |
| 6503 | sfk->kw[0].private = fcn; |
| 6504 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6505 | /* Register this new fetch. */ |
| 6506 | sample_register_fetches(sfk); |
| 6507 | |
| 6508 | return 0; |
| 6509 | } |
| 6510 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6511 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6512 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6513 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6514 | { |
| 6515 | struct hlua *hlua = hlua_gethlua(L); |
| 6516 | unsigned int delay; |
| 6517 | unsigned int wakeup_ms; |
| 6518 | |
| 6519 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6520 | |
| 6521 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6522 | wakeup_ms = tick_add(now_ms, delay); |
| 6523 | hlua->wake_time = wakeup_ms; |
| 6524 | return 0; |
| 6525 | } |
| 6526 | |
| 6527 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6528 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6529 | * wrapper during the initialisation period. This function may return any |
| 6530 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6531 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6532 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6533 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6534 | 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] | 6535 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6536 | { |
| 6537 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6538 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6539 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6540 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6541 | |
| 6542 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6543 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6544 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6545 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6546 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6547 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6548 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6549 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6550 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6551 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6552 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6553 | * Lua context can be not initialized. This behavior |
| 6554 | * permits to save performances because a systematic |
| 6555 | * Lua initialization cause 5% performances loss. |
| 6556 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6557 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6558 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6559 | if (!s->hlua) { |
| 6560 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6561 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6562 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6563 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6564 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6565 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6566 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6567 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6568 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6569 | } |
| 6570 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6571 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6572 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6573 | |
| 6574 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6575 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6576 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6577 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6578 | else |
| 6579 | error = "critical error"; |
| 6580 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6581 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6582 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6583 | } |
| 6584 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6585 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6586 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6587 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6588 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6589 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6590 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6591 | } |
| 6592 | |
| 6593 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6594 | 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] | 6595 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6596 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6597 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6598 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6599 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6600 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6601 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6602 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6603 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6604 | |
| 6605 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6606 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6607 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6608 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6609 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6611 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6612 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6613 | lua_pushstring(s->hlua->T, *arg); |
| 6614 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6615 | } |
| 6616 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6617 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6618 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6619 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6620 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6621 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6622 | } |
| 6623 | |
Christopher Faulet | 23308eb | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6624 | /* Always reset the analyse expiration timeout for the corresponding |
| 6625 | * channel in case the lua script yield, to be sure to not keep an |
| 6626 | * expired timeout. |
| 6627 | */ |
| 6628 | if (dir == SMP_OPT_DIR_REQ) |
| 6629 | s->req.analyse_exp = TICK_ETERNITY; |
| 6630 | else |
| 6631 | s->res.analyse_exp = TICK_ETERNITY; |
| 6632 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6633 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6634 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6635 | /* finished. */ |
| 6636 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6637 | /* Catch the return value */ |
| 6638 | if (lua_gettop(s->hlua->T) > 0) |
| 6639 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6640 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6641 | /* Set timeout in the required channel. */ |
| 6642 | if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) { |
| 6643 | if (dir == SMP_OPT_DIR_REQ) |
| 6644 | s->req.analyse_exp = s->hlua->wake_time; |
| 6645 | else |
| 6646 | s->res.analyse_exp = s->hlua->wake_time; |
| 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. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6653 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | 81921b1 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6654 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6655 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6656 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6657 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6658 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6659 | /* Some actions can be wake up when a "write" event |
| 6660 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6661 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6662 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6663 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6664 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6665 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6666 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6667 | act_ret = ACT_RET_YIELD; |
| 6668 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6669 | |
| 6670 | /* finished with error. */ |
| 6671 | case HLUA_E_ERRMSG: |
| 6672 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6673 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6674 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6675 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6676 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6677 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6678 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6679 | 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] | 6680 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6681 | |
| 6682 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6683 | 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] | 6684 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6685 | |
| 6686 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6687 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6688 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6689 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6690 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6691 | case HLUA_E_ERR: |
| 6692 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6693 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6694 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6695 | |
| 6696 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6697 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6698 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6699 | |
| 6700 | end: |
| 6701 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6702 | } |
| 6703 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6704 | 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] | 6705 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6706 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6707 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6708 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6709 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6710 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6714 | { |
| 6715 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6716 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6717 | struct task *task; |
| 6718 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6719 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6720 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6721 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6722 | if (!hlua) { |
| 6723 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6724 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6725 | return 0; |
| 6726 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6727 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6728 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6729 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6730 | |
| 6731 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6732 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6733 | if (!task) { |
| 6734 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6735 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6736 | return 0; |
| 6737 | } |
| 6738 | task->nice = 0; |
| 6739 | task->context = ctx; |
| 6740 | task->process = hlua_applet_wakeup; |
| 6741 | ctx->ctx.hlua_apptcp.task = task; |
| 6742 | |
| 6743 | /* In the execution wrappers linked with a stream, the |
| 6744 | * Lua context can be not initialized. This behavior |
| 6745 | * permits to save performances because a systematic |
| 6746 | * Lua initialization cause 5% performances loss. |
| 6747 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6748 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6749 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6750 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6751 | return 0; |
| 6752 | } |
| 6753 | |
| 6754 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6755 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6756 | |
| 6757 | /* The following Lua calls can fail. */ |
| 6758 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6759 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6760 | error = lua_tostring(hlua->T, -1); |
| 6761 | else |
| 6762 | error = "critical error"; |
| 6763 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6764 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6765 | return 0; |
| 6766 | } |
| 6767 | |
| 6768 | /* Check stack available size. */ |
| 6769 | if (!lua_checkstack(hlua->T, 1)) { |
| 6770 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6771 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6772 | RESET_SAFE_LJMP(hlua->T); |
| 6773 | return 0; |
| 6774 | } |
| 6775 | |
| 6776 | /* Restore the function in the stack. */ |
| 6777 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6778 | |
| 6779 | /* Create and and push object stream in the stack. */ |
| 6780 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6781 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6782 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6783 | RESET_SAFE_LJMP(hlua->T); |
| 6784 | return 0; |
| 6785 | } |
| 6786 | hlua->nargs = 1; |
| 6787 | |
| 6788 | /* push keywords in the stack. */ |
| 6789 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6790 | if (!lua_checkstack(hlua->T, 1)) { |
| 6791 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6792 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6793 | RESET_SAFE_LJMP(hlua->T); |
| 6794 | return 0; |
| 6795 | } |
| 6796 | lua_pushstring(hlua->T, *arg); |
| 6797 | hlua->nargs++; |
| 6798 | } |
| 6799 | |
| 6800 | RESET_SAFE_LJMP(hlua->T); |
| 6801 | |
| 6802 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6803 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6804 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6805 | |
| 6806 | return 1; |
| 6807 | } |
| 6808 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6809 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6810 | { |
| 6811 | struct stream_interface *si = ctx->owner; |
| 6812 | struct stream *strm = si_strm(si); |
| 6813 | struct channel *res = si_ic(si); |
| 6814 | struct act_rule *rule = ctx->rule; |
| 6815 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6816 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6817 | |
| 6818 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6819 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6820 | /* eat the whole request */ |
| 6821 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6822 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6823 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6824 | |
| 6825 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6826 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6827 | return; |
| 6828 | |
| 6829 | /* Execute the function. */ |
| 6830 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6831 | /* finished. */ |
| 6832 | case HLUA_E_OK: |
| 6833 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6834 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6835 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6836 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6837 | res->flags |= CF_READ_NULL; |
| 6838 | si_shutr(si); |
| 6839 | return; |
| 6840 | |
| 6841 | /* yield. */ |
| 6842 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6843 | if (hlua->wake_time != TICK_ETERNITY) |
| 6844 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6845 | return; |
| 6846 | |
| 6847 | /* finished with error. */ |
| 6848 | case HLUA_E_ERRMSG: |
| 6849 | /* Display log. */ |
| 6850 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6851 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6852 | lua_pop(hlua->T, 1); |
| 6853 | goto error; |
| 6854 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6855 | case HLUA_E_ETMOUT: |
| 6856 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6857 | rule->arg.hlua_rule->fcn.name); |
| 6858 | goto error; |
| 6859 | |
| 6860 | case HLUA_E_NOMEM: |
| 6861 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6862 | rule->arg.hlua_rule->fcn.name); |
| 6863 | goto error; |
| 6864 | |
| 6865 | case HLUA_E_YIELD: /* unexpected */ |
| 6866 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6867 | rule->arg.hlua_rule->fcn.name); |
| 6868 | goto error; |
| 6869 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6870 | case HLUA_E_ERR: |
| 6871 | /* Display log. */ |
| 6872 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6873 | rule->arg.hlua_rule->fcn.name); |
| 6874 | goto error; |
| 6875 | |
| 6876 | default: |
| 6877 | goto error; |
| 6878 | } |
| 6879 | |
| 6880 | error: |
| 6881 | |
| 6882 | /* For all other cases, just close the stream. */ |
| 6883 | si_shutw(si); |
| 6884 | si_shutr(si); |
| 6885 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6886 | } |
| 6887 | |
| 6888 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6889 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6890 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6891 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6892 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6893 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6894 | } |
| 6895 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6896 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6897 | * an errors occurs and -1 if more data are required for initializing |
| 6898 | * the applet. |
| 6899 | */ |
| 6900 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6901 | { |
| 6902 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6903 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6904 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6905 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6906 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6907 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6908 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6909 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6910 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6911 | if (!hlua) { |
| 6912 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6913 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6914 | return 0; |
| 6915 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6916 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6917 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6918 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6919 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6920 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6921 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6922 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6923 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6924 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6925 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6926 | if (!task) { |
| 6927 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6928 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6929 | return 0; |
| 6930 | } |
| 6931 | task->nice = 0; |
| 6932 | task->context = ctx; |
| 6933 | task->process = hlua_applet_wakeup; |
| 6934 | ctx->ctx.hlua_apphttp.task = task; |
| 6935 | |
| 6936 | /* In the execution wrappers linked with a stream, the |
| 6937 | * Lua context can be not initialized. This behavior |
| 6938 | * permits to save performances because a systematic |
| 6939 | * Lua initialization cause 5% performances loss. |
| 6940 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6941 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6942 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6943 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6944 | return 0; |
| 6945 | } |
| 6946 | |
| 6947 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6948 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6949 | |
| 6950 | /* The following Lua calls can fail. */ |
| 6951 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6952 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6953 | error = lua_tostring(hlua->T, -1); |
| 6954 | else |
| 6955 | error = "critical error"; |
| 6956 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6957 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6958 | return 0; |
| 6959 | } |
| 6960 | |
| 6961 | /* Check stack available size. */ |
| 6962 | if (!lua_checkstack(hlua->T, 1)) { |
| 6963 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6964 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6965 | RESET_SAFE_LJMP(hlua->T); |
| 6966 | return 0; |
| 6967 | } |
| 6968 | |
| 6969 | /* Restore the function in the stack. */ |
| 6970 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6971 | |
| 6972 | /* Create and and push object stream in the stack. */ |
| 6973 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6974 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6975 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6976 | RESET_SAFE_LJMP(hlua->T); |
| 6977 | return 0; |
| 6978 | } |
| 6979 | hlua->nargs = 1; |
| 6980 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6981 | /* push keywords in the stack. */ |
| 6982 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6983 | if (!lua_checkstack(hlua->T, 1)) { |
| 6984 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6985 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6986 | RESET_SAFE_LJMP(hlua->T); |
| 6987 | return 0; |
| 6988 | } |
| 6989 | lua_pushstring(hlua->T, *arg); |
| 6990 | hlua->nargs++; |
| 6991 | } |
| 6992 | |
| 6993 | RESET_SAFE_LJMP(hlua->T); |
| 6994 | |
| 6995 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6996 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6997 | |
| 6998 | return 1; |
| 6999 | } |
| 7000 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7001 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7002 | { |
| 7003 | struct stream_interface *si = ctx->owner; |
| 7004 | struct stream *strm = si_strm(si); |
| 7005 | struct channel *req = si_oc(si); |
| 7006 | struct channel *res = si_ic(si); |
| 7007 | struct act_rule *rule = ctx->rule; |
| 7008 | struct proxy *px = strm->be; |
| 7009 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7010 | struct htx *req_htx, *res_htx; |
| 7011 | |
| 7012 | res_htx = htx_from_buf(&res->buf); |
| 7013 | |
| 7014 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7015 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7016 | goto out; |
| 7017 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7018 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7019 | if (!b_size(&res->buf)) { |
| 7020 | si_rx_room_blk(si); |
| 7021 | goto out; |
| 7022 | } |
| 7023 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7024 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7025 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7026 | |
| 7027 | /* Set the currently running flag. */ |
| 7028 | if (!HLUA_IS_RUNNING(hlua) && |
| 7029 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7030 | struct htx_blk *blk; |
| 7031 | size_t count = co_data(req); |
| 7032 | |
| 7033 | if (!count) { |
| 7034 | si_cant_get(si); |
| 7035 | goto out; |
| 7036 | } |
| 7037 | |
| 7038 | /* We need to flush the request header. This left the body for |
| 7039 | * the Lua. |
| 7040 | */ |
| 7041 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7042 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7043 | while (count && blk) { |
| 7044 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7045 | uint32_t sz = htx_get_blksz(blk); |
| 7046 | |
| 7047 | if (sz > count) { |
| 7048 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7049 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7050 | goto out; |
| 7051 | } |
| 7052 | |
| 7053 | count -= sz; |
| 7054 | co_set_data(req, co_data(req) - sz); |
| 7055 | blk = htx_remove_blk(req_htx, blk); |
| 7056 | |
| 7057 | if (type == HTX_BLK_EOH) |
| 7058 | break; |
| 7059 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7060 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7061 | } |
| 7062 | |
| 7063 | /* Executes The applet if it is not done. */ |
| 7064 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7065 | |
| 7066 | /* Execute the function. */ |
| 7067 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7068 | /* finished. */ |
| 7069 | case HLUA_E_OK: |
| 7070 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7071 | break; |
| 7072 | |
| 7073 | /* yield. */ |
| 7074 | case HLUA_E_AGAIN: |
| 7075 | if (hlua->wake_time != TICK_ETERNITY) |
| 7076 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7077 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7078 | |
| 7079 | /* finished with error. */ |
| 7080 | case HLUA_E_ERRMSG: |
| 7081 | /* Display log. */ |
| 7082 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7083 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7084 | lua_pop(hlua->T, 1); |
| 7085 | goto error; |
| 7086 | |
| 7087 | case HLUA_E_ETMOUT: |
| 7088 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7089 | rule->arg.hlua_rule->fcn.name); |
| 7090 | goto error; |
| 7091 | |
| 7092 | case HLUA_E_NOMEM: |
| 7093 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7094 | rule->arg.hlua_rule->fcn.name); |
| 7095 | goto error; |
| 7096 | |
| 7097 | case HLUA_E_YIELD: /* unexpected */ |
| 7098 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7099 | rule->arg.hlua_rule->fcn.name); |
| 7100 | goto error; |
| 7101 | |
| 7102 | case HLUA_E_ERR: |
| 7103 | /* Display log. */ |
| 7104 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7105 | rule->arg.hlua_rule->fcn.name); |
| 7106 | goto error; |
| 7107 | |
| 7108 | default: |
| 7109 | goto error; |
| 7110 | } |
| 7111 | } |
| 7112 | |
| 7113 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7114 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7115 | goto done; |
| 7116 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7117 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7118 | goto error; |
| 7119 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7120 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7121 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7122 | si_rx_room_blk(si); |
| 7123 | goto out; |
| 7124 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7125 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7126 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7127 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7128 | } |
| 7129 | |
| 7130 | done: |
| 7131 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7132 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7133 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7134 | si_shutr(si); |
| 7135 | } |
| 7136 | |
| 7137 | /* eat the whole request */ |
| 7138 | if (co_data(req)) { |
| 7139 | req_htx = htx_from_buf(&req->buf); |
| 7140 | co_htx_skip(req, req_htx, co_data(req)); |
| 7141 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7142 | } |
| 7143 | } |
| 7144 | |
| 7145 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7146 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7147 | return; |
| 7148 | |
| 7149 | error: |
| 7150 | |
| 7151 | /* If we are in HTTP mode, and we are not send any |
| 7152 | * data, return a 500 server error in best effort: |
| 7153 | * if there is no room available in the buffer, |
| 7154 | * just close the connection. |
| 7155 | */ |
| 7156 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7157 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7158 | |
| 7159 | channel_erase(res); |
| 7160 | res->buf.data = b_data(err); |
| 7161 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7162 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7163 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7164 | } |
| 7165 | if (!(strm->flags & SF_ERR_MASK)) |
| 7166 | strm->flags |= SF_ERR_RESOURCE; |
| 7167 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7168 | goto done; |
| 7169 | } |
| 7170 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7171 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7172 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7173 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7174 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7175 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7176 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7177 | } |
| 7178 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7179 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7180 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7181 | * |
| 7182 | * This function can fail with an abort() due to an Lua critical error. |
| 7183 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7184 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7185 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7186 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7187 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7188 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7189 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7190 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7191 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7192 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7193 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7194 | if (!rule->arg.hlua_rule) { |
| 7195 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7196 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7197 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7198 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7199 | /* Memory for arguments. */ |
| 7200 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7201 | if (!rule->arg.hlua_rule->args) { |
| 7202 | memprintf(err, "out of memory error"); |
| 7203 | return ACT_RET_PRS_ERR; |
| 7204 | } |
| 7205 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7206 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7207 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7208 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7209 | /* Expect some arguments */ |
| 7210 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7211 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7212 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7213 | return ACT_RET_PRS_ERR; |
| 7214 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7215 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7216 | if (!rule->arg.hlua_rule->args[i]) { |
| 7217 | memprintf(err, "out of memory error"); |
| 7218 | return ACT_RET_PRS_ERR; |
| 7219 | } |
| 7220 | (*cur_arg)++; |
| 7221 | } |
| 7222 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7223 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7224 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7225 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7226 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7227 | } |
| 7228 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7229 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7230 | struct act_rule *rule, char **err) |
| 7231 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7232 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7233 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7234 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7235 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7236 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7237 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7238 | * the call of this analyzer. |
| 7239 | */ |
| 7240 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7241 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7242 | return ACT_RET_PRS_ERR; |
| 7243 | } |
| 7244 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7245 | /* Memory for the rule. */ |
| 7246 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7247 | if (!rule->arg.hlua_rule) { |
| 7248 | memprintf(err, "out of memory error"); |
| 7249 | return ACT_RET_PRS_ERR; |
| 7250 | } |
| 7251 | |
| 7252 | /* Reference the Lua function and store the reference. */ |
| 7253 | rule->arg.hlua_rule->fcn = *fcn; |
| 7254 | |
| 7255 | /* TODO: later accept arguments. */ |
| 7256 | rule->arg.hlua_rule->args = NULL; |
| 7257 | |
| 7258 | /* Add applet pointer in the rule. */ |
| 7259 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7260 | rule->applet.name = fcn->name; |
| 7261 | rule->applet.init = hlua_applet_http_init; |
| 7262 | rule->applet.fct = hlua_applet_http_fct; |
| 7263 | rule->applet.release = hlua_applet_http_release; |
| 7264 | rule->applet.timeout = hlua_timeout_applet; |
| 7265 | |
| 7266 | return ACT_RET_PRS_OK; |
| 7267 | } |
| 7268 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7269 | /* This function is an LUA binding used for registering |
| 7270 | * "sample-conv" functions. It expects a converter name used |
| 7271 | * in the haproxy configuration file, and an LUA function. |
| 7272 | */ |
| 7273 | __LJMP static int hlua_register_action(lua_State *L) |
| 7274 | { |
| 7275 | struct action_kw_list *akl; |
| 7276 | const char *name; |
| 7277 | int ref; |
| 7278 | int len; |
| 7279 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7280 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7281 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7282 | /* Initialise the number of expected arguments at 0. */ |
| 7283 | nargs = 0; |
| 7284 | |
| 7285 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7286 | 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] | 7287 | |
| 7288 | /* First argument : converter name. */ |
| 7289 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7290 | |
| 7291 | /* Second argument : environment. */ |
| 7292 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7293 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7294 | |
| 7295 | /* Third argument : lua function. */ |
| 7296 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7297 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7298 | /* 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] | 7299 | if (lua_gettop(L) >= 4) |
| 7300 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7301 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7302 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7303 | lua_pushnil(L); |
| 7304 | while (lua_next(L, 2) != 0) { |
| 7305 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7306 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7307 | |
| 7308 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7309 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7310 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7311 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7312 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7313 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7314 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7315 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7316 | |
| 7317 | /* Fill fcn. */ |
| 7318 | fcn->name = strdup(name); |
| 7319 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7320 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7321 | fcn->function_ref = ref; |
| 7322 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7323 | /* Set the expected number od arguments. */ |
| 7324 | fcn->nargs = nargs; |
| 7325 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7326 | /* List head */ |
| 7327 | akl->list.n = akl->list.p = NULL; |
| 7328 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7329 | /* action keyword. */ |
| 7330 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7331 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7332 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7333 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7334 | |
| 7335 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7336 | |
| 7337 | akl->kw[0].match_pfx = 0; |
| 7338 | akl->kw[0].private = fcn; |
| 7339 | akl->kw[0].parse = action_register_lua; |
| 7340 | |
| 7341 | /* select the action registering point. */ |
| 7342 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7343 | tcp_req_cont_keywords_register(akl); |
| 7344 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7345 | tcp_res_cont_keywords_register(akl); |
| 7346 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7347 | http_req_keywords_register(akl); |
| 7348 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7349 | http_res_keywords_register(akl); |
| 7350 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7351 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7352 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7353 | "are expected.", lua_tostring(L, -1))); |
| 7354 | |
| 7355 | /* pop the environment string. */ |
| 7356 | lua_pop(L, 1); |
| 7357 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7358 | return ACT_RET_PRS_OK; |
| 7359 | } |
| 7360 | |
| 7361 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7362 | struct act_rule *rule, char **err) |
| 7363 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7364 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7365 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7366 | if (px->mode == PR_MODE_HTTP) { |
| 7367 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7368 | return ACT_RET_PRS_ERR; |
| 7369 | } |
| 7370 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7371 | /* Memory for the rule. */ |
| 7372 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7373 | if (!rule->arg.hlua_rule) { |
| 7374 | memprintf(err, "out of memory error"); |
| 7375 | return ACT_RET_PRS_ERR; |
| 7376 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7377 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7378 | /* Reference the Lua function and store the reference. */ |
| 7379 | rule->arg.hlua_rule->fcn = *fcn; |
| 7380 | |
| 7381 | /* TODO: later accept arguments. */ |
| 7382 | rule->arg.hlua_rule->args = NULL; |
| 7383 | |
| 7384 | /* Add applet pointer in the rule. */ |
| 7385 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7386 | rule->applet.name = fcn->name; |
| 7387 | rule->applet.init = hlua_applet_tcp_init; |
| 7388 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7389 | rule->applet.release = hlua_applet_tcp_release; |
| 7390 | rule->applet.timeout = hlua_timeout_applet; |
| 7391 | |
| 7392 | return 0; |
| 7393 | } |
| 7394 | |
| 7395 | /* This function is an LUA binding used for registering |
| 7396 | * "sample-conv" functions. It expects a converter name used |
| 7397 | * in the haproxy configuration file, and an LUA function. |
| 7398 | */ |
| 7399 | __LJMP static int hlua_register_service(lua_State *L) |
| 7400 | { |
| 7401 | struct action_kw_list *akl; |
| 7402 | const char *name; |
| 7403 | const char *env; |
| 7404 | int ref; |
| 7405 | int len; |
| 7406 | struct hlua_function *fcn; |
| 7407 | |
| 7408 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7409 | |
| 7410 | /* First argument : converter name. */ |
| 7411 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7412 | |
| 7413 | /* Second argument : environment. */ |
| 7414 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7415 | |
| 7416 | /* Third argument : lua function. */ |
| 7417 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7418 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7419 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7420 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7421 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7422 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7423 | fcn = calloc(1, sizeof(*fcn)); |
| 7424 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7425 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7426 | |
| 7427 | /* Fill fcn. */ |
| 7428 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7429 | fcn->name = calloc(1, len); |
| 7430 | if (!fcn->name) |
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 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7433 | fcn->function_ref = ref; |
| 7434 | |
| 7435 | /* List head */ |
| 7436 | akl->list.n = akl->list.p = NULL; |
| 7437 | |
| 7438 | /* converter keyword. */ |
| 7439 | len = strlen("lua.") + strlen(name) + 1; |
| 7440 | akl->kw[0].kw = calloc(1, len); |
| 7441 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7442 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7443 | |
| 7444 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7445 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7446 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7447 | if (strcmp(env, "tcp") == 0) |
| 7448 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7449 | else if (strcmp(env, "http") == 0) |
| 7450 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7451 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7452 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7453 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7454 | |
| 7455 | akl->kw[0].match_pfx = 0; |
| 7456 | akl->kw[0].private = fcn; |
| 7457 | |
| 7458 | /* End of array. */ |
| 7459 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7460 | |
| 7461 | /* Register this new converter */ |
| 7462 | service_keywords_register(akl); |
| 7463 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7464 | return 0; |
| 7465 | } |
| 7466 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7467 | /* This function initialises Lua cli handler. It copies the |
| 7468 | * arguments in the Lua stack and create channel IO objects. |
| 7469 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7470 | 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] | 7471 | { |
| 7472 | struct hlua *hlua; |
| 7473 | struct hlua_function *fcn; |
| 7474 | int i; |
| 7475 | const char *error; |
| 7476 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7477 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7478 | appctx->ctx.hlua_cli.fcn = private; |
| 7479 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7480 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7481 | if (!hlua) { |
| 7482 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7483 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7484 | } |
| 7485 | HLUA_INIT(hlua); |
| 7486 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7487 | |
| 7488 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7489 | * 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] | 7490 | * applet_http. It is absolutely compatible. |
| 7491 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7492 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7493 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7494 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7495 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7496 | } |
| 7497 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7498 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7499 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7500 | |
| 7501 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7502 | 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] | 7503 | 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] | 7504 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7505 | } |
| 7506 | |
| 7507 | /* The following Lua calls can fail. */ |
| 7508 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7509 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7510 | error = lua_tostring(hlua->T, -1); |
| 7511 | else |
| 7512 | error = "critical error"; |
| 7513 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7514 | goto error; |
| 7515 | } |
| 7516 | |
| 7517 | /* Check stack available size. */ |
| 7518 | if (!lua_checkstack(hlua->T, 2)) { |
| 7519 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7520 | goto error; |
| 7521 | } |
| 7522 | |
| 7523 | /* Restore the function in the stack. */ |
| 7524 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7525 | |
| 7526 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7527 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7528 | */ |
| 7529 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7530 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7531 | goto error; |
| 7532 | } |
| 7533 | hlua->nargs = 1; |
| 7534 | |
| 7535 | /* push keywords in the stack. */ |
| 7536 | for (i = 0; *args[i]; i++) { |
| 7537 | /* Check stack available size. */ |
| 7538 | if (!lua_checkstack(hlua->T, 1)) { |
| 7539 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7540 | goto error; |
| 7541 | } |
| 7542 | lua_pushstring(hlua->T, args[i]); |
| 7543 | hlua->nargs++; |
| 7544 | } |
| 7545 | |
| 7546 | /* We must initialize the execution timeouts. */ |
| 7547 | hlua->max_time = hlua_timeout_session; |
| 7548 | |
| 7549 | /* At this point the execution is safe. */ |
| 7550 | RESET_SAFE_LJMP(hlua->T); |
| 7551 | |
| 7552 | /* It's ok */ |
| 7553 | return 0; |
| 7554 | |
| 7555 | /* It's not ok. */ |
| 7556 | error: |
| 7557 | RESET_SAFE_LJMP(hlua->T); |
| 7558 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7559 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7560 | return 1; |
| 7561 | } |
| 7562 | |
| 7563 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7564 | { |
| 7565 | struct hlua *hlua; |
| 7566 | struct stream_interface *si; |
| 7567 | struct hlua_function *fcn; |
| 7568 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7569 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7570 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7571 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7572 | |
| 7573 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7574 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7575 | return 1; |
| 7576 | |
| 7577 | /* Execute the function. */ |
| 7578 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7579 | |
| 7580 | /* finished. */ |
| 7581 | case HLUA_E_OK: |
| 7582 | return 1; |
| 7583 | |
| 7584 | /* yield. */ |
| 7585 | case HLUA_E_AGAIN: |
| 7586 | /* We want write. */ |
| 7587 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7588 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7589 | /* Set the timeout. */ |
| 7590 | if (hlua->wake_time != TICK_ETERNITY) |
| 7591 | task_schedule(hlua->task, hlua->wake_time); |
| 7592 | return 0; |
| 7593 | |
| 7594 | /* finished with error. */ |
| 7595 | case HLUA_E_ERRMSG: |
| 7596 | /* Display log. */ |
| 7597 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7598 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7599 | lua_pop(hlua->T, 1); |
| 7600 | return 1; |
| 7601 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7602 | case HLUA_E_ETMOUT: |
| 7603 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7604 | fcn->name); |
| 7605 | return 1; |
| 7606 | |
| 7607 | case HLUA_E_NOMEM: |
| 7608 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7609 | fcn->name); |
| 7610 | return 1; |
| 7611 | |
| 7612 | case HLUA_E_YIELD: /* unexpected */ |
| 7613 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7614 | fcn->name); |
| 7615 | return 1; |
| 7616 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7617 | case HLUA_E_ERR: |
| 7618 | /* Display log. */ |
| 7619 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7620 | fcn->name); |
| 7621 | return 1; |
| 7622 | |
| 7623 | default: |
| 7624 | return 1; |
| 7625 | } |
| 7626 | |
| 7627 | return 1; |
| 7628 | } |
| 7629 | |
| 7630 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7631 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7632 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7633 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7634 | } |
| 7635 | |
| 7636 | /* This function is an LUA binding used for registering |
| 7637 | * new keywords in the cli. It expects a list of keywords |
| 7638 | * which are the "path". It is limited to 5 keywords. A |
| 7639 | * description of the command, a function to be executed |
| 7640 | * for the parsing and a function for io handlers. |
| 7641 | */ |
| 7642 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7643 | { |
| 7644 | struct cli_kw_list *cli_kws; |
| 7645 | const char *message; |
| 7646 | int ref_io; |
| 7647 | int len; |
| 7648 | struct hlua_function *fcn; |
| 7649 | int index; |
| 7650 | int i; |
| 7651 | |
| 7652 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7653 | |
| 7654 | /* First argument : an array of maximum 5 keywords. */ |
| 7655 | if (!lua_istable(L, 1)) |
| 7656 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7657 | |
| 7658 | /* Second argument : string with contextual message. */ |
| 7659 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7660 | |
| 7661 | /* Third and fourth argument : lua function. */ |
| 7662 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7663 | |
| 7664 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7665 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7666 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7667 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7668 | fcn = calloc(1, sizeof(*fcn)); |
| 7669 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7670 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7671 | |
| 7672 | /* Fill path. */ |
| 7673 | index = 0; |
| 7674 | lua_pushnil(L); |
| 7675 | while(lua_next(L, 1) != 0) { |
| 7676 | if (index >= 5) |
| 7677 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7678 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7679 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7680 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7681 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7682 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7683 | index++; |
| 7684 | lua_pop(L, 1); |
| 7685 | } |
| 7686 | |
| 7687 | /* Copy help message. */ |
| 7688 | cli_kws->kw[0].usage = strdup(message); |
| 7689 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7690 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7691 | |
| 7692 | /* Fill fcn io handler. */ |
| 7693 | len = strlen("<lua.cli>") + 1; |
| 7694 | for (i = 0; i < index; i++) |
| 7695 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7696 | fcn->name = calloc(1, len); |
| 7697 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7698 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7699 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7700 | for (i = 0; i < index; i++) { |
| 7701 | strncat((char *)fcn->name, ".", len); |
| 7702 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7703 | } |
| 7704 | strncat((char *)fcn->name, ">", len); |
| 7705 | fcn->function_ref = ref_io; |
| 7706 | |
| 7707 | /* Fill last entries. */ |
| 7708 | cli_kws->kw[0].private = fcn; |
| 7709 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7710 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7711 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7712 | |
| 7713 | /* Register this new converter */ |
| 7714 | cli_register_kw(cli_kws); |
| 7715 | |
| 7716 | return 0; |
| 7717 | } |
| 7718 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7719 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7720 | struct proxy *defpx, const char *file, int line, |
| 7721 | char **err, unsigned int *timeout) |
| 7722 | { |
| 7723 | const char *error; |
| 7724 | |
| 7725 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7726 | if (error == PARSE_TIME_OVER) { |
| 7727 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7728 | args[1], args[0]); |
| 7729 | return -1; |
| 7730 | } |
| 7731 | else if (error == PARSE_TIME_UNDER) { |
| 7732 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7733 | args[1], args[0]); |
| 7734 | return -1; |
| 7735 | } |
| 7736 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7737 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7738 | return -1; |
| 7739 | } |
| 7740 | return 0; |
| 7741 | } |
| 7742 | |
| 7743 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7744 | struct proxy *defpx, const char *file, int line, |
| 7745 | char **err) |
| 7746 | { |
| 7747 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7748 | file, line, err, &hlua_timeout_session); |
| 7749 | } |
| 7750 | |
| 7751 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7752 | struct proxy *defpx, const char *file, int line, |
| 7753 | char **err) |
| 7754 | { |
| 7755 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7756 | file, line, err, &hlua_timeout_task); |
| 7757 | } |
| 7758 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7759 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7760 | struct proxy *defpx, const char *file, int line, |
| 7761 | char **err) |
| 7762 | { |
| 7763 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7764 | file, line, err, &hlua_timeout_applet); |
| 7765 | } |
| 7766 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7767 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7768 | struct proxy *defpx, const char *file, int line, |
| 7769 | char **err) |
| 7770 | { |
| 7771 | char *error; |
| 7772 | |
| 7773 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7774 | if (*error != '\0') { |
| 7775 | memprintf(err, "%s: invalid number", args[0]); |
| 7776 | return -1; |
| 7777 | } |
| 7778 | return 0; |
| 7779 | } |
| 7780 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7781 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7782 | struct proxy *defpx, const char *file, int line, |
| 7783 | char **err) |
| 7784 | { |
| 7785 | char *error; |
| 7786 | |
| 7787 | if (*(args[1]) == 0) { |
| 7788 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7789 | return -1; |
| 7790 | } |
| 7791 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7792 | if (*error != '\0') { |
| 7793 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7794 | return -1; |
| 7795 | } |
| 7796 | return 0; |
| 7797 | } |
| 7798 | |
| 7799 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7800 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7801 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7802 | * the main lua entry point. |
| 7803 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7804 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7805 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7806 | * |
| 7807 | * In some error case, LUA set an error message in top of the stack. This function |
| 7808 | * 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] | 7809 | * |
| 7810 | * This function can fail with an abort() due to an Lua critical error. |
| 7811 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7812 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7813 | */ |
| 7814 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7815 | struct proxy *defpx, const char *file, int line, |
| 7816 | char **err) |
| 7817 | { |
| 7818 | int error; |
| 7819 | |
| 7820 | /* Just load and compile the file. */ |
| 7821 | error = luaL_loadfile(gL.T, args[1]); |
| 7822 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7823 | 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] | 7824 | lua_pop(gL.T, 1); |
| 7825 | return -1; |
| 7826 | } |
| 7827 | |
| 7828 | /* If no syntax error where detected, execute the code. */ |
| 7829 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7830 | switch (error) { |
| 7831 | case LUA_OK: |
| 7832 | break; |
| 7833 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7834 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7835 | lua_pop(gL.T, 1); |
| 7836 | return -1; |
| 7837 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7838 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7839 | return -1; |
| 7840 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7841 | 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] | 7842 | lua_pop(gL.T, 1); |
| 7843 | return -1; |
| 7844 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7845 | 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] | 7846 | lua_pop(gL.T, 1); |
| 7847 | return -1; |
| 7848 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7849 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7850 | lua_pop(gL.T, 1); |
| 7851 | return -1; |
| 7852 | } |
| 7853 | |
| 7854 | return 0; |
| 7855 | } |
| 7856 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7857 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7858 | * in the given <ctx>. |
| 7859 | */ |
| 7860 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7861 | { |
| 7862 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7863 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7864 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7865 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7866 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7867 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7868 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7869 | |
| 7870 | return 0; |
| 7871 | } |
| 7872 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7873 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7874 | struct proxy *defpx, const char *file, int line, |
| 7875 | char **err) |
| 7876 | { |
| 7877 | char *path; |
| 7878 | char *type = "path"; |
| 7879 | if (too_many_args(2, args, err, NULL)) { |
| 7880 | return -1; |
| 7881 | } |
| 7882 | |
| 7883 | if (!(*args[1])) { |
| 7884 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7885 | return -1; |
| 7886 | } |
| 7887 | path = args[1]; |
| 7888 | |
| 7889 | if (*args[2]) { |
| 7890 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7891 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7892 | return -1; |
| 7893 | } |
| 7894 | type = args[2]; |
| 7895 | } |
| 7896 | |
| 7897 | return hlua_prepend_path(gL, type, path); |
| 7898 | } |
| 7899 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7900 | /* configuration keywords declaration */ |
| 7901 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7902 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7903 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7904 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7905 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7906 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7907 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7908 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7909 | { 0, NULL, NULL }, |
| 7910 | }}; |
| 7911 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7912 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7913 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7914 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7915 | /* This function can fail with an abort() due to an Lua critical error. |
| 7916 | * We are in the initialisation process of HAProxy, this abort() is |
| 7917 | * tolerated. |
| 7918 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7919 | int hlua_post_init() |
| 7920 | { |
| 7921 | struct hlua_init_function *init; |
| 7922 | const char *msg; |
| 7923 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7924 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7925 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7926 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7927 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7928 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7929 | error = lua_tostring(gL.T, -1); |
| 7930 | else |
| 7931 | error = "critical error"; |
| 7932 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7933 | exit(1); |
| 7934 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7935 | |
| 7936 | #if USE_OPENSSL |
| 7937 | /* Initialize SSL server. */ |
| 7938 | if (socket_ssl.xprt->prepare_srv) { |
| 7939 | int saved_used_backed = global.ssl_used_backend; |
| 7940 | // don't affect maxconn automatic computation |
| 7941 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7942 | global.ssl_used_backend = saved_used_backed; |
| 7943 | } |
| 7944 | #endif |
| 7945 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7946 | hlua_fcn_post_init(gL.T); |
| 7947 | RESET_SAFE_LJMP(gL.T); |
| 7948 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7949 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7950 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7951 | ret = hlua_ctx_resume(&gL, 0); |
| 7952 | switch (ret) { |
| 7953 | case HLUA_E_OK: |
| 7954 | lua_pop(gL.T, -1); |
| 7955 | return 1; |
| 7956 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7957 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7958 | return 0; |
| 7959 | case HLUA_E_ERRMSG: |
| 7960 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7961 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7962 | return 0; |
| 7963 | case HLUA_E_ERR: |
| 7964 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7965 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7966 | return 0; |
| 7967 | } |
| 7968 | } |
| 7969 | return 1; |
| 7970 | } |
| 7971 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7972 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7973 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7974 | * is the previously allocated size or the kind of object in case of a new |
| 7975 | * allocation. <nsize> is the requested new size. |
| 7976 | */ |
| 7977 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7978 | { |
| 7979 | struct hlua_mem_allocator *zone = ud; |
| 7980 | |
| 7981 | if (nsize == 0) { |
| 7982 | /* it's a free */ |
| 7983 | if (ptr) |
| 7984 | zone->allocated -= osize; |
| 7985 | free(ptr); |
| 7986 | return NULL; |
| 7987 | } |
| 7988 | |
| 7989 | if (!ptr) { |
| 7990 | /* it's a new allocation */ |
| 7991 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7992 | return NULL; |
| 7993 | |
| 7994 | ptr = malloc(nsize); |
| 7995 | if (ptr) |
| 7996 | zone->allocated += nsize; |
| 7997 | return ptr; |
| 7998 | } |
| 7999 | |
| 8000 | /* it's a realloc */ |
| 8001 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8002 | return NULL; |
| 8003 | |
| 8004 | ptr = realloc(ptr, nsize); |
| 8005 | if (ptr) |
| 8006 | zone->allocated += nsize - osize; |
| 8007 | return ptr; |
| 8008 | } |
| 8009 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8010 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8011 | * We are in the initialisation process of HAProxy, this abort() is |
| 8012 | * tolerated. |
| 8013 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8014 | void hlua_init(void) |
| 8015 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8016 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8017 | int idx; |
| 8018 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8019 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8020 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8021 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8022 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8023 | struct srv_kw *kw; |
| 8024 | int tmp_error; |
| 8025 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8026 | char *args[] = { /* SSL client configuration. */ |
| 8027 | "ssl", |
| 8028 | "verify", |
| 8029 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8030 | NULL |
| 8031 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8032 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8033 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8034 | /* Init main lua stack. */ |
| 8035 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8036 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8037 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8038 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8039 | hlua_sethlua(&gL); |
| 8040 | gL.Tref = LUA_REFNIL; |
| 8041 | gL.task = NULL; |
| 8042 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8043 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8044 | * the Lua function can fail with an abort. We are in the initialisation |
| 8045 | * process of HAProxy, this abort() is tolerated. |
| 8046 | */ |
| 8047 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8048 | /* Initialise lua. */ |
| 8049 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8050 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8051 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8052 | #ifdef HLUA_PREPEND_PATH |
| 8053 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8054 | #endif |
| 8055 | #ifdef HLUA_PREPEND_CPATH |
| 8056 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8057 | #endif |
| 8058 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8059 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8060 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8061 | /* Set safe environment for the initialisation. */ |
| 8062 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8063 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8064 | error_msg = lua_tostring(gL.T, -1); |
| 8065 | else |
| 8066 | error_msg = "critical error"; |
| 8067 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8068 | exit(1); |
| 8069 | } |
| 8070 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8071 | /* |
| 8072 | * |
| 8073 | * Create "core" object. |
| 8074 | * |
| 8075 | */ |
| 8076 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8077 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8078 | lua_newtable(gL.T); |
| 8079 | |
| 8080 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8081 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8082 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8083 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8084 | /* Register special functions. */ |
| 8085 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8086 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8087 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8088 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8089 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8090 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8091 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8092 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8093 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8094 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8095 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8096 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8097 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8098 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8099 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8100 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8101 | hlua_class_function(gL.T, "log", hlua_log); |
| 8102 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8103 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8104 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8105 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8106 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8107 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8108 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8109 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8110 | |
| 8111 | /* |
| 8112 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8113 | * Create "act" object. |
| 8114 | * |
| 8115 | */ |
| 8116 | |
| 8117 | /* This table entry is the object "act" base. */ |
| 8118 | lua_newtable(gL.T); |
| 8119 | |
| 8120 | /* push action return constants */ |
| 8121 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8122 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8123 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8124 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8125 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8126 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8127 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8128 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8129 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8130 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8131 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8132 | lua_setglobal(gL.T, "act"); |
| 8133 | |
| 8134 | /* |
| 8135 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8136 | * Register class Map |
| 8137 | * |
| 8138 | */ |
| 8139 | |
| 8140 | /* This table entry is the object "Map" base. */ |
| 8141 | lua_newtable(gL.T); |
| 8142 | |
| 8143 | /* register pattern types. */ |
| 8144 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8145 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8146 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8147 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8148 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8149 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8150 | |
| 8151 | /* register constructor. */ |
| 8152 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8153 | |
| 8154 | /* Create and fill the metatable. */ |
| 8155 | lua_newtable(gL.T); |
| 8156 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8157 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8158 | lua_pushstring(gL.T, "__index"); |
| 8159 | lua_newtable(gL.T); |
| 8160 | |
| 8161 | /* Register . */ |
| 8162 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8163 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8164 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8165 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8166 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8167 | /* Register previous table in the registry with reference and named entry. |
| 8168 | * The function hlua_register_metatable() pops the stack, so we |
| 8169 | * previously create a copy of the table. |
| 8170 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8171 | 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] | 8172 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8173 | |
| 8174 | /* Assign the metatable to the mai Map object. */ |
| 8175 | lua_setmetatable(gL.T, -2); |
| 8176 | |
| 8177 | /* Set a name to the table. */ |
| 8178 | lua_setglobal(gL.T, "Map"); |
| 8179 | |
| 8180 | /* |
| 8181 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8182 | * Register class Channel |
| 8183 | * |
| 8184 | */ |
| 8185 | |
| 8186 | /* Create and fill the metatable. */ |
| 8187 | lua_newtable(gL.T); |
| 8188 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8189 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8190 | lua_pushstring(gL.T, "__index"); |
| 8191 | lua_newtable(gL.T); |
| 8192 | |
| 8193 | /* Register . */ |
| 8194 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8195 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8196 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8197 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8198 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8199 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8200 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8201 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8202 | 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] | 8203 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8204 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8205 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8206 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8207 | |
| 8208 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8209 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8210 | |
| 8211 | /* |
| 8212 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8213 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8214 | * |
| 8215 | */ |
| 8216 | |
| 8217 | /* Create and fill the metatable. */ |
| 8218 | lua_newtable(gL.T); |
| 8219 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8220 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8221 | lua_pushstring(gL.T, "__index"); |
| 8222 | lua_newtable(gL.T); |
| 8223 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8224 | /* Browse existing fetches and create the associated |
| 8225 | * object method. |
| 8226 | */ |
| 8227 | sf = NULL; |
| 8228 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8229 | |
| 8230 | /* Dont register the keywork if the arguments check function are |
| 8231 | * not safe during the runtime. |
| 8232 | */ |
| 8233 | if ((sf->val_args != NULL) && |
| 8234 | (sf->val_args != val_payload_lv) && |
| 8235 | (sf->val_args != val_hdr)) |
| 8236 | continue; |
| 8237 | |
| 8238 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8239 | * by an underscore. |
| 8240 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8241 | strncpy(trash.area, sf->kw, trash.size); |
| 8242 | trash.area[trash.size - 1] = '\0'; |
| 8243 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8244 | if (*p == '.' || *p == '-' || *p == '+') |
| 8245 | *p = '_'; |
| 8246 | |
| 8247 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8248 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8249 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8250 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8251 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8252 | } |
| 8253 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8254 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8255 | |
| 8256 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8257 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8258 | |
| 8259 | /* |
| 8260 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8261 | * Register class Converters |
| 8262 | * |
| 8263 | */ |
| 8264 | |
| 8265 | /* Create and fill the metatable. */ |
| 8266 | lua_newtable(gL.T); |
| 8267 | |
| 8268 | /* Create and fill the __index entry. */ |
| 8269 | lua_pushstring(gL.T, "__index"); |
| 8270 | lua_newtable(gL.T); |
| 8271 | |
| 8272 | /* Browse existing converters and create the associated |
| 8273 | * object method. |
| 8274 | */ |
| 8275 | sc = NULL; |
| 8276 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8277 | /* Dont register the keywork if the arguments check function are |
| 8278 | * not safe during the runtime. |
| 8279 | */ |
| 8280 | if (sc->val_args != NULL) |
| 8281 | continue; |
| 8282 | |
| 8283 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8284 | * by an underscore. |
| 8285 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8286 | strncpy(trash.area, sc->kw, trash.size); |
| 8287 | trash.area[trash.size - 1] = '\0'; |
| 8288 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8289 | if (*p == '.' || *p == '-' || *p == '+') |
| 8290 | *p = '_'; |
| 8291 | |
| 8292 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8293 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8294 | lua_pushlightuserdata(gL.T, sc); |
| 8295 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8296 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8297 | } |
| 8298 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8299 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8300 | |
| 8301 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8302 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8303 | |
| 8304 | /* |
| 8305 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8306 | * Register class HTTP |
| 8307 | * |
| 8308 | */ |
| 8309 | |
| 8310 | /* Create and fill the metatable. */ |
| 8311 | lua_newtable(gL.T); |
| 8312 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8313 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8314 | lua_pushstring(gL.T, "__index"); |
| 8315 | lua_newtable(gL.T); |
| 8316 | |
| 8317 | /* Register Lua functions. */ |
| 8318 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8319 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8320 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8321 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8322 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8323 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8324 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8325 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8326 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8327 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8328 | |
| 8329 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8330 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8331 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8332 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8333 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8334 | 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] | 8335 | 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] | 8336 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8337 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8338 | |
| 8339 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8340 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8341 | |
| 8342 | /* |
| 8343 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8344 | * Register class AppletTCP |
| 8345 | * |
| 8346 | */ |
| 8347 | |
| 8348 | /* Create and fill the metatable. */ |
| 8349 | lua_newtable(gL.T); |
| 8350 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8351 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8352 | lua_pushstring(gL.T, "__index"); |
| 8353 | lua_newtable(gL.T); |
| 8354 | |
| 8355 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8356 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8357 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8358 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8359 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8360 | 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] | 8361 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8362 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8363 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8364 | |
| 8365 | lua_settable(gL.T, -3); |
| 8366 | |
| 8367 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8368 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8369 | |
| 8370 | /* |
| 8371 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8372 | * Register class AppletHTTP |
| 8373 | * |
| 8374 | */ |
| 8375 | |
| 8376 | /* Create and fill the metatable. */ |
| 8377 | lua_newtable(gL.T); |
| 8378 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8379 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8380 | lua_pushstring(gL.T, "__index"); |
| 8381 | lua_newtable(gL.T); |
| 8382 | |
| 8383 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8384 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8385 | 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] | 8386 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8387 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8388 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8389 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8390 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8391 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8392 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8393 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8394 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8395 | |
| 8396 | lua_settable(gL.T, -3); |
| 8397 | |
| 8398 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8399 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8400 | |
| 8401 | /* |
| 8402 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8403 | * Register class TXN |
| 8404 | * |
| 8405 | */ |
| 8406 | |
| 8407 | /* Create and fill the metatable. */ |
| 8408 | lua_newtable(gL.T); |
| 8409 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8410 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8411 | lua_pushstring(gL.T, "__index"); |
| 8412 | lua_newtable(gL.T); |
| 8413 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8414 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8415 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8416 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8417 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8418 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8419 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8420 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8421 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8422 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8423 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8424 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8425 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8426 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8427 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8428 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8429 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8430 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8431 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8432 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8433 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8434 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8435 | |
| 8436 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8437 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8438 | |
| 8439 | /* |
| 8440 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8441 | * Register class reply |
| 8442 | * |
| 8443 | */ |
| 8444 | lua_newtable(gL.T); |
| 8445 | lua_pushstring(gL.T, "__index"); |
| 8446 | lua_newtable(gL.T); |
| 8447 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8448 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8449 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8450 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8451 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8452 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8453 | |
| 8454 | |
| 8455 | /* |
| 8456 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8457 | * Register class Socket |
| 8458 | * |
| 8459 | */ |
| 8460 | |
| 8461 | /* Create and fill the metatable. */ |
| 8462 | lua_newtable(gL.T); |
| 8463 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8464 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8465 | lua_pushstring(gL.T, "__index"); |
| 8466 | lua_newtable(gL.T); |
| 8467 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8468 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8469 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8470 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8471 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8472 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8473 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8474 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8475 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8476 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8477 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8478 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8479 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8480 | 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] | 8481 | |
| 8482 | /* Register the garbage collector entry. */ |
| 8483 | lua_pushstring(gL.T, "__gc"); |
| 8484 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8485 | 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] | 8486 | |
| 8487 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8488 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8489 | |
| 8490 | /* Proxy and server configuration initialisation. */ |
| 8491 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8492 | init_new_proxy(&socket_proxy); |
| 8493 | socket_proxy.parent = NULL; |
| 8494 | socket_proxy.last_change = now.tv_sec; |
| 8495 | socket_proxy.id = "LUA-SOCKET"; |
| 8496 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8497 | socket_proxy.maxconn = 0; |
| 8498 | socket_proxy.accept = NULL; |
| 8499 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8500 | socket_proxy.srv = NULL; |
| 8501 | socket_proxy.conn_retries = 0; |
| 8502 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8503 | |
| 8504 | /* Init TCP server: unchanged parameters */ |
| 8505 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8506 | socket_tcp.next = NULL; |
| 8507 | socket_tcp.proxy = &socket_proxy; |
| 8508 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8509 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8510 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8511 | socket_tcp.idle_conns = NULL; |
| 8512 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8513 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8514 | socket_tcp.last_change = 0; |
| 8515 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8516 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8517 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8518 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8519 | |
| 8520 | /* XXX: Copy default parameter from default server, |
| 8521 | * but the default server is not initialized. |
| 8522 | */ |
| 8523 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8524 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8525 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8526 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8527 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8528 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8529 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8530 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8531 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8532 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8533 | |
| 8534 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8535 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8536 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8537 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8538 | socket_tcp.check.server = &socket_tcp; |
| 8539 | |
| 8540 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8541 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8542 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8543 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8544 | socket_tcp.agent.server = &socket_tcp; |
| 8545 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8546 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8547 | |
| 8548 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8549 | /* Init TCP server: unchanged parameters */ |
| 8550 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8551 | socket_ssl.next = NULL; |
| 8552 | socket_ssl.proxy = &socket_proxy; |
| 8553 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8554 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8555 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8556 | socket_ssl.idle_conns = NULL; |
| 8557 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8558 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8559 | socket_ssl.last_change = 0; |
| 8560 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8561 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8562 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8563 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8564 | |
| 8565 | /* XXX: Copy default parameter from default server, |
| 8566 | * but the default server is not initialized. |
| 8567 | */ |
| 8568 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8569 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8570 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8571 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8572 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8573 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8574 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8575 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8576 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8577 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8578 | |
| 8579 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8580 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8581 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8582 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8583 | socket_ssl.check.server = &socket_ssl; |
| 8584 | |
| 8585 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8586 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8587 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8588 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8589 | socket_ssl.agent.server = &socket_ssl; |
| 8590 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8591 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8592 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8593 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8594 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8595 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8596 | /* |
| 8597 | * |
| 8598 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8599 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8600 | * features like client certificates and ssl_verify. |
| 8601 | * |
| 8602 | */ |
| 8603 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8604 | if (tmp_error != 0) { |
| 8605 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8606 | abort(); /* This must be never arrives because the command line |
| 8607 | not editable by the user. */ |
| 8608 | } |
| 8609 | idx += kw->skip; |
| 8610 | } |
| 8611 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8612 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8613 | |
| 8614 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8615 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8616 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8617 | static void hlua_register_build_options(void) |
| 8618 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8619 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8620 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8621 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8622 | hap_register_build_opts(ptr, 1); |
| 8623 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8624 | |
| 8625 | INITCALL0(STG_REGISTER, hlua_register_build_options); |