Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 14 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 16 | #include <lauxlib.h> |
| 17 | #include <lua.h> |
| 18 | #include <lualib.h> |
| 19 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 20 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 21 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 24 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 26 | #include <haproxy/api.h> |
| 27 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 28 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 29 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 30 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 31 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 32 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 33 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 34 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 35 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 36 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 37 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 38 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 39 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 40 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 41 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 42 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 43 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 44 | #include <haproxy/payload.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 45 | #include <haproxy/proxy-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 46 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 47 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 48 | #include <haproxy/server-t.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 49 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 50 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 51 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 52 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 53 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 54 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 55 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 56 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 57 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/xref.h> |
| 59 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 60 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 61 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 62 | * macro is used only for identifying the function that can |
| 63 | * not return because a longjmp is executed. |
| 64 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 65 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 66 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 67 | */ |
| 68 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 69 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | #define MAY_LJMP(func) func |
| 71 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 72 | /* This couple of function executes securely some Lua calls outside of |
| 73 | * the lua runtime environment. Each Lua call can return a longjmp |
| 74 | * if it encounter a memory error. |
| 75 | * |
| 76 | * Lua documentation extract: |
| 77 | * |
| 78 | * If an error happens outside any protected environment, Lua calls |
| 79 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 80 | * exiting the host application. Your panic function can avoid this |
| 81 | * exit by never returning (e.g., doing a long jump to your own |
| 82 | * recovery point outside Lua). |
| 83 | * |
| 84 | * The panic function runs as if it were a message handler (see |
| 85 | * §2.3); in particular, the error message is at the top of the |
| 86 | * stack. However, there is no guarantee about stack space. To push |
| 87 | * anything on the stack, the panic function must first check the |
| 88 | * available space (see §4.2). |
| 89 | * |
| 90 | * We must check all the Lua entry point. This includes: |
| 91 | * - The include/proto/hlua.h exported functions |
| 92 | * - the task wrapper function |
| 93 | * - The action wrapper function |
| 94 | * - The converters wrapper function |
| 95 | * - The sample-fetch wrapper functions |
| 96 | * |
| 97 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 98 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 99 | * |
| 100 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 101 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 102 | * because they must be exists in the program stack when the longjmp |
| 103 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 104 | * |
| 105 | * Note that the Lua processing is not really thread safe. It provides |
| 106 | * heavy system which consists to add our own lock function in the Lua |
| 107 | * code and recompile the library. This system will probably not accepted |
| 108 | * by maintainers of various distribs. |
| 109 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 110 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 111 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 112 | * of function and a lua_unlock() at the end of the function. So I |
| 113 | * conclude that the Lua thread safe mode just perform a mutex around |
| 114 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 115 | * easier for distro maintainers. |
| 116 | * |
| 117 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 118 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 119 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 120 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 121 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 122 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 124 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 125 | |
| 126 | #define SET_SAFE_LJMP(__L) \ |
| 127 | ({ \ |
| 128 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 129 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 130 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 131 | lua_atpanic(__L, hlua_panic_safe); \ |
| 132 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 133 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | } else { \ |
| 135 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 136 | ret = 1; \ |
| 137 | } \ |
| 138 | ret; \ |
| 139 | }) |
| 140 | |
| 141 | /* If we are the last function catching Lua errors, we |
| 142 | * must reset the panic function. |
| 143 | */ |
| 144 | #define RESET_SAFE_LJMP(__L) \ |
| 145 | do { \ |
| 146 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 147 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 148 | } while(0) |
| 149 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 150 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 151 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 152 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 153 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 154 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 155 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 156 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 157 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 158 | /* The main Lua execution context. */ |
| 159 | struct hlua gL; |
| 160 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 161 | /* This is the memory pool containing struct lua for applets |
| 162 | * (including cli). |
| 163 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 164 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 165 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 166 | /* Used for Socket connection. */ |
| 167 | static struct proxy socket_proxy; |
| 168 | static struct server socket_tcp; |
| 169 | #ifdef USE_OPENSSL |
| 170 | static struct server socket_ssl; |
| 171 | #endif |
| 172 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 173 | /* List head of the function called at the initialisation time. */ |
| 174 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 175 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 176 | /* The following variables contains the reference of the different |
| 177 | * Lua classes. These references are useful for identify metadata |
| 178 | * associated with an object. |
| 179 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 180 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 181 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 182 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 183 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 184 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 185 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 186 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 187 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 188 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 189 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 190 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 191 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 192 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 193 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 194 | * because a task may remain alive during all the haproxy execution. |
| 195 | */ |
| 196 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 197 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 198 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 199 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 200 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 201 | * it is used for preventing infinite loops. |
| 202 | * |
| 203 | * I test the scheer with an infinite loop containing one incrementation |
| 204 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 205 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 206 | * to one interrupt each 10 000 instructions. |
| 207 | * |
| 208 | * configured | Number of |
| 209 | * instructions | loops executed |
| 210 | * between two | in milions |
| 211 | * forced yields | |
| 212 | * ---------------+--------------- |
| 213 | * 10 | 160 |
| 214 | * 500 | 670 |
| 215 | * 1000 | 680 |
| 216 | * 5000 | 700 |
| 217 | * 7000 | 700 |
| 218 | * 8000 | 700 |
| 219 | * 9000 | 710 <- ceil |
| 220 | * 10000 | 710 |
| 221 | * 100000 | 710 |
| 222 | * 1000000 | 710 |
| 223 | * |
| 224 | */ |
| 225 | static unsigned int hlua_nb_instruction = 10000; |
| 226 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 227 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 228 | * be enforced on any memory allocation. |
| 229 | */ |
| 230 | struct hlua_mem_allocator { |
| 231 | size_t allocated; |
| 232 | size_t limit; |
| 233 | }; |
| 234 | |
| 235 | static struct hlua_mem_allocator hlua_global_allocator; |
| 236 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 237 | /* These functions converts types between HAProxy internal args or |
| 238 | * sample and LUA types. Another function permits to check if the |
| 239 | * LUA stack contains arguments according with an required ARG_T |
| 240 | * format. |
| 241 | */ |
| 242 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 243 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 244 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 245 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 246 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 247 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 248 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 249 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 250 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 251 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 252 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 253 | do { \ |
| 254 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 255 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 256 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 257 | } while (0) |
| 258 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 259 | /* Used to check an Lua function type in the stack. It creates and |
| 260 | * returns a reference of the function. This function throws an |
| 261 | * error if the rgument is not a "function". |
| 262 | */ |
| 263 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 264 | { |
| 265 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 266 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 267 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 268 | } |
| 269 | lua_pushvalue(L, argno); |
| 270 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 271 | } |
| 272 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 273 | /* Return the string that is of the top of the stack. */ |
| 274 | const char *hlua_get_top_error_string(lua_State *L) |
| 275 | { |
| 276 | if (lua_gettop(L) < 1) |
| 277 | return "unknown error"; |
| 278 | if (lua_type(L, -1) != LUA_TSTRING) |
| 279 | return "unknown error"; |
| 280 | return lua_tostring(L, -1); |
| 281 | } |
| 282 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 283 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 284 | { |
| 285 | lua_Debug ar; |
| 286 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 287 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 288 | int filled = 0; |
| 289 | |
| 290 | while (lua_getstack(L, level++, &ar)) { |
| 291 | |
| 292 | /* Add separator */ |
| 293 | if (filled) |
| 294 | chunk_appendf(msg, ", "); |
| 295 | filled = 1; |
| 296 | |
| 297 | /* Fill fields: |
| 298 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 299 | * 'l': fills in the field currentline; |
| 300 | * 'n': fills in the field name and namewhat; |
| 301 | * 't': fills in the field istailcall; |
| 302 | */ |
| 303 | lua_getinfo(L, "Slnt", &ar); |
| 304 | |
| 305 | /* Append code localisation */ |
| 306 | if (ar.currentline > 0) |
| 307 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 308 | else |
| 309 | chunk_appendf(msg, "%s ", ar.short_src); |
| 310 | |
| 311 | /* |
| 312 | * Get function name |
| 313 | * |
| 314 | * if namewhat is no empty, name is defined. |
| 315 | * what contains "Lua" for Lua function, "C" for C function, |
| 316 | * or "main" for main code. |
| 317 | */ |
| 318 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 319 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 320 | |
| 321 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 322 | chunk_appendf(msg, "main chunk"); |
| 323 | |
| 324 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 325 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 326 | |
| 327 | else /* nothing left... */ |
| 328 | chunk_appendf(msg, "?"); |
| 329 | |
| 330 | |
| 331 | /* Display tailed call */ |
| 332 | if (ar.istailcall) |
| 333 | chunk_appendf(msg, " ..."); |
| 334 | } |
| 335 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 336 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 340 | /* This function check the number of arguments available in the |
| 341 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 342 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 343 | */ |
| 344 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 345 | { |
| 346 | if (lua_gettop(L) == nb) |
| 347 | return; |
| 348 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 349 | } |
| 350 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 351 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 352 | * and the line number where the error is encountered. |
| 353 | */ |
| 354 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 355 | { |
| 356 | va_list argp; |
| 357 | va_start(argp, fmt); |
| 358 | luaL_where(L, 1); |
| 359 | lua_pushvfstring(L, fmt, argp); |
| 360 | va_end(argp); |
| 361 | lua_concat(L, 2); |
| 362 | return 1; |
| 363 | } |
| 364 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 365 | /* This functions is used with sample fetch and converters. It |
| 366 | * converts the HAProxy configuration argument in a lua stack |
| 367 | * values. |
| 368 | * |
| 369 | * It takes an array of "arg", and each entry of the array is |
| 370 | * converted and pushed in the LUA stack. |
| 371 | */ |
| 372 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 373 | { |
| 374 | switch (arg->type) { |
| 375 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 376 | case ARGT_TIME: |
| 377 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 378 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | break; |
| 380 | |
| 381 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 382 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 383 | break; |
| 384 | |
| 385 | case ARGT_IPV4: |
| 386 | case ARGT_IPV6: |
| 387 | case ARGT_MSK4: |
| 388 | case ARGT_MSK6: |
| 389 | case ARGT_FE: |
| 390 | case ARGT_BE: |
| 391 | case ARGT_TAB: |
| 392 | case ARGT_SRV: |
| 393 | case ARGT_USR: |
| 394 | case ARGT_MAP: |
| 395 | default: |
| 396 | lua_pushnil(L); |
| 397 | break; |
| 398 | } |
| 399 | return 1; |
| 400 | } |
| 401 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 402 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 403 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 404 | * with sample fetch wrappers. The input arguments are given to the |
| 405 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 406 | */ |
| 407 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 408 | { |
| 409 | switch (lua_type(L, ud)) { |
| 410 | |
| 411 | case LUA_TNUMBER: |
| 412 | case LUA_TBOOLEAN: |
| 413 | arg->type = ARGT_SINT; |
| 414 | arg->data.sint = lua_tointeger(L, ud); |
| 415 | break; |
| 416 | |
| 417 | case LUA_TSTRING: |
| 418 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 419 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 420 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 421 | arg->data.str.size = arg->data.str.data; |
| 422 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case LUA_TUSERDATA: |
| 426 | case LUA_TNIL: |
| 427 | case LUA_TTABLE: |
| 428 | case LUA_TFUNCTION: |
| 429 | case LUA_TTHREAD: |
| 430 | case LUA_TLIGHTUSERDATA: |
| 431 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 432 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | /* the following functions are used to convert a struct sample |
| 439 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 440 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 441 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 442 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 443 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 444 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 445 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 447 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | case SMP_T_BIN: |
| 451 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 452 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 453 | break; |
| 454 | |
| 455 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 456 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 458 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 459 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 460 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 461 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 462 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 463 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 464 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 465 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 466 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 467 | break; |
| 468 | default: |
| 469 | lua_pushnil(L); |
| 470 | break; |
| 471 | } |
| 472 | break; |
| 473 | |
| 474 | case SMP_T_IPV4: |
| 475 | case SMP_T_IPV6: |
| 476 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 477 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 478 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 479 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 480 | else |
| 481 | lua_pushnil(L); |
| 482 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 483 | default: |
| 484 | lua_pushnil(L); |
| 485 | break; |
| 486 | } |
| 487 | return 1; |
| 488 | } |
| 489 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 490 | /* the following functions are used to convert a struct sample |
| 491 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 492 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 493 | */ |
| 494 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 495 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 496 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 497 | |
| 498 | case SMP_T_BIN: |
| 499 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 500 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 501 | break; |
| 502 | |
| 503 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 504 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 505 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 506 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 507 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 508 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 509 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 510 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 511 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 512 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 513 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 514 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 515 | break; |
| 516 | default: |
| 517 | lua_pushstring(L, ""); |
| 518 | break; |
| 519 | } |
| 520 | break; |
| 521 | |
| 522 | case SMP_T_SINT: |
| 523 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 524 | case SMP_T_IPV4: |
| 525 | case SMP_T_IPV6: |
| 526 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 527 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 528 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 529 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 530 | else |
| 531 | lua_pushstring(L, ""); |
| 532 | break; |
| 533 | default: |
| 534 | lua_pushstring(L, ""); |
| 535 | break; |
| 536 | } |
| 537 | return 1; |
| 538 | } |
| 539 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 540 | /* the following functions are used to convert an Lua type in a |
| 541 | * struct sample. This is useful to provide data from a converter |
| 542 | * to the LUA code. |
| 543 | */ |
| 544 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 545 | { |
| 546 | switch (lua_type(L, ud)) { |
| 547 | |
| 548 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 549 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 550 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 551 | break; |
| 552 | |
| 553 | |
| 554 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 555 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 556 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 557 | break; |
| 558 | |
| 559 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 560 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 562 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 563 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 564 | smp->data.u.str.size = smp->data.u.str.data; |
| 565 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | break; |
| 567 | |
| 568 | case LUA_TUSERDATA: |
| 569 | case LUA_TNIL: |
| 570 | case LUA_TTABLE: |
| 571 | case LUA_TFUNCTION: |
| 572 | case LUA_TTHREAD: |
| 573 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 574 | case LUA_TNONE: |
| 575 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 576 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 577 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | return 1; |
| 581 | } |
| 582 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 583 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 584 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 585 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 586 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 587 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 588 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 589 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 590 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 591 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | { |
| 593 | int min_arg; |
| 594 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 595 | struct proxy *px; |
| 596 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 597 | |
| 598 | idx = 0; |
| 599 | min_arg = ARGM(mask); |
| 600 | mask >>= ARGM_BITS; |
| 601 | |
| 602 | while (1) { |
| 603 | |
| 604 | /* Check oversize. */ |
| 605 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 606 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* Check for mandatory arguments. */ |
| 610 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 611 | if (idx < min_arg) { |
| 612 | |
| 613 | /* If miss other argument than the first one, we return an error. */ |
| 614 | if (idx > 0) |
| 615 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 616 | |
| 617 | /* If first argument have a certain type, some default values |
| 618 | * may be used. See the function smp_resolve_args(). |
| 619 | */ |
| 620 | switch (mask & ARGT_MASK) { |
| 621 | |
| 622 | case ARGT_FE: |
| 623 | if (!(p->cap & PR_CAP_FE)) |
| 624 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 625 | argp[idx].data.prx = p; |
| 626 | argp[idx].type = ARGT_FE; |
| 627 | argp[idx+1].type = ARGT_STOP; |
| 628 | break; |
| 629 | |
| 630 | case ARGT_BE: |
| 631 | if (!(p->cap & PR_CAP_BE)) |
| 632 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 633 | argp[idx].data.prx = p; |
| 634 | argp[idx].type = ARGT_BE; |
| 635 | argp[idx+1].type = ARGT_STOP; |
| 636 | break; |
| 637 | |
| 638 | case ARGT_TAB: |
| 639 | argp[idx].data.prx = p; |
| 640 | argp[idx].type = ARGT_TAB; |
| 641 | argp[idx+1].type = ARGT_STOP; |
| 642 | break; |
| 643 | |
| 644 | default: |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 646 | break; |
| 647 | } |
| 648 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 652 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 653 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 654 | argp[idx].type != ARGT_STOP) { |
| 655 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 656 | } |
| 657 | |
| 658 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 659 | argp[idx].type == ARGT_STOP) { |
| 660 | return 0; |
| 661 | } |
| 662 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 663 | /* Convert some argument types. */ |
| 664 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 665 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 666 | if (argp[idx].type != ARGT_SINT) |
| 667 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 668 | argp[idx].type = ARGT_SINT; |
| 669 | break; |
| 670 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 671 | case ARGT_TIME: |
| 672 | if (argp[idx].type != ARGT_SINT) |
| 673 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 674 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 675 | break; |
| 676 | |
| 677 | case ARGT_SIZE: |
| 678 | if (argp[idx].type != ARGT_SINT) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 680 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 681 | break; |
| 682 | |
| 683 | case ARGT_FE: |
| 684 | if (argp[idx].type != ARGT_STR) |
| 685 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 686 | memcpy(trash.area, argp[idx].data.str.area, |
| 687 | argp[idx].data.str.data); |
| 688 | trash.area[argp[idx].data.str.data] = 0; |
| 689 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 690 | if (!argp[idx].data.prx) |
| 691 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 692 | argp[idx].type = ARGT_FE; |
| 693 | break; |
| 694 | |
| 695 | case ARGT_BE: |
| 696 | if (argp[idx].type != ARGT_STR) |
| 697 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 698 | memcpy(trash.area, argp[idx].data.str.area, |
| 699 | argp[idx].data.str.data); |
| 700 | trash.area[argp[idx].data.str.data] = 0; |
| 701 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 702 | if (!argp[idx].data.prx) |
| 703 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 704 | argp[idx].type = ARGT_BE; |
| 705 | break; |
| 706 | |
| 707 | case ARGT_TAB: |
| 708 | if (argp[idx].type != ARGT_STR) |
| 709 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 710 | memcpy(trash.area, argp[idx].data.str.area, |
| 711 | argp[idx].data.str.data); |
| 712 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 713 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 714 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 716 | argp[idx].type = ARGT_TAB; |
| 717 | break; |
| 718 | |
| 719 | case ARGT_SRV: |
| 720 | if (argp[idx].type != ARGT_STR) |
| 721 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 722 | memcpy(trash.area, argp[idx].data.str.area, |
| 723 | argp[idx].data.str.data); |
| 724 | trash.area[argp[idx].data.str.data] = 0; |
| 725 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 726 | if (sname) { |
| 727 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 728 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 729 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 730 | if (!px) |
| 731 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 732 | } |
| 733 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 734 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 735 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 736 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 737 | argp[idx].data.srv = findserver(px, sname); |
| 738 | if (!argp[idx].data.srv) |
| 739 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 740 | argp[idx].type = ARGT_SRV; |
| 741 | break; |
| 742 | |
| 743 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 744 | memcpy(trash.area, argp[idx].data.str.area, |
| 745 | argp[idx].data.str.data); |
| 746 | trash.area[argp[idx].data.str.data] = 0; |
| 747 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 748 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 749 | argp[idx].type = ARGT_IPV4; |
| 750 | break; |
| 751 | |
| 752 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 753 | memcpy(trash.area, argp[idx].data.str.area, |
| 754 | argp[idx].data.str.data); |
| 755 | trash.area[argp[idx].data.str.data] = 0; |
| 756 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 757 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 758 | argp[idx].type = ARGT_MSK4; |
| 759 | break; |
| 760 | |
| 761 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 762 | memcpy(trash.area, argp[idx].data.str.area, |
| 763 | argp[idx].data.str.data); |
| 764 | trash.area[argp[idx].data.str.data] = 0; |
| 765 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 766 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 767 | argp[idx].type = ARGT_IPV6; |
| 768 | break; |
| 769 | |
| 770 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 771 | memcpy(trash.area, argp[idx].data.str.area, |
| 772 | argp[idx].data.str.data); |
| 773 | trash.area[argp[idx].data.str.data] = 0; |
| 774 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 775 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 776 | argp[idx].type = ARGT_MSK6; |
| 777 | break; |
| 778 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | case ARGT_MAP: |
| 780 | case ARGT_REG: |
| 781 | case ARGT_USR: |
| 782 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 783 | break; |
| 784 | } |
| 785 | |
| 786 | /* Check for type of argument. */ |
| 787 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 788 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 789 | arg_type_names[(mask & ARGT_MASK)], |
| 790 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 791 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 792 | } |
| 793 | |
| 794 | /* Next argument. */ |
| 795 | mask >>= ARGT_BITS; |
| 796 | idx++; |
| 797 | } |
| 798 | } |
| 799 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 800 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 801 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 802 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 803 | * |
| 804 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 805 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 806 | */ |
| 807 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 808 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 809 | struct hlua **hlua = lua_getextraspace(L); |
| 810 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 811 | } |
| 812 | static inline void hlua_sethlua(struct hlua *hlua) |
| 813 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 814 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 815 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 818 | /* This function is used to send logs. It try to send on screen (stderr) |
| 819 | * and on the default syslog server. |
| 820 | */ |
| 821 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 822 | { |
| 823 | struct tm tm; |
| 824 | char *p; |
| 825 | |
| 826 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 827 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 828 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 829 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 830 | /* Break the message if exceed the buffer size. */ |
| 831 | *(p-4) = ' '; |
| 832 | *(p-3) = '.'; |
| 833 | *(p-2) = '.'; |
| 834 | *(p-1) = '.'; |
| 835 | break; |
| 836 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 837 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 838 | *p = *msg; |
| 839 | else |
| 840 | *p = '.'; |
| 841 | } |
| 842 | *p = '\0'; |
| 843 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 844 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 845 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 846 | get_localtime(date.tv_sec, &tm); |
| 847 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 848 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 849 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 850 | fflush(stderr); |
| 851 | } |
| 852 | } |
| 853 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 854 | /* This function just ensure that the yield will be always |
| 855 | * returned with a timeout and permit to set some flags |
| 856 | */ |
| 857 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 858 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 859 | { |
| 860 | struct hlua *hlua = hlua_gethlua(L); |
| 861 | |
| 862 | /* Set the wake timeout. If timeout is required, we set |
| 863 | * the expiration time. |
| 864 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 865 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 866 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 867 | hlua->flags |= flags; |
| 868 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 869 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 870 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 871 | } |
| 872 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 873 | /* This function initialises the Lua environment stored in the stream. |
| 874 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 875 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 876 | * |
| 877 | * This function is particular. it initialises a new Lua thread. If the |
| 878 | * initialisation fails (example: out of memory error), the lua function |
| 879 | * throws an error (longjmp). |
| 880 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 881 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 882 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 883 | * threads appear, the safe environment set a lock to ensure only one |
| 884 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 885 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 886 | * |
| 887 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 888 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 889 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 890 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 891 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 892 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 893 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 894 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 895 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 896 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 897 | if (!already_safe) { |
| 898 | if (!SET_SAFE_LJMP(gL.T)) { |
| 899 | lua->Tref = LUA_REFNIL; |
| 900 | return 0; |
| 901 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 902 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 903 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 904 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 905 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 906 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 907 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 908 | lua->T = lua_newthread(gL.T); |
| 909 | if (!lua->T) { |
| 910 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 911 | if (!already_safe) |
| 912 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 913 | return 0; |
| 914 | } |
| 915 | hlua_sethlua(lua); |
| 916 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 917 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 918 | if (!already_safe) |
| 919 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 920 | return 1; |
| 921 | } |
| 922 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 923 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 925 | * is not freed. |
| 926 | */ |
| 927 | void hlua_ctx_destroy(struct hlua *lua) |
| 928 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 929 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 930 | return; |
| 931 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 932 | if (!lua->T) |
| 933 | goto end; |
| 934 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 935 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 936 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 937 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 938 | if (!SET_SAFE_LJMP(lua->T)) |
| 939 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 940 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 941 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 942 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 943 | if (!SET_SAFE_LJMP(gL.T)) |
| 944 | return; |
| 945 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 946 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 947 | /* Forces a garbage collecting process. If the Lua program is finished |
| 948 | * without error, we run the GC on the thread pointer. Its freed all |
| 949 | * the unused memory. |
| 950 | * If the thread is finnish with an error or is currently yielded, |
| 951 | * it seems that the GC applied on the thread doesn't clean anything, |
| 952 | * so e run the GC on the main thread. |
| 953 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 954 | * the garbage collection. |
| 955 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 956 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 957 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 958 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 959 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 960 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 961 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 962 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 963 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 964 | |
| 965 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 966 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* This function is used to restore the Lua context when a coroutine |
| 970 | * fails. This function copy the common memory between old coroutine |
| 971 | * and the new coroutine. The old coroutine is destroyed, and its |
| 972 | * replaced by the new coroutine. |
| 973 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 974 | * as string error message and it is copied in the new stack. |
| 975 | */ |
| 976 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 977 | { |
| 978 | lua_State *T; |
| 979 | int new_ref; |
| 980 | |
| 981 | /* Renew the main LUA stack doesn't have sense. */ |
| 982 | if (lua == &gL) |
| 983 | return 0; |
| 984 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 985 | /* New Lua coroutine. */ |
| 986 | T = lua_newthread(gL.T); |
| 987 | if (!T) |
| 988 | return 0; |
| 989 | |
| 990 | /* Copy last error message. */ |
| 991 | if (keep_msg) |
| 992 | lua_xmove(lua->T, T, 1); |
| 993 | |
| 994 | /* Copy data between the coroutines. */ |
| 995 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 996 | lua_xmove(lua->T, T, 1); |
| 997 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 998 | |
| 999 | /* Destroy old data. */ |
| 1000 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1001 | |
| 1002 | /* The thread is garbage collected by Lua. */ |
| 1003 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1004 | |
| 1005 | /* Fill the struct with the new coroutine values. */ |
| 1006 | lua->Mref = new_ref; |
| 1007 | lua->T = T; |
| 1008 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1009 | |
| 1010 | /* Set context. */ |
| 1011 | hlua_sethlua(lua); |
| 1012 | |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1016 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1017 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1018 | struct hlua *hlua = hlua_gethlua(L); |
| 1019 | |
| 1020 | /* Lua cannot yield when its returning from a function, |
| 1021 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1022 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1023 | */ |
| 1024 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1025 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | /* restore the interrupt condition. */ |
| 1030 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1031 | |
| 1032 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1033 | * If the state is not yieldable, trying yield causes an error. |
| 1034 | */ |
| 1035 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1036 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1037 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1038 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1039 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1040 | hlua->run_time += now_ms - hlua->start_time; |
| 1041 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1042 | lua_pushfstring(L, "execution timeout"); |
| 1043 | WILL_LJMP(lua_error(L)); |
| 1044 | } |
| 1045 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1046 | /* Update the start time. */ |
| 1047 | hlua->start_time = now_ms; |
| 1048 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1049 | /* Try to interrupt the process at the end of the current |
| 1050 | * unyieldable function. |
| 1051 | */ |
| 1052 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1055 | /* This function start or resumes the Lua stack execution. If the flag |
| 1056 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1057 | * The function return an error. |
| 1058 | * |
| 1059 | * The function can returns 4 values: |
| 1060 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1061 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1062 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1063 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1064 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1065 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1066 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1068 | * LUA code. |
| 1069 | */ |
| 1070 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1071 | { |
| 1072 | int ret; |
| 1073 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1074 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1075 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1076 | /* Initialise run time counter. */ |
| 1077 | if (!HLUA_IS_RUNNING(lua)) |
| 1078 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1079 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1080 | /* Lock the whole Lua execution. This lock must be before the |
| 1081 | * label "resume_execution". |
| 1082 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1083 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1084 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1085 | resume_execution: |
| 1086 | |
| 1087 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1088 | * instructions. it is used for preventing infinite loops. |
| 1089 | */ |
| 1090 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1091 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1092 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1093 | HLUA_SET_RUN(lua); |
| 1094 | HLUA_CLR_CTRLYIELD(lua); |
| 1095 | HLUA_CLR_WAKERESWR(lua); |
| 1096 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1097 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1098 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1099 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1100 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1101 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1102 | /* Call the function. */ |
| 1103 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1104 | switch (ret) { |
| 1105 | |
| 1106 | case LUA_OK: |
| 1107 | ret = HLUA_E_OK; |
| 1108 | break; |
| 1109 | |
| 1110 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1111 | /* Check if the execution timeout is expired. It it is the case, we |
| 1112 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1113 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1114 | tv_update_date(0, 1); |
| 1115 | lua->run_time += now_ms - lua->start_time; |
| 1116 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1117 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1118 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1119 | break; |
| 1120 | } |
| 1121 | /* Process the forced yield. if the general yield is not allowed or |
| 1122 | * if no task were associated this the current Lua execution |
| 1123 | * coroutine, we resume the execution. Else we want to return in the |
| 1124 | * scheduler and we want to be waked up again, to continue the |
| 1125 | * current Lua execution. So we schedule our own task. |
| 1126 | */ |
| 1127 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1128 | if (!yield_allowed || !lua->task) |
| 1129 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1130 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1131 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1132 | if (!yield_allowed) { |
| 1133 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1134 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1135 | break; |
| 1136 | } |
| 1137 | ret = HLUA_E_AGAIN; |
| 1138 | break; |
| 1139 | |
| 1140 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1141 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1142 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1143 | * because the errors ares the only one mean to return immediately |
| 1144 | * from and lua execution. |
| 1145 | */ |
| 1146 | if (lua->flags & HLUA_EXIT) { |
| 1147 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1148 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1149 | break; |
| 1150 | } |
| 1151 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1152 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1153 | if (!lua_checkstack(lua->T, 1)) { |
| 1154 | ret = HLUA_E_ERR; |
| 1155 | break; |
| 1156 | } |
| 1157 | msg = lua_tostring(lua->T, -1); |
| 1158 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1159 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1160 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1161 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1162 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1163 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1164 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1165 | ret = HLUA_E_ERRMSG; |
| 1166 | break; |
| 1167 | |
| 1168 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1169 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1171 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | break; |
| 1173 | |
| 1174 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1175 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1176 | if (!lua_checkstack(lua->T, 1)) { |
| 1177 | ret = HLUA_E_ERR; |
| 1178 | break; |
| 1179 | } |
| 1180 | msg = lua_tostring(lua->T, -1); |
| 1181 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1182 | lua_pop(lua->T, 1); |
| 1183 | if (msg) |
| 1184 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1185 | else |
| 1186 | lua_pushfstring(lua->T, "message handler error"); |
| 1187 | ret = HLUA_E_ERRMSG; |
| 1188 | break; |
| 1189 | |
| 1190 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1191 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1192 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1193 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | switch (ret) { |
| 1198 | case HLUA_E_AGAIN: |
| 1199 | break; |
| 1200 | |
| 1201 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1202 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1203 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1204 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | break; |
| 1206 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1207 | case HLUA_E_ETMOUT: |
| 1208 | case HLUA_E_NOMEM: |
| 1209 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1211 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1212 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1213 | hlua_ctx_renew(lua, 0); |
| 1214 | break; |
| 1215 | |
| 1216 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1217 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1218 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1222 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1223 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1224 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1225 | return ret; |
| 1226 | } |
| 1227 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1228 | /* This function exit the current code. */ |
| 1229 | __LJMP static int hlua_done(lua_State *L) |
| 1230 | { |
| 1231 | struct hlua *hlua = hlua_gethlua(L); |
| 1232 | |
| 1233 | hlua->flags |= HLUA_EXIT; |
| 1234 | WILL_LJMP(lua_error(L)); |
| 1235 | |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1239 | /* This function is an LUA binding. It provides a function |
| 1240 | * for deleting ACL from a referenced ACL file. |
| 1241 | */ |
| 1242 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1243 | { |
| 1244 | const char *name; |
| 1245 | const char *key; |
| 1246 | struct pat_ref *ref; |
| 1247 | |
| 1248 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1249 | |
| 1250 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1251 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1252 | |
| 1253 | ref = pat_ref_lookup(name); |
| 1254 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1255 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1256 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1257 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1258 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1259 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | /* This function is an LUA binding. It provides a function |
| 1264 | * for deleting map entry from a referenced map file. |
| 1265 | */ |
| 1266 | static int hlua_del_map(lua_State *L) |
| 1267 | { |
| 1268 | const char *name; |
| 1269 | const char *key; |
| 1270 | struct pat_ref *ref; |
| 1271 | |
| 1272 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1273 | |
| 1274 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1275 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1276 | |
| 1277 | ref = pat_ref_lookup(name); |
| 1278 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1279 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1280 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1281 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1282 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1283 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | /* This function is an LUA binding. It provides a function |
| 1288 | * for adding ACL pattern from a referenced ACL file. |
| 1289 | */ |
| 1290 | static int hlua_add_acl(lua_State *L) |
| 1291 | { |
| 1292 | const char *name; |
| 1293 | const char *key; |
| 1294 | struct pat_ref *ref; |
| 1295 | |
| 1296 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1297 | |
| 1298 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1299 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1300 | |
| 1301 | ref = pat_ref_lookup(name); |
| 1302 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1303 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1304 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1305 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1306 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1307 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1308 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | /* This function is an LUA binding. It provides a function |
| 1313 | * for setting map pattern and sample from a referenced map |
| 1314 | * file. |
| 1315 | */ |
| 1316 | static int hlua_set_map(lua_State *L) |
| 1317 | { |
| 1318 | const char *name; |
| 1319 | const char *key; |
| 1320 | const char *value; |
| 1321 | struct pat_ref *ref; |
| 1322 | |
| 1323 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1324 | |
| 1325 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1326 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1327 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1328 | |
| 1329 | ref = pat_ref_lookup(name); |
| 1330 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1331 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1332 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1333 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1334 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1335 | pat_ref_set(ref, key, value, NULL); |
| 1336 | else |
| 1337 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1338 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1339 | return 0; |
| 1340 | } |
| 1341 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1342 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1343 | * 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] | 1344 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1345 | * the name of the object (_G[<name>] = <metable> ). |
| 1346 | * |
| 1347 | * A metable is a table that modify the standard behavior of a standard |
| 1348 | * access to the associated data. The entries of this new metatable are |
| 1349 | * defined as is: |
| 1350 | * |
| 1351 | * http://lua-users.org/wiki/MetatableEvents |
| 1352 | * |
| 1353 | * __index |
| 1354 | * |
| 1355 | * we access an absent field in a table, the result is nil. This is |
| 1356 | * true, but it is not the whole truth. Actually, such access triggers |
| 1357 | * the interpreter to look for an __index metamethod: If there is no |
| 1358 | * such method, as usually happens, then the access results in nil; |
| 1359 | * otherwise, the metamethod will provide the result. |
| 1360 | * |
| 1361 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1362 | * the key does not appear in the table, but the metatable has an __index |
| 1363 | * property: |
| 1364 | * |
| 1365 | * - if the value is a function, the function is called, passing in the |
| 1366 | * table and the key; the return value of that function is returned as |
| 1367 | * the result. |
| 1368 | * |
| 1369 | * - if the value is another table, the value of the key in that table is |
| 1370 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1371 | * table's metatable has an __index property, then it continues on up) |
| 1372 | * |
| 1373 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1374 | * |
| 1375 | * http://www.lua.org/pil/13.4.1.html |
| 1376 | * |
| 1377 | * __newindex |
| 1378 | * |
| 1379 | * Like __index, but control property assignment. |
| 1380 | * |
| 1381 | * __mode - Control weak references. A string value with one or both |
| 1382 | * of the characters 'k' and 'v' which specifies that the the |
| 1383 | * keys and/or values in the table are weak references. |
| 1384 | * |
| 1385 | * __call - Treat a table like a function. When a table is followed by |
| 1386 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1387 | * a __call key pointing to a function, that function is invoked |
| 1388 | * (passing any specified arguments) and the return value is |
| 1389 | * returned. |
| 1390 | * |
| 1391 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1392 | * called, if the metatable for myTable has a __metatable |
| 1393 | * key, the value of that key is returned instead of the |
| 1394 | * actual metatable. |
| 1395 | * |
| 1396 | * __tostring - Control string representation. When the builtin |
| 1397 | * "tostring( myTable )" function is called, if the metatable |
| 1398 | * for myTable has a __tostring property set to a function, |
| 1399 | * that function is invoked (passing myTable to it) and the |
| 1400 | * return value is used as the string representation. |
| 1401 | * |
| 1402 | * __len - Control table length. When the table length is requested using |
| 1403 | * the length operator ( '#' ), if the metatable for myTable has |
| 1404 | * a __len key pointing to a function, that function is invoked |
| 1405 | * (passing myTable to it) and the return value used as the value |
| 1406 | * of "#myTable". |
| 1407 | * |
| 1408 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1409 | * collected, if the metatable has a __gc field pointing to a |
| 1410 | * function, that function is first invoked, passing the userdata |
| 1411 | * to it. The __gc metamethod is not called for tables. |
| 1412 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1413 | * |
| 1414 | * Special metamethods for redefining standard operators: |
| 1415 | * http://www.lua.org/pil/13.1.html |
| 1416 | * |
| 1417 | * __add "+" |
| 1418 | * __sub "-" |
| 1419 | * __mul "*" |
| 1420 | * __div "/" |
| 1421 | * __unm "!" |
| 1422 | * __pow "^" |
| 1423 | * __concat ".." |
| 1424 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1425 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1426 | * http://www.lua.org/pil/13.2.html |
| 1427 | * |
| 1428 | * __eq "==" |
| 1429 | * __lt "<" |
| 1430 | * __le "<=" |
| 1431 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1432 | |
| 1433 | /* |
| 1434 | * |
| 1435 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1436 | * Class Map |
| 1437 | * |
| 1438 | * |
| 1439 | */ |
| 1440 | |
| 1441 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1442 | * a class session, otherwise it throws an error. |
| 1443 | */ |
| 1444 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1445 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1446 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /* This function is the map constructor. It don't need |
| 1450 | * the class Map object. It creates and return a new Map |
| 1451 | * object. It must be called only during "body" or "init" |
| 1452 | * context because it process some filesystem accesses. |
| 1453 | */ |
| 1454 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1455 | { |
| 1456 | const char *fn; |
| 1457 | int match = PAT_MATCH_STR; |
| 1458 | struct sample_conv conv; |
| 1459 | const char *file = ""; |
| 1460 | int line = 0; |
| 1461 | lua_Debug ar; |
| 1462 | char *err = NULL; |
| 1463 | struct arg args[2]; |
| 1464 | |
| 1465 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1466 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1467 | |
| 1468 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1469 | |
| 1470 | if (lua_gettop(L) >= 2) { |
| 1471 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1472 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1473 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1474 | } |
| 1475 | |
| 1476 | /* Get Lua filename and line number. */ |
| 1477 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1478 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1479 | if (ar.currentline > 0) { /* is there info? */ |
| 1480 | file = ar.short_src; |
| 1481 | line = ar.currentline; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | /* fill fake sample_conv struct. */ |
| 1486 | conv.kw = ""; /* unused. */ |
| 1487 | conv.process = NULL; /* unused. */ |
| 1488 | conv.arg_mask = 0; /* unused. */ |
| 1489 | conv.val_args = NULL; /* unused. */ |
| 1490 | conv.out_type = SMP_T_STR; |
| 1491 | conv.private = (void *)(long)match; |
| 1492 | switch (match) { |
| 1493 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1494 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1495 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1496 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1497 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1498 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1499 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1500 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1501 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1502 | default: |
| 1503 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1504 | } |
| 1505 | |
| 1506 | /* fill fake args. */ |
| 1507 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1508 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1509 | args[1].type = ARGT_STOP; |
| 1510 | |
| 1511 | /* load the map. */ |
| 1512 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1513 | /* error case: we cant use luaL_error because we must |
| 1514 | * free the err variable. |
| 1515 | */ |
| 1516 | luaL_where(L, 1); |
| 1517 | lua_pushfstring(L, "'new': %s.", err); |
| 1518 | lua_concat(L, 2); |
| 1519 | free(err); |
| 1520 | WILL_LJMP(lua_error(L)); |
| 1521 | } |
| 1522 | |
| 1523 | /* create the lua object. */ |
| 1524 | lua_newtable(L); |
| 1525 | lua_pushlightuserdata(L, args[0].data.map); |
| 1526 | lua_rawseti(L, -2, 0); |
| 1527 | |
| 1528 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1529 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1530 | lua_setmetatable(L, -2); |
| 1531 | |
| 1532 | |
| 1533 | return 1; |
| 1534 | } |
| 1535 | |
| 1536 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1537 | { |
| 1538 | struct map_descriptor *desc; |
| 1539 | struct pattern *pat; |
| 1540 | struct sample smp; |
| 1541 | |
| 1542 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1543 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1544 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1545 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1546 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1547 | } |
| 1548 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1549 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1550 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1551 | 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] | 1552 | } |
| 1553 | |
| 1554 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1555 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1556 | if (str) |
| 1557 | lua_pushstring(L, ""); |
| 1558 | else |
| 1559 | lua_pushnil(L); |
| 1560 | return 1; |
| 1561 | } |
| 1562 | |
| 1563 | /* 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] | 1564 | 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] | 1565 | return 1; |
| 1566 | } |
| 1567 | |
| 1568 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1569 | { |
| 1570 | return _hlua_map_lookup(L, 0); |
| 1571 | } |
| 1572 | |
| 1573 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1574 | { |
| 1575 | return _hlua_map_lookup(L, 1); |
| 1576 | } |
| 1577 | |
| 1578 | /* |
| 1579 | * |
| 1580 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1581 | * Class Socket |
| 1582 | * |
| 1583 | * |
| 1584 | */ |
| 1585 | |
| 1586 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1587 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1588 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /* 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] | 1592 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1593 | * received. |
| 1594 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1595 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1596 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1597 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1598 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1599 | if (appctx->ctx.hlua_cosocket.die) { |
| 1600 | si_shutw(si); |
| 1601 | si_shutr(si); |
| 1602 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1603 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1604 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1605 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1606 | } |
| 1607 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1608 | /* If we cant write, wakeup the pending write signals. */ |
| 1609 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1610 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1611 | |
| 1612 | /* If we cant read, wakeup the pending read signals. */ |
| 1613 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1614 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1615 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1616 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1617 | * to be notified whenever the connection completes. |
| 1618 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1619 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1620 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1621 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1622 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1623 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1624 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | |
| 1626 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1627 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1628 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1629 | /* 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] | 1630 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1631 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1634 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1635 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1636 | |
| 1637 | /* Some data were injected in the buffer, notify the stream |
| 1638 | * interface. |
| 1639 | */ |
| 1640 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1641 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1642 | |
| 1643 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1644 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1645 | */ |
| 1646 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1647 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | } |
| 1649 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1650 | /* This function is called when the "struct stream" is destroyed. |
| 1651 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | * Wake all the pending signals. |
| 1653 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1654 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1656 | struct xref *peer; |
| 1657 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1658 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1659 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1660 | if (peer) |
| 1661 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | |
| 1663 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1664 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1665 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1669 | * uses this object. If the stream does not exists, just quit. |
| 1670 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1671 | * pending signal can rest in the read and write lists. destroy |
| 1672 | * it. |
| 1673 | */ |
| 1674 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1675 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1676 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1677 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1678 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1679 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1680 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1681 | |
| 1682 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1683 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1684 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1686 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1688 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1689 | appctx->ctx.hlua_cosocket.die = 1; |
| 1690 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1692 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1693 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1698 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1700 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1702 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1704 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1705 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1707 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1708 | |
| 1709 | /* Check if we run on the same thread than the xreator thread. |
| 1710 | * We cannot access to the socket if the thread is different. |
| 1711 | */ |
| 1712 | if (socket->tid != tid) |
| 1713 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1714 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1715 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1716 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1718 | |
| 1719 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1720 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1721 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1722 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1723 | appctx->ctx.hlua_cosocket.die = 1; |
| 1724 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1725 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1726 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1727 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1728 | return 0; |
| 1729 | } |
| 1730 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1731 | /* The close function calls close_helper. |
| 1732 | */ |
| 1733 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1734 | { |
| 1735 | MAY_LJMP(check_args(L, 1, "close")); |
| 1736 | return hlua_socket_close_helper(L); |
| 1737 | } |
| 1738 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1739 | /* This Lua function assumes that the stack contain three parameters. |
| 1740 | * 1 - USERDATA containing a struct socket |
| 1741 | * 2 - INTEGER with values of the macro defined below |
| 1742 | * If the integer is -1, we must read at most one line. |
| 1743 | * If the integer is -2, we ust read all the data until the |
| 1744 | * end of the stream. |
| 1745 | * If the integer is positive value, we must read a number of |
| 1746 | * bytes corresponding to this value. |
| 1747 | */ |
| 1748 | #define HLSR_READ_LINE (-1) |
| 1749 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1750 | __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] | 1751 | { |
| 1752 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1753 | int wanted = lua_tointeger(L, 2); |
| 1754 | struct hlua *hlua = hlua_gethlua(L); |
| 1755 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1756 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1757 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1758 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1759 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1760 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1761 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1762 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1763 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1764 | struct stream_interface *si; |
| 1765 | struct stream *s; |
| 1766 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1767 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1768 | |
| 1769 | /* Check if this lua stack is schedulable. */ |
| 1770 | if (!hlua || !hlua->task) |
| 1771 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1772 | "'frontend', 'backend' or 'task'")); |
| 1773 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1774 | /* Check if we run on the same thread than the xreator thread. |
| 1775 | * We cannot access to the socket if the thread is different. |
| 1776 | */ |
| 1777 | if (socket->tid != tid) |
| 1778 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1779 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1780 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1781 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1782 | if (!peer) |
| 1783 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1784 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1785 | si = appctx->owner; |
| 1786 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1787 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1788 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1790 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1791 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1792 | if (nblk < 0) /* Connection close. */ |
| 1793 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1794 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1795 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1796 | |
| 1797 | /* remove final \r\n. */ |
| 1798 | if (nblk == 1) { |
| 1799 | if (blk1[len1-1] == '\n') { |
| 1800 | len1--; |
| 1801 | skip_at_end++; |
| 1802 | if (blk1[len1-1] == '\r') { |
| 1803 | len1--; |
| 1804 | skip_at_end++; |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | else { |
| 1809 | if (blk2[len2-1] == '\n') { |
| 1810 | len2--; |
| 1811 | skip_at_end++; |
| 1812 | if (blk2[len2-1] == '\r') { |
| 1813 | len2--; |
| 1814 | skip_at_end++; |
| 1815 | } |
| 1816 | } |
| 1817 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1821 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1822 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | if (nblk < 0) /* Connection close. */ |
| 1824 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1825 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1826 | goto connection_empty; |
| 1827 | } |
| 1828 | |
| 1829 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1830 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1831 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | if (nblk < 0) /* Connection close. */ |
| 1833 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1834 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | goto connection_empty; |
| 1836 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1837 | missing_bytes = wanted - socket->b.n; |
| 1838 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1839 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1840 | len1 = missing_bytes; |
| 1841 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1842 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | len = len1; |
| 1846 | |
| 1847 | luaL_addlstring(&socket->b, blk1, len1); |
| 1848 | if (nblk == 2) { |
| 1849 | len += len2; |
| 1850 | luaL_addlstring(&socket->b, blk2, len2); |
| 1851 | } |
| 1852 | |
| 1853 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1854 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | |
| 1856 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1857 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1858 | |
| 1859 | /* If the pattern reclaim to read all the data |
| 1860 | * in the connection, got out. |
| 1861 | */ |
| 1862 | if (wanted == HLSR_READ_ALL) |
| 1863 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1864 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1865 | goto connection_empty; |
| 1866 | |
| 1867 | /* Return result. */ |
| 1868 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1869 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1870 | return 1; |
| 1871 | |
| 1872 | connection_closed: |
| 1873 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1874 | xref_unlock(&socket->xref, peer); |
| 1875 | |
| 1876 | no_peer: |
| 1877 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1878 | /* If the buffer containds data. */ |
| 1879 | if (socket->b.n > 0) { |
| 1880 | luaL_pushresult(&socket->b); |
| 1881 | return 1; |
| 1882 | } |
| 1883 | lua_pushnil(L); |
| 1884 | lua_pushstring(L, "connection closed."); |
| 1885 | return 2; |
| 1886 | |
| 1887 | connection_empty: |
| 1888 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1889 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1890 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1892 | } |
| 1893 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1894 | 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] | 1895 | return 0; |
| 1896 | } |
| 1897 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1898 | /* This Lua function gets two parameters. The first one can be string |
| 1899 | * or a number. If the string is "*l", the user requires one line. If |
| 1900 | * 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] | 1901 | * If the value is a number, the user require a number of bytes equal |
| 1902 | * to the value. The default value is "*l" (a line). |
| 1903 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1904 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | * integer takes this values: |
| 1906 | * -1 : read a line |
| 1907 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1908 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1910 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | * concatenated with the read data. |
| 1912 | */ |
| 1913 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1914 | { |
| 1915 | int wanted = HLSR_READ_LINE; |
| 1916 | const char *pattern; |
| 1917 | int type; |
| 1918 | char *error; |
| 1919 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1920 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1921 | |
| 1922 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1923 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1924 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1925 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1927 | /* Check if we run on the same thread than the xreator thread. |
| 1928 | * We cannot access to the socket if the thread is different. |
| 1929 | */ |
| 1930 | if (socket->tid != tid) |
| 1931 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1932 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | /* check for pattern. */ |
| 1934 | if (lua_gettop(L) >= 2) { |
| 1935 | type = lua_type(L, 2); |
| 1936 | if (type == LUA_TSTRING) { |
| 1937 | pattern = lua_tostring(L, 2); |
| 1938 | if (strcmp(pattern, "*a") == 0) |
| 1939 | wanted = HLSR_READ_ALL; |
| 1940 | else if (strcmp(pattern, "*l") == 0) |
| 1941 | wanted = HLSR_READ_LINE; |
| 1942 | else { |
| 1943 | wanted = strtoll(pattern, &error, 10); |
| 1944 | if (*error != '\0') |
| 1945 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1946 | } |
| 1947 | } |
| 1948 | else if (type == LUA_TNUMBER) { |
| 1949 | wanted = lua_tointeger(L, 2); |
| 1950 | if (wanted < 0) |
| 1951 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | /* Set pattern. */ |
| 1956 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1957 | |
| 1958 | /* Check if we would replace the top by itself. */ |
| 1959 | if (lua_gettop(L) != 2) |
| 1960 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1961 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1962 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1963 | luaL_buffinit(L, &socket->b); |
| 1964 | |
| 1965 | /* Check prefix. */ |
| 1966 | if (lua_gettop(L) >= 3) { |
| 1967 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1968 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1969 | pattern = lua_tolstring(L, 3, &len); |
| 1970 | luaL_addlstring(&socket->b, pattern, len); |
| 1971 | } |
| 1972 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1973 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1977 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1978 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1979 | 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] | 1980 | { |
| 1981 | struct hlua_socket *socket; |
| 1982 | struct hlua *hlua = hlua_gethlua(L); |
| 1983 | struct appctx *appctx; |
| 1984 | size_t buf_len; |
| 1985 | const char *buf; |
| 1986 | int len; |
| 1987 | int send_len; |
| 1988 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1989 | struct xref *peer; |
| 1990 | struct stream_interface *si; |
| 1991 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | |
| 1993 | /* Check if this lua stack is schedulable. */ |
| 1994 | if (!hlua || !hlua->task) |
| 1995 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1996 | "'frontend', 'backend' or 'task'")); |
| 1997 | |
| 1998 | /* Get object */ |
| 1999 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2000 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2001 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2002 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2003 | /* Check if we run on the same thread than the xreator thread. |
| 2004 | * We cannot access to the socket if the thread is different. |
| 2005 | */ |
| 2006 | if (socket->tid != tid) |
| 2007 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2008 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2009 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2010 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2011 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2012 | lua_pushinteger(L, -1); |
| 2013 | return 1; |
| 2014 | } |
| 2015 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2016 | si = appctx->owner; |
| 2017 | s = si_strm(si); |
| 2018 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2019 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2020 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2021 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | lua_pushinteger(L, -1); |
| 2023 | return 1; |
| 2024 | } |
| 2025 | |
| 2026 | /* Update the input buffer data. */ |
| 2027 | buf += sent; |
| 2028 | send_len = buf_len - sent; |
| 2029 | |
| 2030 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2031 | if (sent >= buf_len) { |
| 2032 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2033 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2034 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2035 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2036 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2037 | * the request buffer if its not required. |
| 2038 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2039 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2040 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2041 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2042 | } |
| 2043 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2044 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2045 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2046 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2047 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2048 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | |
| 2050 | /* send data */ |
| 2051 | if (len < send_len) |
| 2052 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2053 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | |
| 2055 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2056 | * the data" (-2) are not expected because the available length |
| 2057 | * is tested. |
| 2058 | * Other unknown error are also not expected. |
| 2059 | */ |
| 2060 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2061 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2062 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2063 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2064 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2065 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2066 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2067 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2068 | return 1; |
| 2069 | } |
| 2070 | |
| 2071 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2072 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2073 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2074 | s->req.rex = TICK_ETERNITY; |
| 2075 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | |
| 2077 | /* Update length sent. */ |
| 2078 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2079 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | |
| 2081 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2082 | if (sent + len >= buf_len) { |
| 2083 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2085 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | |
| 2087 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2088 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2089 | xref_unlock(&socket->xref, peer); |
| 2090 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2091 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2092 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2093 | 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] | 2094 | return 0; |
| 2095 | } |
| 2096 | |
| 2097 | /* This function initiate the send of data. It just check the input |
| 2098 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2099 | * 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] | 2100 | * "hlua_socket_write_yield" that can yield. |
| 2101 | * |
| 2102 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2103 | * the associated object. The second is a string buffer. The third is |
| 2104 | * a facultative integer that represents where is the buffer position |
| 2105 | * of the start of the data that can send. The first byte is the |
| 2106 | * position "1". The default value is "1". The fourth argument is a |
| 2107 | * facultative integer that represents where is the buffer position |
| 2108 | * of the end of the data that can send. The default is the last byte. |
| 2109 | */ |
| 2110 | static int hlua_socket_send(struct lua_State *L) |
| 2111 | { |
| 2112 | int i; |
| 2113 | int j; |
| 2114 | const char *buf; |
| 2115 | size_t buf_len; |
| 2116 | |
| 2117 | /* Check number of arguments. */ |
| 2118 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2119 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2120 | |
| 2121 | /* Get the string. */ |
| 2122 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2123 | |
| 2124 | /* Get and check j. */ |
| 2125 | if (lua_gettop(L) == 4) { |
| 2126 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2127 | if (j < 0) |
| 2128 | j = buf_len + j + 1; |
| 2129 | if (j > buf_len) |
| 2130 | j = buf_len + 1; |
| 2131 | lua_pop(L, 1); |
| 2132 | } |
| 2133 | else |
| 2134 | j = buf_len; |
| 2135 | |
| 2136 | /* Get and check i. */ |
| 2137 | if (lua_gettop(L) == 3) { |
| 2138 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2139 | if (i < 0) |
| 2140 | i = buf_len + i + 1; |
| 2141 | if (i > buf_len) |
| 2142 | i = buf_len + 1; |
| 2143 | lua_pop(L, 1); |
| 2144 | } else |
| 2145 | i = 1; |
| 2146 | |
| 2147 | /* Check bth i and j. */ |
| 2148 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2149 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | return 1; |
| 2151 | } |
| 2152 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2153 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2154 | return 1; |
| 2155 | } |
| 2156 | if (i == 0) |
| 2157 | i = 1; |
| 2158 | if (j == 0) |
| 2159 | j = 1; |
| 2160 | |
| 2161 | /* Pop the string. */ |
| 2162 | lua_pop(L, 1); |
| 2163 | |
| 2164 | /* Update the buffer length. */ |
| 2165 | buf += i - 1; |
| 2166 | buf_len = j - i + 1; |
| 2167 | lua_pushlstring(L, buf, buf_len); |
| 2168 | |
| 2169 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2170 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2171 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2172 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | } |
| 2174 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2175 | #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] | 2176 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2177 | { |
| 2178 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2179 | int ret; |
| 2180 | int len; |
| 2181 | char *p; |
| 2182 | |
| 2183 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2184 | if (ret <= 0) { |
| 2185 | lua_pushnil(L); |
| 2186 | return 1; |
| 2187 | } |
| 2188 | |
| 2189 | if (ret == AF_UNIX) { |
| 2190 | lua_pushstring(L, buffer+1); |
| 2191 | return 1; |
| 2192 | } |
| 2193 | else if (ret == AF_INET6) { |
| 2194 | buffer[0] = '['; |
| 2195 | len = strlen(buffer); |
| 2196 | buffer[len] = ']'; |
| 2197 | len++; |
| 2198 | buffer[len] = ':'; |
| 2199 | len++; |
| 2200 | p = buffer; |
| 2201 | } |
| 2202 | else if (ret == AF_INET) { |
| 2203 | p = buffer + 1; |
| 2204 | len = strlen(p); |
| 2205 | p[len] = ':'; |
| 2206 | len++; |
| 2207 | } |
| 2208 | else { |
| 2209 | lua_pushnil(L); |
| 2210 | return 1; |
| 2211 | } |
| 2212 | |
| 2213 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2214 | lua_pushnil(L); |
| 2215 | return 1; |
| 2216 | } |
| 2217 | |
| 2218 | lua_pushstring(L, p); |
| 2219 | return 1; |
| 2220 | } |
| 2221 | |
| 2222 | /* Returns information about the peer of the connection. */ |
| 2223 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2224 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2225 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2226 | struct xref *peer; |
| 2227 | struct appctx *appctx; |
| 2228 | struct stream_interface *si; |
| 2229 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2230 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2231 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2233 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2234 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2236 | /* Check if we run on the same thread than the xreator thread. |
| 2237 | * We cannot access to the socket if the thread is different. |
| 2238 | */ |
| 2239 | if (socket->tid != tid) |
| 2240 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2241 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2242 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2243 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2244 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | lua_pushnil(L); |
| 2246 | return 1; |
| 2247 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2248 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2249 | si = appctx->owner; |
| 2250 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2252 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2253 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2254 | lua_pushnil(L); |
| 2255 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2256 | } |
| 2257 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2258 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2259 | xref_unlock(&socket->xref, peer); |
| 2260 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* Returns information about my connection side. */ |
| 2264 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2265 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2266 | struct hlua_socket *socket; |
| 2267 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2268 | struct appctx *appctx; |
| 2269 | struct xref *peer; |
| 2270 | struct stream_interface *si; |
| 2271 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2272 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2273 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2274 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2275 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2276 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2277 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2278 | /* Check if we run on the same thread than the xreator thread. |
| 2279 | * We cannot access to the socket if the thread is different. |
| 2280 | */ |
| 2281 | if (socket->tid != tid) |
| 2282 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2283 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2284 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2285 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2286 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | lua_pushnil(L); |
| 2288 | return 1; |
| 2289 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2290 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2291 | si = appctx->owner; |
| 2292 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2294 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2295 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2296 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | lua_pushnil(L); |
| 2298 | return 1; |
| 2299 | } |
| 2300 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2301 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2302 | xref_unlock(&socket->xref, peer); |
| 2303 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2307 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | .obj_type = OBJ_TYPE_APPLET, |
| 2309 | .name = "<LUA_TCP>", |
| 2310 | .fct = hlua_socket_handler, |
| 2311 | .release = hlua_socket_release, |
| 2312 | }; |
| 2313 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2314 | __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] | 2315 | { |
| 2316 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2317 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2318 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2320 | struct stream_interface *si; |
| 2321 | struct stream *s; |
| 2322 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2323 | /* Check if we run on the same thread than the xreator thread. |
| 2324 | * We cannot access to the socket if the thread is different. |
| 2325 | */ |
| 2326 | if (socket->tid != tid) |
| 2327 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2328 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2329 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2330 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2332 | lua_pushnil(L); |
| 2333 | lua_pushstring(L, "Can't connect"); |
| 2334 | return 2; |
| 2335 | } |
| 2336 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2337 | si = appctx->owner; |
| 2338 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2339 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2340 | /* Check if we run on the same thread than the xreator thread. |
| 2341 | * We cannot access to the socket if the thread is different. |
| 2342 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2343 | if (socket->tid != tid) { |
| 2344 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2345 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2346 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2347 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2348 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2349 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2350 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | lua_pushnil(L); |
| 2352 | lua_pushstring(L, "Can't connect"); |
| 2353 | return 2; |
| 2354 | } |
| 2355 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2356 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2357 | |
| 2358 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2359 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | lua_pushinteger(L, 1); |
| 2362 | return 1; |
| 2363 | } |
| 2364 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2365 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2366 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2368 | } |
| 2369 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2370 | 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] | 2371 | return 0; |
| 2372 | } |
| 2373 | |
| 2374 | /* This function fail or initite the connection. */ |
| 2375 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2376 | { |
| 2377 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2378 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2379 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2380 | struct hlua *hlua; |
| 2381 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2382 | int low, high; |
| 2383 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2384 | struct xref *peer; |
| 2385 | struct stream_interface *si; |
| 2386 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2388 | if (lua_gettop(L) < 2) |
| 2389 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | |
| 2391 | /* Get args. */ |
| 2392 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2393 | |
| 2394 | /* Check if we run on the same thread than the xreator thread. |
| 2395 | * We cannot access to the socket if the thread is different. |
| 2396 | */ |
| 2397 | if (socket->tid != tid) |
| 2398 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2399 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2401 | if (lua_gettop(L) >= 3) { |
| 2402 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2403 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2404 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2405 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2406 | * that are not enclosed within square brackets. |
| 2407 | */ |
| 2408 | if (port > 0) { |
| 2409 | luaL_buffinit(L, &b); |
| 2410 | luaL_addstring(&b, ip); |
| 2411 | luaL_addchar(&b, ':'); |
| 2412 | luaL_pushresult(&b); |
| 2413 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2414 | } |
| 2415 | } |
| 2416 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2417 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2418 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2419 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2420 | lua_pushnil(L); |
| 2421 | return 1; |
| 2422 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2423 | |
| 2424 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2425 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2426 | if (!addr) { |
| 2427 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2428 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2429 | } |
| 2430 | if (low != high) { |
| 2431 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2432 | 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] | 2433 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2434 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2435 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2436 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2437 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2438 | if (port == -1) { |
| 2439 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2440 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2441 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2442 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2443 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2444 | if (port == -1) { |
| 2445 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2446 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2447 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2448 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2449 | } |
| 2450 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2451 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2452 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2453 | si = appctx->owner; |
| 2454 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2455 | |
| 2456 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2457 | xref_unlock(&socket->xref, peer); |
| 2458 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2459 | } |
| 2460 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2461 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2462 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2463 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2464 | |
| 2465 | /* inform the stream that we want to be notified whenever the |
| 2466 | * connection completes. |
| 2467 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2468 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2469 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2470 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2471 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2472 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2473 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2474 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2475 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2476 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2477 | } |
| 2478 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2479 | |
| 2480 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2481 | /* Return yield waiting for connection. */ |
| 2482 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2483 | 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] | 2484 | |
| 2485 | return 0; |
| 2486 | } |
| 2487 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2488 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2489 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2490 | { |
| 2491 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2492 | struct xref *peer; |
| 2493 | struct appctx *appctx; |
| 2494 | struct stream_interface *si; |
| 2495 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | |
| 2497 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2498 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2499 | |
| 2500 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2501 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2502 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2503 | lua_pushnil(L); |
| 2504 | return 1; |
| 2505 | } |
| 2506 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2507 | si = appctx->owner; |
| 2508 | s = si_strm(si); |
| 2509 | |
| 2510 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2511 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2512 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2513 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2514 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2515 | |
| 2516 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2517 | { |
| 2518 | return 0; |
| 2519 | } |
| 2520 | |
| 2521 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2522 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2523 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2524 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2525 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2526 | struct xref *peer; |
| 2527 | struct appctx *appctx; |
| 2528 | struct stream_interface *si; |
| 2529 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2530 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2531 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2532 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2533 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2534 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2535 | /* convert the timeout to millis */ |
| 2536 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2537 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2538 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2539 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2540 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2541 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2542 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2543 | 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] | 2544 | |
| 2545 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2546 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2547 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2548 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2549 | /* Check if we run on the same thread than the xreator thread. |
| 2550 | * We cannot access to the socket if the thread is different. |
| 2551 | */ |
| 2552 | if (socket->tid != tid) |
| 2553 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2554 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2555 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2556 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2557 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2558 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2559 | WILL_LJMP(lua_error(L)); |
| 2560 | return 0; |
| 2561 | } |
| 2562 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2563 | si = appctx->owner; |
| 2564 | s = si_strm(si); |
| 2565 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2566 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2567 | s->req.rto = tmout; |
| 2568 | s->req.wto = tmout; |
| 2569 | s->res.rto = tmout; |
| 2570 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2571 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2572 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2573 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2574 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2575 | |
| 2576 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2577 | task_queue(s->task); |
| 2578 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2579 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2581 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2582 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2586 | { |
| 2587 | struct hlua_socket *socket; |
| 2588 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2589 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2590 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2591 | |
| 2592 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2593 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2594 | hlua_pusherror(L, "socket: full stack"); |
| 2595 | goto out_fail_conf; |
| 2596 | } |
| 2597 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2598 | /* Create the object: obj[0] = userdata. */ |
| 2599 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2601 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2602 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2603 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2605 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2606 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2607 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2608 | goto out_fail_conf; |
| 2609 | } |
| 2610 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2611 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2613 | lua_setmetatable(L, -2); |
| 2614 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2615 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2616 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2617 | if (!appctx) { |
| 2618 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2619 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2620 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2621 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2622 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2623 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2624 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2625 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2626 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2627 | /* Now create a session, task and stream for this applet */ |
| 2628 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2629 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2631 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2634 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2635 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2637 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2638 | } |
| 2639 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2640 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2641 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2643 | /* Configure "right" stream interface. this "si" is used to connect |
| 2644 | * and retrieve data from the server. The connection is initialized |
| 2645 | * with the "struct server". |
| 2646 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2647 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | |
| 2649 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2650 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2651 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | return 1; |
| 2654 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2655 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2656 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2657 | out_fail_sess: |
| 2658 | appctx_free(appctx); |
| 2659 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2660 | WILL_LJMP(lua_error(L)); |
| 2661 | return 0; |
| 2662 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2663 | |
| 2664 | /* |
| 2665 | * |
| 2666 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2667 | * Class Channel |
| 2668 | * |
| 2669 | * |
| 2670 | */ |
| 2671 | |
| 2672 | /* Returns the struct hlua_channel join to the class channel in the |
| 2673 | * stack entry "ud" or throws an argument error. |
| 2674 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2675 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2676 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2677 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2678 | } |
| 2679 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2680 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2681 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2682 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2683 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2684 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2685 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2686 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | return 0; |
| 2688 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2689 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2690 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2691 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2692 | |
| 2693 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2694 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2695 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2696 | return 1; |
| 2697 | } |
| 2698 | |
| 2699 | /* Duplicate all the data present in the input channel and put it |
| 2700 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2701 | * the stack if the channel is closed and all the data are consumed, |
| 2702 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2703 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2704 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2705 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2706 | { |
| 2707 | char *blk1; |
| 2708 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2709 | size_t len1; |
| 2710 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2711 | int ret; |
| 2712 | luaL_Buffer b; |
| 2713 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2714 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2715 | if (unlikely(ret == 0)) |
| 2716 | return 0; |
| 2717 | |
| 2718 | if (unlikely(ret < 0)) { |
| 2719 | lua_pushnil(L); |
| 2720 | return -1; |
| 2721 | } |
| 2722 | |
| 2723 | luaL_buffinit(L, &b); |
| 2724 | luaL_addlstring(&b, blk1, len1); |
| 2725 | if (unlikely(ret == 2)) |
| 2726 | luaL_addlstring(&b, blk2, len2); |
| 2727 | luaL_pushresult(&b); |
| 2728 | |
| 2729 | if (unlikely(ret == 2)) |
| 2730 | return len1 + len2; |
| 2731 | return len1; |
| 2732 | } |
| 2733 | |
| 2734 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2735 | * a yield. This function keep the data in the buffer. |
| 2736 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2737 | __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] | 2738 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2739 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2740 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2741 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2742 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2743 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2744 | WILL_LJMP(lua_error(L)); |
| 2745 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2746 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2747 | 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] | 2748 | return 1; |
| 2749 | } |
| 2750 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2751 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2752 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2753 | { |
| 2754 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2755 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2756 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2757 | } |
| 2758 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2759 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2760 | * a yield. This function consumes the data in the buffer. It returns |
| 2761 | * a string containing the data or a nil pointer if no data are available |
| 2762 | * and the channel is closed. |
| 2763 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2764 | __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] | 2765 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2766 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2767 | int ret; |
| 2768 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2769 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2770 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2771 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2772 | WILL_LJMP(lua_error(L)); |
| 2773 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2774 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2775 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2776 | 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] | 2777 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | if (unlikely(ret == -1)) |
| 2779 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2780 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2781 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | return 1; |
| 2783 | } |
| 2784 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2785 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2786 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2787 | { |
| 2788 | MAY_LJMP(check_args(L, 1, "get")); |
| 2789 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2790 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2791 | } |
| 2792 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2793 | /* This functions consumes and returns one line. If the channel is closed, |
| 2794 | * 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] | 2795 | * 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] | 2796 | * value. |
| 2797 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2798 | __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] | 2799 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | char *blk1; |
| 2801 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2802 | size_t len1; |
| 2803 | size_t len2; |
| 2804 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2805 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2806 | int ret; |
| 2807 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2808 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2809 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2810 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2811 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2812 | WILL_LJMP(lua_error(L)); |
| 2813 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2814 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2815 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2816 | 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] | 2817 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2818 | if (ret == -1) { |
| 2819 | lua_pushnil(L); |
| 2820 | return 1; |
| 2821 | } |
| 2822 | |
| 2823 | luaL_buffinit(L, &b); |
| 2824 | luaL_addlstring(&b, blk1, len1); |
| 2825 | len = len1; |
| 2826 | if (unlikely(ret == 2)) { |
| 2827 | luaL_addlstring(&b, blk2, len2); |
| 2828 | len += len2; |
| 2829 | } |
| 2830 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2831 | 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] | 2832 | return 1; |
| 2833 | } |
| 2834 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2835 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2836 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2837 | { |
| 2838 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2839 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2840 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2841 | } |
| 2842 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2843 | /* This function takes a string as input, and append it at the |
| 2844 | * input side of channel. If the data is too big, but a space |
| 2845 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2846 | * yields. If the data is bigger than the buffer, or if the |
| 2847 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2848 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2849 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2850 | __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] | 2851 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2852 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2853 | size_t len; |
| 2854 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2855 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2856 | int ret; |
| 2857 | int max; |
| 2858 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2859 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2860 | WILL_LJMP(lua_error(L)); |
| 2861 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2862 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2863 | * the request buffer if its not required. |
| 2864 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2865 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2866 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2867 | 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] | 2868 | } |
| 2869 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2870 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2871 | if (max > len - l) |
| 2872 | max = len - l; |
| 2873 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2874 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2875 | if (ret == -2 || ret == -3) { |
| 2876 | lua_pushinteger(L, -1); |
| 2877 | return 1; |
| 2878 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2879 | if (ret == -1) { |
| 2880 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2881 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2882 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2883 | l += ret; |
| 2884 | lua_pop(L, 1); |
| 2885 | lua_pushinteger(L, l); |
| 2886 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2887 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2888 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2889 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2891 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | */ |
| 2893 | return 1; |
| 2894 | } |
| 2895 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2896 | 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] | 2897 | return 1; |
| 2898 | } |
| 2899 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2900 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2901 | * 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] | 2902 | * buffer size is too little for the data. |
| 2903 | */ |
| 2904 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2905 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2906 | size_t len; |
| 2907 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2909 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2910 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2911 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | lua_pushinteger(L, 0); |
| 2913 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2914 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | } |
| 2916 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2917 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2919 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | * or -1 if the channel is closed or if the buffer size is too |
| 2921 | * little for the data. |
| 2922 | */ |
| 2923 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2924 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2925 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2926 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2928 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2929 | lua_pushinteger(L, 0); |
| 2930 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2931 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2932 | WILL_LJMP(lua_error(L)); |
| 2933 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2934 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2935 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2936 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | } |
| 2938 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2939 | /* Append data in the output side of the buffer. This data is immediately |
| 2940 | * sent. The function returns the amount of data written. If the buffer |
| 2941 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2942 | * if the channel is closed. |
| 2943 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2944 | __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] | 2945 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2946 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2947 | size_t len; |
| 2948 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2949 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2950 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2951 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2952 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2953 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2954 | WILL_LJMP(lua_error(L)); |
| 2955 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2956 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2957 | lua_pushinteger(L, -1); |
| 2958 | return 1; |
| 2959 | } |
| 2960 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2961 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2962 | * the request buffer if its not required. |
| 2963 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2964 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2965 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2966 | 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] | 2967 | } |
| 2968 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2969 | /* The written data will be immediately sent, so we can check |
| 2970 | * the available space without taking in account the reserve. |
| 2971 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2972 | * data, because the buffer will be flushed. |
| 2973 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2974 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2975 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2976 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2977 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2978 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2979 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2980 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2981 | return 1; |
| 2982 | |
| 2983 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2984 | if (max > len - l) |
| 2985 | max = len - l; |
| 2986 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2987 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2988 | * detects a non contiguous buffer and realign it. |
| 2989 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 2990 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2991 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2992 | |
| 2993 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2994 | 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] | 2995 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | /* buffer replace considers that the input part is filled. |
| 2997 | * so, I must forward these new data in the output part. |
| 2998 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 2999 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | |
| 3001 | l += max; |
| 3002 | lua_pop(L, 1); |
| 3003 | lua_pushinteger(L, l); |
| 3004 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3005 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3006 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3007 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3008 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3009 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3010 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3012 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3013 | if (l < len) { |
| 3014 | /* If we are waiting for space in the response buffer, we |
| 3015 | * must set the flag WAKERESWR. This flag required the task |
| 3016 | * wake up if any activity is detected on the response buffer. |
| 3017 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3018 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3019 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3020 | else |
| 3021 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3022 | 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] | 3023 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | |
| 3025 | return 1; |
| 3026 | } |
| 3027 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3028 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3029 | * yield the LUA process, and resume it without checking the |
| 3030 | * input arguments. |
| 3031 | */ |
| 3032 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3033 | { |
| 3034 | MAY_LJMP(check_args(L, 2, "send")); |
| 3035 | lua_pushinteger(L, 0); |
| 3036 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3037 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | /* This function forward and amount of butes. The data pass from |
| 3041 | * the input side of the buffer to the output side, and can be |
| 3042 | * forwarded. This function never fails. |
| 3043 | * |
| 3044 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3045 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3046 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3047 | __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] | 3048 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3049 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3050 | int len; |
| 3051 | int l; |
| 3052 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3053 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3054 | |
| 3055 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3056 | |
| 3057 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3058 | WILL_LJMP(lua_error(L)); |
| 3059 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3061 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3062 | |
| 3063 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3064 | if (max > ci_data(chn)) |
| 3065 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3066 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | l += max; |
| 3068 | |
| 3069 | lua_pop(L, 1); |
| 3070 | lua_pushinteger(L, l); |
| 3071 | |
| 3072 | /* Check if it miss bytes to forward. */ |
| 3073 | if (l < len) { |
| 3074 | /* The the input channel or the output channel are closed, we |
| 3075 | * must return the amount of data forwarded. |
| 3076 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3077 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3078 | return 1; |
| 3079 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3080 | /* If we are waiting for space data in the response buffer, we |
| 3081 | * must set the flag WAKERESWR. This flag required the task |
| 3082 | * wake up if any activity is detected on the response buffer. |
| 3083 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3084 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3085 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3086 | else |
| 3087 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3088 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3089 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3090 | 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] | 3091 | } |
| 3092 | |
| 3093 | return 1; |
| 3094 | } |
| 3095 | |
| 3096 | /* Just check the input and prepare the stack for the previous |
| 3097 | * function "hlua_channel_forward_yield" |
| 3098 | */ |
| 3099 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3100 | { |
| 3101 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3102 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3103 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3104 | |
| 3105 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3106 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | /* Just returns the number of bytes available in the input |
| 3110 | * side of the buffer. This function never fails. |
| 3111 | */ |
| 3112 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3113 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3114 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3115 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3117 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3118 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3119 | struct htx *htx = htxbuf(&chn->buf); |
| 3120 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3121 | } |
| 3122 | else |
| 3123 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3124 | return 1; |
| 3125 | } |
| 3126 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3127 | /* Returns true if the channel is full. */ |
| 3128 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3129 | { |
| 3130 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3131 | |
| 3132 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3133 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3134 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3135 | * applet). |
| 3136 | */ |
| 3137 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3138 | return 1; |
| 3139 | } |
| 3140 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3141 | /* Returns true if the channel is the response channel. */ |
| 3142 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3143 | { |
| 3144 | struct channel *chn; |
| 3145 | |
| 3146 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3147 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3148 | |
| 3149 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3150 | return 1; |
| 3151 | } |
| 3152 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3153 | /* Just returns the number of bytes available in the output |
| 3154 | * side of the buffer. This function never fails. |
| 3155 | */ |
| 3156 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3157 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3158 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3159 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3161 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3162 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3163 | return 1; |
| 3164 | } |
| 3165 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3166 | /* |
| 3167 | * |
| 3168 | * |
| 3169 | * Class Fetches |
| 3170 | * |
| 3171 | * |
| 3172 | */ |
| 3173 | |
| 3174 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3175 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3176 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3177 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3178 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3179 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | /* This function creates and push in the stack a fetch object according |
| 3183 | * with a current TXN. |
| 3184 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3185 | 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] | 3186 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3187 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3188 | |
| 3189 | /* Check stack size. */ |
| 3190 | if (!lua_checkstack(L, 3)) |
| 3191 | return 0; |
| 3192 | |
| 3193 | /* Create the object: obj[0] = userdata. |
| 3194 | * Note that the base of the Fetches object is the |
| 3195 | * transaction object. |
| 3196 | */ |
| 3197 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3198 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3199 | lua_rawseti(L, -2, 0); |
| 3200 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3201 | hsmp->s = txn->s; |
| 3202 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3203 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3204 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3205 | |
| 3206 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3207 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3208 | lua_setmetatable(L, -2); |
| 3209 | |
| 3210 | return 1; |
| 3211 | } |
| 3212 | |
| 3213 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3214 | * It uses closure argument to store the associated sample-fetch. It |
| 3215 | * returns only one argument or throws an error. An error is thrown |
| 3216 | * only if an error is encountered during the argument parsing. If |
| 3217 | * the "sample-fetch" function fails, nil is returned. |
| 3218 | */ |
| 3219 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3220 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3221 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3222 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3223 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3224 | int i; |
| 3225 | struct sample smp; |
| 3226 | |
| 3227 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3228 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3229 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3230 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3231 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3232 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3233 | /* Check execution authorization. */ |
| 3234 | if (f->use & SMP_USE_HTTP_ANY && |
| 3235 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3236 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3237 | "is not available in Lua services", f->kw); |
| 3238 | WILL_LJMP(lua_error(L)); |
| 3239 | } |
| 3240 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3241 | /* Get extra arguments. */ |
| 3242 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3243 | if (i >= ARGM_NBARGS) |
| 3244 | break; |
| 3245 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3246 | } |
| 3247 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3248 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3249 | |
| 3250 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3251 | 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] | 3252 | |
| 3253 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3254 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3255 | lua_pushfstring(L, "error in arguments"); |
| 3256 | WILL_LJMP(lua_error(L)); |
| 3257 | } |
| 3258 | |
| 3259 | /* Initialise the sample. */ |
| 3260 | memset(&smp, 0, sizeof(smp)); |
| 3261 | |
| 3262 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3263 | 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] | 3264 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3265 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3266 | lua_pushstring(L, ""); |
| 3267 | else |
| 3268 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3269 | return 1; |
| 3270 | } |
| 3271 | |
| 3272 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3273 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3274 | hlua_smp2lua_str(L, &smp); |
| 3275 | else |
| 3276 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3277 | return 1; |
| 3278 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3279 | |
| 3280 | /* |
| 3281 | * |
| 3282 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3283 | * Class Converters |
| 3284 | * |
| 3285 | * |
| 3286 | */ |
| 3287 | |
| 3288 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3289 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3290 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3291 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3292 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3293 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | /* This function creates and push in the stack a Converters object |
| 3297 | * according with a current TXN. |
| 3298 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3299 | 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] | 3300 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3301 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3302 | |
| 3303 | /* Check stack size. */ |
| 3304 | if (!lua_checkstack(L, 3)) |
| 3305 | return 0; |
| 3306 | |
| 3307 | /* Create the object: obj[0] = userdata. |
| 3308 | * Note that the base of the Converters object is the |
| 3309 | * same than the TXN object. |
| 3310 | */ |
| 3311 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3312 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3313 | lua_rawseti(L, -2, 0); |
| 3314 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3315 | hsmp->s = txn->s; |
| 3316 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3317 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3318 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3319 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3320 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3321 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3322 | lua_setmetatable(L, -2); |
| 3323 | |
| 3324 | return 1; |
| 3325 | } |
| 3326 | |
| 3327 | /* This function is an LUA binding. It is called with each converter. |
| 3328 | * It uses closure argument to store the associated converter. It |
| 3329 | * returns only one argument or throws an error. An error is thrown |
| 3330 | * only if an error is encountered during the argument parsing. If |
| 3331 | * the converter function function fails, nil is returned. |
| 3332 | */ |
| 3333 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3334 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3335 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3336 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3337 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3338 | int i; |
| 3339 | struct sample smp; |
| 3340 | |
| 3341 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3342 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3343 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3344 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3345 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3346 | |
| 3347 | /* Get extra arguments. */ |
| 3348 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3349 | if (i >= ARGM_NBARGS) |
| 3350 | break; |
| 3351 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3352 | } |
| 3353 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3354 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3355 | |
| 3356 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3357 | 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] | 3358 | |
| 3359 | /* Run the special args checker. */ |
| 3360 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3361 | hlua_pusherror(L, "error in arguments"); |
| 3362 | WILL_LJMP(lua_error(L)); |
| 3363 | } |
| 3364 | |
| 3365 | /* Initialise the sample. */ |
| 3366 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3367 | hlua_pusherror(L, "error in the input argument"); |
| 3368 | WILL_LJMP(lua_error(L)); |
| 3369 | } |
| 3370 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3371 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3372 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3373 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3374 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3375 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3376 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3377 | WILL_LJMP(lua_error(L)); |
| 3378 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3379 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3380 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3381 | hlua_pusherror(L, "error during the input argument casting"); |
| 3382 | WILL_LJMP(lua_error(L)); |
| 3383 | } |
| 3384 | |
| 3385 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3386 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3387 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3388 | lua_pushstring(L, ""); |
| 3389 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3390 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3391 | return 1; |
| 3392 | } |
| 3393 | |
| 3394 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3395 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3396 | hlua_smp2lua_str(L, &smp); |
| 3397 | else |
| 3398 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3399 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3400 | } |
| 3401 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3402 | /* |
| 3403 | * |
| 3404 | * |
| 3405 | * Class AppletTCP |
| 3406 | * |
| 3407 | * |
| 3408 | */ |
| 3409 | |
| 3410 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3411 | * a class stream, otherwise it throws an error. |
| 3412 | */ |
| 3413 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3414 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3415 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | /* This function creates and push in the stack an Applet object |
| 3419 | * according with a current TXN. |
| 3420 | */ |
| 3421 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3422 | { |
| 3423 | struct hlua_appctx *appctx; |
| 3424 | struct stream_interface *si = ctx->owner; |
| 3425 | struct stream *s = si_strm(si); |
| 3426 | struct proxy *p = s->be; |
| 3427 | |
| 3428 | /* Check stack size. */ |
| 3429 | if (!lua_checkstack(L, 3)) |
| 3430 | return 0; |
| 3431 | |
| 3432 | /* Create the object: obj[0] = userdata. |
| 3433 | * Note that the base of the Converters object is the |
| 3434 | * same than the TXN object. |
| 3435 | */ |
| 3436 | lua_newtable(L); |
| 3437 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3438 | lua_rawseti(L, -2, 0); |
| 3439 | appctx->appctx = ctx; |
| 3440 | appctx->htxn.s = s; |
| 3441 | appctx->htxn.p = p; |
| 3442 | |
| 3443 | /* Create the "f" field that contains a list of fetches. */ |
| 3444 | lua_pushstring(L, "f"); |
| 3445 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3446 | return 0; |
| 3447 | lua_settable(L, -3); |
| 3448 | |
| 3449 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3450 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3451 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3452 | return 0; |
| 3453 | lua_settable(L, -3); |
| 3454 | |
| 3455 | /* Create the "c" field that contains a list of converters. */ |
| 3456 | lua_pushstring(L, "c"); |
| 3457 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3458 | return 0; |
| 3459 | lua_settable(L, -3); |
| 3460 | |
| 3461 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3462 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3463 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3464 | return 0; |
| 3465 | lua_settable(L, -3); |
| 3466 | |
| 3467 | /* Pop a class stream metatable and affect it to the table. */ |
| 3468 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3469 | lua_setmetatable(L, -2); |
| 3470 | |
| 3471 | return 1; |
| 3472 | } |
| 3473 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3474 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3475 | { |
| 3476 | struct hlua_appctx *appctx; |
| 3477 | struct stream *s; |
| 3478 | const char *name; |
| 3479 | size_t len; |
| 3480 | struct sample smp; |
| 3481 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3482 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3483 | 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] | 3484 | |
| 3485 | /* It is useles to retrieve the stream, but this function |
| 3486 | * runs only in a stream context. |
| 3487 | */ |
| 3488 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3489 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3490 | s = appctx->htxn.s; |
| 3491 | |
| 3492 | /* Converts the third argument in a sample. */ |
| 3493 | hlua_lua2smp(L, 3, &smp); |
| 3494 | |
| 3495 | /* Store the sample in a variable. */ |
| 3496 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3497 | |
| 3498 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3499 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3500 | else |
| 3501 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3502 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3503 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3507 | { |
| 3508 | struct hlua_appctx *appctx; |
| 3509 | struct stream *s; |
| 3510 | const char *name; |
| 3511 | size_t len; |
| 3512 | struct sample smp; |
| 3513 | |
| 3514 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3515 | |
| 3516 | /* It is useles to retrieve the stream, but this function |
| 3517 | * runs only in a stream context. |
| 3518 | */ |
| 3519 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3520 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3521 | s = appctx->htxn.s; |
| 3522 | |
| 3523 | /* Unset the variable. */ |
| 3524 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3525 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3526 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3530 | { |
| 3531 | struct hlua_appctx *appctx; |
| 3532 | struct stream *s; |
| 3533 | const char *name; |
| 3534 | size_t len; |
| 3535 | struct sample smp; |
| 3536 | |
| 3537 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3538 | |
| 3539 | /* It is useles to retrieve the stream, but this function |
| 3540 | * runs only in a stream context. |
| 3541 | */ |
| 3542 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3543 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3544 | s = appctx->htxn.s; |
| 3545 | |
| 3546 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3547 | if (!vars_get_by_name(name, len, &smp)) { |
| 3548 | lua_pushnil(L); |
| 3549 | return 1; |
| 3550 | } |
| 3551 | |
| 3552 | return hlua_smp2lua(L, &smp); |
| 3553 | } |
| 3554 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3555 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3556 | { |
| 3557 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3558 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3559 | struct hlua *hlua; |
| 3560 | |
| 3561 | /* 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] | 3562 | if (!s->hlua) |
| 3563 | return 0; |
| 3564 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3565 | |
| 3566 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3567 | |
| 3568 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3569 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3570 | |
| 3571 | /* Get and store new value. */ |
| 3572 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3573 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3574 | |
| 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3579 | { |
| 3580 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3581 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3582 | struct hlua *hlua; |
| 3583 | |
| 3584 | /* 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] | 3585 | if (!s->hlua) { |
| 3586 | lua_pushnil(L); |
| 3587 | return 1; |
| 3588 | } |
| 3589 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3590 | |
| 3591 | /* Push configuration index in the stack. */ |
| 3592 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3593 | |
| 3594 | return 1; |
| 3595 | } |
| 3596 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3597 | /* If expected data not yet available, it returns a yield. This function |
| 3598 | * consumes the data in the buffer. It returns a string containing the |
| 3599 | * data. This string can be empty. |
| 3600 | */ |
| 3601 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3602 | { |
| 3603 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3604 | struct stream_interface *si = appctx->appctx->owner; |
| 3605 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3606 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3607 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3608 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3609 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3610 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3611 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3612 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3613 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3614 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3615 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3616 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3617 | 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] | 3618 | } |
| 3619 | |
| 3620 | /* End of data: commit the total strings and return. */ |
| 3621 | if (ret < 0) { |
| 3622 | luaL_pushresult(&appctx->b); |
| 3623 | return 1; |
| 3624 | } |
| 3625 | |
| 3626 | /* Ensure that the block 2 length is usable. */ |
| 3627 | if (ret == 1) |
| 3628 | len2 = 0; |
| 3629 | |
| 3630 | /* dont check the max length read and dont check. */ |
| 3631 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3632 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3633 | |
| 3634 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3635 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3636 | luaL_pushresult(&appctx->b); |
| 3637 | return 1; |
| 3638 | } |
| 3639 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3640 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3641 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3642 | { |
| 3643 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3644 | |
| 3645 | /* Initialise the string catenation. */ |
| 3646 | luaL_buffinit(L, &appctx->b); |
| 3647 | |
| 3648 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3649 | } |
| 3650 | |
| 3651 | /* If expected data not yet available, it returns a yield. This function |
| 3652 | * consumes the data in the buffer. It returns a string containing the |
| 3653 | * data. This string can be empty. |
| 3654 | */ |
| 3655 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3656 | { |
| 3657 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3658 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3659 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3660 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3661 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3662 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3663 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3664 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3665 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3666 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3667 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3668 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3669 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3670 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3671 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3672 | 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] | 3673 | } |
| 3674 | |
| 3675 | /* End of data: commit the total strings and return. */ |
| 3676 | if (ret < 0) { |
| 3677 | luaL_pushresult(&appctx->b); |
| 3678 | return 1; |
| 3679 | } |
| 3680 | |
| 3681 | /* Ensure that the block 2 length is usable. */ |
| 3682 | if (ret == 1) |
| 3683 | len2 = 0; |
| 3684 | |
| 3685 | if (len == -1) { |
| 3686 | |
| 3687 | /* If len == -1, catenate all the data avalaile and |
| 3688 | * yield because we want to get all the data until |
| 3689 | * the end of data stream. |
| 3690 | */ |
| 3691 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3692 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3693 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3694 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3695 | 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] | 3696 | |
| 3697 | } else { |
| 3698 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3699 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3700 | if (len1 > len) |
| 3701 | len1 = len; |
| 3702 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3703 | len -= len1; |
| 3704 | |
| 3705 | /* Copy the second block. */ |
| 3706 | if (len2 > len) |
| 3707 | len2 = len; |
| 3708 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3709 | len -= len2; |
| 3710 | |
| 3711 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3712 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3713 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3714 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3715 | if (len > 0) { |
| 3716 | lua_pushinteger(L, len); |
| 3717 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3718 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3719 | 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] | 3720 | } |
| 3721 | |
| 3722 | /* return the result. */ |
| 3723 | luaL_pushresult(&appctx->b); |
| 3724 | return 1; |
| 3725 | } |
| 3726 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3727 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3728 | hlua_pusherror(L, "Lua: internal error"); |
| 3729 | WILL_LJMP(lua_error(L)); |
| 3730 | return 0; |
| 3731 | } |
| 3732 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3733 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3734 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3735 | { |
| 3736 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3737 | int len = -1; |
| 3738 | |
| 3739 | if (lua_gettop(L) > 2) |
| 3740 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3741 | if (lua_gettop(L) >= 2) { |
| 3742 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3743 | lua_pop(L, 1); |
| 3744 | } |
| 3745 | |
| 3746 | /* Confirm or set the required length */ |
| 3747 | lua_pushinteger(L, len); |
| 3748 | |
| 3749 | /* Initialise the string catenation. */ |
| 3750 | luaL_buffinit(L, &appctx->b); |
| 3751 | |
| 3752 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3753 | } |
| 3754 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3755 | /* Append data in the output side of the buffer. This data is immediately |
| 3756 | * sent. The function returns the amount of data written. If the buffer |
| 3757 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3758 | * if the channel is closed. |
| 3759 | */ |
| 3760 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3761 | { |
| 3762 | size_t len; |
| 3763 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3764 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3765 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3766 | struct stream_interface *si = appctx->appctx->owner; |
| 3767 | struct channel *chn = si_ic(si); |
| 3768 | int max; |
| 3769 | |
| 3770 | /* Get the max amount of data which can write as input in the channel. */ |
| 3771 | max = channel_recv_max(chn); |
| 3772 | if (max > (len - l)) |
| 3773 | max = len - l; |
| 3774 | |
| 3775 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3776 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3777 | |
| 3778 | /* update counters. */ |
| 3779 | l += max; |
| 3780 | lua_pop(L, 1); |
| 3781 | lua_pushinteger(L, l); |
| 3782 | |
| 3783 | /* If some data is not send, declares the situation to the |
| 3784 | * applet, and returns a yield. |
| 3785 | */ |
| 3786 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3787 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3788 | 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] | 3789 | } |
| 3790 | |
| 3791 | return 1; |
| 3792 | } |
| 3793 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3794 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3795 | * yield the LUA process, and resume it without checking the |
| 3796 | * input arguments. |
| 3797 | */ |
| 3798 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3799 | { |
| 3800 | MAY_LJMP(check_args(L, 2, "send")); |
| 3801 | lua_pushinteger(L, 0); |
| 3802 | |
| 3803 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3804 | } |
| 3805 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3806 | /* |
| 3807 | * |
| 3808 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3809 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3810 | * |
| 3811 | * |
| 3812 | */ |
| 3813 | |
| 3814 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3815 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3816 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3817 | __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] | 3818 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3819 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3820 | } |
| 3821 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3822 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3823 | * according with a current TXN. |
| 3824 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3825 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3826 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3827 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3828 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3829 | struct stream_interface *si = ctx->owner; |
| 3830 | struct stream *s = si_strm(si); |
| 3831 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3832 | struct htx *htx; |
| 3833 | struct htx_blk *blk; |
| 3834 | struct htx_sl *sl; |
| 3835 | struct ist path; |
| 3836 | unsigned long long len = 0; |
| 3837 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3838 | |
| 3839 | /* Check stack size. */ |
| 3840 | if (!lua_checkstack(L, 3)) |
| 3841 | return 0; |
| 3842 | |
| 3843 | /* Create the object: obj[0] = userdata. |
| 3844 | * Note that the base of the Converters object is the |
| 3845 | * same than the TXN object. |
| 3846 | */ |
| 3847 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3848 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3849 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3850 | appctx->appctx = ctx; |
| 3851 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3852 | 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] | 3853 | appctx->htxn.s = s; |
| 3854 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3855 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3856 | /* Create the "f" field that contains a list of fetches. */ |
| 3857 | lua_pushstring(L, "f"); |
| 3858 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3859 | return 0; |
| 3860 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3861 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3862 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3863 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3864 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3865 | return 0; |
| 3866 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3867 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3868 | /* Create the "c" field that contains a list of converters. */ |
| 3869 | lua_pushstring(L, "c"); |
| 3870 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3871 | return 0; |
| 3872 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3873 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3874 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3875 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3876 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3877 | return 0; |
| 3878 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3879 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3880 | htx = htxbuf(&s->req.buf); |
| 3881 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3882 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3883 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3884 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3885 | /* Stores the request method. */ |
| 3886 | lua_pushstring(L, "method"); |
| 3887 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3888 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3889 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3890 | /* Stores the http version. */ |
| 3891 | lua_pushstring(L, "version"); |
| 3892 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3893 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3894 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3895 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3896 | * the array on the top of the stack. |
| 3897 | */ |
| 3898 | lua_pushstring(L, "headers"); |
| 3899 | htxn.s = s; |
| 3900 | htxn.p = px; |
| 3901 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 3902 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3903 | return 0; |
| 3904 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3905 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3906 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3907 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3908 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3909 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3910 | p = path.ptr; |
| 3911 | end = path.ptr + path.len; |
| 3912 | q = p; |
| 3913 | while (q < end && *q != '?') |
| 3914 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3915 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3916 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3917 | lua_pushstring(L, "path"); |
| 3918 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3919 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3920 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3921 | /* Stores the query string. */ |
| 3922 | lua_pushstring(L, "qs"); |
| 3923 | if (*q == '?') |
| 3924 | q++; |
| 3925 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3926 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3927 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3928 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3929 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3930 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3931 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3932 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3933 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3934 | break; |
| 3935 | if (type == HTX_BLK_DATA) |
| 3936 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3937 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3938 | if (htx->extra != ULLONG_MAX) |
| 3939 | len += htx->extra; |
| 3940 | |
| 3941 | /* Stores the request path. */ |
| 3942 | lua_pushstring(L, "length"); |
| 3943 | lua_pushinteger(L, len); |
| 3944 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3945 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3946 | /* Create an empty array of HTTP request headers. */ |
| 3947 | lua_pushstring(L, "response"); |
| 3948 | lua_newtable(L); |
| 3949 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3950 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3951 | /* Pop a class stream metatable and affect it to the table. */ |
| 3952 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3953 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3954 | |
| 3955 | return 1; |
| 3956 | } |
| 3957 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3958 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3959 | { |
| 3960 | struct hlua_appctx *appctx; |
| 3961 | struct stream *s; |
| 3962 | const char *name; |
| 3963 | size_t len; |
| 3964 | struct sample smp; |
| 3965 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3966 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3967 | 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] | 3968 | |
| 3969 | /* It is useles to retrieve the stream, but this function |
| 3970 | * runs only in a stream context. |
| 3971 | */ |
| 3972 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3973 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3974 | s = appctx->htxn.s; |
| 3975 | |
| 3976 | /* Converts the third argument in a sample. */ |
| 3977 | hlua_lua2smp(L, 3, &smp); |
| 3978 | |
| 3979 | /* Store the sample in a variable. */ |
| 3980 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3981 | |
| 3982 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3983 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3984 | else |
| 3985 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3986 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3987 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3991 | { |
| 3992 | struct hlua_appctx *appctx; |
| 3993 | struct stream *s; |
| 3994 | const char *name; |
| 3995 | size_t len; |
| 3996 | struct sample smp; |
| 3997 | |
| 3998 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3999 | |
| 4000 | /* It is useles to retrieve the stream, but this function |
| 4001 | * runs only in a stream context. |
| 4002 | */ |
| 4003 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4004 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4005 | s = appctx->htxn.s; |
| 4006 | |
| 4007 | /* Unset the variable. */ |
| 4008 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4009 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4010 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4011 | } |
| 4012 | |
| 4013 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4014 | { |
| 4015 | struct hlua_appctx *appctx; |
| 4016 | struct stream *s; |
| 4017 | const char *name; |
| 4018 | size_t len; |
| 4019 | struct sample smp; |
| 4020 | |
| 4021 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4022 | |
| 4023 | /* It is useles to retrieve the stream, but this function |
| 4024 | * runs only in a stream context. |
| 4025 | */ |
| 4026 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4027 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4028 | s = appctx->htxn.s; |
| 4029 | |
| 4030 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4031 | if (!vars_get_by_name(name, len, &smp)) { |
| 4032 | lua_pushnil(L); |
| 4033 | return 1; |
| 4034 | } |
| 4035 | |
| 4036 | return hlua_smp2lua(L, &smp); |
| 4037 | } |
| 4038 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4039 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4040 | { |
| 4041 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4042 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4043 | struct hlua *hlua; |
| 4044 | |
| 4045 | /* 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] | 4046 | if (!s->hlua) |
| 4047 | return 0; |
| 4048 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4049 | |
| 4050 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4051 | |
| 4052 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4053 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4054 | |
| 4055 | /* Get and store new value. */ |
| 4056 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4057 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4058 | |
| 4059 | return 0; |
| 4060 | } |
| 4061 | |
| 4062 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4063 | { |
| 4064 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4065 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4066 | struct hlua *hlua; |
| 4067 | |
| 4068 | /* 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] | 4069 | if (!s->hlua) { |
| 4070 | lua_pushnil(L); |
| 4071 | return 1; |
| 4072 | } |
| 4073 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4074 | |
| 4075 | /* Push configuration index in the stack. */ |
| 4076 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4077 | |
| 4078 | return 1; |
| 4079 | } |
| 4080 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4081 | /* If expected data not yet available, it returns a yield. This function |
| 4082 | * consumes the data in the buffer. It returns a string containing the |
| 4083 | * data. This string can be empty. |
| 4084 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4085 | __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] | 4086 | { |
| 4087 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4088 | struct stream_interface *si = appctx->appctx->owner; |
| 4089 | struct channel *req = si_oc(si); |
| 4090 | struct htx *htx; |
| 4091 | struct htx_blk *blk; |
| 4092 | size_t count; |
| 4093 | int stop = 0; |
| 4094 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4095 | htx = htx_from_buf(&req->buf); |
| 4096 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4097 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4098 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4099 | while (count && !stop && blk) { |
| 4100 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4101 | uint32_t sz = htx_get_blksz(blk); |
| 4102 | struct ist v; |
| 4103 | uint32_t vlen; |
| 4104 | char *nl; |
| 4105 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4106 | if (type == HTX_BLK_EOM) { |
| 4107 | stop = 1; |
| 4108 | break; |
| 4109 | } |
| 4110 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4111 | vlen = sz; |
| 4112 | if (vlen > count) { |
| 4113 | if (type != HTX_BLK_DATA) |
| 4114 | break; |
| 4115 | vlen = count; |
| 4116 | } |
| 4117 | |
| 4118 | switch (type) { |
| 4119 | case HTX_BLK_UNUSED: |
| 4120 | break; |
| 4121 | |
| 4122 | case HTX_BLK_DATA: |
| 4123 | v = htx_get_blk_value(htx, blk); |
| 4124 | v.len = vlen; |
| 4125 | nl = istchr(v, '\n'); |
| 4126 | if (nl != NULL) { |
| 4127 | stop = 1; |
| 4128 | vlen = nl - v.ptr + 1; |
| 4129 | } |
| 4130 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4131 | break; |
| 4132 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4133 | case HTX_BLK_TLR: |
| 4134 | case HTX_BLK_EOM: |
| 4135 | stop = 1; |
| 4136 | break; |
| 4137 | |
| 4138 | default: |
| 4139 | break; |
| 4140 | } |
| 4141 | |
| 4142 | co_set_data(req, co_data(req) - vlen); |
| 4143 | count -= vlen; |
| 4144 | if (sz == vlen) |
| 4145 | blk = htx_remove_blk(htx, blk); |
| 4146 | else { |
| 4147 | htx_cut_data_blk(htx, blk, vlen); |
| 4148 | break; |
| 4149 | } |
| 4150 | } |
| 4151 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4152 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4153 | if (!stop) { |
| 4154 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4155 | 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] | 4156 | } |
| 4157 | |
| 4158 | /* return the result. */ |
| 4159 | luaL_pushresult(&appctx->b); |
| 4160 | return 1; |
| 4161 | } |
| 4162 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4163 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4164 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4165 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4166 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4167 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4168 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4169 | /* Initialise the string catenation. */ |
| 4170 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4171 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4172 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4173 | } |
| 4174 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4175 | /* If expected data not yet available, it returns a yield. This function |
| 4176 | * consumes the data in the buffer. It returns a string containing the |
| 4177 | * data. This string can be empty. |
| 4178 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4179 | __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] | 4180 | { |
| 4181 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4182 | struct stream_interface *si = appctx->appctx->owner; |
| 4183 | struct channel *req = si_oc(si); |
| 4184 | struct htx *htx; |
| 4185 | struct htx_blk *blk; |
| 4186 | size_t count; |
| 4187 | int len; |
| 4188 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4189 | htx = htx_from_buf(&req->buf); |
| 4190 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4191 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4192 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4193 | while (count && len && blk) { |
| 4194 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4195 | uint32_t sz = htx_get_blksz(blk); |
| 4196 | struct ist v; |
| 4197 | uint32_t vlen; |
| 4198 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4199 | if (type == HTX_BLK_EOM) { |
| 4200 | len = 0; |
| 4201 | break; |
| 4202 | } |
| 4203 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4204 | vlen = sz; |
| 4205 | if (len > 0 && vlen > len) |
| 4206 | vlen = len; |
| 4207 | if (vlen > count) { |
| 4208 | if (type != HTX_BLK_DATA) |
| 4209 | break; |
| 4210 | vlen = count; |
| 4211 | } |
| 4212 | |
| 4213 | switch (type) { |
| 4214 | case HTX_BLK_UNUSED: |
| 4215 | break; |
| 4216 | |
| 4217 | case HTX_BLK_DATA: |
| 4218 | v = htx_get_blk_value(htx, blk); |
| 4219 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4220 | break; |
| 4221 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4222 | case HTX_BLK_TLR: |
| 4223 | case HTX_BLK_EOM: |
| 4224 | len = 0; |
| 4225 | break; |
| 4226 | |
| 4227 | default: |
| 4228 | break; |
| 4229 | } |
| 4230 | |
| 4231 | co_set_data(req, co_data(req) - vlen); |
| 4232 | count -= vlen; |
| 4233 | if (len > 0) |
| 4234 | len -= vlen; |
| 4235 | if (sz == vlen) |
| 4236 | blk = htx_remove_blk(htx, blk); |
| 4237 | else { |
| 4238 | htx_cut_data_blk(htx, blk, vlen); |
| 4239 | break; |
| 4240 | } |
| 4241 | } |
| 4242 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4243 | htx_to_buf(htx, &req->buf); |
| 4244 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4245 | /* If we are no other data available, yield waiting for new data. */ |
| 4246 | if (len) { |
| 4247 | if (len > 0) { |
| 4248 | lua_pushinteger(L, len); |
| 4249 | lua_replace(L, 2); |
| 4250 | } |
| 4251 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4252 | 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] | 4253 | } |
| 4254 | |
| 4255 | /* return the result. */ |
| 4256 | luaL_pushresult(&appctx->b); |
| 4257 | return 1; |
| 4258 | } |
| 4259 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4260 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4261 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4262 | { |
| 4263 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4264 | int len = -1; |
| 4265 | |
| 4266 | /* Check arguments. */ |
| 4267 | if (lua_gettop(L) > 2) |
| 4268 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4269 | if (lua_gettop(L) >= 2) { |
| 4270 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4271 | lua_pop(L, 1); |
| 4272 | } |
| 4273 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4274 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4275 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4276 | /* Initialise the string catenation. */ |
| 4277 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4278 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4279 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4280 | } |
| 4281 | |
| 4282 | /* Append data in the output side of the buffer. This data is immediately |
| 4283 | * sent. The function returns the amount of data written. If the buffer |
| 4284 | * cannot contain the data, the function yields. The function returns -1 |
| 4285 | * if the channel is closed. |
| 4286 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4287 | __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] | 4288 | { |
| 4289 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4290 | struct stream_interface *si = appctx->appctx->owner; |
| 4291 | struct channel *res = si_ic(si); |
| 4292 | struct htx *htx = htx_from_buf(&res->buf); |
| 4293 | const char *data; |
| 4294 | size_t len; |
| 4295 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4296 | int max; |
| 4297 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4298 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4299 | if (!max) |
| 4300 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4301 | |
| 4302 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4303 | |
| 4304 | /* Get the max amount of data which can write as input in the channel. */ |
| 4305 | if (max > (len - l)) |
| 4306 | max = len - l; |
| 4307 | |
| 4308 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4309 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4310 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4311 | |
| 4312 | /* update counters. */ |
| 4313 | l += max; |
| 4314 | lua_pop(L, 1); |
| 4315 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4316 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4317 | /* If some data is not send, declares the situation to the |
| 4318 | * applet, and returns a yield. |
| 4319 | */ |
| 4320 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4321 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4322 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4323 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4324 | 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] | 4325 | } |
| 4326 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4327 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4328 | return 1; |
| 4329 | } |
| 4330 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4331 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4332 | * yield the LUA process, and resume it without checking the |
| 4333 | * input arguments. |
| 4334 | */ |
| 4335 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4336 | { |
| 4337 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4338 | |
| 4339 | /* We want to send some data. Headers must be sent. */ |
| 4340 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4341 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4342 | WILL_LJMP(lua_error(L)); |
| 4343 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4344 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4345 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4346 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4347 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4348 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4352 | { |
| 4353 | const char *name; |
| 4354 | int ret; |
| 4355 | |
| 4356 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4357 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4358 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4359 | |
| 4360 | /* Push in the stack the "response" entry. */ |
| 4361 | ret = lua_getfield(L, 1, "response"); |
| 4362 | if (ret != LUA_TTABLE) { |
| 4363 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4364 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4365 | WILL_LJMP(lua_error(L)); |
| 4366 | } |
| 4367 | |
| 4368 | /* check if the header is already registered if it is not |
| 4369 | * the case, register it. |
| 4370 | */ |
| 4371 | ret = lua_getfield(L, -1, name); |
| 4372 | if (ret == LUA_TNIL) { |
| 4373 | |
| 4374 | /* Entry not found. */ |
| 4375 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4376 | |
| 4377 | /* Insert the new header name in the array in the top of the stack. |
| 4378 | * It left the new array in the top of the stack. |
| 4379 | */ |
| 4380 | lua_newtable(L); |
| 4381 | lua_pushvalue(L, 2); |
| 4382 | lua_pushvalue(L, -2); |
| 4383 | lua_settable(L, -4); |
| 4384 | |
| 4385 | } else if (ret != LUA_TTABLE) { |
| 4386 | |
| 4387 | /* corruption error. */ |
| 4388 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4389 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4390 | WILL_LJMP(lua_error(L)); |
| 4391 | } |
| 4392 | |
| 4393 | /* Now the top od thestack is an array of values. We push |
| 4394 | * the header value as new entry. |
| 4395 | */ |
| 4396 | lua_pushvalue(L, 3); |
| 4397 | ret = lua_rawlen(L, -2); |
| 4398 | lua_rawseti(L, -2, ret + 1); |
| 4399 | lua_pushboolean(L, 1); |
| 4400 | return 1; |
| 4401 | } |
| 4402 | |
| 4403 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4404 | { |
| 4405 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4406 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4407 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4408 | |
| 4409 | if (status < 100 || status > 599) { |
| 4410 | lua_pushboolean(L, 0); |
| 4411 | return 1; |
| 4412 | } |
| 4413 | |
| 4414 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4415 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4416 | lua_pushboolean(L, 1); |
| 4417 | return 1; |
| 4418 | } |
| 4419 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4420 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4421 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4422 | { |
| 4423 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4424 | struct stream_interface *si = appctx->appctx->owner; |
| 4425 | struct channel *res = si_ic(si); |
| 4426 | struct htx *htx; |
| 4427 | struct htx_sl *sl; |
| 4428 | struct h1m h1m; |
| 4429 | const char *status, *reason; |
| 4430 | const char *name, *value; |
| 4431 | size_t nlen, vlen; |
| 4432 | unsigned int flags; |
| 4433 | |
| 4434 | /* Send the message at once. */ |
| 4435 | htx = htx_from_buf(&res->buf); |
| 4436 | h1m_init_res(&h1m); |
| 4437 | |
| 4438 | /* Use the same http version than the request. */ |
| 4439 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4440 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4441 | if (reason == NULL) |
| 4442 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4443 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4444 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4445 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4446 | } |
| 4447 | else { |
| 4448 | flags = HTX_SL_F_IS_RESP; |
| 4449 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4450 | } |
| 4451 | if (!sl) { |
| 4452 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4453 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4454 | WILL_LJMP(lua_error(L)); |
| 4455 | } |
| 4456 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4457 | |
| 4458 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4459 | lua_pushvalue(L, 0); |
| 4460 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4461 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4462 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4463 | WILL_LJMP(lua_error(L)); |
| 4464 | } |
| 4465 | |
| 4466 | /* Browse the list of headers. */ |
| 4467 | lua_pushnil(L); |
| 4468 | while(lua_next(L, -2) != 0) { |
| 4469 | /* We expect a string as -2. */ |
| 4470 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4471 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4472 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4473 | lua_typename(L, lua_type(L, -2))); |
| 4474 | WILL_LJMP(lua_error(L)); |
| 4475 | } |
| 4476 | name = lua_tolstring(L, -2, &nlen); |
| 4477 | |
| 4478 | /* We expect an array as -1. */ |
| 4479 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4480 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4481 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4482 | name, |
| 4483 | lua_typename(L, lua_type(L, -1))); |
| 4484 | WILL_LJMP(lua_error(L)); |
| 4485 | } |
| 4486 | |
| 4487 | /* Browse the table who is on the top of the stack. */ |
| 4488 | lua_pushnil(L); |
| 4489 | while(lua_next(L, -2) != 0) { |
| 4490 | int id; |
| 4491 | |
| 4492 | /* We expect a number as -2. */ |
| 4493 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4494 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4495 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4496 | name, |
| 4497 | lua_typename(L, lua_type(L, -2))); |
| 4498 | WILL_LJMP(lua_error(L)); |
| 4499 | } |
| 4500 | id = lua_tointeger(L, -2); |
| 4501 | |
| 4502 | /* We expect a string as -2. */ |
| 4503 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4504 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4505 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4506 | name, id, |
| 4507 | lua_typename(L, lua_type(L, -1))); |
| 4508 | WILL_LJMP(lua_error(L)); |
| 4509 | } |
| 4510 | value = lua_tolstring(L, -1, &vlen); |
| 4511 | |
| 4512 | /* Simple Protocol checks. */ |
| 4513 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4514 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4515 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4516 | struct ist v = ist2(value, vlen); |
| 4517 | int ret; |
| 4518 | |
| 4519 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4520 | if (ret < 0) { |
| 4521 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4522 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4523 | name); |
| 4524 | WILL_LJMP(lua_error(L)); |
| 4525 | } |
| 4526 | else if (ret == 0) |
| 4527 | goto next; /* Skip it */ |
| 4528 | } |
| 4529 | |
| 4530 | /* Add a new header */ |
| 4531 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4532 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4533 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4534 | name); |
| 4535 | WILL_LJMP(lua_error(L)); |
| 4536 | } |
| 4537 | next: |
| 4538 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4539 | lua_pop(L, 1); |
| 4540 | } |
| 4541 | |
| 4542 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4543 | lua_pop(L, 1); |
| 4544 | } |
| 4545 | |
| 4546 | if (h1m.flags & H1_MF_CHNK) |
| 4547 | h1m.flags &= ~H1_MF_CLEN; |
| 4548 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4549 | h1m.flags |= H1_MF_XFER_LEN; |
| 4550 | |
| 4551 | /* Uset HTX start-line flags */ |
| 4552 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4553 | flags |= HTX_SL_F_XFER_ENC; |
| 4554 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4555 | flags |= HTX_SL_F_XFER_LEN; |
| 4556 | if (h1m.flags & H1_MF_CHNK) |
| 4557 | flags |= HTX_SL_F_CHNK; |
| 4558 | else if (h1m.flags & H1_MF_CLEN) |
| 4559 | flags |= HTX_SL_F_CLEN; |
| 4560 | if (h1m.body_len == 0) |
| 4561 | flags |= HTX_SL_F_BODYLESS; |
| 4562 | } |
| 4563 | sl->flags |= flags; |
| 4564 | |
| 4565 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4566 | * and the status code implies the presence of a message body, we must |
| 4567 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4568 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4569 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4570 | */ |
| 4571 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4572 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4573 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4574 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4575 | /* Add a new header */ |
| 4576 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4577 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4578 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4579 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4580 | WILL_LJMP(lua_error(L)); |
| 4581 | } |
| 4582 | } |
| 4583 | |
| 4584 | /* Finalize headers. */ |
| 4585 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4586 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4587 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4588 | WILL_LJMP(lua_error(L)); |
| 4589 | } |
| 4590 | |
| 4591 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4592 | b_reset(&res->buf); |
| 4593 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4594 | WILL_LJMP(lua_error(L)); |
| 4595 | } |
| 4596 | |
| 4597 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4598 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4599 | |
| 4600 | /* Headers sent, set the flag. */ |
| 4601 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4602 | return 0; |
| 4603 | |
| 4604 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4605 | /* We will build the status line and the headers of the HTTP response. |
| 4606 | * We will try send at once if its not possible, we give back the hand |
| 4607 | * waiting for more room. |
| 4608 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4609 | __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] | 4610 | { |
| 4611 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4612 | struct stream_interface *si = appctx->appctx->owner; |
| 4613 | struct channel *res = si_ic(si); |
| 4614 | |
| 4615 | if (co_data(res)) { |
| 4616 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4617 | 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] | 4618 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4619 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4620 | } |
| 4621 | |
| 4622 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4623 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4624 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4625 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4626 | } |
| 4627 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4628 | /* |
| 4629 | * |
| 4630 | * |
| 4631 | * Class HTTP |
| 4632 | * |
| 4633 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4634 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4635 | |
| 4636 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4637 | * a class stream, otherwise it throws an error. |
| 4638 | */ |
| 4639 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4640 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4641 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4642 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4643 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4644 | /* This function creates and push in the stack a HTTP object |
| 4645 | * according with a current TXN. |
| 4646 | */ |
| 4647 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4648 | { |
| 4649 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4650 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4651 | /* Check stack size. */ |
| 4652 | if (!lua_checkstack(L, 3)) |
| 4653 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4654 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4655 | /* Create the object: obj[0] = userdata. |
| 4656 | * Note that the base of the Converters object is the |
| 4657 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4658 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4659 | lua_newtable(L); |
| 4660 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4661 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4662 | |
| 4663 | htxn->s = txn->s; |
| 4664 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4665 | htxn->dir = txn->dir; |
| 4666 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4667 | |
| 4668 | /* Pop a class stream metatable and affect it to the table. */ |
| 4669 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4670 | lua_setmetatable(L, -2); |
| 4671 | |
| 4672 | return 1; |
| 4673 | } |
| 4674 | |
| 4675 | /* This function creates ans returns an array of HTTP headers. |
| 4676 | * This function does not fails. It is used as wrapper with the |
| 4677 | * 2 following functions. |
| 4678 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4679 | __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] | 4680 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4681 | struct htx *htx; |
| 4682 | int32_t pos; |
| 4683 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4684 | /* Create the table. */ |
| 4685 | lua_newtable(L); |
| 4686 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4687 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4688 | htx = htxbuf(&msg->chn->buf); |
| 4689 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4690 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4691 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4692 | struct ist n, v; |
| 4693 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4694 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4695 | if (type == HTX_BLK_HDR) { |
| 4696 | n = htx_get_blk_name(htx,blk); |
| 4697 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4698 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4699 | else if (type == HTX_BLK_EOH) |
| 4700 | break; |
| 4701 | else |
| 4702 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4703 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4704 | /* Check for existing entry: |
| 4705 | * assume that the table is on the top of the stack, and |
| 4706 | * push the key in the stack, the function lua_gettable() |
| 4707 | * perform the lookup. |
| 4708 | */ |
| 4709 | lua_pushlstring(L, n.ptr, n.len); |
| 4710 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4711 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4712 | switch (lua_type(L, -1)) { |
| 4713 | case LUA_TNIL: |
| 4714 | /* Table not found, create it. */ |
| 4715 | lua_pop(L, 1); /* remove the nil value. */ |
| 4716 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4717 | lua_newtable(L); /* create and push empty table. */ |
| 4718 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4719 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4720 | 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] | 4721 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4722 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4723 | case LUA_TTABLE: |
| 4724 | /* Entry found: push the value in the table. */ |
| 4725 | len = lua_rawlen(L, -1); |
| 4726 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4727 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4728 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4729 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4730 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4731 | default: |
| 4732 | /* Other cases are errors. */ |
| 4733 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4734 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4735 | } |
| 4736 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4737 | return 1; |
| 4738 | } |
| 4739 | |
| 4740 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4741 | { |
| 4742 | struct hlua_txn *htxn; |
| 4743 | |
| 4744 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4745 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4746 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4747 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4748 | WILL_LJMP(lua_error(L)); |
| 4749 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4750 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4754 | { |
| 4755 | struct hlua_txn *htxn; |
| 4756 | |
| 4757 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4758 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4759 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4760 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4761 | WILL_LJMP(lua_error(L)); |
| 4762 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4763 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | /* This function replace full header, or just a value in |
| 4767 | * the request or in the response. It is a wrapper fir the |
| 4768 | * 4 following functions. |
| 4769 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4770 | __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] | 4771 | { |
| 4772 | size_t name_len; |
| 4773 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4774 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4775 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4776 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4777 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4779 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4780 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4781 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4782 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4783 | 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] | 4784 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4785 | return 0; |
| 4786 | } |
| 4787 | |
| 4788 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4789 | { |
| 4790 | struct hlua_txn *htxn; |
| 4791 | |
| 4792 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4793 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4794 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4795 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4796 | WILL_LJMP(lua_error(L)); |
| 4797 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4798 | 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] | 4799 | } |
| 4800 | |
| 4801 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4802 | { |
| 4803 | struct hlua_txn *htxn; |
| 4804 | |
| 4805 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4806 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4807 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4808 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4809 | WILL_LJMP(lua_error(L)); |
| 4810 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4811 | 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] | 4812 | } |
| 4813 | |
| 4814 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4815 | { |
| 4816 | struct hlua_txn *htxn; |
| 4817 | |
| 4818 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4819 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4820 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4821 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4822 | WILL_LJMP(lua_error(L)); |
| 4823 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4824 | 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] | 4825 | } |
| 4826 | |
| 4827 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4828 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4829 | struct hlua_txn *htxn; |
| 4830 | |
| 4831 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4832 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4833 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4834 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4835 | WILL_LJMP(lua_error(L)); |
| 4836 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4837 | 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] | 4838 | } |
| 4839 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4840 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4841 | * It is a wrapper for the 2 following functions. |
| 4842 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4843 | __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] | 4844 | { |
| 4845 | size_t len; |
| 4846 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4847 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4848 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4849 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4850 | ctx.blk = NULL; |
| 4851 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4852 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4853 | return 0; |
| 4854 | } |
| 4855 | |
| 4856 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4857 | { |
| 4858 | struct hlua_txn *htxn; |
| 4859 | |
| 4860 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4861 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4862 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4863 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4864 | WILL_LJMP(lua_error(L)); |
| 4865 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4866 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4867 | } |
| 4868 | |
| 4869 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4870 | { |
| 4871 | struct hlua_txn *htxn; |
| 4872 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4873 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4874 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4875 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4876 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4877 | WILL_LJMP(lua_error(L)); |
| 4878 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4879 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | /* This function adds an header. It is a wrapper used by |
| 4883 | * the 2 following functions. |
| 4884 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4885 | __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] | 4886 | { |
| 4887 | size_t name_len; |
| 4888 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4889 | size_t value_len; |
| 4890 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4891 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4892 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4893 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4894 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4895 | return 0; |
| 4896 | } |
| 4897 | |
| 4898 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4899 | { |
| 4900 | struct hlua_txn *htxn; |
| 4901 | |
| 4902 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4903 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4904 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4905 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4906 | WILL_LJMP(lua_error(L)); |
| 4907 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4908 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4912 | { |
| 4913 | struct hlua_txn *htxn; |
| 4914 | |
| 4915 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4916 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4917 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4918 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4919 | WILL_LJMP(lua_error(L)); |
| 4920 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4921 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4925 | { |
| 4926 | struct hlua_txn *htxn; |
| 4927 | |
| 4928 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4929 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4930 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4931 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4932 | WILL_LJMP(lua_error(L)); |
| 4933 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4934 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 4935 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4939 | { |
| 4940 | struct hlua_txn *htxn; |
| 4941 | |
| 4942 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4943 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4944 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4945 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4946 | WILL_LJMP(lua_error(L)); |
| 4947 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4948 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 4949 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | /* This function set the method. */ |
| 4953 | static int hlua_http_req_set_meth(lua_State *L) |
| 4954 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4955 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4956 | size_t name_len; |
| 4957 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4958 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4959 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4960 | WILL_LJMP(lua_error(L)); |
| 4961 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4962 | 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] | 4963 | return 1; |
| 4964 | } |
| 4965 | |
| 4966 | /* This function set the method. */ |
| 4967 | static int hlua_http_req_set_path(lua_State *L) |
| 4968 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4969 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4970 | size_t name_len; |
| 4971 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4972 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4973 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4974 | WILL_LJMP(lua_error(L)); |
| 4975 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4976 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4977 | return 1; |
| 4978 | } |
| 4979 | |
| 4980 | /* This function set the query-string. */ |
| 4981 | static int hlua_http_req_set_query(lua_State *L) |
| 4982 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4983 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4984 | size_t name_len; |
| 4985 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4986 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4987 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4988 | WILL_LJMP(lua_error(L)); |
| 4989 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4990 | /* Check length. */ |
| 4991 | if (name_len > trash.size - 1) { |
| 4992 | lua_pushboolean(L, 0); |
| 4993 | return 1; |
| 4994 | } |
| 4995 | |
| 4996 | /* Add the mark question as prefix. */ |
| 4997 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4998 | trash.area[trash.data++] = '?'; |
| 4999 | memcpy(trash.area + trash.data, name, name_len); |
| 5000 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5001 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5002 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5003 | 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] | 5004 | return 1; |
| 5005 | } |
| 5006 | |
| 5007 | /* This function set the uri. */ |
| 5008 | static int hlua_http_req_set_uri(lua_State *L) |
| 5009 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5010 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5011 | size_t name_len; |
| 5012 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5013 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5014 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5015 | WILL_LJMP(lua_error(L)); |
| 5016 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5017 | 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] | 5018 | return 1; |
| 5019 | } |
| 5020 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5021 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5022 | static int hlua_http_res_set_status(lua_State *L) |
| 5023 | { |
| 5024 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5025 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5026 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5027 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5028 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5029 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5030 | WILL_LJMP(lua_error(L)); |
| 5031 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5032 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5033 | return 0; |
| 5034 | } |
| 5035 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5036 | /* |
| 5037 | * |
| 5038 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5039 | * Class TXN |
| 5040 | * |
| 5041 | * |
| 5042 | */ |
| 5043 | |
| 5044 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5045 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5046 | */ |
| 5047 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5048 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5049 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5050 | } |
| 5051 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5052 | __LJMP static int hlua_set_var(lua_State *L) |
| 5053 | { |
| 5054 | struct hlua_txn *htxn; |
| 5055 | const char *name; |
| 5056 | size_t len; |
| 5057 | struct sample smp; |
| 5058 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5059 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5060 | 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] | 5061 | |
| 5062 | /* It is useles to retrieve the stream, but this function |
| 5063 | * runs only in a stream context. |
| 5064 | */ |
| 5065 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5066 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5067 | |
| 5068 | /* Converts the third argument in a sample. */ |
| 5069 | hlua_lua2smp(L, 3, &smp); |
| 5070 | |
| 5071 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5072 | 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] | 5073 | |
| 5074 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5075 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5076 | else |
| 5077 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5078 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5079 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5080 | } |
| 5081 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5082 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5083 | { |
| 5084 | struct hlua_txn *htxn; |
| 5085 | const char *name; |
| 5086 | size_t len; |
| 5087 | struct sample smp; |
| 5088 | |
| 5089 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5090 | |
| 5091 | /* It is useles to retrieve the stream, but this function |
| 5092 | * runs only in a stream context. |
| 5093 | */ |
| 5094 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5095 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5096 | |
| 5097 | /* Unset the variable. */ |
| 5098 | 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] | 5099 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5100 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5101 | } |
| 5102 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5103 | __LJMP static int hlua_get_var(lua_State *L) |
| 5104 | { |
| 5105 | struct hlua_txn *htxn; |
| 5106 | const char *name; |
| 5107 | size_t len; |
| 5108 | struct sample smp; |
| 5109 | |
| 5110 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5111 | |
| 5112 | /* It is useles to retrieve the stream, but this function |
| 5113 | * runs only in a stream context. |
| 5114 | */ |
| 5115 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5116 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5117 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5118 | 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] | 5119 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5120 | lua_pushnil(L); |
| 5121 | return 1; |
| 5122 | } |
| 5123 | |
| 5124 | return hlua_smp2lua(L, &smp); |
| 5125 | } |
| 5126 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5127 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5128 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5129 | struct hlua *hlua; |
| 5130 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5131 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5132 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5133 | /* It is useles to retrieve the stream, but this function |
| 5134 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5135 | */ |
| 5136 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5137 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5138 | |
| 5139 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5140 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5141 | |
| 5142 | /* Get and store new value. */ |
| 5143 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5144 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5145 | |
| 5146 | return 0; |
| 5147 | } |
| 5148 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5149 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5150 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5151 | struct hlua *hlua; |
| 5152 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5153 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5154 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5155 | /* It is useles to retrieve the stream, but this function |
| 5156 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5157 | */ |
| 5158 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5159 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5160 | |
| 5161 | /* Push configuration index in the stack. */ |
| 5162 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5163 | |
| 5164 | return 1; |
| 5165 | } |
| 5166 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5167 | /* Create stack entry containing a class TXN. This function |
| 5168 | * return 0 if the stack does not contains free slots, |
| 5169 | * otherwise it returns 1. |
| 5170 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5171 | 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] | 5172 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5173 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5174 | |
| 5175 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5176 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5177 | return 0; |
| 5178 | |
| 5179 | /* NOTE: The allocation never fails. The failure |
| 5180 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5181 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5182 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5183 | /* Create the object: obj[0] = userdata. */ |
| 5184 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5185 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5186 | lua_rawseti(L, -2, 0); |
| 5187 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5188 | htxn->s = s; |
| 5189 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5190 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5191 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5192 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5193 | /* Create the "f" field that contains a list of fetches. */ |
| 5194 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5195 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5196 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5197 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5198 | |
| 5199 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5200 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5201 | 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] | 5202 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5203 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5204 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5205 | /* Create the "c" field that contains a list of converters. */ |
| 5206 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5207 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5208 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5209 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5210 | |
| 5211 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5212 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5213 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5214 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5215 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5216 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5217 | /* Create the "req" field that contains the request channel object. */ |
| 5218 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5219 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5220 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5221 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5222 | |
| 5223 | /* Create the "res" field that contains the response channel object. */ |
| 5224 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5225 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5226 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5227 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5228 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5229 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5230 | lua_pushstring(L, "http"); |
| 5231 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5232 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5233 | return 0; |
| 5234 | } |
| 5235 | else |
| 5236 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5237 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5238 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5239 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5240 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5241 | lua_setmetatable(L, -2); |
| 5242 | |
| 5243 | return 1; |
| 5244 | } |
| 5245 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5246 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5247 | { |
| 5248 | const char *msg; |
| 5249 | struct hlua_txn *htxn; |
| 5250 | |
| 5251 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5252 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5253 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5254 | |
| 5255 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5256 | return 0; |
| 5257 | } |
| 5258 | |
| 5259 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5260 | { |
| 5261 | int level; |
| 5262 | const char *msg; |
| 5263 | struct hlua_txn *htxn; |
| 5264 | |
| 5265 | MAY_LJMP(check_args(L, 3, "log")); |
| 5266 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5267 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5268 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5269 | |
| 5270 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5271 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5272 | |
| 5273 | hlua_sendlog(htxn->s->be, level, msg); |
| 5274 | return 0; |
| 5275 | } |
| 5276 | |
| 5277 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5278 | { |
| 5279 | const char *msg; |
| 5280 | struct hlua_txn *htxn; |
| 5281 | |
| 5282 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5283 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5284 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5285 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5286 | return 0; |
| 5287 | } |
| 5288 | |
| 5289 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5290 | { |
| 5291 | const char *msg; |
| 5292 | struct hlua_txn *htxn; |
| 5293 | |
| 5294 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5295 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5296 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5297 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5298 | return 0; |
| 5299 | } |
| 5300 | |
| 5301 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5302 | { |
| 5303 | const char *msg; |
| 5304 | struct hlua_txn *htxn; |
| 5305 | |
| 5306 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5307 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5308 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5309 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5310 | return 0; |
| 5311 | } |
| 5312 | |
| 5313 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5314 | { |
| 5315 | const char *msg; |
| 5316 | struct hlua_txn *htxn; |
| 5317 | |
| 5318 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5319 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5320 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5321 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5322 | return 0; |
| 5323 | } |
| 5324 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5325 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5326 | { |
| 5327 | struct hlua_txn *htxn; |
| 5328 | int ll; |
| 5329 | |
| 5330 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5331 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5332 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5333 | |
| 5334 | if (ll < 0 || ll > 7) |
| 5335 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5336 | |
| 5337 | htxn->s->logs.level = ll; |
| 5338 | return 0; |
| 5339 | } |
| 5340 | |
| 5341 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5342 | { |
| 5343 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5344 | int tos; |
| 5345 | |
| 5346 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5347 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5348 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5349 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5350 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5351 | return 0; |
| 5352 | } |
| 5353 | |
| 5354 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5355 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5356 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5357 | int mark; |
| 5358 | |
| 5359 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5360 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5361 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5362 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5363 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5364 | return 0; |
| 5365 | } |
| 5366 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5367 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5368 | { |
| 5369 | struct hlua_txn *htxn; |
| 5370 | |
| 5371 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5372 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5373 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5374 | return 0; |
| 5375 | } |
| 5376 | |
| 5377 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5378 | { |
| 5379 | struct hlua_txn *htxn; |
| 5380 | |
| 5381 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5382 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5383 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5384 | return 0; |
| 5385 | } |
| 5386 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5387 | /* 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] | 5388 | * 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] | 5389 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5390 | * error. The Reply must be on top of the stack. |
| 5391 | */ |
| 5392 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5393 | { |
| 5394 | struct htx *htx; |
| 5395 | struct htx_sl *sl; |
| 5396 | struct h1m h1m; |
| 5397 | const char *status, *reason, *body; |
| 5398 | size_t status_len, reason_len, body_len; |
| 5399 | int ret, code, flags; |
| 5400 | |
| 5401 | code = 200; |
| 5402 | status = "200"; |
| 5403 | status_len = 3; |
| 5404 | ret = lua_getfield(L, -1, "status"); |
| 5405 | if (ret == LUA_TNUMBER) { |
| 5406 | code = lua_tointeger(L, -1); |
| 5407 | status = lua_tolstring(L, -1, &status_len); |
| 5408 | } |
| 5409 | lua_pop(L, 1); |
| 5410 | |
| 5411 | reason = http_get_reason(code); |
| 5412 | reason_len = strlen(reason); |
| 5413 | ret = lua_getfield(L, -1, "reason"); |
| 5414 | if (ret == LUA_TSTRING) |
| 5415 | reason = lua_tolstring(L, -1, &reason_len); |
| 5416 | lua_pop(L, 1); |
| 5417 | |
| 5418 | body = NULL; |
| 5419 | body_len = 0; |
| 5420 | ret = lua_getfield(L, -1, "body"); |
| 5421 | if (ret == LUA_TSTRING) |
| 5422 | body = lua_tolstring(L, -1, &body_len); |
| 5423 | lua_pop(L, 1); |
| 5424 | |
| 5425 | /* Prepare the response before inserting the headers */ |
| 5426 | h1m_init_res(&h1m); |
| 5427 | htx = htx_from_buf(&s->res.buf); |
| 5428 | channel_htx_truncate(&s->res, htx); |
| 5429 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5430 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5431 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5432 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5433 | } |
| 5434 | else { |
| 5435 | flags = HTX_SL_F_IS_RESP; |
| 5436 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5437 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5438 | } |
| 5439 | if (!sl) |
| 5440 | goto fail; |
| 5441 | sl->info.res.status = code; |
| 5442 | |
| 5443 | /* Push in the stack the "headers" entry. */ |
| 5444 | ret = lua_getfield(L, -1, "headers"); |
| 5445 | if (ret != LUA_TTABLE) |
| 5446 | goto skip_headers; |
| 5447 | |
| 5448 | lua_pushnil(L); |
| 5449 | while (lua_next(L, -2) != 0) { |
| 5450 | struct ist name, value; |
| 5451 | const char *n, *v; |
| 5452 | size_t nlen, vlen; |
| 5453 | |
| 5454 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5455 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5456 | goto next_hdr; |
| 5457 | } |
| 5458 | |
| 5459 | n = lua_tolstring(L, -2, &nlen); |
| 5460 | name = ist2(n, nlen); |
| 5461 | if (isteqi(name, ist("content-length"))) { |
| 5462 | /* Always skip content-length header. It will be added |
| 5463 | * later with the correct len |
| 5464 | */ |
| 5465 | goto next_hdr; |
| 5466 | } |
| 5467 | |
| 5468 | /* Loop on header's values */ |
| 5469 | lua_pushnil(L); |
| 5470 | while (lua_next(L, -2)) { |
| 5471 | if (!lua_isstring(L, -1)) { |
| 5472 | /* Skip the value if it is not a string */ |
| 5473 | goto next_value; |
| 5474 | } |
| 5475 | |
| 5476 | v = lua_tolstring(L, -1, &vlen); |
| 5477 | value = ist2(v, vlen); |
| 5478 | |
| 5479 | if (isteqi(name, ist("transfer-encoding"))) |
| 5480 | h1_parse_xfer_enc_header(&h1m, value); |
| 5481 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5482 | goto fail; |
| 5483 | |
| 5484 | next_value: |
| 5485 | lua_pop(L, 1); |
| 5486 | } |
| 5487 | |
| 5488 | next_hdr: |
| 5489 | lua_pop(L, 1); |
| 5490 | } |
| 5491 | skip_headers: |
| 5492 | lua_pop(L, 1); |
| 5493 | |
| 5494 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5495 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5496 | const char *clen = ultoa(body_len); |
| 5497 | |
| 5498 | h1m.flags |= H1_MF_CLEN; |
| 5499 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5500 | goto fail; |
| 5501 | } |
| 5502 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5503 | h1m.flags |= H1_MF_XFER_LEN; |
| 5504 | |
| 5505 | /* Update HTX start-line flags */ |
| 5506 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5507 | flags |= HTX_SL_F_XFER_ENC; |
| 5508 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5509 | flags |= HTX_SL_F_XFER_LEN; |
| 5510 | if (h1m.flags & H1_MF_CHNK) |
| 5511 | flags |= HTX_SL_F_CHNK; |
| 5512 | else if (h1m.flags & H1_MF_CLEN) |
| 5513 | flags |= HTX_SL_F_CLEN; |
| 5514 | if (h1m.body_len == 0) |
| 5515 | flags |= HTX_SL_F_BODYLESS; |
| 5516 | } |
| 5517 | sl->flags |= flags; |
| 5518 | |
| 5519 | |
| 5520 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5521 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5522 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5523 | goto fail; |
| 5524 | |
| 5525 | /* Now, forward the response and terminate the transaction */ |
| 5526 | s->txn->status = code; |
| 5527 | htx_to_buf(htx, &s->res.buf); |
| 5528 | if (!http_forward_proxy_resp(s, 1)) |
| 5529 | goto fail; |
| 5530 | |
| 5531 | return 1; |
| 5532 | |
| 5533 | fail: |
| 5534 | channel_htx_truncate(&s->res, htx); |
| 5535 | return 0; |
| 5536 | } |
| 5537 | |
| 5538 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5539 | * processing is just aborted. Nothing is returned to the client and all |
| 5540 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5541 | * is forwarded to the client before terminating the transaction. On success, |
| 5542 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5543 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5544 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5545 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5546 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5547 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5548 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5549 | struct stream *s; |
| 5550 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5551 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5552 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5553 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5554 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5555 | * just end the execution of the current lua code. */ |
| 5556 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5557 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5558 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5559 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5560 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5561 | struct channel *req = &s->req; |
| 5562 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5563 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5564 | channel_auto_read(req); |
| 5565 | channel_abort(req); |
| 5566 | channel_auto_close(req); |
| 5567 | channel_erase(req); |
| 5568 | |
| 5569 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5570 | channel_auto_read(res); |
| 5571 | channel_auto_close(res); |
| 5572 | channel_shutr_now(res); |
| 5573 | |
| 5574 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5575 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5576 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5577 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5578 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5579 | /* No reply or invalid reply */ |
| 5580 | s->txn->status = 0; |
| 5581 | http_reply_and_close(s, 0, NULL); |
| 5582 | } |
| 5583 | else { |
| 5584 | /* Remove extra args to have the reply on top of the stack */ |
| 5585 | if (lua_gettop(L) > 2) |
| 5586 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5587 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5588 | if (!hlua_txn_forward_reply(L, s)) { |
| 5589 | if (!(s->flags & SF_ERR_MASK)) |
| 5590 | s->flags |= SF_ERR_PRXCOND; |
| 5591 | lua_pushinteger(L, ACT_RET_ERR); |
| 5592 | WILL_LJMP(hlua_done(L)); |
| 5593 | return 0; /* Never reached */ |
| 5594 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5595 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5596 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5597 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5598 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5599 | /* let's log the request time */ |
| 5600 | s->logs.tv_request = now; |
| 5601 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5602 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5603 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5604 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5605 | done: |
| 5606 | if (!(s->flags & SF_ERR_MASK)) |
| 5607 | s->flags |= SF_ERR_LOCAL; |
| 5608 | if (!(s->flags & SF_FINST_MASK)) |
| 5609 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5610 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5611 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5612 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5613 | return 0; |
| 5614 | } |
| 5615 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5616 | /* |
| 5617 | * |
| 5618 | * |
| 5619 | * Class REPLY |
| 5620 | * |
| 5621 | * |
| 5622 | */ |
| 5623 | |
| 5624 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5625 | * free slots, the function fails and returns 0; |
| 5626 | */ |
| 5627 | static int hlua_txn_reply_new(lua_State *L) |
| 5628 | { |
| 5629 | struct hlua_txn *htxn; |
| 5630 | const char *reason, *body = NULL; |
| 5631 | int ret, status; |
| 5632 | |
| 5633 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5634 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5635 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5636 | WILL_LJMP(lua_error(L)); |
| 5637 | } |
| 5638 | |
| 5639 | /* Default value */ |
| 5640 | status = 200; |
| 5641 | reason = http_get_reason(status); |
| 5642 | |
| 5643 | if (lua_istable(L, 2)) { |
| 5644 | /* load status and reason from the table argument at index 2 */ |
| 5645 | ret = lua_getfield(L, 2, "status"); |
| 5646 | if (ret == LUA_TNIL) |
| 5647 | goto reason; |
| 5648 | else if (ret != LUA_TNUMBER) { |
| 5649 | /* invalid status: ignore the reason */ |
| 5650 | goto body; |
| 5651 | } |
| 5652 | status = lua_tointeger(L, -1); |
| 5653 | |
| 5654 | reason: |
| 5655 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5656 | ret = lua_getfield(L, 2, "reason"); |
| 5657 | if (ret == LUA_TSTRING) |
| 5658 | reason = lua_tostring(L, -1); |
| 5659 | |
| 5660 | body: |
| 5661 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5662 | ret = lua_getfield(L, 2, "body"); |
| 5663 | if (ret == LUA_TSTRING) |
| 5664 | body = lua_tostring(L, -1); |
| 5665 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5666 | } |
| 5667 | |
| 5668 | /* Create the Reply table */ |
| 5669 | lua_newtable(L); |
| 5670 | |
| 5671 | /* Add status element */ |
| 5672 | lua_pushstring(L, "status"); |
| 5673 | lua_pushinteger(L, status); |
| 5674 | lua_settable(L, -3); |
| 5675 | |
| 5676 | /* Add reason element */ |
| 5677 | reason = http_get_reason(status); |
| 5678 | lua_pushstring(L, "reason"); |
| 5679 | lua_pushstring(L, reason); |
| 5680 | lua_settable(L, -3); |
| 5681 | |
| 5682 | /* Add body element, nil if undefined */ |
| 5683 | lua_pushstring(L, "body"); |
| 5684 | if (body) |
| 5685 | lua_pushstring(L, body); |
| 5686 | else |
| 5687 | lua_pushnil(L); |
| 5688 | lua_settable(L, -3); |
| 5689 | |
| 5690 | /* Add headers element */ |
| 5691 | lua_pushstring(L, "headers"); |
| 5692 | lua_newtable(L); |
| 5693 | |
| 5694 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5695 | if (lua_istable(L, 2)) { |
| 5696 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5697 | ret = lua_getfield(L, 2, "headers"); |
| 5698 | if (ret == LUA_TTABLE) { |
| 5699 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5700 | lua_pushnil(L); |
| 5701 | while (lua_next(L, -2) != 0) { |
| 5702 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5703 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5704 | /* invalid value type, skip it */ |
| 5705 | lua_pop(L, 1); |
| 5706 | continue; |
| 5707 | } |
| 5708 | |
| 5709 | |
| 5710 | /* Duplicate the key and swap it with the value. */ |
| 5711 | lua_pushvalue(L, -2); |
| 5712 | lua_insert(L, -2); |
| 5713 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5714 | |
| 5715 | lua_newtable(L); |
| 5716 | lua_insert(L, -2); |
| 5717 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5718 | |
| 5719 | if (lua_isstring(L, -1)) { |
| 5720 | /* push the value in the inner table */ |
| 5721 | lua_rawseti(L, -2, 1); |
| 5722 | } |
| 5723 | else { /* table */ |
| 5724 | lua_pushnil(L); |
| 5725 | while (lua_next(L, -2) != 0) { |
| 5726 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5727 | if (!lua_isstring(L, -1)) { |
| 5728 | /* invalid value type, skip it*/ |
| 5729 | lua_pop(L, 1); |
| 5730 | continue; |
| 5731 | } |
| 5732 | /* push the value in the inner table */ |
| 5733 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5734 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5735 | } |
| 5736 | lua_pop(L, 1); |
| 5737 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5738 | } |
| 5739 | |
| 5740 | /* push (k,v) on the stack in the headers table: |
| 5741 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5742 | */ |
| 5743 | lua_settable(L, -5); |
| 5744 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5745 | } |
| 5746 | } |
| 5747 | lua_pop(L, 1); |
| 5748 | } |
| 5749 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5750 | lua_settable(L, -3); |
| 5751 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5752 | |
| 5753 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5754 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5755 | lua_setmetatable(L, -2); |
| 5756 | return 1; |
| 5757 | } |
| 5758 | |
| 5759 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5760 | * provided, the default one corresponding to the status code is used. |
| 5761 | */ |
| 5762 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5763 | { |
| 5764 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5765 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5766 | |
| 5767 | /* First argument (self) must be a table */ |
| 5768 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5769 | |
| 5770 | if (status < 100 || status > 599) { |
| 5771 | lua_pushboolean(L, 0); |
| 5772 | return 1; |
| 5773 | } |
| 5774 | if (!reason) |
| 5775 | reason = http_get_reason(status); |
| 5776 | |
| 5777 | lua_pushinteger(L, status); |
| 5778 | lua_setfield(L, 1, "status"); |
| 5779 | |
| 5780 | lua_pushstring(L, reason); |
| 5781 | lua_setfield(L, 1, "reason"); |
| 5782 | |
| 5783 | lua_pushboolean(L, 1); |
| 5784 | return 1; |
| 5785 | } |
| 5786 | |
| 5787 | /* Add a header into the reply object. Each header name is associated to an |
| 5788 | * array of values in the "headers" table. If the header name is not found, a |
| 5789 | * new entry is created. |
| 5790 | */ |
| 5791 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5792 | { |
| 5793 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5794 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5795 | int ret; |
| 5796 | |
| 5797 | /* First argument (self) must be a table */ |
| 5798 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5799 | |
| 5800 | /* Push in the stack the "headers" entry. */ |
| 5801 | ret = lua_getfield(L, 1, "headers"); |
| 5802 | if (ret != LUA_TTABLE) { |
| 5803 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5804 | WILL_LJMP(lua_error(L)); |
| 5805 | } |
| 5806 | |
| 5807 | /* check if the header is already registered. If not, register it. */ |
| 5808 | ret = lua_getfield(L, -1, name); |
| 5809 | if (ret == LUA_TNIL) { |
| 5810 | /* Entry not found. */ |
| 5811 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5812 | |
| 5813 | /* Insert the new header name in the array in the top of the stack. |
| 5814 | * It left the new array in the top of the stack. |
| 5815 | */ |
| 5816 | lua_newtable(L); |
| 5817 | lua_pushstring(L, name); |
| 5818 | lua_pushvalue(L, -2); |
| 5819 | lua_settable(L, -4); |
| 5820 | } |
| 5821 | else if (ret != LUA_TTABLE) { |
| 5822 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5823 | WILL_LJMP(lua_error(L)); |
| 5824 | } |
| 5825 | |
| 5826 | /* Now the top od thestack is an array of values. We push |
| 5827 | * the header value as new entry. |
| 5828 | */ |
| 5829 | lua_pushstring(L, value); |
| 5830 | ret = lua_rawlen(L, -2); |
| 5831 | lua_rawseti(L, -2, ret + 1); |
| 5832 | |
| 5833 | lua_pushboolean(L, 1); |
| 5834 | return 1; |
| 5835 | } |
| 5836 | |
| 5837 | /* Remove all occurrences of a given header name. */ |
| 5838 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5839 | { |
| 5840 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5841 | int ret; |
| 5842 | |
| 5843 | /* First argument (self) must be a table */ |
| 5844 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5845 | |
| 5846 | /* Push in the stack the "headers" entry. */ |
| 5847 | ret = lua_getfield(L, 1, "headers"); |
| 5848 | if (ret != LUA_TTABLE) { |
| 5849 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 5850 | WILL_LJMP(lua_error(L)); |
| 5851 | } |
| 5852 | |
| 5853 | lua_pushstring(L, name); |
| 5854 | lua_pushnil(L); |
| 5855 | lua_settable(L, -3); |
| 5856 | |
| 5857 | lua_pushboolean(L, 1); |
| 5858 | return 1; |
| 5859 | } |
| 5860 | |
| 5861 | /* Set the reply's body. Overwrite any existing entry. */ |
| 5862 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 5863 | { |
| 5864 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5865 | |
| 5866 | /* First argument (self) must be a table */ |
| 5867 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5868 | |
| 5869 | lua_pushstring(L, payload); |
| 5870 | lua_setfield(L, 1, "body"); |
| 5871 | |
| 5872 | lua_pushboolean(L, 1); |
| 5873 | return 1; |
| 5874 | } |
| 5875 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5876 | __LJMP static int hlua_log(lua_State *L) |
| 5877 | { |
| 5878 | int level; |
| 5879 | const char *msg; |
| 5880 | |
| 5881 | MAY_LJMP(check_args(L, 2, "log")); |
| 5882 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5883 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5884 | |
| 5885 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5886 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5887 | |
| 5888 | hlua_sendlog(NULL, level, msg); |
| 5889 | return 0; |
| 5890 | } |
| 5891 | |
| 5892 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5893 | { |
| 5894 | const char *msg; |
| 5895 | |
| 5896 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5897 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5898 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5899 | return 0; |
| 5900 | } |
| 5901 | |
| 5902 | __LJMP static int hlua_log_info(lua_State *L) |
| 5903 | { |
| 5904 | const char *msg; |
| 5905 | |
| 5906 | MAY_LJMP(check_args(L, 1, "info")); |
| 5907 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5908 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5909 | return 0; |
| 5910 | } |
| 5911 | |
| 5912 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5913 | { |
| 5914 | const char *msg; |
| 5915 | |
| 5916 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5917 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5918 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5919 | return 0; |
| 5920 | } |
| 5921 | |
| 5922 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5923 | { |
| 5924 | const char *msg; |
| 5925 | |
| 5926 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5927 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5928 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5929 | return 0; |
| 5930 | } |
| 5931 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5932 | __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] | 5933 | { |
| 5934 | int wakeup_ms = lua_tointeger(L, -1); |
| 5935 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5936 | 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] | 5937 | return 0; |
| 5938 | } |
| 5939 | |
| 5940 | __LJMP static int hlua_sleep(lua_State *L) |
| 5941 | { |
| 5942 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5943 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5944 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5945 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5946 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5947 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5948 | wakeup_ms = tick_add(now_ms, delay); |
| 5949 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5950 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5951 | 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] | 5952 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5953 | } |
| 5954 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5955 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5956 | { |
| 5957 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5958 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5959 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5960 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5961 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5962 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5963 | wakeup_ms = tick_add(now_ms, delay); |
| 5964 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5965 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5966 | 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] | 5967 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5968 | } |
| 5969 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5970 | /* This functionis an LUA binding. it permits to give back |
| 5971 | * the hand at the HAProxy scheduler. It is used when the |
| 5972 | * LUA processing consumes a lot of time. |
| 5973 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5974 | __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] | 5975 | { |
| 5976 | return 0; |
| 5977 | } |
| 5978 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5979 | __LJMP static int hlua_yield(lua_State *L) |
| 5980 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5981 | 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] | 5982 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5983 | } |
| 5984 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5985 | /* This function change the nice of the currently executed |
| 5986 | * task. It is used set low or high priority at the current |
| 5987 | * task. |
| 5988 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5989 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5990 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5991 | struct hlua *hlua; |
| 5992 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5993 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5994 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5995 | hlua = hlua_gethlua(L); |
| 5996 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5997 | |
| 5998 | /* If he task is not set, I'm in a start mode. */ |
| 5999 | if (!hlua || !hlua->task) |
| 6000 | return 0; |
| 6001 | |
| 6002 | if (nice < -1024) |
| 6003 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6004 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6005 | nice = 1024; |
| 6006 | |
| 6007 | hlua->task->nice = nice; |
| 6008 | return 0; |
| 6009 | } |
| 6010 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6011 | /* 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] | 6012 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6013 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6014 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6015 | * |
| 6016 | * Task wrapper are longjmp safe because the only one Lua code |
| 6017 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6018 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6019 | 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] | 6020 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6021 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6022 | enum hlua_exec status; |
| 6023 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6024 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6025 | task_set_affinity(task, tid_bit); |
| 6026 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6027 | /* If it is the first call to the task, we must initialize the |
| 6028 | * execution timeouts. |
| 6029 | */ |
| 6030 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6031 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6032 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6033 | /* Execute the Lua code. */ |
| 6034 | status = hlua_ctx_resume(hlua, 1); |
| 6035 | |
| 6036 | switch (status) { |
| 6037 | /* finished or yield */ |
| 6038 | case HLUA_E_OK: |
| 6039 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6040 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6041 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6042 | break; |
| 6043 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6044 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6045 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6046 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6047 | break; |
| 6048 | |
| 6049 | /* finished with error. */ |
| 6050 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6051 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6052 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6053 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6054 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6055 | break; |
| 6056 | |
| 6057 | case HLUA_E_ERR: |
| 6058 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6059 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6060 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6061 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6062 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6063 | break; |
| 6064 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6065 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6066 | } |
| 6067 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6068 | /* This function is an LUA binding that register LUA function to be |
| 6069 | * executed after the HAProxy configuration parsing and before the |
| 6070 | * HAProxy scheduler starts. This function expect only one LUA |
| 6071 | * argument that is a function. This function returns nothing, but |
| 6072 | * throws if an error is encountered. |
| 6073 | */ |
| 6074 | __LJMP static int hlua_register_init(lua_State *L) |
| 6075 | { |
| 6076 | struct hlua_init_function *init; |
| 6077 | int ref; |
| 6078 | |
| 6079 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6080 | |
| 6081 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6082 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6083 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6084 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6085 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6086 | |
| 6087 | init->function_ref = ref; |
| 6088 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6089 | return 0; |
| 6090 | } |
| 6091 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6092 | /* This functio is an LUA binding. It permits to register a task |
| 6093 | * executed in parallel of the main HAroxy activity. The task is |
| 6094 | * created and it is set in the HAProxy scheduler. It can be called |
| 6095 | * from the "init" section, "post init" or during the runtime. |
| 6096 | * |
| 6097 | * Lua prototype: |
| 6098 | * |
| 6099 | * <none> core.register_task(<function>) |
| 6100 | */ |
| 6101 | static int hlua_register_task(lua_State *L) |
| 6102 | { |
| 6103 | struct hlua *hlua; |
| 6104 | struct task *task; |
| 6105 | int ref; |
| 6106 | |
| 6107 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6108 | |
| 6109 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6110 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6111 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6112 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6113 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6114 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6115 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6116 | if (!task) |
| 6117 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6118 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6119 | task->context = hlua; |
| 6120 | task->process = hlua_process_task; |
| 6121 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6122 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6123 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6124 | |
| 6125 | /* Restore the function in the stack. */ |
| 6126 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6127 | hlua->nargs = 0; |
| 6128 | |
| 6129 | /* Schedule task. */ |
| 6130 | task_schedule(task, now_ms); |
| 6131 | |
| 6132 | return 0; |
| 6133 | } |
| 6134 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6135 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6136 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6137 | * resume converters. |
| 6138 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6139 | 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] | 6140 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6141 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6142 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6143 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6144 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6145 | if (!stream) |
| 6146 | return 0; |
| 6147 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6148 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6149 | * Lua context can be not initialized. This behavior |
| 6150 | * permits to save performances because a systematic |
| 6151 | * Lua initialization cause 5% performances loss. |
| 6152 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6153 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6154 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6155 | if (!stream->hlua) { |
| 6156 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6157 | return 0; |
| 6158 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6159 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6160 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6161 | return 0; |
| 6162 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6163 | } |
| 6164 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6165 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6166 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6167 | |
| 6168 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6169 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6170 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6171 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6172 | else |
| 6173 | error = "critical error"; |
| 6174 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6175 | return 0; |
| 6176 | } |
| 6177 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6178 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6179 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6180 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6181 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6182 | return 0; |
| 6183 | } |
| 6184 | |
| 6185 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6186 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6187 | |
| 6188 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6189 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6190 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6191 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6192 | return 0; |
| 6193 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6194 | hlua_smp2lua(stream->hlua->T, smp); |
| 6195 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6196 | |
| 6197 | /* push keywords in the stack. */ |
| 6198 | if (arg_p) { |
| 6199 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6200 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6201 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6202 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6203 | return 0; |
| 6204 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6205 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6206 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6207 | } |
| 6208 | } |
| 6209 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6210 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6211 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6212 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6213 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6214 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6215 | } |
| 6216 | |
| 6217 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6218 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6219 | /* finished. */ |
| 6220 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6221 | /* If the stack is empty, the function fails. */ |
| 6222 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6223 | return 0; |
| 6224 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6225 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6226 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6227 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6228 | return 1; |
| 6229 | |
| 6230 | /* yield. */ |
| 6231 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6232 | 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] | 6233 | return 0; |
| 6234 | |
| 6235 | /* finished with error. */ |
| 6236 | case HLUA_E_ERRMSG: |
| 6237 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6238 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6239 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6240 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6241 | return 0; |
| 6242 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6243 | case HLUA_E_ETMOUT: |
| 6244 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6245 | return 0; |
| 6246 | |
| 6247 | case HLUA_E_NOMEM: |
| 6248 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6249 | return 0; |
| 6250 | |
| 6251 | case HLUA_E_YIELD: |
| 6252 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6253 | return 0; |
| 6254 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6255 | case HLUA_E_ERR: |
| 6256 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6257 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6258 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6259 | |
| 6260 | default: |
| 6261 | return 0; |
| 6262 | } |
| 6263 | } |
| 6264 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6265 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6266 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6267 | * resume sample-fetches. This function will be called by the sample |
| 6268 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6269 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6270 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6271 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6272 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6273 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6274 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6275 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6276 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6277 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6278 | if (!stream) |
| 6279 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6280 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6281 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6282 | * Lua context can be not initialized. This behavior |
| 6283 | * permits to save performances because a systematic |
| 6284 | * Lua initialization cause 5% performances loss. |
| 6285 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6286 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6287 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6288 | if (!stream->hlua) { |
| 6289 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6290 | return 0; |
| 6291 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6292 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6293 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6294 | return 0; |
| 6295 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6296 | } |
| 6297 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6298 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6299 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6300 | |
| 6301 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6302 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6303 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6304 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6305 | else |
| 6306 | error = "critical error"; |
| 6307 | 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] | 6308 | return 0; |
| 6309 | } |
| 6310 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6311 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6312 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6313 | 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] | 6314 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6315 | return 0; |
| 6316 | } |
| 6317 | |
| 6318 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6319 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6320 | |
| 6321 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6322 | 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] | 6323 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6324 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6325 | return 0; |
| 6326 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6327 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6328 | |
| 6329 | /* push keywords in the stack. */ |
| 6330 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6331 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6332 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6333 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6334 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6335 | return 0; |
| 6336 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6337 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6338 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6339 | } |
| 6340 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6341 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6342 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6343 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6344 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6345 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6346 | } |
| 6347 | |
| 6348 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6349 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6350 | /* finished. */ |
| 6351 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6352 | /* If the stack is empty, the function fails. */ |
| 6353 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6354 | return 0; |
| 6355 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6356 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6357 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6358 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6359 | |
| 6360 | /* Set the end of execution flag. */ |
| 6361 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6362 | return 1; |
| 6363 | |
| 6364 | /* yield. */ |
| 6365 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6366 | 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] | 6367 | return 0; |
| 6368 | |
| 6369 | /* finished with error. */ |
| 6370 | case HLUA_E_ERRMSG: |
| 6371 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6372 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6373 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6374 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6375 | return 0; |
| 6376 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6377 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6378 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6379 | return 0; |
| 6380 | |
| 6381 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6382 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6383 | return 0; |
| 6384 | |
| 6385 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6386 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6387 | return 0; |
| 6388 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6389 | case HLUA_E_ERR: |
| 6390 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6391 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6392 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6393 | |
| 6394 | default: |
| 6395 | return 0; |
| 6396 | } |
| 6397 | } |
| 6398 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6399 | /* This function is an LUA binding used for registering |
| 6400 | * "sample-conv" functions. It expects a converter name used |
| 6401 | * in the haproxy configuration file, and an LUA function. |
| 6402 | */ |
| 6403 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6404 | { |
| 6405 | struct sample_conv_kw_list *sck; |
| 6406 | const char *name; |
| 6407 | int ref; |
| 6408 | int len; |
| 6409 | struct hlua_function *fcn; |
| 6410 | |
| 6411 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6412 | |
| 6413 | /* First argument : converter name. */ |
| 6414 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6415 | |
| 6416 | /* Second argument : lua function. */ |
| 6417 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6418 | |
| 6419 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6420 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6421 | if (!sck) |
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 | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6423 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6424 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6425 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6426 | |
| 6427 | /* Fill fcn. */ |
| 6428 | fcn->name = strdup(name); |
| 6429 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6430 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6431 | fcn->function_ref = ref; |
| 6432 | |
| 6433 | /* List head */ |
| 6434 | sck->list.n = sck->list.p = NULL; |
| 6435 | |
| 6436 | /* converter keyword. */ |
| 6437 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6438 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6439 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6440 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6441 | |
| 6442 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6443 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6444 | 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] | 6445 | sck->kw[0].val_args = NULL; |
| 6446 | sck->kw[0].in_type = SMP_T_STR; |
| 6447 | sck->kw[0].out_type = SMP_T_STR; |
| 6448 | sck->kw[0].private = fcn; |
| 6449 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6450 | /* Register this new converter */ |
| 6451 | sample_register_convs(sck); |
| 6452 | |
| 6453 | return 0; |
| 6454 | } |
| 6455 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6456 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6457 | * "sample-fetch" functions. It expects a converter name used |
| 6458 | * in the haproxy configuration file, and an LUA function. |
| 6459 | */ |
| 6460 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6461 | { |
| 6462 | const char *name; |
| 6463 | int ref; |
| 6464 | int len; |
| 6465 | struct sample_fetch_kw_list *sfk; |
| 6466 | struct hlua_function *fcn; |
| 6467 | |
| 6468 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6469 | |
| 6470 | /* First argument : sample-fetch name. */ |
| 6471 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6472 | |
| 6473 | /* Second argument : lua function. */ |
| 6474 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6475 | |
| 6476 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6477 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6478 | if (!sfk) |
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 | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6480 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6481 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6482 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6483 | |
| 6484 | /* Fill fcn. */ |
| 6485 | fcn->name = strdup(name); |
| 6486 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6487 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6488 | fcn->function_ref = ref; |
| 6489 | |
| 6490 | /* List head */ |
| 6491 | sfk->list.n = sfk->list.p = NULL; |
| 6492 | |
| 6493 | /* sample-fetch keyword. */ |
| 6494 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6495 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6496 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6497 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6498 | |
| 6499 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6500 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6501 | 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] | 6502 | sfk->kw[0].val_args = NULL; |
| 6503 | sfk->kw[0].out_type = SMP_T_STR; |
| 6504 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6505 | sfk->kw[0].val = 0; |
| 6506 | sfk->kw[0].private = fcn; |
| 6507 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6508 | /* Register this new fetch. */ |
| 6509 | sample_register_fetches(sfk); |
| 6510 | |
| 6511 | return 0; |
| 6512 | } |
| 6513 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6514 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6515 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6516 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6517 | { |
| 6518 | struct hlua *hlua = hlua_gethlua(L); |
| 6519 | unsigned int delay; |
| 6520 | unsigned int wakeup_ms; |
| 6521 | |
| 6522 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6523 | |
| 6524 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6525 | wakeup_ms = tick_add(now_ms, delay); |
| 6526 | hlua->wake_time = wakeup_ms; |
| 6527 | return 0; |
| 6528 | } |
| 6529 | |
| 6530 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6531 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6532 | * wrapper during the initialisation period. This function may return any |
| 6533 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6534 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6535 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6536 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6537 | 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] | 6538 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6539 | { |
| 6540 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6541 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6542 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6543 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6544 | |
| 6545 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6546 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6547 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6548 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6549 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6550 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6551 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6552 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6553 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6554 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6555 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6556 | * Lua context can be not initialized. This behavior |
| 6557 | * permits to save performances because a systematic |
| 6558 | * Lua initialization cause 5% performances loss. |
| 6559 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6560 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6561 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6562 | if (!s->hlua) { |
| 6563 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6564 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6565 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6566 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6567 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6568 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6569 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6570 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6571 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6572 | } |
| 6573 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6574 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6575 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6576 | |
| 6577 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6578 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6579 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6580 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6581 | else |
| 6582 | error = "critical error"; |
| 6583 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6584 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6585 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6586 | } |
| 6587 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6588 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6589 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6590 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6591 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6592 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6593 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6597 | 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] | 6598 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6599 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6600 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6601 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6602 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6603 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6604 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6605 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6606 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6607 | |
| 6608 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6609 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6611 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6612 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6613 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6614 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6615 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6616 | lua_pushstring(s->hlua->T, *arg); |
| 6617 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6618 | } |
| 6619 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6620 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6621 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6622 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6623 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6624 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6625 | } |
| 6626 | |
Christopher Faulet | 23308eb | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6627 | /* Always reset the analyse expiration timeout for the corresponding |
| 6628 | * channel in case the lua script yield, to be sure to not keep an |
| 6629 | * expired timeout. |
| 6630 | */ |
| 6631 | if (dir == SMP_OPT_DIR_REQ) |
| 6632 | s->req.analyse_exp = TICK_ETERNITY; |
| 6633 | else |
| 6634 | s->res.analyse_exp = TICK_ETERNITY; |
| 6635 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6636 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6637 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6638 | /* finished. */ |
| 6639 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6640 | /* Catch the return value */ |
| 6641 | if (lua_gettop(s->hlua->T) > 0) |
| 6642 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6643 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6644 | /* Set timeout in the required channel. */ |
| 6645 | if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) { |
| 6646 | if (dir == SMP_OPT_DIR_REQ) |
| 6647 | s->req.analyse_exp = s->hlua->wake_time; |
| 6648 | else |
| 6649 | s->res.analyse_exp = s->hlua->wake_time; |
| 6650 | } |
| 6651 | goto end; |
| 6652 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6653 | /* yield. */ |
| 6654 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6655 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6656 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | 81921b1 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6657 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6658 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6659 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6660 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6661 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6662 | /* Some actions can be wake up when a "write" event |
| 6663 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6664 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6665 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6666 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6667 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6668 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6669 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6670 | act_ret = ACT_RET_YIELD; |
| 6671 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6672 | |
| 6673 | /* finished with error. */ |
| 6674 | case HLUA_E_ERRMSG: |
| 6675 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6676 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6677 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6678 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6679 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6680 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6681 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6682 | 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] | 6683 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6684 | |
| 6685 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6686 | 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] | 6687 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6688 | |
| 6689 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6690 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6691 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6692 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6693 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6694 | case HLUA_E_ERR: |
| 6695 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6696 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6697 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6698 | |
| 6699 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6700 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6701 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6702 | |
| 6703 | end: |
| 6704 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6705 | } |
| 6706 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6707 | 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] | 6708 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6709 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6710 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6711 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6712 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6713 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6717 | { |
| 6718 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6719 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6720 | struct task *task; |
| 6721 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6722 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6723 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6724 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6725 | if (!hlua) { |
| 6726 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6727 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6728 | return 0; |
| 6729 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6730 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6731 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6732 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6733 | |
| 6734 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6735 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6736 | if (!task) { |
| 6737 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6738 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6739 | return 0; |
| 6740 | } |
| 6741 | task->nice = 0; |
| 6742 | task->context = ctx; |
| 6743 | task->process = hlua_applet_wakeup; |
| 6744 | ctx->ctx.hlua_apptcp.task = task; |
| 6745 | |
| 6746 | /* In the execution wrappers linked with a stream, the |
| 6747 | * Lua context can be not initialized. This behavior |
| 6748 | * permits to save performances because a systematic |
| 6749 | * Lua initialization cause 5% performances loss. |
| 6750 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6751 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6752 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6753 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6754 | return 0; |
| 6755 | } |
| 6756 | |
| 6757 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6758 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6759 | |
| 6760 | /* The following Lua calls can fail. */ |
| 6761 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6762 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6763 | error = lua_tostring(hlua->T, -1); |
| 6764 | else |
| 6765 | error = "critical error"; |
| 6766 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6767 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6768 | return 0; |
| 6769 | } |
| 6770 | |
| 6771 | /* Check stack available size. */ |
| 6772 | if (!lua_checkstack(hlua->T, 1)) { |
| 6773 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6774 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6775 | RESET_SAFE_LJMP(hlua->T); |
| 6776 | return 0; |
| 6777 | } |
| 6778 | |
| 6779 | /* Restore the function in the stack. */ |
| 6780 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6781 | |
| 6782 | /* Create and and push object stream in the stack. */ |
| 6783 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6784 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6785 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6786 | RESET_SAFE_LJMP(hlua->T); |
| 6787 | return 0; |
| 6788 | } |
| 6789 | hlua->nargs = 1; |
| 6790 | |
| 6791 | /* push keywords in the stack. */ |
| 6792 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6793 | if (!lua_checkstack(hlua->T, 1)) { |
| 6794 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6795 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6796 | RESET_SAFE_LJMP(hlua->T); |
| 6797 | return 0; |
| 6798 | } |
| 6799 | lua_pushstring(hlua->T, *arg); |
| 6800 | hlua->nargs++; |
| 6801 | } |
| 6802 | |
| 6803 | RESET_SAFE_LJMP(hlua->T); |
| 6804 | |
| 6805 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6806 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6807 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6808 | |
| 6809 | return 1; |
| 6810 | } |
| 6811 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6812 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6813 | { |
| 6814 | struct stream_interface *si = ctx->owner; |
| 6815 | struct stream *strm = si_strm(si); |
| 6816 | struct channel *res = si_ic(si); |
| 6817 | struct act_rule *rule = ctx->rule; |
| 6818 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6819 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6820 | |
| 6821 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6822 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6823 | /* eat the whole request */ |
| 6824 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6825 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6826 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6827 | |
| 6828 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6829 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6830 | return; |
| 6831 | |
| 6832 | /* Execute the function. */ |
| 6833 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6834 | /* finished. */ |
| 6835 | case HLUA_E_OK: |
| 6836 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6837 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6838 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6839 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6840 | res->flags |= CF_READ_NULL; |
| 6841 | si_shutr(si); |
| 6842 | return; |
| 6843 | |
| 6844 | /* yield. */ |
| 6845 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6846 | if (hlua->wake_time != TICK_ETERNITY) |
| 6847 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6848 | return; |
| 6849 | |
| 6850 | /* finished with error. */ |
| 6851 | case HLUA_E_ERRMSG: |
| 6852 | /* Display log. */ |
| 6853 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6854 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6855 | lua_pop(hlua->T, 1); |
| 6856 | goto error; |
| 6857 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6858 | case HLUA_E_ETMOUT: |
| 6859 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6860 | rule->arg.hlua_rule->fcn.name); |
| 6861 | goto error; |
| 6862 | |
| 6863 | case HLUA_E_NOMEM: |
| 6864 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6865 | rule->arg.hlua_rule->fcn.name); |
| 6866 | goto error; |
| 6867 | |
| 6868 | case HLUA_E_YIELD: /* unexpected */ |
| 6869 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6870 | rule->arg.hlua_rule->fcn.name); |
| 6871 | goto error; |
| 6872 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6873 | case HLUA_E_ERR: |
| 6874 | /* Display log. */ |
| 6875 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6876 | rule->arg.hlua_rule->fcn.name); |
| 6877 | goto error; |
| 6878 | |
| 6879 | default: |
| 6880 | goto error; |
| 6881 | } |
| 6882 | |
| 6883 | error: |
| 6884 | |
| 6885 | /* For all other cases, just close the stream. */ |
| 6886 | si_shutw(si); |
| 6887 | si_shutr(si); |
| 6888 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6889 | } |
| 6890 | |
| 6891 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6892 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6893 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6894 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6895 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6896 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6897 | } |
| 6898 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6899 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6900 | * an errors occurs and -1 if more data are required for initializing |
| 6901 | * the applet. |
| 6902 | */ |
| 6903 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6904 | { |
| 6905 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6906 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6907 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6908 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6909 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6910 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6911 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6912 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6913 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6914 | if (!hlua) { |
| 6915 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6916 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6917 | return 0; |
| 6918 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6919 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6920 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6921 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6922 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6923 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6924 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6925 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6926 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6927 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6928 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6929 | if (!task) { |
| 6930 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6931 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6932 | return 0; |
| 6933 | } |
| 6934 | task->nice = 0; |
| 6935 | task->context = ctx; |
| 6936 | task->process = hlua_applet_wakeup; |
| 6937 | ctx->ctx.hlua_apphttp.task = task; |
| 6938 | |
| 6939 | /* In the execution wrappers linked with a stream, the |
| 6940 | * Lua context can be not initialized. This behavior |
| 6941 | * permits to save performances because a systematic |
| 6942 | * Lua initialization cause 5% performances loss. |
| 6943 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6944 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6945 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6946 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6947 | return 0; |
| 6948 | } |
| 6949 | |
| 6950 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6951 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6952 | |
| 6953 | /* The following Lua calls can fail. */ |
| 6954 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6955 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6956 | error = lua_tostring(hlua->T, -1); |
| 6957 | else |
| 6958 | error = "critical error"; |
| 6959 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6960 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6961 | return 0; |
| 6962 | } |
| 6963 | |
| 6964 | /* Check stack available size. */ |
| 6965 | if (!lua_checkstack(hlua->T, 1)) { |
| 6966 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6967 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6968 | RESET_SAFE_LJMP(hlua->T); |
| 6969 | return 0; |
| 6970 | } |
| 6971 | |
| 6972 | /* Restore the function in the stack. */ |
| 6973 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6974 | |
| 6975 | /* Create and and push object stream in the stack. */ |
| 6976 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6977 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6978 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6979 | RESET_SAFE_LJMP(hlua->T); |
| 6980 | return 0; |
| 6981 | } |
| 6982 | hlua->nargs = 1; |
| 6983 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6984 | /* push keywords in the stack. */ |
| 6985 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6986 | if (!lua_checkstack(hlua->T, 1)) { |
| 6987 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6988 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6989 | RESET_SAFE_LJMP(hlua->T); |
| 6990 | return 0; |
| 6991 | } |
| 6992 | lua_pushstring(hlua->T, *arg); |
| 6993 | hlua->nargs++; |
| 6994 | } |
| 6995 | |
| 6996 | RESET_SAFE_LJMP(hlua->T); |
| 6997 | |
| 6998 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6999 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7000 | |
| 7001 | return 1; |
| 7002 | } |
| 7003 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7004 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7005 | { |
| 7006 | struct stream_interface *si = ctx->owner; |
| 7007 | struct stream *strm = si_strm(si); |
| 7008 | struct channel *req = si_oc(si); |
| 7009 | struct channel *res = si_ic(si); |
| 7010 | struct act_rule *rule = ctx->rule; |
| 7011 | struct proxy *px = strm->be; |
| 7012 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7013 | struct htx *req_htx, *res_htx; |
| 7014 | |
| 7015 | res_htx = htx_from_buf(&res->buf); |
| 7016 | |
| 7017 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7018 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7019 | goto out; |
| 7020 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7021 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7022 | if (!b_size(&res->buf)) { |
| 7023 | si_rx_room_blk(si); |
| 7024 | goto out; |
| 7025 | } |
| 7026 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7027 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7028 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7029 | |
| 7030 | /* Set the currently running flag. */ |
| 7031 | if (!HLUA_IS_RUNNING(hlua) && |
| 7032 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7033 | struct htx_blk *blk; |
| 7034 | size_t count = co_data(req); |
| 7035 | |
| 7036 | if (!count) { |
| 7037 | si_cant_get(si); |
| 7038 | goto out; |
| 7039 | } |
| 7040 | |
| 7041 | /* We need to flush the request header. This left the body for |
| 7042 | * the Lua. |
| 7043 | */ |
| 7044 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7045 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7046 | while (count && blk) { |
| 7047 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7048 | uint32_t sz = htx_get_blksz(blk); |
| 7049 | |
| 7050 | if (sz > count) { |
| 7051 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7052 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7053 | goto out; |
| 7054 | } |
| 7055 | |
| 7056 | count -= sz; |
| 7057 | co_set_data(req, co_data(req) - sz); |
| 7058 | blk = htx_remove_blk(req_htx, blk); |
| 7059 | |
| 7060 | if (type == HTX_BLK_EOH) |
| 7061 | break; |
| 7062 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7063 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7064 | } |
| 7065 | |
| 7066 | /* Executes The applet if it is not done. */ |
| 7067 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7068 | |
| 7069 | /* Execute the function. */ |
| 7070 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7071 | /* finished. */ |
| 7072 | case HLUA_E_OK: |
| 7073 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7074 | break; |
| 7075 | |
| 7076 | /* yield. */ |
| 7077 | case HLUA_E_AGAIN: |
| 7078 | if (hlua->wake_time != TICK_ETERNITY) |
| 7079 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7080 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7081 | |
| 7082 | /* finished with error. */ |
| 7083 | case HLUA_E_ERRMSG: |
| 7084 | /* Display log. */ |
| 7085 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7086 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7087 | lua_pop(hlua->T, 1); |
| 7088 | goto error; |
| 7089 | |
| 7090 | case HLUA_E_ETMOUT: |
| 7091 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7092 | rule->arg.hlua_rule->fcn.name); |
| 7093 | goto error; |
| 7094 | |
| 7095 | case HLUA_E_NOMEM: |
| 7096 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7097 | rule->arg.hlua_rule->fcn.name); |
| 7098 | goto error; |
| 7099 | |
| 7100 | case HLUA_E_YIELD: /* unexpected */ |
| 7101 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7102 | rule->arg.hlua_rule->fcn.name); |
| 7103 | goto error; |
| 7104 | |
| 7105 | case HLUA_E_ERR: |
| 7106 | /* Display log. */ |
| 7107 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7108 | rule->arg.hlua_rule->fcn.name); |
| 7109 | goto error; |
| 7110 | |
| 7111 | default: |
| 7112 | goto error; |
| 7113 | } |
| 7114 | } |
| 7115 | |
| 7116 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7117 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7118 | goto done; |
| 7119 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7120 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7121 | goto error; |
| 7122 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7123 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7124 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7125 | si_rx_room_blk(si); |
| 7126 | goto out; |
| 7127 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7128 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7129 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7130 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7131 | } |
| 7132 | |
| 7133 | done: |
| 7134 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7135 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7136 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7137 | si_shutr(si); |
| 7138 | } |
| 7139 | |
| 7140 | /* eat the whole request */ |
| 7141 | if (co_data(req)) { |
| 7142 | req_htx = htx_from_buf(&req->buf); |
| 7143 | co_htx_skip(req, req_htx, co_data(req)); |
| 7144 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7145 | } |
| 7146 | } |
| 7147 | |
| 7148 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7149 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7150 | return; |
| 7151 | |
| 7152 | error: |
| 7153 | |
| 7154 | /* If we are in HTTP mode, and we are not send any |
| 7155 | * data, return a 500 server error in best effort: |
| 7156 | * if there is no room available in the buffer, |
| 7157 | * just close the connection. |
| 7158 | */ |
| 7159 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7160 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7161 | |
| 7162 | channel_erase(res); |
| 7163 | res->buf.data = b_data(err); |
| 7164 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7165 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7166 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7167 | } |
| 7168 | if (!(strm->flags & SF_ERR_MASK)) |
| 7169 | strm->flags |= SF_ERR_RESOURCE; |
| 7170 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7171 | goto done; |
| 7172 | } |
| 7173 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7174 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7175 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7176 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7177 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7178 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7179 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7180 | } |
| 7181 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7182 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7183 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7184 | * |
| 7185 | * This function can fail with an abort() due to an Lua critical error. |
| 7186 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7187 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7188 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7189 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7190 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7191 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7192 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7193 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7194 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7195 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7196 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7197 | if (!rule->arg.hlua_rule) { |
| 7198 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7199 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7200 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7201 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7202 | /* Memory for arguments. */ |
| 7203 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7204 | if (!rule->arg.hlua_rule->args) { |
| 7205 | memprintf(err, "out of memory error"); |
| 7206 | return ACT_RET_PRS_ERR; |
| 7207 | } |
| 7208 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7209 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7210 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7211 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7212 | /* Expect some arguments */ |
| 7213 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7214 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7215 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7216 | return ACT_RET_PRS_ERR; |
| 7217 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7218 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7219 | if (!rule->arg.hlua_rule->args[i]) { |
| 7220 | memprintf(err, "out of memory error"); |
| 7221 | return ACT_RET_PRS_ERR; |
| 7222 | } |
| 7223 | (*cur_arg)++; |
| 7224 | } |
| 7225 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7226 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7227 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7228 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7229 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7230 | } |
| 7231 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7232 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7233 | struct act_rule *rule, char **err) |
| 7234 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7235 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7236 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7237 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7238 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7239 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7240 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7241 | * the call of this analyzer. |
| 7242 | */ |
| 7243 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7244 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7245 | return ACT_RET_PRS_ERR; |
| 7246 | } |
| 7247 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7248 | /* Memory for the rule. */ |
| 7249 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7250 | if (!rule->arg.hlua_rule) { |
| 7251 | memprintf(err, "out of memory error"); |
| 7252 | return ACT_RET_PRS_ERR; |
| 7253 | } |
| 7254 | |
| 7255 | /* Reference the Lua function and store the reference. */ |
| 7256 | rule->arg.hlua_rule->fcn = *fcn; |
| 7257 | |
| 7258 | /* TODO: later accept arguments. */ |
| 7259 | rule->arg.hlua_rule->args = NULL; |
| 7260 | |
| 7261 | /* Add applet pointer in the rule. */ |
| 7262 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7263 | rule->applet.name = fcn->name; |
| 7264 | rule->applet.init = hlua_applet_http_init; |
| 7265 | rule->applet.fct = hlua_applet_http_fct; |
| 7266 | rule->applet.release = hlua_applet_http_release; |
| 7267 | rule->applet.timeout = hlua_timeout_applet; |
| 7268 | |
| 7269 | return ACT_RET_PRS_OK; |
| 7270 | } |
| 7271 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7272 | /* This function is an LUA binding used for registering |
| 7273 | * "sample-conv" functions. It expects a converter name used |
| 7274 | * in the haproxy configuration file, and an LUA function. |
| 7275 | */ |
| 7276 | __LJMP static int hlua_register_action(lua_State *L) |
| 7277 | { |
| 7278 | struct action_kw_list *akl; |
| 7279 | const char *name; |
| 7280 | int ref; |
| 7281 | int len; |
| 7282 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7283 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7284 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7285 | /* Initialise the number of expected arguments at 0. */ |
| 7286 | nargs = 0; |
| 7287 | |
| 7288 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7289 | 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] | 7290 | |
| 7291 | /* First argument : converter name. */ |
| 7292 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7293 | |
| 7294 | /* Second argument : environment. */ |
| 7295 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7296 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7297 | |
| 7298 | /* Third argument : lua function. */ |
| 7299 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7300 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7301 | /* 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] | 7302 | if (lua_gettop(L) >= 4) |
| 7303 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7304 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7305 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7306 | lua_pushnil(L); |
| 7307 | while (lua_next(L, 2) != 0) { |
| 7308 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7309 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7310 | |
| 7311 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7312 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7313 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7314 | if (!akl) |
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 | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7316 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7317 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7318 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7319 | |
| 7320 | /* Fill fcn. */ |
| 7321 | fcn->name = strdup(name); |
| 7322 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7323 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7324 | fcn->function_ref = ref; |
| 7325 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7326 | /* Set the expected number od arguments. */ |
| 7327 | fcn->nargs = nargs; |
| 7328 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7329 | /* List head */ |
| 7330 | akl->list.n = akl->list.p = NULL; |
| 7331 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7332 | /* action keyword. */ |
| 7333 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7334 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7335 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7336 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7337 | |
| 7338 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7339 | |
| 7340 | akl->kw[0].match_pfx = 0; |
| 7341 | akl->kw[0].private = fcn; |
| 7342 | akl->kw[0].parse = action_register_lua; |
| 7343 | |
| 7344 | /* select the action registering point. */ |
| 7345 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7346 | tcp_req_cont_keywords_register(akl); |
| 7347 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7348 | tcp_res_cont_keywords_register(akl); |
| 7349 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7350 | http_req_keywords_register(akl); |
| 7351 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7352 | http_res_keywords_register(akl); |
| 7353 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7354 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7355 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7356 | "are expected.", lua_tostring(L, -1))); |
| 7357 | |
| 7358 | /* pop the environment string. */ |
| 7359 | lua_pop(L, 1); |
| 7360 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7361 | return ACT_RET_PRS_OK; |
| 7362 | } |
| 7363 | |
| 7364 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7365 | struct act_rule *rule, char **err) |
| 7366 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7367 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7368 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7369 | if (px->mode == PR_MODE_HTTP) { |
| 7370 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7371 | return ACT_RET_PRS_ERR; |
| 7372 | } |
| 7373 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7374 | /* Memory for the rule. */ |
| 7375 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7376 | if (!rule->arg.hlua_rule) { |
| 7377 | memprintf(err, "out of memory error"); |
| 7378 | return ACT_RET_PRS_ERR; |
| 7379 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7380 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7381 | /* Reference the Lua function and store the reference. */ |
| 7382 | rule->arg.hlua_rule->fcn = *fcn; |
| 7383 | |
| 7384 | /* TODO: later accept arguments. */ |
| 7385 | rule->arg.hlua_rule->args = NULL; |
| 7386 | |
| 7387 | /* Add applet pointer in the rule. */ |
| 7388 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7389 | rule->applet.name = fcn->name; |
| 7390 | rule->applet.init = hlua_applet_tcp_init; |
| 7391 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7392 | rule->applet.release = hlua_applet_tcp_release; |
| 7393 | rule->applet.timeout = hlua_timeout_applet; |
| 7394 | |
| 7395 | return 0; |
| 7396 | } |
| 7397 | |
| 7398 | /* This function is an LUA binding used for registering |
| 7399 | * "sample-conv" functions. It expects a converter name used |
| 7400 | * in the haproxy configuration file, and an LUA function. |
| 7401 | */ |
| 7402 | __LJMP static int hlua_register_service(lua_State *L) |
| 7403 | { |
| 7404 | struct action_kw_list *akl; |
| 7405 | const char *name; |
| 7406 | const char *env; |
| 7407 | int ref; |
| 7408 | int len; |
| 7409 | struct hlua_function *fcn; |
| 7410 | |
| 7411 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7412 | |
| 7413 | /* First argument : converter name. */ |
| 7414 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7415 | |
| 7416 | /* Second argument : environment. */ |
| 7417 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7418 | |
| 7419 | /* Third argument : lua function. */ |
| 7420 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7421 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7422 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7423 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7424 | if (!akl) |
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 | fcn = calloc(1, sizeof(*fcn)); |
| 7427 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7428 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7429 | |
| 7430 | /* Fill fcn. */ |
| 7431 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7432 | fcn->name = calloc(1, len); |
| 7433 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7434 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7435 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7436 | fcn->function_ref = ref; |
| 7437 | |
| 7438 | /* List head */ |
| 7439 | akl->list.n = akl->list.p = NULL; |
| 7440 | |
| 7441 | /* converter keyword. */ |
| 7442 | len = strlen("lua.") + strlen(name) + 1; |
| 7443 | akl->kw[0].kw = calloc(1, len); |
| 7444 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7445 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7446 | |
| 7447 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7448 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7449 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7450 | if (strcmp(env, "tcp") == 0) |
| 7451 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7452 | else if (strcmp(env, "http") == 0) |
| 7453 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7454 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7455 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7456 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7457 | |
| 7458 | akl->kw[0].match_pfx = 0; |
| 7459 | akl->kw[0].private = fcn; |
| 7460 | |
| 7461 | /* End of array. */ |
| 7462 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7463 | |
| 7464 | /* Register this new converter */ |
| 7465 | service_keywords_register(akl); |
| 7466 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7467 | return 0; |
| 7468 | } |
| 7469 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7470 | /* This function initialises Lua cli handler. It copies the |
| 7471 | * arguments in the Lua stack and create channel IO objects. |
| 7472 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7473 | 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] | 7474 | { |
| 7475 | struct hlua *hlua; |
| 7476 | struct hlua_function *fcn; |
| 7477 | int i; |
| 7478 | const char *error; |
| 7479 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7480 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7481 | appctx->ctx.hlua_cli.fcn = private; |
| 7482 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7483 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7484 | if (!hlua) { |
| 7485 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7486 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7487 | } |
| 7488 | HLUA_INIT(hlua); |
| 7489 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7490 | |
| 7491 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7492 | * 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] | 7493 | * applet_http. It is absolutely compatible. |
| 7494 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7495 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7496 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7497 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7498 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7499 | } |
| 7500 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7501 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7502 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7503 | |
| 7504 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7505 | 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] | 7506 | 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] | 7507 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7508 | } |
| 7509 | |
| 7510 | /* The following Lua calls can fail. */ |
| 7511 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7512 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7513 | error = lua_tostring(hlua->T, -1); |
| 7514 | else |
| 7515 | error = "critical error"; |
| 7516 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7517 | goto error; |
| 7518 | } |
| 7519 | |
| 7520 | /* Check stack available size. */ |
| 7521 | if (!lua_checkstack(hlua->T, 2)) { |
| 7522 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7523 | goto error; |
| 7524 | } |
| 7525 | |
| 7526 | /* Restore the function in the stack. */ |
| 7527 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7528 | |
| 7529 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7530 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7531 | */ |
| 7532 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7533 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7534 | goto error; |
| 7535 | } |
| 7536 | hlua->nargs = 1; |
| 7537 | |
| 7538 | /* push keywords in the stack. */ |
| 7539 | for (i = 0; *args[i]; i++) { |
| 7540 | /* Check stack available size. */ |
| 7541 | if (!lua_checkstack(hlua->T, 1)) { |
| 7542 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7543 | goto error; |
| 7544 | } |
| 7545 | lua_pushstring(hlua->T, args[i]); |
| 7546 | hlua->nargs++; |
| 7547 | } |
| 7548 | |
| 7549 | /* We must initialize the execution timeouts. */ |
| 7550 | hlua->max_time = hlua_timeout_session; |
| 7551 | |
| 7552 | /* At this point the execution is safe. */ |
| 7553 | RESET_SAFE_LJMP(hlua->T); |
| 7554 | |
| 7555 | /* It's ok */ |
| 7556 | return 0; |
| 7557 | |
| 7558 | /* It's not ok. */ |
| 7559 | error: |
| 7560 | RESET_SAFE_LJMP(hlua->T); |
| 7561 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7562 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7563 | return 1; |
| 7564 | } |
| 7565 | |
| 7566 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7567 | { |
| 7568 | struct hlua *hlua; |
| 7569 | struct stream_interface *si; |
| 7570 | struct hlua_function *fcn; |
| 7571 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7572 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7573 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7574 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7575 | |
| 7576 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7577 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7578 | return 1; |
| 7579 | |
| 7580 | /* Execute the function. */ |
| 7581 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7582 | |
| 7583 | /* finished. */ |
| 7584 | case HLUA_E_OK: |
| 7585 | return 1; |
| 7586 | |
| 7587 | /* yield. */ |
| 7588 | case HLUA_E_AGAIN: |
| 7589 | /* We want write. */ |
| 7590 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7591 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7592 | /* Set the timeout. */ |
| 7593 | if (hlua->wake_time != TICK_ETERNITY) |
| 7594 | task_schedule(hlua->task, hlua->wake_time); |
| 7595 | return 0; |
| 7596 | |
| 7597 | /* finished with error. */ |
| 7598 | case HLUA_E_ERRMSG: |
| 7599 | /* Display log. */ |
| 7600 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7601 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7602 | lua_pop(hlua->T, 1); |
| 7603 | return 1; |
| 7604 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7605 | case HLUA_E_ETMOUT: |
| 7606 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7607 | fcn->name); |
| 7608 | return 1; |
| 7609 | |
| 7610 | case HLUA_E_NOMEM: |
| 7611 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7612 | fcn->name); |
| 7613 | return 1; |
| 7614 | |
| 7615 | case HLUA_E_YIELD: /* unexpected */ |
| 7616 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7617 | fcn->name); |
| 7618 | return 1; |
| 7619 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7620 | case HLUA_E_ERR: |
| 7621 | /* Display log. */ |
| 7622 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7623 | fcn->name); |
| 7624 | return 1; |
| 7625 | |
| 7626 | default: |
| 7627 | return 1; |
| 7628 | } |
| 7629 | |
| 7630 | return 1; |
| 7631 | } |
| 7632 | |
| 7633 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7634 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7635 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7636 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7637 | } |
| 7638 | |
| 7639 | /* This function is an LUA binding used for registering |
| 7640 | * new keywords in the cli. It expects a list of keywords |
| 7641 | * which are the "path". It is limited to 5 keywords. A |
| 7642 | * description of the command, a function to be executed |
| 7643 | * for the parsing and a function for io handlers. |
| 7644 | */ |
| 7645 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7646 | { |
| 7647 | struct cli_kw_list *cli_kws; |
| 7648 | const char *message; |
| 7649 | int ref_io; |
| 7650 | int len; |
| 7651 | struct hlua_function *fcn; |
| 7652 | int index; |
| 7653 | int i; |
| 7654 | |
| 7655 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7656 | |
| 7657 | /* First argument : an array of maximum 5 keywords. */ |
| 7658 | if (!lua_istable(L, 1)) |
| 7659 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7660 | |
| 7661 | /* Second argument : string with contextual message. */ |
| 7662 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7663 | |
| 7664 | /* Third and fourth argument : lua function. */ |
| 7665 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7666 | |
| 7667 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7668 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7669 | if (!cli_kws) |
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 | fcn = calloc(1, sizeof(*fcn)); |
| 7672 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7673 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7674 | |
| 7675 | /* Fill path. */ |
| 7676 | index = 0; |
| 7677 | lua_pushnil(L); |
| 7678 | while(lua_next(L, 1) != 0) { |
| 7679 | if (index >= 5) |
| 7680 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7681 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7682 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7683 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7684 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7685 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7686 | index++; |
| 7687 | lua_pop(L, 1); |
| 7688 | } |
| 7689 | |
| 7690 | /* Copy help message. */ |
| 7691 | cli_kws->kw[0].usage = strdup(message); |
| 7692 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7693 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7694 | |
| 7695 | /* Fill fcn io handler. */ |
| 7696 | len = strlen("<lua.cli>") + 1; |
| 7697 | for (i = 0; i < index; i++) |
| 7698 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7699 | fcn->name = calloc(1, len); |
| 7700 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7701 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7702 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7703 | for (i = 0; i < index; i++) { |
| 7704 | strncat((char *)fcn->name, ".", len); |
| 7705 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7706 | } |
| 7707 | strncat((char *)fcn->name, ">", len); |
| 7708 | fcn->function_ref = ref_io; |
| 7709 | |
| 7710 | /* Fill last entries. */ |
| 7711 | cli_kws->kw[0].private = fcn; |
| 7712 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7713 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7714 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7715 | |
| 7716 | /* Register this new converter */ |
| 7717 | cli_register_kw(cli_kws); |
| 7718 | |
| 7719 | return 0; |
| 7720 | } |
| 7721 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7722 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7723 | struct proxy *defpx, const char *file, int line, |
| 7724 | char **err, unsigned int *timeout) |
| 7725 | { |
| 7726 | const char *error; |
| 7727 | |
| 7728 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7729 | if (error == PARSE_TIME_OVER) { |
| 7730 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7731 | args[1], args[0]); |
| 7732 | return -1; |
| 7733 | } |
| 7734 | else if (error == PARSE_TIME_UNDER) { |
| 7735 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7736 | args[1], args[0]); |
| 7737 | return -1; |
| 7738 | } |
| 7739 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7740 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7741 | return -1; |
| 7742 | } |
| 7743 | return 0; |
| 7744 | } |
| 7745 | |
| 7746 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7747 | struct proxy *defpx, const char *file, int line, |
| 7748 | char **err) |
| 7749 | { |
| 7750 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7751 | file, line, err, &hlua_timeout_session); |
| 7752 | } |
| 7753 | |
| 7754 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7755 | struct proxy *defpx, const char *file, int line, |
| 7756 | char **err) |
| 7757 | { |
| 7758 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7759 | file, line, err, &hlua_timeout_task); |
| 7760 | } |
| 7761 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7762 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7763 | struct proxy *defpx, const char *file, int line, |
| 7764 | char **err) |
| 7765 | { |
| 7766 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7767 | file, line, err, &hlua_timeout_applet); |
| 7768 | } |
| 7769 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7770 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7771 | struct proxy *defpx, const char *file, int line, |
| 7772 | char **err) |
| 7773 | { |
| 7774 | char *error; |
| 7775 | |
| 7776 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7777 | if (*error != '\0') { |
| 7778 | memprintf(err, "%s: invalid number", args[0]); |
| 7779 | return -1; |
| 7780 | } |
| 7781 | return 0; |
| 7782 | } |
| 7783 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7784 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7785 | struct proxy *defpx, const char *file, int line, |
| 7786 | char **err) |
| 7787 | { |
| 7788 | char *error; |
| 7789 | |
| 7790 | if (*(args[1]) == 0) { |
| 7791 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7792 | return -1; |
| 7793 | } |
| 7794 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7795 | if (*error != '\0') { |
| 7796 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7797 | return -1; |
| 7798 | } |
| 7799 | return 0; |
| 7800 | } |
| 7801 | |
| 7802 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7803 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7804 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7805 | * the main lua entry point. |
| 7806 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7807 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7808 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7809 | * |
| 7810 | * In some error case, LUA set an error message in top of the stack. This function |
| 7811 | * 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] | 7812 | * |
| 7813 | * This function can fail with an abort() due to an Lua critical error. |
| 7814 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7815 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7816 | */ |
| 7817 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7818 | struct proxy *defpx, const char *file, int line, |
| 7819 | char **err) |
| 7820 | { |
| 7821 | int error; |
| 7822 | |
| 7823 | /* Just load and compile the file. */ |
| 7824 | error = luaL_loadfile(gL.T, args[1]); |
| 7825 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7826 | 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] | 7827 | lua_pop(gL.T, 1); |
| 7828 | return -1; |
| 7829 | } |
| 7830 | |
| 7831 | /* If no syntax error where detected, execute the code. */ |
| 7832 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7833 | switch (error) { |
| 7834 | case LUA_OK: |
| 7835 | break; |
| 7836 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7837 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7838 | lua_pop(gL.T, 1); |
| 7839 | return -1; |
| 7840 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7841 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7842 | return -1; |
| 7843 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7844 | 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] | 7845 | lua_pop(gL.T, 1); |
| 7846 | return -1; |
| 7847 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7848 | 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] | 7849 | lua_pop(gL.T, 1); |
| 7850 | return -1; |
| 7851 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7852 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7853 | lua_pop(gL.T, 1); |
| 7854 | return -1; |
| 7855 | } |
| 7856 | |
| 7857 | return 0; |
| 7858 | } |
| 7859 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7860 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7861 | * in the given <ctx>. |
| 7862 | */ |
| 7863 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7864 | { |
| 7865 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7866 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7867 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7868 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7869 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7870 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7871 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7872 | |
| 7873 | return 0; |
| 7874 | } |
| 7875 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7876 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7877 | struct proxy *defpx, const char *file, int line, |
| 7878 | char **err) |
| 7879 | { |
| 7880 | char *path; |
| 7881 | char *type = "path"; |
| 7882 | if (too_many_args(2, args, err, NULL)) { |
| 7883 | return -1; |
| 7884 | } |
| 7885 | |
| 7886 | if (!(*args[1])) { |
| 7887 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7888 | return -1; |
| 7889 | } |
| 7890 | path = args[1]; |
| 7891 | |
| 7892 | if (*args[2]) { |
| 7893 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7894 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7895 | return -1; |
| 7896 | } |
| 7897 | type = args[2]; |
| 7898 | } |
| 7899 | |
| 7900 | return hlua_prepend_path(gL, type, path); |
| 7901 | } |
| 7902 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7903 | /* configuration keywords declaration */ |
| 7904 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7905 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7906 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7907 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7908 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7909 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7910 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7911 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7912 | { 0, NULL, NULL }, |
| 7913 | }}; |
| 7914 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7915 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7916 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7917 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7918 | /* This function can fail with an abort() due to an Lua critical error. |
| 7919 | * We are in the initialisation process of HAProxy, this abort() is |
| 7920 | * tolerated. |
| 7921 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7922 | int hlua_post_init() |
| 7923 | { |
| 7924 | struct hlua_init_function *init; |
| 7925 | const char *msg; |
| 7926 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7927 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7928 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7929 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7930 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7931 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7932 | error = lua_tostring(gL.T, -1); |
| 7933 | else |
| 7934 | error = "critical error"; |
| 7935 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7936 | exit(1); |
| 7937 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7938 | |
| 7939 | #if USE_OPENSSL |
| 7940 | /* Initialize SSL server. */ |
| 7941 | if (socket_ssl.xprt->prepare_srv) { |
| 7942 | int saved_used_backed = global.ssl_used_backend; |
| 7943 | // don't affect maxconn automatic computation |
| 7944 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7945 | global.ssl_used_backend = saved_used_backed; |
| 7946 | } |
| 7947 | #endif |
| 7948 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7949 | hlua_fcn_post_init(gL.T); |
| 7950 | RESET_SAFE_LJMP(gL.T); |
| 7951 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7952 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7953 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7954 | ret = hlua_ctx_resume(&gL, 0); |
| 7955 | switch (ret) { |
| 7956 | case HLUA_E_OK: |
| 7957 | lua_pop(gL.T, -1); |
| 7958 | return 1; |
| 7959 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7960 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7961 | return 0; |
| 7962 | case HLUA_E_ERRMSG: |
| 7963 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7964 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7965 | return 0; |
| 7966 | case HLUA_E_ERR: |
| 7967 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7968 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7969 | return 0; |
| 7970 | } |
| 7971 | } |
| 7972 | return 1; |
| 7973 | } |
| 7974 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7975 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7976 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7977 | * is the previously allocated size or the kind of object in case of a new |
| 7978 | * allocation. <nsize> is the requested new size. |
| 7979 | */ |
| 7980 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7981 | { |
| 7982 | struct hlua_mem_allocator *zone = ud; |
| 7983 | |
| 7984 | if (nsize == 0) { |
| 7985 | /* it's a free */ |
| 7986 | if (ptr) |
| 7987 | zone->allocated -= osize; |
| 7988 | free(ptr); |
| 7989 | return NULL; |
| 7990 | } |
| 7991 | |
| 7992 | if (!ptr) { |
| 7993 | /* it's a new allocation */ |
| 7994 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7995 | return NULL; |
| 7996 | |
| 7997 | ptr = malloc(nsize); |
| 7998 | if (ptr) |
| 7999 | zone->allocated += nsize; |
| 8000 | return ptr; |
| 8001 | } |
| 8002 | |
| 8003 | /* it's a realloc */ |
| 8004 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8005 | return NULL; |
| 8006 | |
| 8007 | ptr = realloc(ptr, nsize); |
| 8008 | if (ptr) |
| 8009 | zone->allocated += nsize - osize; |
| 8010 | return ptr; |
| 8011 | } |
| 8012 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8013 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8014 | * We are in the initialisation process of HAProxy, this abort() is |
| 8015 | * tolerated. |
| 8016 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8017 | void hlua_init(void) |
| 8018 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8019 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8020 | int idx; |
| 8021 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8022 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8023 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8024 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8025 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8026 | struct srv_kw *kw; |
| 8027 | int tmp_error; |
| 8028 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8029 | char *args[] = { /* SSL client configuration. */ |
| 8030 | "ssl", |
| 8031 | "verify", |
| 8032 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8033 | NULL |
| 8034 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8035 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8036 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8037 | /* Init main lua stack. */ |
| 8038 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8039 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8040 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8041 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8042 | hlua_sethlua(&gL); |
| 8043 | gL.Tref = LUA_REFNIL; |
| 8044 | gL.task = NULL; |
| 8045 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8046 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8047 | * the Lua function can fail with an abort. We are in the initialisation |
| 8048 | * process of HAProxy, this abort() is tolerated. |
| 8049 | */ |
| 8050 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8051 | /* Initialise lua. */ |
| 8052 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8053 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8054 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8055 | #ifdef HLUA_PREPEND_PATH |
| 8056 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8057 | #endif |
| 8058 | #ifdef HLUA_PREPEND_CPATH |
| 8059 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8060 | #endif |
| 8061 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8062 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8063 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8064 | /* Set safe environment for the initialisation. */ |
| 8065 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8066 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8067 | error_msg = lua_tostring(gL.T, -1); |
| 8068 | else |
| 8069 | error_msg = "critical error"; |
| 8070 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8071 | exit(1); |
| 8072 | } |
| 8073 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8074 | /* |
| 8075 | * |
| 8076 | * Create "core" object. |
| 8077 | * |
| 8078 | */ |
| 8079 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8080 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8081 | lua_newtable(gL.T); |
| 8082 | |
| 8083 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8084 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8085 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8086 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8087 | /* Register special functions. */ |
| 8088 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8089 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8090 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8091 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8092 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8093 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8094 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8095 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8096 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8097 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8098 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8099 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8100 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8101 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8102 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8103 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8104 | hlua_class_function(gL.T, "log", hlua_log); |
| 8105 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8106 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8107 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8108 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8109 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8110 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8111 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8112 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8113 | |
| 8114 | /* |
| 8115 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8116 | * Create "act" object. |
| 8117 | * |
| 8118 | */ |
| 8119 | |
| 8120 | /* This table entry is the object "act" base. */ |
| 8121 | lua_newtable(gL.T); |
| 8122 | |
| 8123 | /* push action return constants */ |
| 8124 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8125 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8126 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8127 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8128 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8129 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8130 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8131 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8132 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8133 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8134 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8135 | lua_setglobal(gL.T, "act"); |
| 8136 | |
| 8137 | /* |
| 8138 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8139 | * Register class Map |
| 8140 | * |
| 8141 | */ |
| 8142 | |
| 8143 | /* This table entry is the object "Map" base. */ |
| 8144 | lua_newtable(gL.T); |
| 8145 | |
| 8146 | /* register pattern types. */ |
| 8147 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8148 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8149 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8150 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8151 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8152 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8153 | |
| 8154 | /* register constructor. */ |
| 8155 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8156 | |
| 8157 | /* Create and fill the metatable. */ |
| 8158 | lua_newtable(gL.T); |
| 8159 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8160 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8161 | lua_pushstring(gL.T, "__index"); |
| 8162 | lua_newtable(gL.T); |
| 8163 | |
| 8164 | /* Register . */ |
| 8165 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8166 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8167 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8168 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8169 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8170 | /* Register previous table in the registry with reference and named entry. |
| 8171 | * The function hlua_register_metatable() pops the stack, so we |
| 8172 | * previously create a copy of the table. |
| 8173 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8174 | 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] | 8175 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8176 | |
| 8177 | /* Assign the metatable to the mai Map object. */ |
| 8178 | lua_setmetatable(gL.T, -2); |
| 8179 | |
| 8180 | /* Set a name to the table. */ |
| 8181 | lua_setglobal(gL.T, "Map"); |
| 8182 | |
| 8183 | /* |
| 8184 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8185 | * Register class Channel |
| 8186 | * |
| 8187 | */ |
| 8188 | |
| 8189 | /* Create and fill the metatable. */ |
| 8190 | lua_newtable(gL.T); |
| 8191 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8192 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8193 | lua_pushstring(gL.T, "__index"); |
| 8194 | lua_newtable(gL.T); |
| 8195 | |
| 8196 | /* Register . */ |
| 8197 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8198 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8199 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8200 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8201 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8202 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8203 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8204 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8205 | 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] | 8206 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8207 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8208 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8209 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8210 | |
| 8211 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8212 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8213 | |
| 8214 | /* |
| 8215 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8216 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8217 | * |
| 8218 | */ |
| 8219 | |
| 8220 | /* Create and fill the metatable. */ |
| 8221 | lua_newtable(gL.T); |
| 8222 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8223 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8224 | lua_pushstring(gL.T, "__index"); |
| 8225 | lua_newtable(gL.T); |
| 8226 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8227 | /* Browse existing fetches and create the associated |
| 8228 | * object method. |
| 8229 | */ |
| 8230 | sf = NULL; |
| 8231 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8232 | |
| 8233 | /* Dont register the keywork if the arguments check function are |
| 8234 | * not safe during the runtime. |
| 8235 | */ |
| 8236 | if ((sf->val_args != NULL) && |
| 8237 | (sf->val_args != val_payload_lv) && |
| 8238 | (sf->val_args != val_hdr)) |
| 8239 | continue; |
| 8240 | |
| 8241 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8242 | * by an underscore. |
| 8243 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8244 | strncpy(trash.area, sf->kw, trash.size); |
| 8245 | trash.area[trash.size - 1] = '\0'; |
| 8246 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8247 | if (*p == '.' || *p == '-' || *p == '+') |
| 8248 | *p = '_'; |
| 8249 | |
| 8250 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8251 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8252 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8253 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8254 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8255 | } |
| 8256 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8257 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8258 | |
| 8259 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8260 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8261 | |
| 8262 | /* |
| 8263 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8264 | * Register class Converters |
| 8265 | * |
| 8266 | */ |
| 8267 | |
| 8268 | /* Create and fill the metatable. */ |
| 8269 | lua_newtable(gL.T); |
| 8270 | |
| 8271 | /* Create and fill the __index entry. */ |
| 8272 | lua_pushstring(gL.T, "__index"); |
| 8273 | lua_newtable(gL.T); |
| 8274 | |
| 8275 | /* Browse existing converters and create the associated |
| 8276 | * object method. |
| 8277 | */ |
| 8278 | sc = NULL; |
| 8279 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8280 | /* Dont register the keywork if the arguments check function are |
| 8281 | * not safe during the runtime. |
| 8282 | */ |
| 8283 | if (sc->val_args != NULL) |
| 8284 | continue; |
| 8285 | |
| 8286 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8287 | * by an underscore. |
| 8288 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8289 | strncpy(trash.area, sc->kw, trash.size); |
| 8290 | trash.area[trash.size - 1] = '\0'; |
| 8291 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8292 | if (*p == '.' || *p == '-' || *p == '+') |
| 8293 | *p = '_'; |
| 8294 | |
| 8295 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8296 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8297 | lua_pushlightuserdata(gL.T, sc); |
| 8298 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
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 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8302 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8303 | |
| 8304 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8305 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8306 | |
| 8307 | /* |
| 8308 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8309 | * Register class HTTP |
| 8310 | * |
| 8311 | */ |
| 8312 | |
| 8313 | /* Create and fill the metatable. */ |
| 8314 | lua_newtable(gL.T); |
| 8315 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8316 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8317 | lua_pushstring(gL.T, "__index"); |
| 8318 | lua_newtable(gL.T); |
| 8319 | |
| 8320 | /* Register Lua functions. */ |
| 8321 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8322 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8323 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8324 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8325 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8326 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8327 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8328 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8329 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8330 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8331 | |
| 8332 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8333 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8334 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8335 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8336 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8337 | 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] | 8338 | 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] | 8339 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8340 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8341 | |
| 8342 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8343 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8344 | |
| 8345 | /* |
| 8346 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8347 | * Register class AppletTCP |
| 8348 | * |
| 8349 | */ |
| 8350 | |
| 8351 | /* Create and fill the metatable. */ |
| 8352 | lua_newtable(gL.T); |
| 8353 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8354 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8355 | lua_pushstring(gL.T, "__index"); |
| 8356 | lua_newtable(gL.T); |
| 8357 | |
| 8358 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8359 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8360 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8361 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8362 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8363 | 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] | 8364 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8365 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8366 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8367 | |
| 8368 | lua_settable(gL.T, -3); |
| 8369 | |
| 8370 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8371 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8372 | |
| 8373 | /* |
| 8374 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8375 | * Register class AppletHTTP |
| 8376 | * |
| 8377 | */ |
| 8378 | |
| 8379 | /* Create and fill the metatable. */ |
| 8380 | lua_newtable(gL.T); |
| 8381 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8382 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8383 | lua_pushstring(gL.T, "__index"); |
| 8384 | lua_newtable(gL.T); |
| 8385 | |
| 8386 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8387 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8388 | 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] | 8389 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8390 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8391 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8392 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8393 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8394 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8395 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8396 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8397 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8398 | |
| 8399 | lua_settable(gL.T, -3); |
| 8400 | |
| 8401 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8402 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8403 | |
| 8404 | /* |
| 8405 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8406 | * Register class TXN |
| 8407 | * |
| 8408 | */ |
| 8409 | |
| 8410 | /* Create and fill the metatable. */ |
| 8411 | lua_newtable(gL.T); |
| 8412 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8413 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8414 | lua_pushstring(gL.T, "__index"); |
| 8415 | lua_newtable(gL.T); |
| 8416 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8417 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8418 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8419 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8420 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8421 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8422 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8423 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8424 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8425 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8426 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8427 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8428 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8429 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8430 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8431 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8432 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8433 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8434 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8435 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8436 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8437 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8438 | |
| 8439 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8440 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8441 | |
| 8442 | /* |
| 8443 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8444 | * Register class reply |
| 8445 | * |
| 8446 | */ |
| 8447 | lua_newtable(gL.T); |
| 8448 | lua_pushstring(gL.T, "__index"); |
| 8449 | lua_newtable(gL.T); |
| 8450 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8451 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8452 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8453 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8454 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8455 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8456 | |
| 8457 | |
| 8458 | /* |
| 8459 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8460 | * Register class Socket |
| 8461 | * |
| 8462 | */ |
| 8463 | |
| 8464 | /* Create and fill the metatable. */ |
| 8465 | lua_newtable(gL.T); |
| 8466 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8467 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8468 | lua_pushstring(gL.T, "__index"); |
| 8469 | lua_newtable(gL.T); |
| 8470 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8471 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8472 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8473 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8474 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8475 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8476 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8477 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8478 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8479 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8480 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8481 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8482 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8483 | 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] | 8484 | |
| 8485 | /* Register the garbage collector entry. */ |
| 8486 | lua_pushstring(gL.T, "__gc"); |
| 8487 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8488 | lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8489 | |
| 8490 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8491 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8492 | |
| 8493 | /* Proxy and server configuration initialisation. */ |
| 8494 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8495 | init_new_proxy(&socket_proxy); |
| 8496 | socket_proxy.parent = NULL; |
| 8497 | socket_proxy.last_change = now.tv_sec; |
| 8498 | socket_proxy.id = "LUA-SOCKET"; |
| 8499 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8500 | socket_proxy.maxconn = 0; |
| 8501 | socket_proxy.accept = NULL; |
| 8502 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8503 | socket_proxy.srv = NULL; |
| 8504 | socket_proxy.conn_retries = 0; |
| 8505 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8506 | |
| 8507 | /* Init TCP server: unchanged parameters */ |
| 8508 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8509 | socket_tcp.next = NULL; |
| 8510 | socket_tcp.proxy = &socket_proxy; |
| 8511 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8512 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8513 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8514 | socket_tcp.idle_conns = NULL; |
| 8515 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8516 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8517 | socket_tcp.last_change = 0; |
| 8518 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8519 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8520 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8521 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8522 | |
| 8523 | /* XXX: Copy default parameter from default server, |
| 8524 | * but the default server is not initialized. |
| 8525 | */ |
| 8526 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8527 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8528 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8529 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8530 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8531 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8532 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8533 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8534 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8535 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8536 | |
| 8537 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8538 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8539 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8540 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8541 | socket_tcp.check.server = &socket_tcp; |
| 8542 | |
| 8543 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8544 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8545 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8546 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8547 | socket_tcp.agent.server = &socket_tcp; |
| 8548 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8549 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8550 | |
| 8551 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8552 | /* Init TCP server: unchanged parameters */ |
| 8553 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8554 | socket_ssl.next = NULL; |
| 8555 | socket_ssl.proxy = &socket_proxy; |
| 8556 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8557 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8558 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8559 | socket_ssl.idle_conns = NULL; |
| 8560 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8561 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8562 | socket_ssl.last_change = 0; |
| 8563 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8564 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8565 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8566 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8567 | |
| 8568 | /* XXX: Copy default parameter from default server, |
| 8569 | * but the default server is not initialized. |
| 8570 | */ |
| 8571 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8572 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8573 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8574 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8575 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8576 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8577 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8578 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8579 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8580 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8581 | |
| 8582 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8583 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8584 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8585 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8586 | socket_ssl.check.server = &socket_ssl; |
| 8587 | |
| 8588 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8589 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8590 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8591 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8592 | socket_ssl.agent.server = &socket_ssl; |
| 8593 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8594 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8595 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8596 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8597 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8598 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8599 | /* |
| 8600 | * |
| 8601 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8602 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8603 | * features like client certificates and ssl_verify. |
| 8604 | * |
| 8605 | */ |
| 8606 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8607 | if (tmp_error != 0) { |
| 8608 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8609 | abort(); /* This must be never arrives because the command line |
| 8610 | not editable by the user. */ |
| 8611 | } |
| 8612 | idx += kw->skip; |
| 8613 | } |
| 8614 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8615 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8616 | |
| 8617 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8618 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8619 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8620 | static void hlua_register_build_options(void) |
| 8621 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8622 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8623 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8624 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8625 | hap_register_build_opts(ptr, 1); |
| 8626 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8627 | |
| 8628 | INITCALL0(STG_REGISTER, hlua_register_build_options); |